Skip to content

test(sync/init/update): state-matrix tests for every template writer — absent, sequant-owned, user-modified, local symlink, foreign symlink, directory #1097

Closed

Description

@admarble

Blocked by: #1053, #1106 Tier: mechanical Doc: guard plan 2026-09-18 (16-bug classification)

Why

Four of the recent sync/init bugs share one test gap: the writer was tested from a single starting state. #1053's copy mode was tested from an empty scripts/dev, so "foreign symlink present" was never exercised. #1071's init was tested on a fresh repo, never on an initialized one. #1078's sync was tested against an unmodified .claude/settings.json. #1030's template walk was never tested against a destination that a different writer owns. The ownership rule in #1090 decides what each writer should do per state; this issue makes every writer prove it does that for every state.

Change

src/lib/__tests__/writer-state-matrix.test.ts: a table-driven suite over

Each cell asserts the post-state (content, symlink-ness, target), the printed decision (overwrite / preserved / merged / replaced), and that --dry-run's listed decision equals what the apply run did. Real filesystem in a temp dir, no mocks of fs.

Release-graph plan — guard-2026-09 (2026-09-20)

Graph: guard-2026-09 Tier: mechanical Wave: 2
May touch: src/lib/__tests__/writer-state-matrix.test.ts (new); read-only on src/lib/templates.ts, src/commands/init.ts, src/commands/sync.ts, src/commands/update.ts; CHANGELOG.md

The npm run build && npm test AC is verified by the runner from a clean shell after the PR opens (PLAN I-1); the agent runs only the test files its ACs name. Mutation markers use failedTest starting with the test file path (I-4). Real filesystem in a temp dir, no fs mocks (I-6). AC-3 depends on #1053's fix; AC-1 uses the per-site gate from #1106 to enumerate writers. A cell that finds a writer doing the wrong thing is a new bug issue, not a scope expansion here.

Acceptance criteria

Not in scope

The ownership policy itself (#1090). If #1090 lands a different decision for a cell, the cell's expected value changes; the matrix does not.

Activity

  1. added
    enhancement
    cli
    on Sep 19, 2026
  2. admarble commented on Sep 20, 2026

    @admarble
    ContributorAuthor

    Runner context — guard-2026-09 wave 2 launch, 2026-09-20. Base: origin/main at bef0ac98 (all seven wave-1 PRs merged: #1086 scrub, #1104 marker docs, #1053, #1106 ownership gate, #1095 parsers, #1096 conformance suite, #1070 failing verdicts post). These rules override the exec AND qa skills' defaults for this run:

    • No full npm test in the worktree — this applies to the qa phase too. Under concurrent phase agents that one call exceeds the 30-minute no-progress wall and the phase is killed (three wave-1 phases died this way). Verify each AC by its own command; run only the test files the ACs name. The runner runs the full suite once per PR from a clean shell and records it on the PR.
    • A named test file red before your change: the suite is now hermetic (bug(test): suite isn't hermetic against the SEQUANT_* env phases inject — exec sees red tests it didn't cause and opens no PR #1086), so a red file is real. Name the issue number and the base-vs-head result in the PR body.
    • Mutation marker (gate-test ACs): <!-- SEQUANT_MUTATION: {"ac":"AC-N","mutation":"<what you deleted>","failedTest":"<test file path> > <describe> > <case>"} --> in the PR body. failedTest MUST start with the test file path. Run parseMutationMarkers on your body before opening the PR. Commit before mutating.
    • 30-minute no-progress wall per phase. Commit a WIP checkpoint before any long command; every command foreground, with timeout.
    • Commits: single-line git commit -m "...", no Co-Authored-By. Push the branch and open the PR yourself (one wave-1 exec forgot). Never rebase or force-push after pushing. One AC per line in the PR body, ids AC-N only.
    • Stop at the PR. Do not merge.

    Issue-specific: New file only (src/lib/__tests__/writer-state-matrix.test.ts); the writers are read-only. Real filesystem in a temp dir, no fs mocks. Never create or remove anything under the real repo (process.cwd()); a wave-1 fixture did and was fixed. AC-3 asserts #1053's behavior by the merged code; AC-1 enumerates writers via the per-site gate from #1106 (src/lib/ownership-gate.ts).

  3. admarble commented on Sep 20, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (6 ACs, 1 directory)

    AC Quality Check

    Warnings only. All six ACs are single-line and carry a Verify: command.

    • AC-1 "every (writer × state × policy) cell" is not enumerated. Not every writer writes every destination (sync never touches .gitignore; init has no --dry-run). The plan defines the cell set explicitly (Open Question 1).
    • AC-2 to AC-5 select cells with -t "<phrase>". vitest -t is a substring match, so test titles must contain exactly these phrases. -t "foreign symlink" also matches every other foreign-symlink cell, and all of them must be green.
    • AC-6 npm run build && npm test is run by the runner from a clean shell after the PR opens (runner context comment). The agent runs only this file.

    Scope Assessment

    Non-Goals: "Not in scope": the ownership policy itself (#1090). A cell that finds a writer misbehaving becomes a new bug issue, not a fix here. The writers are read-only.

    Metric Value Status
    Feature count 1 (one new test file) ✅
    AC items 6 ✅
    Directory spread 1 (src/lib/__tests__/) ✅

    Verdict: ✅ SCOPE_OK


    Acceptance Criteria

    AC-1: matrix covers every cell, no todo/skip

    Verification: Unit Test (real filesystem, temp dir). vitest run green and grep -c "it.todo\|it.skip" → 0.
    Scenario: Given the writer × state × destination table → When vitest runs → Then every cell is a real it and none is skipped.
    Assumptions: Cell set is defined per Open Question 1. #1090/#1106 are merged on origin/main (OWNERSHIP_RULES and ownership-gate.ts are present).

    AC-2: dry-run and apply agree in every cell

    Verification: Unit Test, -t "dry-run matches apply", plus a recorded mutation.
    Scenario: Given each cell → When sync --dry-run and update --dry-run run, then the apply run → Then the printed decision line (overwrite/preserved/merged) for that path is equal.
    Mutation: in resolveOwnershipDecisions (src/commands/sync.ts), change the .mcp.json merged push to overwrite. The .mcp.json dry-run cell must fail. Commit first, restore after.

    AC-3: #1053 cell, copy mode + foreign symlink → regular file with template content

    Verification: Unit Test, -t "foreign symlink".
    Scenario: Given scripts/dev/<x>.sh is a symlink into a directory outside the project → When init/sync/update run → Then lstat says regular file and the bytes equal the template.
    Assumptions: Templates resolved from the sequant checkout are outside the temp project, so resolveScriptsSymlinkTarget picks copy mode. The local-symlink cell needs a node_modules/sequant/templates/scripts fixture to reach symlink mode.

    AC-4: #1071 cell, init --agent codex on an initialized repo

    Verification: Unit Test, -t "init on initialized".
    Scenario: Given .sequant/settings.json with tuned non-default keys → When init --agent codex → Then a deep diff shows only run.agent changed.

    AC-5: #1078 cell, settings.local.json survives, settings.json overwritten

    Verification: Unit Test, -t "settings.local.json survives", plus a recorded mutation.
    Scenario: Given a project PreToolUse hook in .claude/settings.local.json and a hand-edited .claude/settings.json → When sync and update run → Then settings.local.json is byte-identical, settings.json equals the template, and sync --dry-run lists .claude/settings.json as overwrite.
    Mutation: make sync also write settings.local.json; the cell must fail. Commit first, restore after.

    AC-6: npm run build && npm test green

    Verification: runner-verified (see AC Quality Check).


    Implementation Plan

    Phase 1: Spike (gates the rest)

    1. In the existing worktree, write one cell end-to-end (sync × user-modified × AGENTS.md). Use mkdtemp and process.chdir(tmp) in beforeEach, restore in afterEach. Assert process.cwd() !== repoRoot before every writer call. Never spy process.cwd; sync.test.ts:55 shows what an unmocked cwd does to the real repo. Time it: the file has no .integration. in its name, so it runs in the unit project on the 5 s default timeout (fix(test): tsx-spawning integration tests blow vitest's 5s default — phase-registry should use the prebuilt dist like its five siblings #842). Set an explicit per-test or describe timeout if a cell takes more than about 1 s; do not rename the file, the AC Verify: commands name this path.
    2. Find how init (interactive inquirer prompts at init.ts:690-1362) and update (package-manager and version preflight, update.ts:95) run non-interactively. Prefer force/yes and SEQUANT_TEMPLATES_DIR; mocking inquirer is acceptable (the "no mocks" rule is about fs).

    Phase 2: State table and writers

    1. Table-drive (it.each) over 7 states (absent, byte-identical, user-modified, marker + changed body, local symlink, foreign symlink, directory) × the destinations: sequant-owned = .claude/settings.json, .claude/skills/<x>/SKILL.md; user-owned = constitution, .sequant/settings.json, AGENTS.md; merge = .mcp.json, .gitignore; plus scripts/dev/<x>.sh. Read policies via ownershipPolicy() so the expected value comes from the landed table.
    2. Per cell assert post-state (content, lstat symlink-ness, target) and the printed decision by capturing console.log. Match the <verb>: <path> line, not chalk colours.

    Phase 3: dry-run parity and named cells

    1. dry-run matches apply cells for sync and update. init has no dry-run, so it is not in that set.
    2. The named cells for AC-3, AC-4, AC-5. Titles contain the AC's exact -t phrase.

    Phase 4: Evidence

    1. Commit. Run the AC-2 and AC-5 mutations, one at a time and restoring each, and record <!-- SEQUANT_MUTATION: {"ac":"AC-2","mutation":"...","failedTest":"src/lib/__tests__/writer-state-matrix.test.ts > <describe> > <case>"} --> in the PR body. failedTest must start with the file path. Add a CHANGELOG.md line.

    Design Review

    1. Where does this logic belong? One new test file in src/lib/__tests__/. The issue fixes the path. Writers are read-only.
    2. Simplest correct approach? Call initCommand/syncCommand/updateCommand in-process on a temp cwd (the pattern in update.integration.test.ts). Rejected: spawning the CLI per cell (about 10x slower, no need); mocking fs (forbidden by the issue).
    3. Existing pattern? templates.test.ts "ownership policy covers…" for reading the policy table; update.integration.test.ts for the chdir + temp templates dir setup.
    4. What would a senior reviewer challenge? "Hard-coded expectations will drift from fix(sync/init/update): one ownership rule for project files — the clobbering class fixed file-by-file in #708/#814/#990/#1042/#1071/#1078 #1090." Derive the expected verb from ownershipPolicy(). Then the matrix cannot restate the table and always agree with it.

    Feature Quality Planning

    • Run time: the cell count is the main risk. It is roughly 5 writers × 2 (force) × 7 states × 9 destinations, so about 630 combinations. Prune with the N/A rule (Open Question 1), not by skipping tests.
    • Real-repo safety: the cwd guard (step 1) is required. A wave-1 fixture wrote into the real repo.
    • Directory-in-place cells may crash the writers (EISDIR). Assert the observed behaviour. If it is a bug, file an issue and keep the test as the documented current behaviour; do not fix it here.
    • Platform: symlink cells are POSIX-only. Guard with isNativeWindows() the way the source does.

    Open Questions

    1. What counts as a cell when a writer never touches a destination? Recommendation: every applicable (writer, destination) pair gets a state sweep; a non-applicable pair is one it asserting the destination is byte-identical after the run. That keeps AC-1's "no todo/skip" literal and avoids empty cells. Impact if wrong: the count and the reviewer's expectations differ.
    2. Real bundled templates or a small fixture templates dir? Recommendation: real templates for .claude/settings.json and one skill, so the assertion is "equals the template" for real. Use SEQUANT_TEMPLATES_DIR only if the spike shows the timing is unworkable.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: The deliverable is a test file, so testgen would stub the thing being built. No UI. Two gate ACs (2 and 5) need mutation records, which /qa §6i parses. Runner rules apply: run only this file, commit before mutating, stop at the PR.


    Label Review

    Current: enhancement, cli
    Recommended: enhancement, cli (add planned)
    Reason: no change to the type or area.
    Quality Loop: won't auto-enable (SCOPE_OK).

  4. added
    planned
    on Sep 20, 2026
  5. admarble commented on Sep 20, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 0/4 met

    Gaps:

    Suggestions:

    • Add expect(new Set(DESTS.map(d => d.path))).toEqual(…) or an assertion that every OWNERSHIP_RULES entry has ≥1 representative destination (or an explicit documented exclusion list), so a new writer destination cannot land uncovered.
    • Either rename the defect test to what it asserts (names a well-formed issue reference) or make it check openness via a static allowlist.
    • The V letter (replaced, but the symlink's old target was modified on the way) is documented in the header and implemented in classify(), but occurs in 0 grid cells. Harmless, worth a one-word note that it is a tripwire-only code.
  6. added a commit that references this issue on Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clienhancementplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions