Skip to content

An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.

test(daemon): pin pairing codes and session keys, the lockfile and where state lives, and the log

Shahriare4bebc678ad7
diff --git a/src/daemon/auth-store.test.ts b/src/daemon/auth-store.test.ts
new file mode 100644
index 0000000..f6bb5fa
--- /dev/null
+++ b/src/daemon/auth-store.test.ts
@@ -0,0 +1,112 @@
+import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import {
+ clearAuth,
+ consumePairing,
+ createPairing,
+ createSession,
+ hasPendingPairing,
+ listSessions,
+ pendingPairings,
+ revokeSessions,
+ sessionFor,
+ touchSession,
+} from './auth-store';
+import { stateDir } from './lockfile';
+
+const authPath = join(stateDir, 'auth.json');
+const extension = 'chrome-extension://abcdefghijklmnopabcdefghijklmnop';
+
+beforeEach(() => {
+ mkdirSync(stateDir, { recursive: true });
+ clearAuth();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('pairing codes', () => {
+ test('a code is eight characters that cannot be misread, valid for ten minutes', () => {
+ vi.useFakeTimers({ now: new Date('2026-09-21T10:00:00Z') });
+ const { code, expiresAt } = createPairing();
+ expect([/^[ABCDEFGHJKMNPQRSTWXYZ23456789]{8}$/.test(code), expiresAt - Date.now()]).toEqual([true, 10 * 60_000]);
+ });
+
+ test('a new code replaces the one before it', () => {
+ createPairing();
+ const { code } = createPairing();
+ expect(pendingPairings()).toEqual([code]);
+ });
+
+ test('a code stops being pending once it expires', () => {
+ vi.useFakeTimers({ now: new Date('2026-09-21T10:00:00Z') });
+ createPairing();
+ vi.setSystemTime(new Date('2026-09-21T10:10:01Z'));
+ expect([pendingPairings(), hasPendingPairing()]).toEqual([[], false]);
+ });
+
+ test('a code can be used once', () => {
+ const { code } = createPairing();
+ consumePairing(code);
+ expect(hasPendingPairing()).toBe(false);
+ });
+});
+
+describe('sessions', () => {
+ test("pairing gives the browser a 256-bit key, bound to the extension's origin", () => {
+ const session = createSession(extension, '0.6.2');
+ expect([Buffer.from(session.key, 'base64url').length, sessionFor(extension)]).toEqual([32, session]);
+ });
+
+ test('pairing the same browser again replaces its key rather than adding a second', () => {
+ const first = createSession(extension, '0.6.1');
+ const second = createSession(extension, '0.6.2');
+ expect([listSessions(), first.key === second.key]).toEqual([[second], false]);
+ });
+
+ test('seeing a browser again updates when it was last seen, and nothing else', () => {
+ vi.useFakeTimers({ now: new Date('2026-09-21T10:00:00Z') });
+ const paired = createSession(extension, '0.6.2');
+ vi.setSystemTime(new Date('2026-09-21T12:00:00Z'));
+ touchSession(extension);
+ expect(sessionFor(extension)).toEqual({ ...paired, lastSeenAt: '2026-09-21T12:00:00.000Z' });
+ });
+
+ test('seeing a browser that never paired changes nothing', () => {
+ touchSession('moz-extension://stranger');
+ expect(listSessions()).toEqual([]);
+ });
+
+ test('revoking removes the sessions that match, and says how many', () => {
+ createSession(extension, '0.6.2');
+ createSession('moz-extension://firefox-uuid', '0.6.2');
+ expect([revokeSessions((session) => session.origin.startsWith('moz-')), listSessions().map((session) => session.origin)]).toEqual([1, [extension]]);
+ });
+
+ test('a revoked browser has no key left to present', () => {
+ createSession(extension, '0.6.2');
+ revokeSessions(() => true);
+ expect(sessionFor(extension)).toBeNull();
+ });
+});
+
+describe('the file', () => {
+ test('is readable only by the user', () => {
+ createSession(extension, '0.6.2');
+ expect(statSync(authPath).mode & 0o777).toBe(0o600);
+ });
+
+ test('a damaged file pairs nothing, and the next write replaces it', () => {
+ writeFileSync(authPath, '{"sessions": [');
+ const session = createSession(extension, '0.6.2');
+ expect(JSON.parse(readFileSync(authPath, 'utf8'))).toEqual({ pairings: [], sessions: [session] });
+ });
+
+ test('clearing it forgets every browser', () => {
+ createSession(extension, '0.6.2');
+ clearAuth();
+ expect([existsSync(authPath), listSessions()]).toEqual([false, []]);
+ });
+});
diff --git a/src/daemon/lockfile.test.ts b/src/daemon/lockfile.test.ts
new file mode 100644
index 0000000..8a182f7
--- /dev/null
+++ b/src/daemon/lockfile.test.ts
@@ -0,0 +1,85 @@
+import { spawnSync } from 'node:child_process';
+import { mkdirSync, statSync, writeFileSync } from 'node:fs';
+import { homedir } from 'node:os';
+import { join } from 'node:path';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import { clearLockfile, isRunning, lockfilePath, readLockfile, stateDir, writeLockfile, type Lockfile } from './lockfile';
+import { extensionDir, installStampPath, userDir } from './paths';
+
+const lock: Lockfile = { pid: process.pid, port: 49_152, token: 'secret-token', protocolVersion: 9, daemonVersion: '0.6.2' };
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+});
+
+describe('where state lives', () => {
+ const pathsWith = async (home: string | undefined) => {
+ vi.resetModules();
+ vi.stubEnv('BROWSENTIC_HOME', home);
+ return import('./paths');
+ };
+
+ test('BROWSENTIC_HOME moves the private state', async () => {
+ expect((await pathsWith('/tmp/elsewhere')).stateDir).toBe('/tmp/elsewhere');
+ });
+
+ test('without it, state is ~/.browsentic', async () => {
+ expect((await pathsWith(undefined)).stateDir).toBe(join(homedir(), '.browsentic'));
+ });
+
+ // Chrome derives an unpacked extension's id from its path, so this one must not follow BROWSENTIC_HOME.
+ test('the files you open, and the extension, stay in ~/browsentic wherever state goes', async () => {
+ const paths = await pathsWith('/tmp/elsewhere');
+ expect([paths.userDir, paths.extensionDir()]).toEqual([join(homedir(), 'browsentic'), join(homedir(), 'browsentic', 'extension', 'chrome-mv3')]);
+ });
+
+ test('setup --dir can put the extension somewhere else', () => {
+ expect(extensionDir('/var/app/browsentic')).toBe('/var/app/browsentic');
+ });
+
+ test('the install stamp sits in the extension directory', () => {
+ expect(installStampPath(join(userDir, 'extension'))).toBe(join(userDir, 'extension', '.browsentic-install.json'));
+ });
+});
+
+describe('the lockfile', () => {
+ beforeEach(() => {
+ mkdirSync(stateDir, { recursive: true });
+ clearLockfile();
+ });
+
+ test('what is written is what is read back', () => {
+ writeLockfile(lock);
+ expect(readLockfile()).toEqual(lock);
+ });
+
+ test('it holds the control token, so only the user can read it', () => {
+ writeLockfile(lock);
+ expect(statSync(lockfilePath).mode & 0o777).toBe(0o600);
+ });
+
+ test('no lockfile means no daemon', () => {
+ expect(readLockfile()).toBeNull();
+ });
+
+ test('a lockfile without a port and token is no daemon either', () => {
+ writeFileSync(lockfilePath, JSON.stringify({ pid: 1, port: '8765' }));
+ expect(readLockfile()).toBeNull();
+ });
+
+ test('a lockfile that is not JSON is no daemon', () => {
+ writeFileSync(lockfilePath, 'daemon.json');
+ expect(readLockfile()).toBeNull();
+ });
+});
+
+describe('whether a daemon is still running', () => {
+ test('this process is', () => {
+ expect(isRunning(process.pid)).toBe(true);
+ });
+
+ test('one that has exited is not', () => {
+ const exited = spawnSync(process.execPath, ['-e', '']).pid;
+ expect(isRunning(exited)).toBe(false);
+ });
+});
diff --git a/src/daemon/log.test.ts b/src/daemon/log.test.ts
new file mode 100644
index 0000000..09301b0
--- /dev/null
+++ b/src/daemon/log.test.ts
@@ -0,0 +1,40 @@
+import { existsSync, readFileSync, rmSync, truncateSync, writeFileSync } from 'node:fs';
+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
+import { logPath } from './lockfile';
+import { log } from './log';
+
+beforeEach(() => {
+ rmSync(logPath, { force: true });
+ rmSync(`${logPath}.1`, { force: true });
+});
+
+afterEach(() => {
+ vi.unstubAllEnvs();
+ vi.restoreAllMocks();
+});
+
+describe('the daemon log', () => {
+ test('each line is stamped, with any detail after the message', () => {
+ log('probe finished', { ready: true });
+ expect(readFileSync(logPath, 'utf8')).toMatch(/^\d{4}-\d\d-\d\dT[\d:.]+Z probe finished {"ready":true}\n$/);
+ });
+
+ test('an error is written as its name and message, not its stack', () => {
+ log('spawn failed', new TypeError('bin is not a string'));
+ expect(readFileSync(logPath, 'utf8')).toContain('spawn failed TypeError: bin is not a string\n');
+ });
+
+ test('past 2 MB it moves aside and starts again', () => {
+ writeFileSync(logPath, '');
+ truncateSync(logPath, 2 * 1024 * 1024 + 1);
+ log('fresh start');
+ expect([existsSync(`${logPath}.1`), readFileSync(logPath, 'utf8').includes('fresh start')]).toEqual([true, true]);
+ });
+
+ test('with BROWSENTIC_DEBUG set, each line also goes to stderr', () => {
+ vi.stubEnv('BROWSENTIC_DEBUG', '1');
+ const stderr = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
+ log('debugging', 'detail');
+ expect(String(stderr.mock.calls[0][0])).toMatch(/ debugging detail\n$/);
+ });
+});