Task 2 of Plan 01-11 (Puppeteer UAT harness).
Test hook surface:
- src/test-hooks/types.ts: canonical MokoshTestSurface — handlers
(onClicked, onStartup, notificationOnClicked), notificationCount,
lastNotificationOptions<true>, notificationIds, getCurrentStream,
getSegmentCount. globalThis.__mokoshTest ambient declaration.
- src/test-hooks/sw-hooks.ts: SW-side hook. Monkey-patches addListener
on chrome.action.onClicked / chrome.runtime.onStartup / chrome
.notifications.onClicked to capture handler refs while chaining to
the original. Wraps chrome.notifications.create across all four
overload shapes (id+options+cb, options+cb, id+options→Promise,
options→Promise) to increment notificationCount, save
lastNotificationOptions, push resolved id into notificationIds.
- src/test-hooks/offscreen-hooks.ts: offscreen-side hook. Exports
setCurrentStream + setSegmentCountGetter; the recorder calls both
inside startRecording after the mediaStream + segments assignments.
getCurrentStream getter closes over the cell so the harness reads
the live MediaStream for displaySurface inspection + 'ended'
dispatch (Bug B BLOCKER per RESEARCH §7).
- tests/uat/lib/test-hook-contract.d.ts: manual harness-side mirror of
MokoshTestSurface (decoupled from src/ to keep tests/ import-clean
per RESEARCH §11 resolution 5; drift risk documented inline).
Production-side wires (gated by __MOKOSH_UAT__ token):
- src/background/index.ts top-of-module: `if (__MOKOSH_UAT__) { await
import('../test-hooks/sw-hooks'); }`. MUST run before any chrome.*
addListener call below — top-of-module placement satisfies this.
- src/offscreen/recorder.ts top-of-module: symmetric gated dynamic
import + module-scoped testHooks reference.
- src/offscreen/recorder.ts inside startRecording (after mediaStream
assignment): `if (__MOKOSH_UAT__) { testHooks?.setCurrentStream(stream);
testHooks?.setSegmentCountGetter(() => segments.length); }`
- src/offscreen/recorder.ts inside onUserStoppedSharing (after
mediaStream = null): `if (__MOKOSH_UAT__) { testHooks?.setCurrentStream(null); }`
— T-1-11-05 (Repudiation: stale stream ref) mitigation.
Build-time token wiring:
- vite.config.ts: declares `define: { __MOKOSH_UAT__: 'false' }` (prod
default) + bumps `build.target: 'es2022'` so the top-level await in
the gated dynamic imports compiles (MDN: Chrome 89 / Edge 89 /
Firefox 89 / Safari 15 support TLA; MV3 floor Chrome 88 is
effectively Chrome 89+ in field — comfortably inside the envelope).
- vite.test.config.ts: overrides `define: { __MOKOSH_UAT__: 'true' }`
so the test bundle has the hooks active.
- vitest.config.ts: declares `define: { __MOKOSH_UAT__: 'false' }` for
vitest's own source-loading runs. CRITICAL — without this, vitest
would throw `ReferenceError: __MOKOSH_UAT__ is not defined` when
loading src/background/index.ts; OR if we'd used `import.meta.env.MODE
=== 'test'` (RESEARCH §6's initial guidance), vitest's default
MODE='test' would have ACTIVATED the hooks under unit tests +
clobbered every existing vi.fn() chrome.notifications.create mock.
The dedicated `__MOKOSH_UAT__` token sidesteps both failure modes
cleanly — a refinement on RESEARCH §6 documented in the comment
preambles of all three configs.
- globals.d.ts: declares `__MOKOSH_UAT__: boolean` ambient so
`npx tsc --noEmit` passes without per-file annotations.
- tsconfig.json: include adds `globals.d.ts`.
Notification options generic refinement:
- chrome.notifications.NotificationOptions is declared with a
`<true | false>` generic distinguishing "create" (all required —
true) from "update" (all optional — false). Plan 01-11's production
code always uses the create shape; types.ts + sw-hooks.ts pin to
`NotificationOptions<true>` so the harness reads iconUrl etc. as
definitely-present.
Verification:
- npx tsc --noEmit: exit 0
- npm run build: exit 0
- grep -rln '__mokoshTest\|simulateUserStop\|getSegmentCount\|setCurrentStream\|setSegmentCountGetter' dist/:
ZERO matches (Tier-1 gate stays GREEN)
- npm run build:test: exit 0; dist-test/ emits separate sw-hooks-*.js
+ offscreen-hooks-*.js chunks (the gated dynamic imports survive
tree-shaking when __MOKOSH_UAT__ === true)
- grep -rln '__mokoshTest' dist-test/: 2 matches
(assets/sw-hooks-*.js + assets/offscreen-hooks-*.js)
- SKIP_BUILD=1 npx vitest run: 89/89 GREEN
(83 baseline + 6 Tier-1 hook-leak surfaces)
- sw-bundle-import.test.ts: GREEN (the gated dynamic import does not
break production module init — the `if (false)` branch is never
reachable so the await + import are dead code in dist/)
In-flight bugs auto-fixed (Rule 1 + Rule 3):
- Rule 3: original RESEARCH §6 plan called for `import.meta.env.MODE
=== 'test'` as the gate; switched to `__MOKOSH_UAT__` define-token
after observing vitest contamination (vitest defaults MODE='test'
→ hooks activated under unit tests → 8 existing tests broke with
"Cannot read properties of undefined (reading 'calls')" because the
hook wrapper replaced vi.fn() mocks). Documented in the comment
preambles of all three configs as a refinement on RESEARCH §6.
- Rule 3: esbuild rejected TLA against the default ES2020 target;
bumped to es2022 (Chrome 89+ supports TLA per MDN — inside MV3
envelope). Recorded in vite.config.ts preamble.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
56 lines
2.6 KiB
TypeScript
56 lines
2.6 KiB
TypeScript
// vite.test.config.ts — Plan 01-11 two-bundle separation.
|
|
//
|
|
// Extends the production `./vite.config.ts` with two delta knobs:
|
|
// 1. `mode: 'test'` — Vite statically replaces `import.meta.env.MODE`
|
|
// everywhere in the input source with the string literal `'test'`.
|
|
// The gated dynamic imports in src/background/index.ts +
|
|
// src/offscreen/recorder.ts (Plan 01-11 Task 2) take the form
|
|
// `if (import.meta.env.MODE === 'test') { await import('../test-hooks/...'); }`.
|
|
// With mode='test' the comparison resolves to a live branch and
|
|
// Rollup KEEPS the dynamic import; with the default mode='production'
|
|
// the comparison is a static dead branch and Rollup tree-shakes the
|
|
// `await import` away entirely (verified by the Tier-1 grep gate
|
|
// `tests/background/no-test-hooks-in-prod-bundle.test.ts`).
|
|
// 2. `build.outDir: 'dist-test'` + `emptyOutDir: true` — emit to a
|
|
// SEPARATE directory so a `npm run build` immediately after this
|
|
// build does not clobber. Puppeteer harness consumes this path via
|
|
// `puppeteer.launch({ enableExtensions: [<abs-path-to-dist-test>] })`.
|
|
//
|
|
// References:
|
|
// - Vite mergeConfig: https://vite.dev/guide/api-javascript.html#mergeconfig
|
|
// - Vite environment variables: https://vite.dev/guide/env-and-mode.html
|
|
// - Rollup tree-shaking literal-comparison dead branches:
|
|
// https://rollupjs.org/plugin-development/#how-rollup-handles-dynamic-imports
|
|
|
|
import { defineConfig, mergeConfig, type UserConfigExport } from 'vite';
|
|
import baseConfig from './vite.config';
|
|
|
|
const baseAsExport: UserConfigExport = baseConfig;
|
|
|
|
export default defineConfig(({ command, mode }) =>
|
|
mergeConfig(
|
|
typeof baseAsExport === 'function'
|
|
? baseAsExport({ command, mode, isPreview: false, isSsrBuild: false })
|
|
: baseAsExport,
|
|
{
|
|
mode: 'test',
|
|
// `define` performs a static text replacement at build time. We use a
|
|
// dedicated `__MOKOSH_UAT__` token (NOT `import.meta.env.MODE`) for the
|
|
// hook-import gate because vitest defaults its mode to 'test' too —
|
|
// gating on MODE would activate the hooks during unit tests and
|
|
// overwrite their vi.fn() mocks for chrome.notifications.create etc.
|
|
// The dedicated token is `false` everywhere except in this test bundle
|
|
// (Plan 01-11 RESEARCH §6 augmented — keeps Vite tree-shake semantics
|
|
// intact while sidestepping the vitest cross-contamination).
|
|
// Reference: https://vite.dev/config/shared-options.html#define
|
|
define: {
|
|
__MOKOSH_UAT__: 'true',
|
|
},
|
|
build: {
|
|
outDir: 'dist-test',
|
|
emptyOutDir: true,
|
|
},
|
|
},
|
|
),
|
|
);
|