Files
Pradeep ElankumaranandLeone Parise 0ac2490f29 feat: VNC plugin for interactive browser login + storage_state export
VNC plugin (plugins/vnc/) exposes Camoufox's virtual display via noVNC,
enabling interactive login for sites with fingerprint-based session
validation, CAPTCHAs, or MFA prompts.

- Plugin subclasses VirtualDisplay to override Xvfb resolution (default
  1920x1080, configurable via plugins.vnc.resolution or VNC_RESOLUTION)
- vnc-watcher.sh detects Camoufox's dynamically-assigned Xvfb display,
  attaches x11vnc, and proxies via noVNC on port 6080
- Registers GET /sessions/:userId/storage_state to export Playwright
  storageState (cookies + localStorage) after interactive login
- Emits session:storage:export event for persistence plugin integration
- System deps declared in apt.txt, installed via install-plugin-deps.sh

Plugin system changes:
- ctx passed by reference (not spread) so plugins can mutate factories
  like ctx.createVirtualDisplay
- pluginConfig moved to 3rd arg of register(app, ctx, pluginConfig)
- server.js exposes createVirtualDisplay factory + VirtualDisplay class
  on pluginCtx

Dockerfile: fix build for local Makefile (bind mounts from dist/),
name first stage for multi-stage support.

Co-authored-by: Leone Parise <leone.parise@gmail.com>
2026-04-18 17:21:50 -07:00

118 lines
4.1 KiB
JavaScript

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { jest } from '@jest/globals';
import {
getUserPersistencePaths,
loadPersistedStorageState,
persistStorageState,
} from '../../lib/persistence.js';
describe('profile persistence helpers', () => {
let tmpDir;
beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'camofox-persistence-'));
});
afterEach(async () => {
if (tmpDir) {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
test('getUserPersistencePaths is deterministic and stays under root', () => {
const first = getUserPersistencePaths(tmpDir, 'agent/profile:default');
const second = getUserPersistencePaths(tmpDir, 'agent/profile:default');
expect(first).toEqual(second);
expect(first.userDir.startsWith(tmpDir)).toBe(true);
expect(first.storageStatePath.startsWith(first.userDir)).toBe(true);
expect(first.metaPath.startsWith(first.userDir)).toBe(true);
expect(path.basename(first.userDir)).not.toContain('/');
expect(path.basename(first.userDir)).not.toContain(':');
});
test('loadPersistedStorageState returns undefined when no state exists', async () => {
await expect(loadPersistedStorageState(tmpDir, 'user-1')).resolves.toBeUndefined();
});
test('persistStorageState writes storage state and metadata, then load returns the storage path', async () => {
const storageState = {
cookies: [{ name: 'session', value: 'abc', domain: '.example.com', path: '/' }],
origins: [{ origin: 'https://app.example.com', localStorage: [{ name: 'foo', value: 'bar' }] }],
};
const context = {
storageState: jest.fn(async ({ path: targetPath }) => {
await fs.writeFile(targetPath, JSON.stringify(storageState, null, 2));
}),
};
const result = await persistStorageState({
profileDir: tmpDir,
userId: 'user-1',
context,
logger: { warn: jest.fn() },
});
expect(result.persisted).toBe(true);
expect(context.storageState).toHaveBeenCalledTimes(1);
const loadedPath = await loadPersistedStorageState(tmpDir, 'user-1');
expect(loadedPath).toBe(result.storageStatePath);
const meta = JSON.parse(await fs.readFile(result.metaPath, 'utf8'));
expect(meta.userId).toBe('user-1');
expect(meta.storageStatePath).toBe(result.storageStatePath);
});
test('loadPersistedStorageState ignores invalid JSON files', async () => {
const { storageStatePath } = getUserPersistencePaths(tmpDir, 'user-2');
await fs.mkdir(path.dirname(storageStatePath), { recursive: true });
await fs.writeFile(storageStatePath, '{not-json');
await expect(loadPersistedStorageState(tmpDir, 'user-2', { warn: jest.fn() })).resolves.toBeUndefined();
});
test('a failed persist leaves the previous storage-state intact and cleans up tmp files', async () => {
const originalState = {
cookies: [{ name: 'orig', value: 'v1', domain: '.example.com', path: '/' }],
};
const goodContext = {
storageState: jest.fn(async ({ path: targetPath }) => {
await fs.writeFile(targetPath, JSON.stringify(originalState, null, 2));
}),
};
const first = await persistStorageState({
profileDir: tmpDir,
userId: 'user-3',
context: goodContext,
logger: { warn: jest.fn() },
});
expect(first.persisted).toBe(true);
const failingContext = {
storageState: jest.fn(async () => {
throw new Error('simulated crash mid-write');
}),
};
const second = await persistStorageState({
profileDir: tmpDir,
userId: 'user-3',
context: failingContext,
logger: { warn: jest.fn() },
});
expect(second.persisted).toBe(false);
const { userDir, storageStatePath } = getUserPersistencePaths(tmpDir, 'user-3');
const loaded = await loadPersistedStorageState(tmpDir, 'user-3');
expect(loaded).toBe(storageStatePath);
const parsed = JSON.parse(await fs.readFile(storageStatePath, 'utf8'));
expect(parsed).toEqual(originalState);
const leftovers = (await fs.readdir(userDir)).filter((name) => name.includes('.tmp-'));
expect(leftovers).toEqual([]);
});
});