Skip to content

Cut CI wall time and reshape the test suite: shard the root job, move input matrices to unit tests #382

Closed

Description

@willgosnold

Problem

The test suite is slow to run and awkward to work in, but it is not mostly redundant. Measurements at 18b8b43:

  • CI wall time on a PR is set by one job. test (root) takes about 17 minutes (16.7, 17.2 and 17.3 in the last three green runs). The three Ubuntu shards take about 3 minutes each. The required check test (ubuntu-latest) needs test-root, so every PR waits about 17 minutes. That job runs bun test --isolate and then the whole e2e suite again against the binary, one after the other, in a single unsharded job.
  • Most of the suite is end to end. About 77% of cases go through the CLI as a subprocess. test/e2e.test.ts is 19,231 lines, 82% of all test code, and holds about 1,700 runtime tests. Run from source, the suite takes about 700 test-seconds, and e2e is 689 of them. No test is slow: 1,887 of 2,327 take under 0.5s, and the slowest takes 4.7s. The cost is the number of tests.
  • The count comes from input matrices, not duplicates. Many rules are tested for every input class × every layout (fork, folder) × every command, each through the real CLI. For example, case-folded .git and .patchbook names, link kinds, and hook-manager configs.
  • Real duplication is small. Per-test statement coverage over the whole suite reaches 96.7% of 5,761 statements. 680 e2e tests share an identical coverage set with another test. A random sample of 93 of them was read test by test: 8 were duplicates or could be merged, 64 were loops over input classes that each matter, and 21 pinned distinct behaviour. That puts the removable count at about 60 e2e tests (~3–4%).

So deleting tests won't fix this. The wins are in CI layout, and in where the input matrices live.

Proposal

In order of payoff. Each step can be its own PR.

1. Take the root job off the critical path

  • Shard test-root with the same three --test-name-pattern patterns the test job uses (fork, vendor, rest).
  • Run e2e as root once, against the compiled binary. Keep the from-source run only for what needs it: the unit files, and the describes that skip under PATCHBOOK_CMD ("init stopped by a preload", "killed by a preload").
  • Keep the job's purpose: a test that needs a refused write and isn't wrapped in permissionTest must still fail as root.
  • Expected: PR wall time drops from ~17 minutes to about the length of the slowest Ubuntu shard plus build, roughly 4–6 minutes. Report the before and after times from real runs.

2. Move input-class matrices down to unit tests

The rule: test a validation rule's full table of inputs once, against the function that decides it, in units.test.ts or paths.test.ts. Keep e2e for proving the CLI wiring: one or two cases per command × layout that show the refusal reaches the user with the right message and leaves the repo untouched.

Candidate tables in test/e2e.test.ts (line numbers at 18b8b43):

Lines Table What it varies
15104–15143 unsafeVendors case spellings of unsafe vendor paths
15373 nestedGits case-folding and zero-width spellings of a git dir
14684, 14805, 14862 "inside .git" loops three loops with the same body and the same expected message; merge into one table first
2138, 5800–5806 folded upstream and .patchbook spellings case, trailing dot, trailing space
5938–5951 escaping backslash and climbing paths per verb
3266–3270, 3331 link refusals at init link kind × file × layout
3879, 4322, 4350, 4364, 4404 LEFTHOOK_PAIRS, NOT_RUNNING × MENTIONS, NOT_ALWAYS, OTHER_NAMES hook-manager config detection

For each table, check first that the deciding function is reachable as a unit. If it isn't, say so in the PR and leave the table in e2e. Don't export internals just for this.

3. Merge the confirmed duplicates

Each pair has the same setup and actions and differs only in what it asserts. Merge the assertions into one test:

  • 8857 "commit takes a mode change to a file the fork added" and 8867 "commit records a mode the index already has"
  • 12195 into 12045 (ignored tracked x.log, edit, note commit)
  • 14950 into 14884 (track rejection in both layouts, plus the .GIT/.Git/.gIt variants)
  • 2750 into the stale-mirror row of the loop at 2927
  • 5999 "record refuses a bad change name with no hand edits" as a row in the label loop at 5988
  • The config.toml instances of the link loop at 3266 duplicate the dangling-link test at 3331 for .patchbook/config.toml. Read both before cutting. The judgement came from a sample reviewer and wasn't double-checked.

4. Split test/e2e.test.ts by top-level describe

Split it into files such as e2e/fork.test.ts, e2e/vendor.test.ts, e2e/intent.test.ts, e2e/hook.test.ts, and so on. Move the shared fixtures (forkTest, vendorTest, hookCommit, startingRepo, …) into a helper module next to test/helpers.ts. Point the CI shards at files, not name patterns.

This is mostly for developer experience: you can run the area you're changing, and diffs and reviews stay readable. Check whether bun test runs files in parallel at the pinned Bun version. If it does, measure the local speed-up. If it doesn't, don't claim one.

5. Low priority: parametrise near-identical unit tests

A shingle-similarity scan found 151 pairs of unit tests above 0.9 similarity, for example setToml at 314/322 and applyStructure at 706/736. They're cheap to run, so only convert them to test.each where it reads better.

Guardrails

  • No behaviour lost. Each PR that removes or moves an e2e case names the test that now covers it. Statement coverage must not drop below the baseline (96.7%). See "Measuring" below.
  • No change to what CI proves:
    • e2e still runs against the compiled binary on Ubuntu, and on macOS for pushes.
    • Root still runs the full suite.
    • The crash-injection check on the binary stays.
  • Follow CONTRIBUTING.md: the e2e/units split rule still holds, it's just applied more strictly.

Measuring

Report before and after for each PR:

  • runtime test count per file;
  • test-seconds per file, using bun test --reporter=junit --reporter-outfile=…;
  • wall time per CI job from gh run view --json jobs.

How statement coverage was measured, so it can be rebuilt:

  • Bun's coverage doesn't follow CLI subprocesses, so the harness preloads a Bun plugin into the CLI through PATCHBOOK_CMD="bun --preload <preload> src/cli.ts".
  • The plugin uses the TypeScript 5 parser to insert a counter before each statement in src/.
  • Each process writes its hit set on exit. A wrapper standing in for bun:test tags each run with the current test's name.
  • Two limits: tests in cli.test.ts that start a plain bun -e child aren't recorded, and the 3 env.test.ts tests that check process.env fail under the harness.
  • A working copy may still be at /tmp/pbcov/harness on the original machine.

Out of scope

  • Coverage thresholds in CI.
  • Lint rules for tests.
  • Changing the qa/ journeys.

What you do now

Wait about 17 minutes per PR for test (root). Locally, run all of test/e2e.test.ts (about 11 minutes serial), or hand-write a -t pattern.

Would you like to write the PR?

Yes. An agent will pick this up.

🤖 Generated with Claude Code

Activity

  1. added
    enhancement
    on Oct 27, 2026
  2. added a commit that references this issue on Oct 27, 2026
  3. willgosnold commented on Oct 27, 2026

    @willgosnold
    OwnerAuthor

    Plan for this issue. Each step is its own PR.

    • Step 1: ci: shard the root test job #383, merged. A PR's CI went from about 17 minutes to 3.2.
    • Step 2 (move input matrices to unit tests): in progress. It also owns two items from step 3, because they sit in the same lines of test/e2e.test.ts. Those items are the 14950 into 14884 merge, and the config.toml rows of the link loop at 3266 against the dangling-link test at 3331.
    • Step 3: test(e2e): merge duplicate tests #384 has the other four items.
    • Step 4 (split test/e2e.test.ts): starts after steps 1 to 3 merge, because it moves every line they touch.
    • Step 5: no PR. None of the 76 flagged unit-test pairs reads better as a test.each table. Most are one-line expect(f(input)).toEqual(output) tests whose names state different rules, and the issue asks for a conversion only where a table reads better.

    🤖 Generated with Claude Code

  4. willgosnold commented on Oct 27, 2026

    @willgosnold
    OwnerAuthor

    All five steps are done. Closing.

    Step PR Result
    1. Take the root job off the critical path #383 A PR's CI went from 16.5 to 16.9 minutes to about 3. Root runs the same three shards as the runner user. As root against the binary, 1622 e2e tests pass, the same as before.
    2. Move input matrices to unit tests #385 152 cases moved from e2e to unit tables. The init link table and LEFTHOOK_PAIRS stay in e2e, and the PR says why.
    3. Merge the confirmed duplicates #384, #385 3 pairs merged in #384, 2 items in #385. 5999 stays its own test: it alone pins that record checks the name before it looks for edits.
    4. Split test/e2e.test.ts #387 13 files in test/e2e/ and a shared test/e2e-fixtures.ts. Every test keeps its full name. CI shards by file.
    5. Parametrise near-identical unit tests none The scan flagged 76 pairs at 0.9 or higher and 79 smaller ones. About 30 of the 76 were read, in the groups the issue names and the largest clusters, and none read better as a test.each table. Most are one-line tests whose names state different rules. The rest were not read.
    Follow-up: run files in parallel #388 bun test --parallel everywhere. Locally, e2e against the binary went from 750 s to 167 s. In CI only the rest shard gains (147 s to 98 s in one run each side), because fork and vendor are one file each.

    Statement coverage, measured with the harness from this issue, is 5569 of 5761 (96.67%) at 82fd7f6 and after every PR, with 0 statements lost.

    Not done, for later:

    • CI wall time is now set by test/e2e/fork.test.ts, at about 2.8 minutes. Splitting ForkTest and VendorTest into several files would lower it. Their nested describes are interleaved with local helpers, so that needs a second shared-helper module.
    • PRE_COMMIT_NOT_ALWAYS, readsAsDotPatchbook and "init and track reject names Windows or a Mac reads as .patchbook" have the same shape as the tables step 2 moved. The new pre-commit unit rows cover only hooks that count as running on every commit. The rules that reject a hook, such as always_run, pass_filenames and args, are still covered only by PRE_COMMIT_NOT_ALWAYS in e2e.
    • Book#refuseLinkedTop is public and decides the upstream.lock link refusal, but no unit test calls it. test: move input matrices from e2e to unit tests #385 kept that case in e2e only.
    • Bugs found in review: unsafeVendor allows vendor folders Windows reads as .patchbook #386 (a vendor folder can be named so Windows reads it as .patchbook) and test: a renamed from-source-only describe drops out of CI without failing #389 (a renamed from-source-only describe drops out of CI without failing).

    🤖 Generated with Claude Code

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

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions