Task 3 of Plan 01-11 (Puppeteer UAT harness).
Harness file tree (tests/uat/):
- harness.test.ts: tsx-runnable top-to-bottom harness entry point.
Runs A0 inline (filesystem grep gate, abort-on-fail T-1-11-01),
then launches Chrome + opens popup bridge + queries manifest, then
iterates A1-A13 stubs. Each stub throws "NOT YET IMPLEMENTED —
Plan 01-11 Task N wires this assertion". Exit code = 0 on full
pass, 1 otherwise. Final line: "UAT harness: N/14 assertions passed".
- lib/launch.ts: launchHarnessBrowser() — wraps puppeteer.launch with
enableExtensions:[dist-test/], headless default (HEADLESS=0
override), --no-sandbox + --auto-select-desktop-capture-source flags.
Polls browser.extensions() until the extension registers (empirically
~100ms but the first call right after launch returns Map(0)).
Opens both a blank page (for triggerExtensionAction) AND the popup
page (the bridge surface). Returns { browser, extension, extensionId,
sw, downloadsDir, page, popup }.
- lib/extension.ts: waitForOffscreenTarget + attachToOffscreen +
countOffscreenTargets. Offscreen attach uses target.type() ===
'background_page' + .asPage() (NOT .page() — RESEARCH §4 Pitfall 1).
- lib/sw.ts: chrome.* state queries via the POPUP page handle (NOT
the WebWorker handle — see architecture note below). getBadgeText,
getPopup, getManifest, getIconSize, getIsRecording (side-channeled
through badge text), fireOnStartup (via __mokoshTestQuery bridge),
sendSyntheticRecordingError, getNotificationSnapshot (via bridge),
keepalivePing (no-op message to wake SW for ~30s).
- lib/offscreen.ts: getDisplaySurface, simulateUserStop (the
dispatchEvent('ended') path per RESEARCH §7 BLOCKER — DO NOT REFACTOR
to track.stop()), getSegmentCount.
- lib/assertions.ts: runAssertion(idx, name, buffers, fn) wrapper —
records pass/fail/duration; on failure dumps last 30 lines of SW
+ offscreen console buffers to stderr before rethrowing. assertEqual
/ assertMatch / assertTrue / assertGte / waitFor polling helper.
- lib/zip.ts: jszip-based assertArchiveShape + extractEntryToFile for
assertions 12 + 13.
- README.md: runtime + local-debug + CI semantics + locale gotcha
+ dev-dep size note + assertion catalog table.
- tsconfig.json: per-tree type-check config (mirrors root tsconfig.json
compiler options but includes the harness tree explicitly).
Architecture refinement (DEVIATION from RESEARCH §1 — Rule 1+3 inline fix):
- RESEARCH §1 sketched `sw.evaluate(() => chrome.action.getBadgeText({}))`
as the chrome.* query path. Empirical probes during Task 3 execution
against Puppeteer 25.0.2 + Chrome 148 + --headless=true revealed two
blockers:
1. Puppeteer's WebWorker.evaluate runs in an ISOLATED WORLD that
carries SW globals (clients, registration, ...) but NOT the
extension's full chrome.* API surface. Object.keys(chrome) inside
sw.evaluate returns ["loadTimes","csi"] — the public webpage
chrome, not the extension chrome.
2. Chrome 148's headless mode aggressively suspends MV3 service
workers; subsequent swTarget.worker() calls return
"Protocol error: No target with given id found".
- WORKAROUND: open the popup page (chrome-extension://<id>/src/popup/
index.html) as a separate Puppeteer Page. The popup has full
chrome.* access (it's an extension context with same privileges as
the SW) AND stable Puppeteer lifetime. For SW-globalThis state
(__mokoshTest in the SW isolate, NOT in the popup), bridge via
chrome.runtime.sendMessage. The popup sends
{ type: '__mokoshTestQuery', op: 'snapshot' | 'fire-on-startup' |
'handler-types' }; the SW hook's onMessage handler responds.
- Bridge implementation added to src/test-hooks/sw-hooks.ts — registers
AFTER the production listeners so it never intercepts production
messages (__mokoshTest* type is unambiguously test-only). Tier-1
grep gate (no-test-hooks-in-prod-bundle.test.ts) continues to enforce
ZERO __mokoshTest occurrences in dist/ — the bridge handler is
tree-shaken alongside the rest of the hook module via the
__MOKOSH_UAT__ gate.
Other configuration changes:
- vitest.config.ts: exclude tests/uat/** from vitest discovery. The
Puppeteer harness is invoked via `npm run test:uat` (not vitest);
running it under vitest would try to launch real Chrome inside a
vitest worker. The .test.ts suffix is retained for editor +
naming-convention consistency with the rest of the tree.
Verification:
- npx tsc --noEmit (src/): exit 0
- npx tsc --noEmit -p tests/uat: exit 0
- npm run build: exit 0
- grep -rln '__mokoshTest|simulateUserStop|getSegmentCount|setCurrentStream|setSegmentCountGetter|__mokoshTestQuery|__mokoshKeepalive' dist/: ZERO matches
- npm run build:test: exit 0; dist-test/ populated with the new bridge code
- SKIP_BUILD=1 npx vitest run: 89/89 GREEN
- SKIP_PROD_REBUILD=1 npx tsx tests/uat/harness.test.ts:
→ A0 [PASS]: production bundle has no test-hook leaks (19ms)
→ Browser launches; popup opens; manifest read succeeds
→ A1-A13 [FAIL]: NOT YET IMPLEMENTED — Plan 01-11 Task N wires this
→ "UAT harness: 1/14 assertions passed, 13 failed (first failure: A1)"
→ Exit code: 1 (expected — 13 RED stubs intentional)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
122 lines
4.0 KiB
TypeScript
122 lines
4.0 KiB
TypeScript
// tests/uat/lib/zip.ts — Plan 01-11 harness archive-shape helper.
|
|
//
|
|
// Assertion 13 verifies the session_report_*.zip produced by the SW's
|
|
// saveArchive contains:
|
|
// - `video/last_30sec.webm` (non-zero size)
|
|
// - `meta.json` whose parsed JSON has `version === <manifest.version>`
|
|
//
|
|
// References:
|
|
// - JSZip: https://stuk.github.io/jszip/documentation/api_jszip.html
|
|
// - Plan 01-07 archive shape (session_report contract):
|
|
// .planning/phases/01-stabilize-video-pipeline/01-07-PLAN.md
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
|
|
import JSZip from 'jszip';
|
|
|
|
/**
|
|
* Outcome of an archive shape inspection. `errors` lists every
|
|
* missing-file / wrong-size / version-mismatch finding.
|
|
*/
|
|
export interface ArchiveShapeResult {
|
|
readonly hasVideoEntry: boolean;
|
|
readonly videoSizeBytes: number;
|
|
readonly hasMetaEntry: boolean;
|
|
readonly metaJson: { version?: unknown } | null;
|
|
readonly errors: ReadonlyArray<string>;
|
|
}
|
|
|
|
/**
|
|
* Open a downloaded session_report_*.zip and verify its shape.
|
|
*
|
|
* @param zipPath - Absolute path to the downloaded .zip file.
|
|
* @param expectedVersion - The version string from chrome.runtime.getManifest().version.
|
|
* @returns Structured shape result. `errors` non-empty == assertion failure.
|
|
*/
|
|
export async function assertArchiveShape(
|
|
zipPath: string,
|
|
expectedVersion: string,
|
|
): Promise<ArchiveShapeResult> {
|
|
const zipBuf = readFileSync(zipPath);
|
|
const zip = await JSZip.loadAsync(zipBuf);
|
|
const errors: string[] = [];
|
|
|
|
// video/last_30sec.webm presence + size
|
|
const videoEntry = zip.file('video/last_30sec.webm');
|
|
let hasVideoEntry = false;
|
|
let videoSizeBytes = 0;
|
|
if (videoEntry === null) {
|
|
errors.push('video/last_30sec.webm entry missing from archive');
|
|
} else {
|
|
hasVideoEntry = true;
|
|
const videoBuf = await videoEntry.async('uint8array');
|
|
videoSizeBytes = videoBuf.byteLength;
|
|
if (videoSizeBytes === 0) {
|
|
errors.push('video/last_30sec.webm entry is zero bytes (no captured video)');
|
|
}
|
|
}
|
|
|
|
// meta.json presence + version match
|
|
const metaEntry = zip.file('meta.json');
|
|
let hasMetaEntry = false;
|
|
let metaJson: { version?: unknown } | null = null;
|
|
if (metaEntry === null) {
|
|
errors.push('meta.json entry missing from archive');
|
|
} else {
|
|
hasMetaEntry = true;
|
|
const metaText = await metaEntry.async('string');
|
|
try {
|
|
metaJson = JSON.parse(metaText) as { version?: unknown };
|
|
} catch (parseErr) {
|
|
const msg = parseErr instanceof Error ? parseErr.message : String(parseErr);
|
|
errors.push(`meta.json failed to parse as JSON: ${msg}`);
|
|
}
|
|
if (metaJson !== null) {
|
|
if (typeof metaJson.version !== 'string') {
|
|
errors.push(
|
|
`meta.json.version expected string, got ${typeof metaJson.version} (${JSON.stringify(metaJson.version)})`,
|
|
);
|
|
} else if (metaJson.version !== expectedVersion) {
|
|
errors.push(
|
|
`meta.json.version mismatch — expected "${expectedVersion}", got "${metaJson.version}"`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
hasVideoEntry,
|
|
videoSizeBytes,
|
|
hasMetaEntry,
|
|
metaJson,
|
|
errors,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Extract a single named entry from a .zip to an absolute filesystem
|
|
* path. Used by assertion 12 (ffprobe gate on video/last_30sec.webm).
|
|
*
|
|
* @param zipPath - Absolute path to the .zip.
|
|
* @param entryName - Name of the entry inside the zip (e.g. 'video/last_30sec.webm').
|
|
* @param outPath - Absolute filesystem path to write the entry to.
|
|
* @returns The number of bytes written.
|
|
* @throws If the entry is missing from the zip.
|
|
*/
|
|
export async function extractEntryToFile(
|
|
zipPath: string,
|
|
entryName: string,
|
|
outPath: string,
|
|
): Promise<number> {
|
|
const { writeFileSync } = await import('node:fs');
|
|
const zipBuf = readFileSync(zipPath);
|
|
const zip = await JSZip.loadAsync(zipBuf);
|
|
const entry = zip.file(entryName);
|
|
if (entry === null) {
|
|
throw new Error(`extractEntryToFile: entry '${entryName}' missing in ${zipPath}`);
|
|
}
|
|
const buf = await entry.async('nodebuffer');
|
|
writeFileSync(outPath, buf);
|
|
return buf.byteLength;
|
|
}
|