Move durable run access and execution control onto the server-backed client, canonicalize run APIs under /api/v1/runs, and switch CLI integration tests to a shared test daemon/storage model with shared-state-safe assertions.
6.9 KiB
Shared Test Storage + Production Auto-Start for CLI Tests
Summary
Use one shared test storage_dir per test session and rely on the existing production auto-start behavior to converge on a single shared daemon for that storage root.
Session identity rules:
- when
NEXTEST_RUN_IDis present, use one shared storage dir for the fullcargo nextest run - when
NEXTEST_RUN_IDis absent, use one shared storage dir per test process
The test harness will not implement its own daemon bootstrap protocol. Instead:
- every test process points
FABRO_STORAGE_DIRat the same test-run storage root - the first CLI command that needs the daemon triggers normal production auto-start
- existing
server.lock,server.json, andfabro.sockbehavior prevents duplicate servers for that shared storage dir - the test harness is responsible only for:
- selecting the shared test storage root
- making test assertions/helpers safe under shared storage
- cleaning up the shared daemon and temp root at the end of the test run
This keeps test behavior aligned with production daemon identity semantics.
Implementation Changes
1. Shared storage_dir in fabro-test
In lib/crates/fabro-test/src/lib.rs:
- Change
TestContext::newto detectNEXTEST_RUN_ID. - Derive a shared test root under temp:
- if
NEXTEST_RUN_IDis present:$TMPDIR/fabro-nextest/<NEXTEST_RUN_ID>/ - otherwise:
$TMPDIR/fabro-test-process/<pid>/ - shared storage dir:
<root>/storage
- if
- Keep
temp_dirandhome_dirper context; onlystorage_dirbecomes shared for the session. - Do not explicitly start the daemon from the harness.
- Keep using the normal CLI command path so the first server-backed command triggers production auto-start against the shared storage dir.
- Add concrete session cleanup coordination using marker files:
- under the session root, create
clients/<pid>marker files - protect create/remove/scan operations with a session lock file
- on
TestContextinit:- acquire lock
- create or refresh this process marker
- remove stale markers for dead PIDs
- release lock
- on process teardown:
- acquire lock
- remove this process marker
- remove any other stale dead markers
- if no markers remain, call
fabro server stop --storage-dir <shared>and remove the shared temp root - release lock
- under the session root, create
- Crash behavior:
- crashed processes may leave stale markers behind
- future init/teardown paths reap dead markers under the same lock
- teardown cleanup is best-effort; if
fabro server stopor root removal fails, later harness initialization remains responsible for authoritative stale-session reaping
- Add stale-run cleanup on harness initialization:
- scan old
fabro-nextest/*roots - if all tracked PIDs for a root are dead, stop any server tied to that root and remove it
- likewise scan old
fabro-test-process/*roots and reap dead process-owned sessions
- scan old
The harness should only coordinate test-run ownership and cleanup, not daemon startup.
2. Per-test labeling for shared-state safety
Add a per-TestContext test case ULID and expose:
fabro_test_run=<NEXTEST_RUN_ID>fabro_test_case=<test-case-ulid>
Update run-creation helpers to append these labels to created runs:
runcreate- detached/create-start helpers
- any workflow fixture helpers that create runs internally
Use existing production --label KEY=VALUE support; do not invent a new namespacing mechanism.
3. Replace isolated-storage helper assumptions
Update helpers in:
lib/crates/fabro-cli/tests/it/cmd/support.rslib/crates/fabro-cli/tests/it/workflow/mod.rs
Specifically:
- remove helpers that assume there is exactly one run in
storage_dir/runs - replace
only_run(context)-style logic with:- exact run-id lookup when the helper already has the run id, or
- test-case-label-based lookup when the helper needs to discover “the run created by this test”
- keep direct store inspection helpers, but always resolve a concrete run first
4. Make shared-daemon tests robust
Update tests to match the shared-daemon model:
- exact-run tests remain strict and use ULIDs directly
- global/listing tests (
ps,runs list,system df, workflow-slug/recency lookup) should assert the presence/properties of the current test’s run(s), not exact global emptiness/counts unless explicitly scoped - when a command offers structured output, prefer parsing that output and filtering to the current test’s run(s) over broad transcript snapshots or loose substring matching
- destructive tests must always be scoped:
- exact run IDs when possible
- otherwise use existing
--labelfilters
- broad destructive operations like “delete everything” are not allowed in shared-daemon tests
Rewrite current tests that depend on ambient exclusivity, especially:
- helpers asserting a single run in storage
pstests expecting global count equalitysystem prunetests filtering only by common workflow names likeSimple
Important Interface / Behavior Changes
fabro-test::TestContextuses a sharedstorage_dirper nextest run whenNEXTEST_RUN_IDis set.- Test-created runs gain deterministic labels:
fabro_test_runfabro_test_case
- Test code must treat shared storage as normal under nextest and avoid assumptions based on storage exclusivity.
Test Plan
fabro-testcoverage:- multiple contexts in one nextest run resolve to the same shared storage dir
- multiple contexts without
NEXTEST_RUN_IDbut in the same process resolve to the same shared storage dir - contexts still get distinct
temp_dirandhome_dir - marker-file cleanup removes stale prior session roots
- last-process cleanup stops the daemon and removes the shared root
- CLI integration updates:
- helper coverage for resolving runs by exact ULID or test-case label
ps,runs list, andsystem prunetests updated to shared-storage-safe assertions- destructive tests verified to target only test-owned runs
- End-to-end acceptance:
- a full
cargo nextest run -p fabro-clishould converge on one daemon perNEXTEST_RUN_IDstorage root - a
cargo test --workspaceinvocation should converge on one daemon per test process, not perTestContext - after the run, no test-owned
fabro.sockdaemon remains for that root - add an early validation test or harness check that concurrent auto-start against the same shared
storage_dirconverges on one daemon under parallel load
- a full
Assumptions and Defaults
- Production auto-start semantics for a single
storage_dirare the source of truth and already provide duplicate-server protection via the existing lock/record path. NEXTEST_RUN_IDis used when available to derive the shared nextest-run root; otherwise the current process PID is used to derive a shared per-process root.- The shared test storage root is fully separate from production storage in both modes.
- No separate harness-managed daemon bootstrap protocol is added.
- No changes to production daemon lifetime semantics are part of this plan.