Files
SubMiner/e2e/harness/cdp.ts
T
sudacode 78eca52b7e test: add headless end-to-end harness (#286)
* test: add headless end-to-end harness and CI job

Boot the built app against a real mpv, a synthetic clip, a fixture Yomitan
dictionary, and a fake AnkiConnect inside a private Xvfb display, then assert
on the overlay DOM and the AnkiConnect requests. Adds the test:e2e lane, an
e2e session CLI for exploratory checks, and a three-platform CI job.

* ci: set e2e temp dir from a step

* test: make e2e session startup robust on slow machines

Bun caps node:test hooks at 5s, so boot the session inside the first test.
Keep pausing until the app's resume retries stop, read cue tokens in one
round trip, and resolve Electron through its package entry so CI downloads
the binary.

* test: harden e2e harness for CI runners

Fail fast when mpv cannot be spawned, read every Yomitan popup frame instead
of trusting target order, and put the chocolatey mpv directory on PATH.

* test: add e2e failure diagnostics and mark Windows job experimental

Report what the overlay showed when a cue never renders, dispose the session
when the overlay cannot be opened, skip popup frames that vanish mid-poll,
and turn off AniSkip lookups in the harness config.

* test: read the lookup popup entry from textContent

* test: capture renderer console and main-side subtitle state in e2e failures

* test: expose the Electron main-process inspector to the e2e harness

Adds session.mainProcess() and 'e2e eval main', and reports main's window
list alongside the overlay state when a cue never renders.

* test: tap subtitle sends and receipts in e2e failure diagnostics

* test: probe alternate main-to-renderer paths in e2e failure diagnostics

* test: select the visible overlay layer, not the primed modal window

Windows and macOS pre-create a hidden modal window that loads the same
renderer document, and the harness could attach to it instead of the
visible overlay. Drops the delivery taps that found this.

* ci: run the Windows e2e job as a normal matrix entry

* test: address review on e2e harness

Slice the app log at a byte offset before decoding, create the session state
file owner-only since it carries the inspector URL, and keep the cue failure
when inspector diagnostics are unavailable.

* test: create the e2e session state file exclusively
2026-10-05 22:00:58 -07:00

134 lines
4.9 KiB
TypeScript

import { waitUntil, type Truthy, type WaitOptions } from './wait';
export type CdpTarget = {
type: string;
title: string;
url: string;
webSocketDebuggerUrl: string;
};
export type CdpPage = {
/** Sends a raw DevTools protocol command, for anything the helpers below do not cover. */
send: (method: string, params?: Record<string, unknown>) => Promise<Record<string, unknown>>;
/** Evaluates an expression in the page, awaiting promises, and returns its JSON value. */
evaluate: <T>(expression: string) => Promise<T>;
/** Polls an expression until it is truthy and returns that value. */
waitFor: <T>(expression: string, options: WaitOptions) => Promise<Truthy<T>>;
/** Console output and uncaught exceptions seen since connecting, oldest first. */
console: string[];
/** PNG of the page as rendered by Chromium; needs no OS screen-capture permission. */
screenshot: () => Promise<Buffer>;
close: () => void;
};
type CdpReply = {
id?: number;
method?: string;
params?: Record<string, unknown>;
error?: { message: string };
result?: Record<string, unknown>;
};
type RemoteObject = { value?: unknown; description?: string };
// Renders a console/exception event as one log line.
function formatConsoleEvent(method: string, params: Record<string, unknown>): string | null {
if (method === 'Runtime.consoleAPICalled') {
const args = Array.isArray(params.args) ? (params.args as RemoteObject[]) : [];
const text = args.map((arg) => arg.description ?? JSON.stringify(arg.value)).join(' ');
return `console.${String(params.type)}: ${text}`;
}
if (method === 'Runtime.exceptionThrown') {
const details = params.exceptionDetails as
| { text?: string; exception?: RemoteObject }
| undefined;
return `exception: ${details?.exception?.description ?? details?.text ?? 'unknown'}`;
}
if (method === 'Log.entryAdded') {
const entry = params.entry as { level?: string; source?: string; text?: string } | undefined;
return `${String(entry?.source)}.${String(entry?.level)}: ${String(entry?.text)}`;
}
return null;
}
type EvaluateResult = {
result: { value?: unknown };
exceptionDetails?: { text: string; exception?: { description?: string } };
};
export async function listCdpTargets(port: number): Promise<CdpTarget[]> {
const response = await fetch(`http://127.0.0.1:${port}/json/list`);
return (await response.json()) as CdpTarget[];
}
export function connectCdpPage(target: CdpTarget): Promise<CdpPage> {
return connectCdp(target.webSocketDebuggerUrl, target.url);
}
/** Connects to any DevTools endpoint: a renderer page or Electron's main-process inspector. */
export async function connectCdp(webSocketDebuggerUrl: string, label: string): Promise<CdpPage> {
const socket = new WebSocket(webSocketDebuggerUrl);
await new Promise<void>((resolve, reject) => {
socket.addEventListener('open', () => resolve(), { once: true });
socket.addEventListener('error', () => reject(new Error(`CDP connection to ${label} failed`)), {
once: true,
});
});
const pending = new Map<number, (reply: CdpReply) => void>();
const consoleLines: string[] = [];
let nextId = 1;
socket.addEventListener('message', (event) => {
const reply: CdpReply = JSON.parse(String(event.data));
if (reply.id === undefined) {
if (reply.method) {
const line = formatConsoleEvent(reply.method, reply.params ?? {});
if (line) consoleLines.push(line);
}
return;
}
pending.get(reply.id)?.(reply);
pending.delete(reply.id);
});
const send = (method: string, params: Record<string, unknown> = {}) =>
new Promise<Record<string, unknown>>((resolve, reject) => {
const id = nextId++;
pending.set(id, (reply) => {
if (reply.error) reject(new Error(`CDP ${method} failed: ${reply.error.message}`));
else resolve(reply.result ?? {});
});
socket.send(JSON.stringify({ id, method, params }));
});
const evaluate = async <T>(expression: string): Promise<T> => {
const reply = (await send('Runtime.evaluate', {
expression,
returnByValue: true,
awaitPromise: true,
})) as EvaluateResult;
if (reply.exceptionDetails) {
const { text, exception } = reply.exceptionDetails;
throw new Error(`Page evaluation threw: ${exception?.description ?? text}`);
}
return reply.result.value as T;
};
await send('Runtime.enable');
// The Node inspector behind Electron's main process has no Log domain.
await send('Log.enable').catch(() => undefined);
return {
send,
evaluate,
console: consoleLines,
waitFor: <T>(expression: string, options: WaitOptions) =>
waitUntil(() => evaluate<T>(expression), options),
screenshot: async () => {
const reply = await send('Page.captureScreenshot', { format: 'png' });
return Buffer.from(String(reply.data), 'base64');
},
close: () => socket.close(),
};
}