refactor(daemon): package-local runtime fakes for the remaining daemon ports [11a-4]

Merged
TH
2mo ago
+3157-2014

What

Daemon-extraction slice 11a-4: eliminates every edge where a file under src/daemon/ imports (or vi.mocks) a non-package module outside src/daemon/. Residue scan: 0 edges (node scripts/daemon-edge-residue-scan.mjs, committed in this PR; definition below).

Stacked on refactor/daemon-test-data-fakes (#3517).

How

Daemon suites now answer platform questions at the daemon's own ports, and every port member is a plain vi.fn spy with a canned default answer that carries no platform rule (owner decision on review):

  • platform-services port (src/daemon/__tests__/platform-services-fixture.ts) — all six members (appleSessionObservation, deviceBootObservation, ensureLocalDeviceReady, resolveRequestedOpenSurface, validateOpenRelaunchTarget, resolveSessionAppBundleIdForTarget) are spies; the former near-verbatim copies of platform-runtime-open-target.ts, platform-runtime-device-boot.ts and platform-runtime-device-ready.ts are deleted, and so is the shared fixtureResolveSessionAppBundleIdForTarget the lifecycle fixture imported.
  • application-lifecycle runtime binding — resolveOpenTarget, prepareApplicationOpen and openApplication are spies whose canned answers delegate to the contracts' canonical direct-lifecycle members (identity pass-through, readiness pause, admitted-hint apply). The macOS frontmost-surface rule and the android stopped-AVD discovery branch are gone. The real composed fixture lives root-side (src/__tests__/daemon-composition/application-lifecycle-runtime-fixture.ts) and is built on the same seam set, so both lanes observe one spy family; the gateway and session-command harness take an injectable lifecycle-operations loader.
  • runtime-hints — hint apply/clear are observed at the fixture's port spies; assertion bodies unchanged.
  • root-module mocks — the four replay-repair platform-runtime-runtime-hints.ts mocks and the session-open-runtime platform-runtime-apple-application-tools.ts mock were dead (no daemon file loads either module; hint apply/clear reach the device through the admitted binding's operations) and are deleted, not retargeted.

Tests that assert what the REAL composed rules answer do not stay in the daemon lane: they move to the composition lane (src/__tests__/daemon-composition/), where the lane's own fixture hands them the root-composed services and the real composed lifecycle. Assertion bodies are copied verbatim; no assertion was edited to fit a fake. The device-ready suite (it pins what the composed readiness port does per device family) whole-file-moves to the composition lane; 19 composition-lane suites that imported the daemon's services fixture now import the composition lane's own real one.

Rule-ownership relocation (this revision, per review)

Sorted with the throwing-fake / canned-success probes against the spy fixture:

  • Relocated to the composition lane (18 tests, names unchanged): session-open-existing open-target/surface cases ×8 → session-lifecycle/session-open-existing.test.ts; open --relaunch target-validation cases ×5 → handlers/session-relaunch-guards.test.ts; device-ready TTL-plus-real-readiness cases ×5 → device/device-ready.test.ts.
  • Kept in the daemon lane, retargeted to the port spies (assertion bodies unchanged): boot-probe answers in request-router-open / request-router-open-claim ×3 now arm observeBootTimeMsFixture; open-target identity ×2 (session-open-runtime, session-open-execution-runtime) answer resolveOpenTargetFixture per case; the app-event lifetime case answers resolveSessionAppBundleIdForTargetFixture; the claim-rollback case injects its failure at prepareApplicationOpenFixture.
  • Dead mocks deleted: 5 (fix for the five remaining root-module mocks).

Per-file test-name parity vs the previous head holds: 2671 names, identical multiset; 18 relocated with their names, 0 renamed, 0 added, 0 dropped.

Residue scan (definition, as requested)

scripts/daemon-edge-residue-scan.mjs walks every .ts/.tsx/.mjs under src/daemon/ and matches each specifier form independently (the earlier ad-hoc scan used one alternation bridging import|export … [\s\S]*? from, which swallowed a vi.mock() specifier sitting between an earlier importOriginal<typeof import(...)> and the file's next static from — that is how five mocks hid under its "0"):

  1. static import/export … from with the keyword anchored at a statement start (line start or ;), so no match ever spans statements (covers import type, multi-line specifier clauses);
  2. side-effect import '…';
  3. dynamic import('…') and require('…') with string or no-substitution template-literal arguments (this also catches importOriginal<typeof import('…')>);
  4. vi.mock / vi.doMock / vi.unmock / vi.importMock / vi.importActual / vi.importOriginal first argument, any position in the file, string or template literal.

A relative specifier that normalizes outside src/daemon/ is an edge; bare specifiers (workspace packages, externals) are allowed. Exit 1 on any edge. Verified: 5 edges on 855bd0f, 0 on this head. Once the daemon is its own package, 11b's R11 package-boundary rule (#3546) enforces this permanently in check:layering; until then this committed scan is the measurement.

Gates

  • unit-core lane (vitest run --project unit-core, full): 1591 files / 13447 passed, 1 skipped, 0 failures; within it daemon lane 316 files / 2306 tests, composition lane 97 files / 638 tests, both 0 failures
  • check:quick, check:layering, check:di-seams, check:gate-manifest, check:fallow, check:affected --run, eager-closure-budgets + test-file-size ratchets, pnpm format: OK

Pure moves / renames dominate the diff; hand-written additions are the port fixtures, the install seam, the composition siblings, and the committed scan.