]> git.ardis.lu - envpass/commitdiff
Add tests to cover window expiration scenario
authorArdis Lu <ardisglu@gmail.com>
Sun, 2 Aug 2026 04:47:24 +0000 (21:47 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 2 Aug 2026 04:47:24 +0000 (21:47 -0700)
test/index.test.js
test/setup.js

index 0b8e2c81110d6540b042ad201334c81c6635ec73..fa89fa880fcea198f31c9d2060d5f1a3dd0de20b 100644 (file)
@@ -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);
index 698b8dfcc4952fcbcad82d1460beb23c68bbe765..c7dd4ca979bf6fabd72a9cef74412d0acf00b711 100644 (file)
@@ -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<void>} Promise which resolves when the Playwright browser has opened the page.
  */
 
 export function mockOpen(t, page) {
+  const { promise, resolve } = /** @type {PromiseWithResolvers<void>}*/(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;
 }
 
 /**
This page took 0.045757 seconds and 4 git commands to generate.