From ddce8013f332fa216e2ec971a25ea82170bbb9f2 Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 6 Jul 2024 01:53:34 -0700 Subject: [PATCH] . --- src/sw.js | 8 +++--- tests/fixtures.js | 14 ---------- tests/{z-license.spec.js => license.spec.js} | 27 ++++++++------------ 3 files changed, 16 insertions(+), 33 deletions(-) rename tests/{z-license.spec.js => license.spec.js} (76%) diff --git a/src/sw.js b/src/sw.js index 3b154da..f9ca4af 100644 --- a/src/sw.js +++ b/src/sw.js @@ -422,9 +422,11 @@ async function checkLicenseStatus() { await chrome.tabs.update(existingTabId, { active: true }) } else { - if (ENV !== 'test') { - await new Promise(resolve => setTimeout(resolve, 7 * 60000)) - } + const delay = 7 * 60000 + const showAt = Date.now() + delay + + await chrome.storage.session.set({ showingSubscriptionMessage: showAt }) + await new Promise(resolve => setTimeout(resolve, delay)) await chrome.tabs.create({ url: 'https://june07.com/nim-subscription/?oUserId=' + oUserId, active: true diff --git a/tests/fixtures.js b/tests/fixtures.js index 1fbf9c4..c4d2bd4 100644 --- a/tests/fixtures.js +++ b/tests/fixtures.js @@ -8,20 +8,6 @@ module.exports = { appName: JSON.parse(fs.readFileSync(join(process.cwd(), '_locales/en/messages.json'), 'utf-8')).appName.message, appVersion: JSON.parse(fs.readFileSync(join(process.cwd(), 'package.json'), 'utf-8')).version, basename, - patch: ({ restore = false } = {}) => { - const manifest = JSON.parse(fs.readFileSync(join(process.cwd(), 'manifest.json'), 'utf-8')) - const sw = fs.readFileSync(join(process.cwd(), manifest.background.service_worker)).toString() - - if (restore) { - const restored = sw.replace(`const ENV = 'test'`, `const ENV = 'production'`) - - fs.writeFileSync(join(process.cwd(), manifest.background.service_worker), restored) - } else { - const patched = sw.replace(`const ENV = 'production'`, `const ENV = 'test'`) - - fs.writeFileSync(join(process.cwd(), manifest.background.service_worker), patched) - } - }, test: test.extend({ context: async ({ }, use, testInfo) => { const pathToExtension = process.env?.PATH_TO_EXTENSION || process.cwd() diff --git a/tests/z-license.spec.js b/tests/license.spec.js similarity index 76% rename from tests/z-license.spec.js rename to tests/license.spec.js index 7613548..d018a5f 100644 --- a/tests/z-license.spec.js +++ b/tests/license.spec.js @@ -1,13 +1,7 @@ const { spawn } = require('child_process') -const { test, expect, ids, randomPort, basename, patch } = require('./fixtures') +const { test, expect, ids, randomPort, basename } = require('./fixtures') module.exports = (async () => { - test.beforeAll(async () => { - patch() - }) - test.afterAll(async () => { - patch({ restore: true }) - }) test.describe.configure({ mode: 'serial' }) test.describe(async () => { test(`${basename(__filename)} - Should show license message`, async ({ page, context, serviceWorker }) => { @@ -36,14 +30,15 @@ module.exports = (async () => { await context.waitForEvent('page') // Wait for the license browser popup to show - let subPage, tries = 0 - while (!subPage && tries < 10) { + let showingSubscriptionMessage, tries = 0 + while (!showingSubscriptionMessage && tries < 10) { tries += 1 - subPage = (await context.pages()).find(page => /https:\/\/june07.com\/nim-subscription/.test(page.url())) ? true : false + const storage = await page.evaluate(async () => await chrome.storage.session.get('showingSubscriptionMessage')) + showingSubscriptionMessage = storage.showingSubscriptionMessage await new Promise(r => setTimeout(r, 500)) } - expect(subPage).toBeTruthy() + expect(showingSubscriptionMessage).not.toBeNull() }) process.kill() @@ -73,15 +68,15 @@ module.exports = (async () => { await (await page.locator(ids.inputs.host)).press('Enter') await context.waitForEvent('page') - let subPage, tries = 0 - while (!subPage && tries < 10) { + let showingSubscriptionMessage, tries = 0 + while (!showingSubscriptionMessage && tries < 10) { tries += 1 - subPage = (await context.pages()).find(page => /https:\/\/june07.com\/nim-subscription/.test(page.url())) ? true : false + const storage = await page.evaluate(async () => await chrome.storage.session.get('showingSubscriptionMessage')) + showingSubscriptionMessage = storage.showingSubscriptionMessage await new Promise(r => setTimeout(r, 500)) } - // Assert that subPage is null or undefined - expect(subPage).toBeFalsy() + expect(showingSubscriptionMessage < (Date.now() + 7 * 60000)).toBeTruthy() }) process.kill()