diff --git a/e2e/daemon-lifecycle.test.ts b/e2e/daemon-lifecycle.test.ts index 95baae3b9..dd87182e6 100644 --- a/e2e/daemon-lifecycle.test.ts +++ b/e2e/daemon-lifecycle.test.ts @@ -49,18 +49,12 @@ describe("daemon lifecycle & recovery", () => { expect(status.json).toMatchObject({ daemon: { health: "running" } }); }); - // Suspected bug: `simlock daemon status` always writes raw JSON via `writeResult` - // (src/cli/index.ts `runDaemon`, the "status" branch) regardless of `--json`, - // unlike its siblings `daemon start`/`daemon stop` which both branch on - // `values.json` to print "Daemon running"/"Daemon stopping" in human mode. This - // contradicts docs/CLI.md ("`daemon ` ... default to a - // human-oriented view ... and accept `--json`"). See final report. - it.fails("daemon status prints a human-oriented view by default", async () => { + it("daemon status prints a human-oriented view by default", async () => { const env = await withDaemon({ mode: "running" }); const statusHuman = await env.cli(["daemon", "status"]); expect(statusHuman.code).toBe(0); - expect(statusHuman.stdout).toContain("Daemon running"); + expect(statusHuman.stdout.split("\n")[0]).toBe("Daemon: running (worker)"); }); it("stops while a detached lease is still outstanding", async () => { diff --git a/src/cli/index.test.ts b/src/cli/index.test.ts index 8ac17bc70..5318dc039 100644 --- a/src/cli/index.test.ts +++ b/src/cli/index.test.ts @@ -1334,6 +1334,19 @@ describe("CLI: daemon status (ADR 0003 §11)", () => { expect(launched).toBe(false); }); + it("prints the human view for daemon status when --json is absent", async () => { + const output = outputCapture(); + + await expect( + runCli( + ["daemon", "status"], + output.environmentWith({ connectExistingAdmin: async () => fakeClient() }), + ), + ).resolves.toBe(0); + + expect(output.stdout.split("\n")[0]).toBe("Daemon: running (worker)"); + }); + it("asks the daemon to purge orphans only once the operator has confirmed", async () => { const output = outputCapture(); const seen: unknown[] = []; diff --git a/src/expected-failures.test.ts b/src/expected-failures.test.ts new file mode 100644 index 000000000..2fd860dc2 --- /dev/null +++ b/src/expected-failures.test.ts @@ -0,0 +1,153 @@ +import { execFileSync } from "node:child_process"; +import { readFileSync, statSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { describe, expect, it } from "vitest"; + +import { stripComments } from "./test-support/imports.js"; + +const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); + +/** + * why: matched against a whole comment-stripped file rather than line by line, so a call whose + * table spans several lines is still found, and prose that merely names the API is not a hit. + * why: the modifier chain is allowed on both sides of `fails` because that is where vitest puts it + * (`it.only` and `fails.each` are both real modifiers there), and a guard with a hole reads as + * coverage the suite no longer has. + */ +const EXPECTED_FAILURE = + /\b(?:it|test|describe)(?:\.[A-Za-z]+(?:\([^()]*\))?)*\.fails\b(?:\.[A-Za-z]+(?:\([^()]*\))?)*\(/g; + +// why: each chain is held in pieces and without its arguments, because a string holding the whole +// call is text the scan below reads and this file would then name itself as an offender. +const expectedFailureSpellings: { shape: string; parts: string[] }[] = [ + { shape: "it.fails", parts: ["it", "fails"] }, + { shape: "test.fails", parts: ["test", "fails"] }, + { shape: "describe.fails", parts: ["describe", "fails"] }, + { shape: "it.skipIf(false).fails", parts: ["it", "skipIf(false)", "fails"] }, + { shape: "it.only.fails", parts: ["it", "only", "fails"] }, + { shape: "it.fails.each", parts: ["it", "fails", "each([[1, 2]])"] }, + { shape: "it.fails.only", parts: ["it", "fails", "only"] }, + { shape: "it.fails.concurrent", parts: ["it", "fails", "concurrent"] }, + { shape: "it.fails.skipIf", parts: ["it", "fails", "skipIf(flag)"] }, +]; + +// why: a matcher that fires on `it.skip` or a bare `it` would hold back code the suite needs, so +// the shapes it must leave alone are pinned beside the ones it must catch. +const otherRegistrations: { shape: string; parts: string[] }[] = [ + { shape: "it.skip", parts: ["it", "skip"] }, + { shape: "it.todo", parts: ["it", "todo"] }, + { shape: "it.only", parts: ["it", "only"] }, + { shape: "it", parts: ["it"] }, + { shape: "expect(promise).rejects.toThrow", parts: ["expect(promise)", "rejects", "toThrow"] }, +]; + +// why: enumerated from `git ls-files` rather than a directory walk, so the scan reaches every +// tree the rule covers and not only the ones this file happens to name (testing rule 4). +const testFiles = execFileSync( + "git", + ["ls-files", "-z", "--", "*.test.ts", "*.test.tsx", "*.spec.ts"], + { cwd: REPO_ROOT, encoding: "utf8" }, +) + .split("\0") + .filter((name) => name.length > 0) + // why: `git ls-files` still lists a path deleted from the working tree, and only a regular + // file can be read. + .filter((name) => { + try { + return statSync(join(REPO_ROOT, name)).isFile(); + } catch { + return false; + } + }); + +function expectedFailuresIn(text: string, name: string): string[] { + const stripped = stripComments(text); + const found: string[] = []; + for (const match of stripped.matchAll(EXPECTED_FAILURE)) { + const line = stripped.slice(0, match.index).split("\n").length; + found.push(`${name}:${line}: ${(match[0] ?? "").split("\n")[0]?.trim() ?? ""}`); + } + return found; +} + +describe("the tracked test files", () => { + it("were enumerated from the git index, in `src/`, `e2e/` and `ui/` alike", () => { + // why: a pathspec or a `cwd` that matched nothing would leave the scan below with no file to + // read, and the rule would read as covered while proving nothing. + for (const tree of ["src", "e2e", "ui"]) { + expect( + testFiles.some((name) => name.startsWith(`${tree}/`)), + `no tracked test file found under ${tree}/`, + ).toBe(true); + } + }); + + it("hold no test written to pass by failing", () => { + const offenders = testFiles.flatMap((name) => + expectedFailuresIn(readFileSync(join(REPO_ROOT, name), "utf8"), name), + ); + + expect(offenders).toEqual([]); + }); +}); + +describe("the expected-failure matcher", () => { + it.each(expectedFailureSpellings)("finds the expected failure in `$shape`", ({ parts }) => { + const call = `${parts.join(".")}("a title", () => {})`; + + expect(expectedFailuresIn(call, "probe.test.ts")).toHaveLength(1); + }); + + it.each(otherRegistrations)("finds no expected failure in `$shape`", ({ parts }) => { + const call = `${parts.join(".")}("a title", () => {})`; + + expect(expectedFailuresIn(call, "probe.test.ts")).toEqual([]); + }); + + it("matches a call whose table is written across several lines", () => { + // why: the call's head is assembled here rather than written out, because a string holding + // the whole call is text the scanner reads and would count against this file. + const call = [ + `${["it", "fails", "each"].join(".")}([`, + " [1, 2],", + "])(", + ' "a title",', + " (a, b) => {},", + ")", + ].join("\n"); + + // why: the expected offender is rebuilt from `call` rather than written out, because a string + // holding the whole call is text this file's own scan reads, and it would then name itself. + expect(expectedFailuresIn(call, "probe.test.ts")).toEqual([ + `probe.test.ts:1: ${call.split("\n")[0]}`, + ]); + }); + + it("reports the line the call is written on, even below a multi-line comment", () => { + // why: the offender is reported as path and line so a reader opens the file at the right + // place; a comment stripped without its newlines would point them at the header instead. + const file = [ + "/*", + " * a header", + " * of several", + " */", + `${["it", "fails"].join(".")}("a title", () => {})`, + ].join("\n"); + // why: the expected report line is assembled from pieces like the calls above, because a + // string holding the whole call is text this file's own scan reads, and it would name itself. + expect(expectedFailuresIn(file, "probe.test.ts")).toEqual([ + `probe.test.ts:5: ${["it", "fails("].join(".")}`, + ]); + }); + + it("does not report a hit for prose that only names the API", () => { + // why: built in pieces for the same reason as the call above — a string holding the whole call + // is text this file's own scan reads, and it would then name itself. + const named = `// ${["it", "fails", "each([[1, 2]])"].join(".")}("a title", () => {})`; + const withProse = [named, 'it.todo("a title")'].join("\n"); + + expect(expectedFailuresIn(withProse, "probe.test.ts")).toEqual([]); + }); +}); diff --git a/src/test-support/imports.ts b/src/test-support/imports.ts index 88ed861bb..7ee8b4175 100644 --- a/src/test-support/imports.ts +++ b/src/test-support/imports.ts @@ -19,7 +19,11 @@ export const srcDir = dirname(dirname(fileURLToPath(import.meta.url))); * naming a forbidden import) is never mistaken for a real one. Good enough for this repo's * source, which never puts `//` inside an import specifier string. */ export function stripComments(source: string): string { - return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, ""); + // why: a block comment leaves its newlines behind, so a report that counts lines in the + // stripped text names the line the reported code is actually written on. + return source + .replace(/\/\*[\s\S]*?\*\//g, (block) => block.replace(/[^\n]/g, "")) + .replace(/\/\/.*$/gm, ""); } /**