From 3190651115dbadb6ffb7574c093bc5df3268d654 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 1 Aug 2026 21:47:24 -0700 Subject: [PATCH] Add tests to cover window expiration scenario --- test/index.test.js | 29 +++++++++++++++++++++++++++-- test/setup.js | 8 +++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 0b8e2c8..fa89fa8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,9 +1,9 @@ import { suite, test } from 'node:test'; -import { deepStrictEqual } from 'node:assert'; +import { deepStrictEqual, rejects } from 'node:assert'; import childProcess from 'node:child_process'; import { encrypt, decrypt } from '#src/index.js'; -import { BROWSER } from '#src/getPrf.js'; +import { BROWSER, WINDOW_EXPIRATION_DURATION } from '#src/getPrf.js'; import { setupPlaywright, setupEnv, mockOpen, assertConsole, MockLogger, fileEqual, fileNotEqual } from '#test/setup.js'; /** Standard value of the .env file used for most setups. */ @@ -86,6 +86,18 @@ suite('e2e', () => { await fileEqual(envFile, env); deepStrictEqual(openMock.mock.calls.length, 0); }); + test('encrypt throws if unable to get passkey', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + const { page, setAutomaticSignIn } = await setupPlaywright(t); + await setAutomaticSignIn(false); + const opened = mockOpen(t, page); + const envFile = await setupEnv(t, STD_ENV); + assertConsole(t, { debug: 0, info: 0, warn: 0, error: 0 }); + + rejects(encrypt({ inFile: envFile }), { name: 'InputError', message: 'Unable to get passkey.' }); + await opened; + t.mock.timers.tick(WINDOW_EXPIRATION_DURATION); + }); test('decrypt does nothing when all variables are already decrypted', async (t) => { const openMock = t.mock.method(BROWSER, 'open'); const envFile = await setupEnv(t, STD_ENV); @@ -107,6 +119,19 @@ suite('e2e', () => { await fileEqual(envFile, env); deepStrictEqual(openMock.mock.calls.length, 0); }); + test('decrypt throws if unable to get passkey', async (t) => { + t.mock.timers.enable({ apis: ['setTimeout'] }); + const { page, setAutomaticSignIn } = await setupPlaywright(t); + await setAutomaticSignIn(false); + const opened = mockOpen(t, page); + const env = 'A=envpass:v1:AAA\nB=envpass:v1:BBB\nC=envpass:v1:CCC\n'; + const envFile = await setupEnv(t, env); + assertConsole(t, { debug: 0, info: 0, warn: 0, error: 0 }); + + rejects(decrypt({ inFile: envFile }), { name: 'InputError', message: 'Unable to get passkey.' }); + await opened; + t.mock.timers.tick(WINDOW_EXPIRATION_DURATION); + }); test('decrypt passes through excess args to execute', async (t) => { const execMock = t.mock.method(childProcess, 'execSync', () => { }); const envFile = await setupEnv(t, STD_ENV); diff --git a/test/setup.js b/test/setup.js index 698b8df..c7dd4ca 100644 --- a/test/setup.js +++ b/test/setup.js @@ -115,12 +115,18 @@ export async function setupFolder(t, env = '') { * Intercept the `open` function to open a Playwright browser to the page instead of the user's own browser. * @param {TestContext} t Test context to mock the `open` function for. * @param {Page} page Playwright `Page` to use to navigate to the URL instead of the user's browser. + * @returns {Promise} Promise which resolves when the Playwright browser has opened the page. */ export function mockOpen(t, page) { + const { promise, resolve } = /** @type {PromiseWithResolvers}*/(Promise.withResolvers()); /** @param {string} url */ - const interceptor = url => { page.goto(url); } + const interceptor = async url => { + await page.goto(url); + resolve(); + } t.mock.method(BROWSER, 'open', interceptor); + return promise; } /** -- 2.47.3