Public: anyone can read this; the crew can change it.
One command tells a contributor every rule their change will meet, before the… #16520
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/16325-one-command-tells-a-contributor-every-ru → main 0 commits fork
7 files
changed +294
−25
AGENTS.md +2
| @@ -98,3 +98,5 @@ A pull that adds a page (`src/web/pages/*.ts`), an MCP tool (`src/http/mcp/tools | ||
| 98 | 98 | `src/spawn/own_code.ts`: `queues`, the tail of each one-at-a-time turn (`oneAtATime`), by data directory and name. `src/actions/pinned.ts` takes two kinds of turn there: `pinned-account:<provider>/<account>`, held from a create's ceiling check until the machine it made is in its row, so creates asked together are counted one after the other, and `pinned-forge:<name>`, held through an Update, Rollback or new claim link, so two never delete and make a machine each. A key leaves when its last turn settles, refusals included (`test/pinned-ceiling.test.ts`); gone on restart, when nothing was in flight. |
| 99 | 99 | `src/crew/sandbox_policy.ts`: `standing`, per forge (a WeakMap on the app), whether the sandbox is required, what the last probe found and how to ask again; written by the crew at its start and after each probe (`crew/dispatch.ts`, `noteSandbox`), read by `sandboxRefusal` wherever an agent process would start (a held run, a conversation turn, a one-shot). The probe is taken again at most once every `SANDBOX_REPROBE_MS` and one at a time (`reprober`), only while something waits on it. One entry per forge, gone on restart, when the crew probes afresh. |
| 100 | 100 | `src/actions/places_refresh.ts`: `running`, the one catalog refresh in flight per forge database (#16203), so concurrent admin reads wait on it; at most one per forge, gone on restart. Its terminal door is `src/tui/screens/places.ts` (`p` pins; `pinPlace` gives the API and the terminal the same gate and the `cloud.place_pinned` event). |
| 101 | ||
| 102 | `bun scripts/gate.ts --explain` (#16325) lists every gate rule (`GATE_RULES`, `scripts/gate.ts`) and every fast tree-reading guard test (`GUARDS`, `scripts/gate_explain.ts`) against your diff, satisfied or not, each with the line that satisfies it and where the rule lives; a refusing gate ends with one line naming all the rules that refused. No network, no socket, about fifteen seconds. In a sandboxed session `bun x tsc --noEmit` without installed dependencies, tests that bind a port or unix socket, and the full suite cannot run: report them as not run. A rule added to `GATE_RULES` is judged and explained at once; a new tree-reading guard goes in `GUARDS`. |
CLAUDE.md +3
| @@ -89,6 +89,9 @@ bun run src/main.ts # boots on :3000, data in ./data | ||
| 89 | 89 | `describe.skipIf(missing("/usr/bin/ssh"))` (`test/helpers/tools.ts`) — |
| 90 | 90 | conditional on the tool, so nothing is lost where the tool is there, and |
| 91 | 91 | the reason is printed so a skip is never read as a pass. |
| 92 | - **Run `bun scripts/gate.ts --explain` first** (#16325): every rule the check will hold your | |
| 93 | diff to, with the line that satisfies it. Full text in AGENTS.md; in a sandbox report | |
| 94 | tsc, port-binding tests and the full suite as not run. | |
| 92 | 95 | - **`bun run check` is the gate**, and it reads its command from |
| 93 | 96 | `forge.json`, so what you see is exactly what the push will be judged by. |
| 94 | 97 | **The forge runs it on every push; a crew run does not.** Before pushing, |
scripts/gate.ts +67 −22
| @@ -951,30 +951,68 @@ export async function readmeCapRefusal(cwd: string = process.cwd()): Promise<str | ||
| 951 | 951 | return readmeCapProblems(before.out, after); |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | /** One rule the gate holds a pull to, as `--explain` lists it (#16325). */ | |
| 955 | export type GateRule = { | |
| 956 | /** What the rule is called in the gate's own words. */ | |
| 957 | name: string; | |
| 958 | /** A refusal fails the check; a note is only said. */ | |
| 959 | kind: "refusal" | "note"; | |
| 960 | /** Where the rule lives: read it there when the line below is not enough. */ | |
| 961 | file: string; | |
| 962 | /** The one line that satisfies it (or waives it). */ | |
| 963 | satisfy: string; | |
| 964 | /** What it says of the clone at `cwd`: empty when satisfied. */ | |
| 965 | run: (cwd: string) => string[] | Promise<string[]>; | |
| 966 | }; | |
| 967 | ||
| 968 | /** | |
| 969 | * Every rule `gateVerdict` applies, in order, with where it lives and what satisfies it. One table: | |
| 970 | * the verdict and `--explain` read the same list, so a rule added here is both judged and | |
| 971 | * explained, and none can be in one and not the other. | |
| 972 | */ | |
| 973 | export const GATE_RULES: readonly GateRule[] = [ | |
| 974 | { name: "gate list", kind: "refusal", file: "scripts/gate.ts (EXCLUDED)", satisfy: "an EXCLUDED entry names a test file that exists, once, with a reason and an issue", run: (c) => gateRefusal(presentTests(c)) }, | |
| 975 | { name: "base gate", kind: "refusal", file: "scripts/gate.ts (baseGateRefusal)", satisfy: "leave EXCLUDED a plain array literal of { file: \"…\" } entries", run: (c) => baseGateRefusal(c) }, | |
| 976 | { name: "slow files", kind: "refusal", file: "scripts/gate.ts (SLOW)", satisfy: "a SLOW entry names an existing file, once, with a reason and an issue", run: (c) => slowRefusal(presentTests(c)) }, | |
| 977 | { name: "migration pins", kind: "refusal", file: "src/kernel/pin_edits.ts", satisfy: "bun scripts/pin-migrations.ts, and commit the new .pin; never edit a migration or pin already on main", run: pinRefusal }, | |
| 978 | { name: "both doors", kind: "refusal", file: "src/kernel/both_doors.ts", satisfy: "add the terminal screen under src/tui/screens/ (imported by an entry) and docs/features/<name>.md, or a commit line `No door: <why>`", run: bothDoorsRefusal }, | |
| 979 | { name: "a test with the change", kind: "refusal", file: "src/kernel/tests_with_change.ts", satisfy: "add or edit a test/*.test.ts, or a commit line `No test: <why>`", run: testsWithChangeRefusal }, | |
| 980 | { name: "name the issue", kind: "refusal", file: "src/kernel/direction.ts", satisfy: "a commit line `Direction: <why> (#123)` naming the issue a new page, screen, how-to or module serves", run: directionRefusal }, | |
| 981 | { name: "stray test", kind: "refusal", file: "scripts/gate.ts (strayTestRefusal)", satisfy: "keep every *.test.ts under test/", run: strayTestRefusal }, | |
| 982 | { name: "review findings", kind: "refusal", file: "src/kernel/review_rules.ts", satisfy: "the finding named in the message names the line to change", run: reviewRefusal }, | |
| 983 | { name: "added lines", kind: "refusal", file: "src/kernel/review_rules.ts (addedProblems)", satisfy: "remove the construct the message names from the lines the pull adds", run: addedRefusal }, | |
| 984 | { name: "rewrap noise", kind: "refusal", file: "scripts/gate.ts (rewrapNoiseRefusal)", satisfy: "do not rewrap comment lines you did not change; bun scripts/gate.ts --fix touches only your files", run: rewrapNoiseRefusal }, | |
| 985 | { name: "dependency reason", kind: "refusal", file: "src/kernel/dependencies.ts", satisfy: "say why each new package is needed in README.md's Dependencies section or docs/DEPENDENCIES.md", run: dependencyRefusal }, | |
| 986 | { name: "README cap", kind: "refusal", file: "src/kernel/readme_cap.ts", satisfy: "put the feature's how-to in docs/features/<name>.md, not README.md", run: readmeCapRefusal }, | |
| 987 | { name: "billing never deletes", kind: "refusal", file: "src/kernel/destroy_reach.ts", satisfy: "no call path from the billing webhook deletes a forge, an app or a child", run: billingDeletesRefusal }, | |
| 988 | { name: "test honesty", kind: "refusal", file: "scripts/test_honesty.ts", satisfy: "wait on a signal (test/helpers/poll.ts), not Bun.sleep(<n>), mock.module or a raised timeout; counts only shrink", run: testHonestyRefusal }, | |
| 989 | { name: "comment width", kind: "note", file: "src/kernel/comment_wrap.ts", satisfy: "bun scripts/gate.ts --fix, and commit what it rewraps", run: (c) => wrapNote(c) }, | |
| 990 | { name: "before/after looks", kind: "note", file: "src/kernel/look_rule.ts", satisfy: "a commit line `Looks: <path>` naming a page of app.looks in forge.json, or `No look: <why>`", run: lookNote }, | |
| 991 | { name: "try it", kind: "note", file: "src/kernel/try_it.ts", satisfy: "say in the pull description how to try the change on its preview", run: tryItNote }, | |
| 992 | { name: "replaces", kind: "note", file: "src/kernel/replaces.ts", satisfy: "a commit line `Replaces: <what it takes the place of>` or `Replaces: nothing, <why>`", run: replacesNote }, | |
| 993 | { name: "duplicate functions", kind: "note", file: "scripts/dupes.ts", satisfy: "reuse the existing function the note names (bun scripts/similar.ts --diff main)", run: dupesNote }, | |
| 994 | ]; | |
| 995 | ||
| 954 | 996 | /** |
| 955 | 997 | * What the gate says of the clone at `cwd`: `notes` are only said, `refusal` fails the check |
| 956 | * (exit 2). The look, wrap and try-it rules are notes (#13362, #14247). | |
| 998 | * (exit 2). The look, wrap and try-it rules are notes (#13362, #14247). Every rule runs and every | |
| 999 | * refusal is returned, never the first alone (#16325). | |
| 957 | 1000 | */ |
| 958 | export async function gateVerdict(cwd: string = process.cwd()): Promise<{ notes: string[]; refusal: string[] }> { | |
| 959 | const present = presentTests(cwd); | |
| 960 | const refusal = [ | |
| 961 | ...gateRefusal(present), | |
| 962 | ...(await baseGateRefusal(cwd)), | |
| 963 | ...slowRefusal(present), | |
| 964 | ...(await pinRefusal(cwd)), | |
| 965 | ...(await bothDoorsRefusal(cwd)), | |
| 966 | ...(await testsWithChangeRefusal(cwd)), | |
| 967 | ...(await directionRefusal(cwd)), | |
| 968 | ...strayTestRefusal(cwd), | |
| 969 | ...reviewRefusal(cwd), | |
| 970 | ...(await addedRefusal(cwd)), | |
| 971 | ...(await rewrapNoiseRefusal(cwd)), | |
| 972 | ...(await dependencyRefusal(cwd)), | |
| 973 | ...(await readmeCapRefusal(cwd)), | |
| 974 | ...billingDeletesRefusal(cwd), | |
| 975 | ...testHonestyRefusal(cwd), | |
| 976 | ]; | |
| 977 | return { notes: [...wrapNote(cwd), ...(await lookNote(cwd)), ...(await tryItNote(cwd)), ...(await replacesNote(cwd)), ...(await dupesNote(cwd))], refusal }; | |
| 1001 | export async function gateVerdict(cwd: string = process.cwd()): Promise<{ notes: string[]; refusal: string[]; refused: string[] }> { | |
| 1002 | // In sequence, as before: rules shell out to git and read FORGE_CHECK_BASE, so order is kept. | |
| 1003 | const said: { name: string; kind: GateRule["kind"]; lines: string[] }[] = []; | |
| 1004 | for (const r of GATE_RULES) said.push({ name: r.name, kind: r.kind, lines: await r.run(cwd) }); | |
| 1005 | const lines = (kind: GateRule["kind"]) => said.filter((s) => s.kind === kind).flatMap((s) => s.lines); | |
| 1006 | const refused = said.filter((s) => s.kind === "refusal" && s.lines.length > 0).map((s) => s.name); | |
| 1007 | return { notes: lines("note"), refusal: lines("refusal"), refused }; | |
| 1008 | } | |
| 1009 | ||
| 1010 | /** | |
| 1011 | * The gate's last line when it refuses: every rule that refused, by name. The check's summary is | |
| 1012 | * the last line of the output, so a pull that fails four rules says four, not the last (#16325). | |
| 1013 | */ | |
| 1014 | export function refusedSummary(refused: readonly string[]): string { | |
| 1015 | return `refused by ${refused.length} ${refused.length === 1 ? "rule" : "rules"}: ${refused.join(", ")} (bun scripts/gate.ts --explain says how to satisfy each)`; | |
| 978 | 1016 | } |
| 979 | 1017 | |
| 980 | 1018 | // `bun scripts/gate.ts`: the audit alone, first in `forge.json`'s check, so |
| @@ -996,10 +1034,17 @@ if (import.meta.main) { | ||
| 996 | 1034 | for (const line of refused) console.error(`gate: ${line}`); |
| 997 | 1035 | process.exit(refused.length === 0 ? 0 : 2); |
| 998 | 1036 | } |
| 999 | const { notes, refusal } = await gateVerdict(); | |
| 1037 | // `--explain`: every rule this diff is asked to satisfy, and every guard test that reads the | |
| 1038 | // tree, satisfied or not (#16325). Nothing is fixed, nothing is pushed. | |
| 1039 | if (process.argv.includes("--explain")) { | |
| 1040 | const { explain } = await import("./gate_explain.ts"); | |
| 1041 | process.exit(await explain(GATE_RULES, ".")); | |
| 1042 | } | |
| 1043 | const { notes, refusal, refused } = await gateVerdict(); | |
| 1000 | 1044 | // Said, never refused: a comment's width stops no check (#12968), nor does the look rule |
| 1001 | 1045 | // (#13362). |
| 1002 | 1046 | for (const line of notes) console.error(`gate: ${line}`); |
| 1003 | 1047 | for (const line of refusal) console.error(`gate: ${line}`); |
| 1048 | if (refused.length > 1) console.error(`gate: ${refusedSummary(refused)}`); | |
| 1004 | 1049 | process.exit(refusal.length === 0 ? 0 : 2); |
| 1005 | 1050 | } |
scripts/gate_explain.ts added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | /** | |
| 2 | * `bun scripts/gate.ts --explain` (#16325): every rule this diff will be asked to satisfy, said at | |
| 3 | * once, satisfied or not, each with the one line that satisfies it and the file the rule lives in. | |
| 4 | * | |
| 5 | * Two halves. The gate's own rules come from `GATE_RULES` (scripts/gate.ts), the table the verdict | |
| 6 | * itself reads, so the list cannot drift from what the check refuses. The guard tests that read the | |
| 7 | * tree (`GUARDS`) are the few that are fast, bind no port and need no network: each runs alone as | |
| 8 | * `bun test <file>` and its failure lines are quoted. Seconds, in a sandbox, without the suite. | |
| 9 | * Anything not listed here runs in the forge's check; `explain` says so. | |
| 10 | */ | |
| 11 | ||
| 12 | import { existsSync } from "node:fs"; | |
| 13 | import { join } from "node:path"; | |
| 14 | import type { GateRule } from "./gate.ts"; | |
| 15 | ||
| 16 | /** A guard test that reads the tree: what it holds, and the line that satisfies it. */ | |
| 17 | export type Guard = { file: string; rule: string; satisfy: string }; | |
| 18 | ||
| 19 | export const GUARDS: readonly Guard[] = [ | |
| 20 | { file: "test/doorless.test.ts", rule: "every page has a terminal door", satisfy: "import the page from a src/tui/ screen, or name it and a src/tui/ file in docs/features/<name>.md; scripts/doorless-allow.json only shrinks" }, | |
| 21 | { file: "test/feature-doors.test.ts", rule: "a how-to naming a page names its terminal file", satisfy: "name the src/tui/ file in the docs/features/<name>.md that names the page; scripts/feature-doors-allow.json only shrinks" }, | |
| 22 | { file: "test/schema-additive.test.ts", rule: "a migration is additive", satisfy: "add columns and tables only: no drop, rename, rebuild, or NOT NULL column without a default" }, | |
| 23 | { file: "test/schema-migrations.test.ts", rule: "migrations boot from production data", satisfy: "keep every migration booting from a copy of production's data: the same build twice, a renumbered rebase" }, | |
| 24 | { file: "test/route-marks.test.ts", rule: "every route is marked admin or open", satisfy: "add the route to its head's file in src/http/marks/ as admin or open" }, | |
| 25 | { file: "test/route-marks-roster.test.ts", rule: "route marks are one file per head", satisfy: "put the marks in src/http/marks/<head>.ts and list the head in the roster" }, | |
| 26 | { file: "test/hollow.test.ts", rule: "nothing is built and unreachable", satisfy: "reach the new action from a route, page or MCP tool, or list it in scripts/hollow-allow.ts under an owning issue" }, | |
| 27 | { file: "test/tui-asks-first.test.ts", rule: "a terminal action asks first", satisfy: "call a destroying action from a terminal key inside confirmScreen/promptScreen's onChoose or onSubmit" }, | |
| 28 | { file: "test/refusals-lead-somewhere.test.ts", rule: "a refusal says where to go", satisfy: "build a sentence that tells the reader to ask someone with the blocked-by helper, so it links where they can act" }, | |
| 29 | { file: "test/spawn-argv.test.ts", rule: "no spawn takes user text as a command", satisfy: "pass an argv array whose program is a literal; name a new spawn by file:line in the test with its reason" }, | |
| 30 | { file: "test/security-floor-doors.test.ts", rule: "every listener carries the security floor", satisfy: "list a new Bun.serve listener in the test with why what it returns carries the security floor" }, | |
| 31 | { file: "test/danger-buttons.test.ts", rule: "a danger button asks first", satisfy: "send a danger button through confirmAction (src/web/ui.ts), which links to /-/confirm" }, | |
| 32 | ]; | |
| 33 | ||
| 34 | /** The lines of a failed `bun test` that say what failed, at most `max`. */ | |
| 35 | export function failureLines(output: string, max = 6): string[] { | |
| 36 | const keep = output | |
| 37 | .split("\n") | |
| 38 | .map((l) => l.trim()) | |
| 39 | .filter((l) => /^(error:|\(fail\)|expect\(|Expected|Received)/.test(l) || (/\b(refused|must|names?)\b.*:/.test(l) && !l.startsWith("at "))); | |
| 40 | return [...new Set(keep)].slice(0, max); | |
| 41 | } | |
| 42 | ||
| 43 | export type Verdict = { name: string; kind: "refusal" | "note" | "guard"; ok: boolean; file: string; satisfy: string; said: string[] }; | |
| 44 | ||
| 45 | /** Runs one guard test alone; its verdict, never a throw. */ | |
| 46 | async function runGuard(g: Guard, cwd: string): Promise<Verdict> { | |
| 47 | const base = { name: g.rule, kind: "guard" as const, file: g.file, satisfy: g.satisfy }; | |
| 48 | if (!existsSync(join(cwd, g.file))) return { ...base, ok: true, said: [] }; | |
| 49 | const proc = Bun.spawn([process.execPath, "test", g.file], { cwd, stdout: "pipe", stderr: "pipe", env: process.env }); | |
| 50 | const [out, err] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]); | |
| 51 | const code = await proc.exited; | |
| 52 | return { ...base, ok: code === 0, said: code === 0 ? [] : failureLines(`${out}\n${err}`) }; | |
| 53 | } | |
| 54 | ||
| 55 | /** The report: one block per rule that is not satisfied, then one line per satisfied rule. */ | |
| 56 | export function render(verdicts: readonly Verdict[], skipped: readonly string[]): string[] { | |
| 57 | const lines: string[] = []; | |
| 58 | const bad = verdicts.filter((v) => !v.ok); | |
| 59 | const good = verdicts.filter((v) => v.ok); | |
| 60 | for (const v of bad) { | |
| 61 | const tag = v.kind === "note" ? "NOTE" : "FAILS"; | |
| 62 | lines.push(`${tag} ${v.name}`, ` rule: ${v.file}`, ` satisfy: ${v.satisfy}`, ...v.said.map((s) => ` said: ${s}`)); | |
| 63 | } | |
| 64 | if (bad.length) lines.push(""); | |
| 65 | for (const v of good) lines.push(`ok ${v.name} (${v.file})`); | |
| 66 | const fails = bad.filter((v) => v.kind !== "note").length; | |
| 67 | const notes = bad.length - fails; | |
| 68 | lines.push("", `${verdicts.length} rules read: ${fails} fail, ${notes} noted, ${good.length} satisfied.`); | |
| 69 | lines.push(`Not read here (the forge's check runs them): ${skipped.join("; ")}.`); | |
| 70 | return lines; | |
| 71 | } | |
| 72 | ||
| 73 | /** What `--explain` cannot run in a sandbox: said, so it is reported as not run. */ | |
| 74 | export const NOT_RUN = [ | |
| 75 | "bun x tsc --noEmit (needs installed dependencies)", | |
| 76 | "tests that bind a port or a unix socket", | |
| 77 | "the rest of the suite (bun scripts/fast.ts names the files your diff reaches)", | |
| 78 | ]; | |
| 79 | ||
| 80 | /** Reads every rule against the tree at `cwd`; prints the report; 0 when none fails, else 2. */ | |
| 81 | export async function explain( | |
| 82 | rules: readonly GateRule[], | |
| 83 | cwd: string, | |
| 84 | guards: readonly Guard[] = GUARDS, | |
| 85 | ): Promise<number> { | |
| 86 | const gate: Verdict[] = []; | |
| 87 | for (const r of rules) { | |
| 88 | const said = await r.run(cwd); | |
| 89 | gate.push({ name: r.name, kind: r.kind, ok: said.length === 0, file: r.file, satisfy: r.satisfy, said }); | |
| 90 | } | |
| 91 | const guarded = await Promise.all(guards.map((g) => runGuard(g, cwd))); | |
| 92 | const verdicts = [...gate, ...guarded]; | |
| 93 | for (const line of render(verdicts, NOT_RUN)) console.log(line); | |
| 94 | return verdicts.some((v) => !v.ok && v.kind !== "note") ? 2 : 0; | |
| 95 | } |
test/gate-base-list.test.ts +1 −1
| @@ -54,7 +54,7 @@ function excludeB(dir: string, alsoDeleteRefusal = false): void { | ||
| 54 | 54 | ); |
| 55 | 55 | if (alsoDeleteRefusal) { |
| 56 | 56 | const kept = source; |
| 57 | source = source.replace("...(await baseGateRefusal(cwd)),", ""); | |
| 57 | source = source.replace("run: (c) => baseGateRefusal(c)", "run: () => []"); | |
| 58 | 58 | expect(source).not.toBe(kept); |
| 59 | 59 | } |
| 60 | 60 | writeFileSync(path, source); |
test/gate-explain.test.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | import { afterAll, expect, test } from "bun:test"; | |
| 2 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 3 | import { join } from "node:path"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { GATE_RULES, gateVerdict, refusedSummary } from "../scripts/gate.ts"; | |
| 6 | import { explain, failureLines, render, GUARDS, type Verdict } from "../scripts/gate_explain.ts"; | |
| 7 | ||
| 8 | const root = mkdtempSync(join(tmpdir(), "gate-explain-")); | |
| 9 | afterAll(() => rmSync(root, { recursive: true, force: true })); | |
| 10 | ||
| 11 | const git = (cwd: string, ...args: string[]) => { | |
| 12 | const r = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "user.email=t@t", ...args], { cwd }); | |
| 13 | if (r.exitCode !== 0) throw new Error(r.stderr.toString()); | |
| 14 | return r.stdout.toString().trim(); | |
| 15 | }; | |
| 16 | const put = (dir: string, file: string, text: string) => { | |
| 17 | mkdirSync(join(dir, file, ".."), { recursive: true }); | |
| 18 | writeFileSync(join(dir, file), text); | |
| 19 | }; | |
| 20 | ||
| 21 | /** A scratch repository: a base commit, then a page, a how-to and a migration with no trailers. */ | |
| 22 | function scratch() { | |
| 23 | const dir = join(root, "scratch"); | |
| 24 | mkdirSync(dir); | |
| 25 | git(dir, "init", "-q", "-b", "work"); | |
| 26 | put(dir, "README.md", "x"); | |
| 27 | put(dir, "src/tui/screens/entries.ts", "export const ENTRIES = [];\n"); | |
| 28 | git(dir, "add", "-A"); | |
| 29 | git(dir, "commit", "-qm", "base"); | |
| 30 | const base = git(dir, "rev-parse", "HEAD"); | |
| 31 | put(dir, "src/web/pages/traffic.ts", "export const x = 1;\n"); | |
| 32 | put(dir, "docs/features/traffic.md", "x"); | |
| 33 | put(dir, "src/core/migrations/9999_traffic.ts", "export const up = 1;\n"); | |
| 34 | git(dir, "add", "-A"); | |
| 35 | git(dir, "commit", "-qm", "Add traffic"); | |
| 36 | return { dir, base }; | |
| 37 | } | |
| 38 | ||
| 39 | test("every rule names the file it lives in and the line that satisfies it", () => { | |
| 40 | const names = GATE_RULES.map((r) => r.name); | |
| 41 | expect(new Set(names).size).toBe(names.length); | |
| 42 | for (const r of GATE_RULES) { | |
| 43 | expect(r.file.length).toBeGreaterThan(0); | |
| 44 | expect(r.satisfy.length).toBeGreaterThan(10); | |
| 45 | } | |
| 46 | for (const g of GUARDS) expect(g.satisfy.length).toBeGreaterThan(10); | |
| 47 | }); | |
| 48 | ||
| 49 | test("on a scratch diff with no trailers, explain lists exactly the rules the gate refuses", async () => { | |
| 50 | const { dir, base } = scratch(); | |
| 51 | process.env.FORGE_CHECK_BASE = base; | |
| 52 | try { | |
| 53 | const verdict = await gateVerdict(dir); | |
| 54 | const logs: string[] = []; | |
| 55 | const log = console.log; | |
| 56 | console.log = (...a: unknown[]) => void logs.push(a.join(" ")); | |
| 57 | let code: number; | |
| 58 | try { | |
| 59 | code = await explain(GATE_RULES, dir, []); | |
| 60 | } finally { | |
| 61 | console.log = log; | |
| 62 | } | |
| 63 | expect(code).toBe(2); | |
| 64 | const failing = logs.filter((l) => l.startsWith("FAILS")).map((l) => l.slice(6).trim()); | |
| 65 | expect(failing).toEqual(verdict.refused); | |
| 66 | // The page lacks a screen, the page and migration lack a test, and the feature names no issue. | |
| 67 | expect(failing).toEqual(expect.arrayContaining(["both doors", "a test with the change", "name the issue"])); | |
| 68 | const text = logs.join("\n"); | |
| 69 | expect(text).toContain("satisfy: add the terminal screen"); | |
| 70 | expect(text).toContain("rule: src/kernel/both_doors.ts"); | |
| 71 | // A rule the diff does not meet is said satisfied, not omitted. | |
| 72 | expect(text).toContain("ok stray test"); | |
| 73 | } finally { | |
| 74 | delete process.env.FORGE_CHECK_BASE; | |
| 75 | } | |
| 76 | }); | |
| 77 | ||
| 78 | test("a failing guard test is listed with what it said; a passing one is ok", () => { | |
| 79 | const v: Verdict[] = [ | |
| 80 | { name: "g", kind: "guard", ok: false, file: "test/g.test.ts", satisfy: "do it", said: ["error: page x has no door"] }, | |
| 81 | { name: "h", kind: "guard", ok: true, file: "test/h.test.ts", satisfy: "do it", said: [] }, | |
| 82 | ]; | |
| 83 | const out = render(v, ["tsc"]).join("\n"); | |
| 84 | expect(out).toContain("FAILS g"); | |
| 85 | expect(out).toContain("said: error: page x has no door"); | |
| 86 | expect(out).toContain("ok h"); | |
| 87 | expect(out).toContain("1 fail"); | |
| 88 | expect(out).toContain("Not read here"); | |
| 89 | expect(failureLines("noise\nerror: boom\nnoise")).toEqual(["error: boom"]); | |
| 90 | }); | |
| 91 | ||
| 92 | test("the gate's last line names every rule that refused, not the last one", () => { | |
| 93 | const line = refusedSummary(["both doors", "a test with the change", "name the issue"]); | |
| 94 | expect(line).toStartWith("refused by 3 rules: both doors, a test with the change, name the issue"); | |
| 95 | }); | |
| 96 | ||
| 97 | test("explain runs guard files: a failing one FAILS with what it said, a passing one is ok, a missing one is ok, and a guard alone makes exit 2", async () => { | |
| 98 | const dir = join(root, "guards"); | |
| 99 | put(dir, "test/bad.test.ts", 'import { test, expect } from "bun:test";\ntest("x", () => { expect(1).toBe(2); });\n'); | |
| 100 | put(dir, "test/good.test.ts", 'import { test, expect } from "bun:test";\ntest("y", () => { expect(1).toBe(1); });\n'); | |
| 101 | const guard = (file: string, rule: string) => ({ file, rule, satisfy: "make it pass please" }); | |
| 102 | const run = async (guards: ReturnType<typeof guard>[]) => { | |
| 103 | const logs: string[] = []; | |
| 104 | const log = console.log; | |
| 105 | console.log = (...a: unknown[]) => void logs.push(a.join(" ")); | |
| 106 | try { | |
| 107 | return { code: await explain([], dir, guards), text: logs.join("\n") }; | |
| 108 | } finally { | |
| 109 | console.log = log; | |
| 110 | } | |
| 111 | }; | |
| 112 | const mixed = await run([guard("test/bad.test.ts", "bad guard"), guard("test/good.test.ts", "good guard"), guard("test/gone.test.ts", "gone guard")]); | |
| 113 | expect(mixed.code).toBe(2); | |
| 114 | expect(mixed.text).toContain("FAILS bad guard"); | |
| 115 | expect(mixed.text).toMatch(/said: (error:|\(fail\)|Expected|Received|expect\()/); | |
| 116 | expect(mixed.text).toContain("ok good guard"); | |
| 117 | expect(mixed.text).toContain("ok gone guard"); | |
| 118 | const passing = await run([guard("test/good.test.ts", "good guard"), guard("test/gone.test.ts", "gone guard")]); | |
| 119 | expect(passing.code).toBe(0); | |
| 120 | }, 30000); |
test/pin-refusal.test.ts +6 −2
| @@ -183,7 +183,7 @@ test("a pull rebased onto main passes, and so does one behind main's newer pins" | ||
| 183 | 183 | expect(await withBase(undefined, () => pinRefusal(dir))).toEqual([]); |
| 184 | 184 | }); |
| 185 | 185 | |
| 186 | test("a migration added without its pin is named with the one-step fix, last, as the check's summary", async () => { | |
| 186 | test("a migration added without its pin is named with the one-step fix, and the check's summary names every rule that refused", async () => { | |
| 187 | 187 | const { dir, base } = repo("unpinned"); |
| 188 | 188 | put(dir, "src/core/migrations/0003-c.ts", "c"); |
| 189 | 189 | put(dir, "src/core/migrations/0004-d.ts", "d"); |
| @@ -197,5 +197,9 @@ test("a migration added without its pin is named with the one-step fix, last, as | ||
| 197 | 197 | const r = Bun.spawnSync(["bun", gateScript], { cwd: dir, env: { ...process.env, FORGE_CHECK_BASE: base } }); |
| 198 | 198 | expect(r.exitCode).toBe(2); |
| 199 | 199 | const lines = r.stderr.toString().split("\n"); |
| 200 | expect(summariseCheckFailure(lines, "check exited with code 2").summary).toBe(`gate: ${named}`); | |
| 200 | expect(lines).toContain(`gate: ${named}`); | |
| 201 | // The fixture also trips "slow files" (it has no test files): the summary says both (#16325). | |
| 202 | const summary = summariseCheckFailure(lines, "check exited with code 2").summary; | |
| 203 | expect(summary).toStartWith("gate: refused by 2 rules: "); | |
| 204 | expect(summary).toContain("migration pins"); | |
| 201 | 205 | }); |
new above
offline