import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from 'bun:test'; import { Database } from 'bun:sqlite'; import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import { EventEmitter } from 'node:events'; import { BrowserManager } from '../src/browser-manager'; import { getCookieProfiles, parseCookieImportArgs, runCookieImport } from '../src/cookie-import-operation'; import { generatePickerCode, handleCookiePickerRoute, hasActivePicker } from '../src/cookie-picker-routes'; import { handleReadCommand } from '../src/read-commands'; import { handleWriteCommand } from '../src/write-commands'; import * as importer from '../src/cookie-import-browser'; let home: string; let homeMock: ReturnType; let currentUrl: string; let page: any; let context: any; let session: any; let bm: BrowserManager; let cdp: any; function installProfile(name = 'Default', domain = '.example.test', badCookie = false) { const dir = path.join(home, '.config/chromium', name); fs.mkdirSync(dir, { recursive: true }); expect(fs.realpathSync(dir).startsWith(fs.realpathSync(home) + path.sep)).toBe(true); const db = new Database(path.join(dir, 'Cookies')); db.run('CREATE TABLE cookies (host_key TEXT, name TEXT, value TEXT, encrypted_value BLOB, path TEXT, expires_utc INTEGER, is_secure INTEGER, is_httponly INTEGER, has_expires INTEGER, samesite INTEGER)'); db.run('INSERT INTO cookies VALUES (?, ?, ?, ?, ?, 0, 1, 1, 0, 1)', [domain, 'session', 'synthetic-session', Buffer.alloc(0), '/']); if (badCookie) db.run('INSERT INTO cookies VALUES (?, ?, ?, ?, ?, 0, 1, 1, 0, 1)', [domain, 'broken', '', Buffer.from('v20synthetic'), '/']); db.close(); } async function route(method: string, pathname: string, body?: unknown, cookie?: string) { const url = new URL('http://127.0.0.1:9470/cookie-picker' + pathname); let pickerInstance = ''; if (cookie) { const document = await handleCookiePickerRoute(new URL(url.origin + '/cookie-picker'), new Request(url.origin + '/cookie-picker', { headers: { Cookie: cookie }, }), bm, 'fixture'); const html = await document.text(); pickerInstance = JSON.parse(html.match(/