mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-10-21 05:01:26 -07:00
* 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
107 lines
3.0 KiB
TypeScript
107 lines
3.0 KiB
TypeScript
import { once } from 'node:events';
|
|
import http from 'node:http';
|
|
import type { AddressInfo } from 'node:net';
|
|
|
|
export type AnkiRequest = { action: string; params: Record<string, unknown> };
|
|
|
|
export type FakeAnki = {
|
|
url: string;
|
|
/** Every AnkiConnect action received so far, with `multi` batches flattened. */
|
|
requests: AnkiRequest[];
|
|
stop: () => Promise<void>;
|
|
};
|
|
|
|
export const FAKE_ANKI_DECK = 'E2E';
|
|
export const FAKE_ANKI_MODEL = 'Lapis';
|
|
|
|
const MODEL_FIELDS = [
|
|
'Expression',
|
|
'ExpressionFurigana',
|
|
'ExpressionReading',
|
|
'ExpressionAudio',
|
|
'SelectionText',
|
|
'MainDefinition',
|
|
'Sentence',
|
|
'SentenceFurigana',
|
|
'SentenceAudio',
|
|
'Picture',
|
|
'Glossary',
|
|
'IsWordAndSentenceCard',
|
|
'IsClickCard',
|
|
'IsSentenceCard',
|
|
'IsAudioCard',
|
|
'MiscInfo',
|
|
];
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
// A recording AnkiConnect stand-in: an empty collection with one deck and one
|
|
// Lapis-shaped note type. Scenarios assert on `requests`, not on stored notes.
|
|
export async function startFakeAnki(): Promise<FakeAnki> {
|
|
const requests: AnkiRequest[] = [];
|
|
let nextNoteId = 1000;
|
|
|
|
const handle = (body: unknown): unknown => {
|
|
if (!isRecord(body) || typeof body.action !== 'string') return null;
|
|
const params = isRecord(body.params) ? body.params : {};
|
|
if (body.action === 'multi') {
|
|
return Array.isArray(params.actions) ? params.actions.map(handle) : [];
|
|
}
|
|
requests.push({ action: body.action, params });
|
|
switch (body.action) {
|
|
case 'version':
|
|
return 6;
|
|
case 'deckNames':
|
|
return [FAKE_ANKI_DECK];
|
|
case 'modelNames':
|
|
return [FAKE_ANKI_MODEL];
|
|
case 'modelFieldNames':
|
|
return MODEL_FIELDS;
|
|
case 'addNote':
|
|
return nextNoteId++;
|
|
case 'findNotes':
|
|
case 'notesInfo':
|
|
return [];
|
|
case 'storeMediaFile':
|
|
return params.filename ?? null;
|
|
default:
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const server = http.createServer((request, response) => {
|
|
response.setHeader('content-type', 'application/json');
|
|
// GET exposes the request log so the session CLI can read it from outside.
|
|
if (request.method === 'GET') {
|
|
response.end(JSON.stringify(requests));
|
|
return;
|
|
}
|
|
const chunks: Buffer[] = [];
|
|
request.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
request.on('end', () => {
|
|
let body: unknown = null;
|
|
try {
|
|
body = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
} catch {
|
|
// Malformed bodies get the same null result AnkiConnect gives unknown actions.
|
|
}
|
|
response.end(JSON.stringify({ result: handle(body), error: null }));
|
|
});
|
|
});
|
|
server.listen(0, '127.0.0.1');
|
|
await once(server, 'listening');
|
|
const { port } = server.address() as AddressInfo;
|
|
|
|
return {
|
|
url: `http://127.0.0.1:${port}`,
|
|
requests,
|
|
stop: async () => {
|
|
server.closeAllConnections();
|
|
server.close();
|
|
await once(server, 'close');
|
|
},
|
|
};
|
|
}
|