From 82298d256c7489810711093f0dde11bde3ea4c9a Mon Sep 17 00:00:00 2001 From: Justin Parker Date: Tue, 3 Nov 2026 21:04:32 -0800 Subject: [PATCH] Add test command discovery and a check runner, and offer a test map in Init AI Assisted --- install.js | 4 +- package.json | 2 +- scripts/test-script-run-check.mjs | 112 +++++++++++ scripts/test-script-test-commands.mjs | 154 +++++++++++++++ scripts/test-skill-test-runs.mjs | 18 +- src/plan2code-init-update.md | 5 +- src/plan2code-init.md | 2 +- src/shared-references/test-runs.md | 6 +- src/skill-scripts/run-check.mjs | 149 ++++++++++++++ src/skill-scripts/test-commands.mjs | 267 ++++++++++++++++++++++++++ 10 files changed, 706 insertions(+), 13 deletions(-) create mode 100644 scripts/test-script-run-check.mjs create mode 100644 scripts/test-script-test-commands.mjs create mode 100644 src/skill-scripts/run-check.mjs create mode 100644 src/skill-scripts/test-commands.mjs diff --git a/install.js b/install.js index fbc6ae9..5199d85 100644 --- a/install.js +++ b/install.js @@ -143,7 +143,7 @@ const SOURCE_PROMPTS = [ displayName: 'Init Mode', description: 'Generate AGENTS.md file for project-specific guidance', isUtility: true, - scripts: ['agents-md.mjs', 'agent-files.mjs', 'test-map.mjs', 'glob.mjs'], + scripts: ['agents-md.mjs', 'agent-files.mjs', 'test-map.mjs', 'glob.mjs', 'test-commands.mjs'], additionalReferences: [ { source: 'web-console', target: 'web-console' }, { source: 'shared-references/helper-strength.md', target: 'helper-strength.md' } @@ -156,7 +156,7 @@ const SOURCE_PROMPTS = [ displayName: 'Init Update Mode', description: 'Update existing AGENTS.md with new learnings', isUtility: true, - scripts: ['agents-md.mjs', 'agent-files.mjs', 'commit-msg.mjs', 'test-map.mjs', 'glob.mjs'], + scripts: ['agents-md.mjs', 'agent-files.mjs', 'commit-msg.mjs', 'test-map.mjs', 'glob.mjs', 'test-commands.mjs'], additionalReferences: [ { source: 'web-console', target: 'web-console' }, { source: 'shared-references/helper-strength.md', target: 'helper-strength.md' } diff --git a/package.json b/package.json index dd73545..aaeed84 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "fix:eol": "node scripts/fix-line-endings.mjs", "play": "node scripts/play.mjs", "pretest": "node scripts/fix-line-endings.mjs", - "test": "node scripts/validate-char-count.js && node install.js --build-skills && node scripts/parse-check.mjs && node scripts/check-skill-table.mjs && node scripts/node-test.mjs scripts/test-line-endings.mjs scripts/test-us-english.mjs scripts/test-script-git.mjs scripts/test-git-repo.mjs scripts/test-skill-scripts.mjs scripts/test-script-test-map.mjs scripts/test-script-test-match.mjs scripts/test-script-changelog.mjs scripts/test-web-console-git.mjs scripts/test-web-console-git-board.mjs scripts/test-web-console-chat.mjs scripts/test-web-console-subagents.mjs scripts/test-web-console-pointers.mjs scripts/test-console-wire-budgets.mjs scripts/test-web-console-speed.mjs scripts/test-web-console-qa.mjs scripts/test-web-console-concurrency.mjs scripts/test-web-console-workspace.mjs scripts/test-web-console-lifecycle.mjs scripts/test-web-console-chat-stress.mjs scripts/test-web-console-server.mjs scripts/test-web-console-session.mjs scripts/test-web-console-logic.mjs scripts/test-web-console-activities.mjs scripts/test-web-console-activities-rapids.mjs scripts/test-web-console-activities-bounce.mjs scripts/test-web-console-activities-bounce-game.mjs scripts/test-web-console-activities-ghoul.mjs scripts/test-web-console-activities-ghoul-graveyard.mjs scripts/test-web-console-activities-ghoul-grounds.mjs scripts/test-web-console-activities-ghoul-chapel.mjs scripts/test-web-console-activities-ghoul-sanatorium.mjs scripts/test-web-console-activities-ghoul-village.mjs scripts/test-web-console-activities-ghoul-mine.mjs scripts/test-web-console-activities-ghoul-carnival.mjs scripts/test-web-console-activities-ghoul-frozen.mjs scripts/test-web-console-activities-ghoul-bog.mjs scripts/test-web-console-activities-ghoul-lantern.mjs scripts/test-web-console-activities-ghoul-marrow.mjs scripts/test-web-console-activities-ghoul-mat.mjs scripts/test-web-console-activities-ghoul-eye.mjs scripts/test-web-console-activities-ghoul-bonestair.mjs scripts/test-web-console-activities-ghoul-heart.mjs scripts/test-web-console-activities-ghoul-ending.mjs scripts/test-web-console-activities-ghoul-ride.mjs scripts/test-web-console-activities-ghoul-cart.mjs scripts/test-web-console-activities-ghoul-sled.mjs scripts/test-web-console-activities-ghoul-banister.mjs scripts/test-web-console-activities-ghoul-gurney.mjs scripts/test-web-console-activities-ghoul-hearse.mjs scripts/test-web-console-activities-ghoul-wagon.mjs scripts/test-web-console-activities-ghoul-pew.mjs scripts/test-web-console-activities-ghoul-mower.mjs scripts/test-web-console-activities-ghoul-tray.mjs scripts/test-web-console-activities-ghoul-arms.mjs scripts/test-web-console-activities-ghoul-dice.mjs scripts/test-web-console-activities-ghoul-gameover.mjs scripts/test-web-console-activities-ghoul-intro.mjs scripts/test-web-console-activities-ghoul-bossintro.mjs scripts/test-web-console-activities-ghoul-story.mjs scripts/test-web-console-activities-ghoul-bones.mjs scripts/test-web-console-activities-ghoul-retry.mjs scripts/test-web-console-activities-bounce-cars.mjs scripts/test-web-console-tones.mjs scripts/test-web-console-skills.mjs scripts/test-web-console-spec-states.mjs scripts/test-web-console-state.mjs scripts/test-web-console-shape.mjs scripts/test-script-pathfinder.mjs scripts/test-skill-test-runs.mjs scripts/test-launcher.mjs scripts/test-agents-file.mjs scripts/test-agent-lookup.mjs scripts/test-web-console-source.mjs scripts/test-web-console-hardening.mjs scripts/test-web-console-embed.mjs scripts/test-web-console-in-app.mjs scripts/test-web-console-loop.mjs scripts/test-web-console-handoffs.mjs scripts/test-web-console-notepad.mjs scripts/test-script-xray.mjs scripts/test-script-finalize.mjs scripts/test-getting-started.mjs scripts/test-install-update.mjs scripts/test-browser-helper.mjs scripts/test-web-console-page-cards.mjs scripts/test-web-console-page-dashboard.mjs scripts/test-web-console-page-session.mjs scripts/test-web-console-page-ask.mjs scripts/test-web-console-page-workspace.mjs scripts/test-web-console-page-help.mjs scripts/test-web-console-page-prefs.mjs scripts/test-web-console-page-git.mjs scripts/test-web-console-page-notepad.mjs scripts/test-web-console-page-send.mjs scripts/test-web-console-page-loop.mjs scripts/test-web-console-page-activities.mjs scripts/test-web-console-page-setup.mjs scripts/test-standalone-check.mjs scripts/test-check-versions.mjs scripts/test-app-meter.mjs" + "test": "node scripts/validate-char-count.js && node install.js --build-skills && node scripts/parse-check.mjs && node scripts/check-skill-table.mjs && node scripts/node-test.mjs scripts/test-line-endings.mjs scripts/test-us-english.mjs scripts/test-script-git.mjs scripts/test-git-repo.mjs scripts/test-skill-scripts.mjs scripts/test-script-test-map.mjs scripts/test-script-test-match.mjs scripts/test-script-test-commands.mjs scripts/test-script-run-check.mjs scripts/test-script-changelog.mjs scripts/test-web-console-git.mjs scripts/test-web-console-git-board.mjs scripts/test-web-console-chat.mjs scripts/test-web-console-subagents.mjs scripts/test-web-console-pointers.mjs scripts/test-console-wire-budgets.mjs scripts/test-web-console-speed.mjs scripts/test-web-console-qa.mjs scripts/test-web-console-concurrency.mjs scripts/test-web-console-workspace.mjs scripts/test-web-console-lifecycle.mjs scripts/test-web-console-chat-stress.mjs scripts/test-web-console-server.mjs scripts/test-web-console-session.mjs scripts/test-web-console-logic.mjs scripts/test-web-console-activities.mjs scripts/test-web-console-activities-rapids.mjs scripts/test-web-console-activities-bounce.mjs scripts/test-web-console-activities-bounce-game.mjs scripts/test-web-console-activities-ghoul.mjs scripts/test-web-console-activities-ghoul-graveyard.mjs scripts/test-web-console-activities-ghoul-grounds.mjs scripts/test-web-console-activities-ghoul-chapel.mjs scripts/test-web-console-activities-ghoul-sanatorium.mjs scripts/test-web-console-activities-ghoul-village.mjs scripts/test-web-console-activities-ghoul-mine.mjs scripts/test-web-console-activities-ghoul-carnival.mjs scripts/test-web-console-activities-ghoul-frozen.mjs scripts/test-web-console-activities-ghoul-bog.mjs scripts/test-web-console-activities-ghoul-lantern.mjs scripts/test-web-console-activities-ghoul-marrow.mjs scripts/test-web-console-activities-ghoul-mat.mjs scripts/test-web-console-activities-ghoul-eye.mjs scripts/test-web-console-activities-ghoul-bonestair.mjs scripts/test-web-console-activities-ghoul-heart.mjs scripts/test-web-console-activities-ghoul-ending.mjs scripts/test-web-console-activities-ghoul-ride.mjs scripts/test-web-console-activities-ghoul-cart.mjs scripts/test-web-console-activities-ghoul-sled.mjs scripts/test-web-console-activities-ghoul-banister.mjs scripts/test-web-console-activities-ghoul-gurney.mjs scripts/test-web-console-activities-ghoul-hearse.mjs scripts/test-web-console-activities-ghoul-wagon.mjs scripts/test-web-console-activities-ghoul-pew.mjs scripts/test-web-console-activities-ghoul-mower.mjs scripts/test-web-console-activities-ghoul-tray.mjs scripts/test-web-console-activities-ghoul-arms.mjs scripts/test-web-console-activities-ghoul-dice.mjs scripts/test-web-console-activities-ghoul-gameover.mjs scripts/test-web-console-activities-ghoul-intro.mjs scripts/test-web-console-activities-ghoul-bossintro.mjs scripts/test-web-console-activities-ghoul-story.mjs scripts/test-web-console-activities-ghoul-bones.mjs scripts/test-web-console-activities-ghoul-retry.mjs scripts/test-web-console-activities-bounce-cars.mjs scripts/test-web-console-tones.mjs scripts/test-web-console-skills.mjs scripts/test-web-console-spec-states.mjs scripts/test-web-console-state.mjs scripts/test-web-console-shape.mjs scripts/test-script-pathfinder.mjs scripts/test-skill-test-runs.mjs scripts/test-launcher.mjs scripts/test-agents-file.mjs scripts/test-agent-lookup.mjs scripts/test-web-console-source.mjs scripts/test-web-console-hardening.mjs scripts/test-web-console-embed.mjs scripts/test-web-console-in-app.mjs scripts/test-web-console-loop.mjs scripts/test-web-console-handoffs.mjs scripts/test-web-console-notepad.mjs scripts/test-script-xray.mjs scripts/test-script-finalize.mjs scripts/test-getting-started.mjs scripts/test-install-update.mjs scripts/test-browser-helper.mjs scripts/test-web-console-page-cards.mjs scripts/test-web-console-page-dashboard.mjs scripts/test-web-console-page-session.mjs scripts/test-web-console-page-ask.mjs scripts/test-web-console-page-workspace.mjs scripts/test-web-console-page-help.mjs scripts/test-web-console-page-prefs.mjs scripts/test-web-console-page-git.mjs scripts/test-web-console-page-notepad.mjs scripts/test-web-console-page-send.mjs scripts/test-web-console-page-loop.mjs scripts/test-web-console-page-activities.mjs scripts/test-web-console-page-setup.mjs scripts/test-standalone-check.mjs scripts/test-check-versions.mjs scripts/test-app-meter.mjs" }, "devDependencies": { "gpt-tokenizer": "2.9.0", diff --git a/scripts/test-script-run-check.mjs b/scripts/test-script-run-check.mjs new file mode 100644 index 0000000..3d26034 --- /dev/null +++ b/scripts/test-script-run-check.mjs @@ -0,0 +1,112 @@ +// run-check.mjs: the four outcomes, the process-tree kill on timeout, the log +// and the tail cap. Runs src/skill-scripts/run-check.mjs directly until a +// skill ships it. See skill-script-helpers.mjs for the fixtures. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; +import { ROOT, NOW, scrubbedEnv, tmp, write } from "./skill-script-helpers.mjs"; + +const SCRIPT = path.join(ROOT, "src", "skill-scripts", "run-check.mjs"); +const NODE = `"${process.execPath}"`; + +function runShared(args, { cwd } = {}) { + const r = spawnSync(process.execPath, [SCRIPT, ...args], { cwd: cwd || ROOT, encoding: "utf8", env: scrubbedEnv({ PLAN2CODE_NOW: NOW }) }); + let json = null; + try { + json = JSON.parse(r.stdout); + } catch {} + return { code: r.status, json, stdout: r.stdout, stderr: r.stderr }; +} + +const alive = (pid) => { + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } +}; + +test("run-check: passed on exit 0", () => { + const r = runShared(["--cmd", `${NODE} -e "process.exit(0)"`]); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.outcome, "passed"); + assert.equal(r.json.exit, 0); + assert.equal(r.json.cwd, "."); + assert.equal(typeof r.json.seconds, "number"); + assert.equal(r.json.log, null); +}); + +test("run-check: failed on exit 1, with the printed lines in the tail", () => { + const dir = tmp("check-fail"); + write(dir, "fail.js", "console.log('first line');\nconsole.error('second line');\nprocess.exit(1);\n"); + const r = runShared(["--cmd", `${NODE} fail.js`, "--cwd", dir]); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.outcome, "failed"); + assert.equal(r.json.exit, 1); + assert.ok(r.json.tail.includes("first line"), JSON.stringify(r.json.tail)); + assert.ok(r.json.tail.includes("second line"), JSON.stringify(r.json.tail)); +}); + +test("run-check: not-started for a made-up command and for a missing --cwd", () => { + const r = runShared(["--cmd", "p2c-no-such-command-xyz --flag"]); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.outcome, "not-started"); + assert.equal(r.json.reason, "command-not-found"); + const c = runShared(["--cmd", `${NODE} -e "process.exit(0)"`, "--cwd", path.join(tmp("check-cwd"), "missing")]); + assert.equal(c.code, 0, c.stderr); + assert.equal(c.json.outcome, "not-started"); + assert.equal(c.json.reason, "cwd-missing"); +}); + +test("run-check: timeout kills the whole process tree", async () => { + const dir = tmp("check-timeout"); + write(dir, "grandchild.js", "require('fs').writeFileSync(require('path').join(__dirname, 'pid.txt'), String(process.pid));\nsetInterval(() => {}, 1000);\n"); + write(dir, "parent.js", "require('child_process').spawn(process.execPath, [require('path').join(__dirname, 'grandchild.js')], { stdio: 'inherit' });\nsetInterval(() => {}, 1000);\n"); + const r = runShared(["--cmd", `${NODE} parent.js`, "--cwd", dir, "--timeout", "2"]); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.outcome, "timeout"); + const pidFile = path.join(dir, "pid.txt"); + assert.ok(fs.existsSync(pidFile), "the grandchild started"); + const pid = Number(fs.readFileSync(pidFile, "utf8")); + const until = Date.now() + 5000; + while (alive(pid) && Date.now() < until) await new Promise((res) => setTimeout(res, 100)); + assert.equal(alive(pid), false, `grandchild ${pid} is gone`); +}); + +test("run-check: --log holds the full output and the tail keeps 60 lines of at most 400 characters", () => { + const dir = tmp("check-log"); + write(dir, "many.js", "for (let i = 1; i <= 100; i++) console.log('line ' + i);\nconsole.log('x'.repeat(500));\n"); + const log = path.join(dir, "logs", "deep", "run.log"); + const r = runShared(["--cmd", `${NODE} many.js`, "--cwd", dir, "--log", log]); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.outcome, "passed"); + assert.equal(r.json.log, log); + const full = fs.readFileSync(log, "utf8").split(/\r?\n/).filter(Boolean); + assert.equal(full.length, 101); + assert.equal(full[0], "line 1"); + assert.equal(r.json.tail.length, 60); + assert.equal(r.json.tail[0], "line 42"); + assert.equal(r.json.tail[59].length, 400); + assert.deepEqual(fs.readdirSync(dir).sort(), ["logs", "many.js"], "nothing written outside --log"); +}); + +test("run-check: bad arguments exit 2", () => { + assert.equal(runShared([]).code, 2); + assert.equal(runShared(["--cmd", "x", "--timeout", "soon"]).code, 2); + assert.equal(runShared(["--cmd", "x", "--timeout", "0"]).code, 2); + assert.equal(runShared(["--cmd", "x", "--timeout", "86401"]).code, 2, "past one day the timer would overflow"); + assert.equal(runShared(["--cmd", "x", "extra"]).code, 2); +}); + +test("run-check and test-commands do nothing when imported", () => { + for (const file of ["run-check.mjs", "test-commands.mjs"]) { + const url = new URL(`file:///${path.join(ROOT, "src", "skill-scripts", file).replace(/\\/g, "/")}`).href; + const r = spawnSync(process.execPath, ["--input-type=module", "-e", `await import(${JSON.stringify(url)});`], { encoding: "utf8", env: scrubbedEnv() }); + assert.equal(r.status, 0, `${file}: ${r.stderr}`); + assert.equal(r.stdout, "", `${file} printed nothing on import`); + } +}); diff --git a/scripts/test-script-test-commands.mjs b/scripts/test-script-test-commands.mjs new file mode 100644 index 0000000..782d686 --- /dev/null +++ b/scripts/test-script-test-commands.mjs @@ -0,0 +1,154 @@ +// test-commands.mjs discover: one fixture per ecosystem, run from the built +// plan2code-init skill. See skill-script-helpers.mjs for how these run. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { runScript, tmp, write } from "./skill-script-helpers.mjs"; + +function discover(files) { + const dir = tmp("cmds"); + for (const [rel, content] of Object.entries(files)) write(dir, rel, typeof content === "string" ? content : JSON.stringify(content)); + const r = runScript("plan2code-init", "test-commands.mjs", ["discover"], { cwd: dir }); + assert.equal(r.code, 0, r.stderr); + assert.equal(r.json.ok, true); + return r.json; +} + +test("npm project: suite, lint, types and build checks", () => { + const r = discover({ "package.json": { scripts: { test: "node --test", lint: "eslint .", typecheck: "tsc --noEmit", build: "tsc" } } }); + assert.deepEqual(r.suite, { cmd: "npm test", why: "package.json scripts.test" }); + assert.deepEqual(r.runners, [{ cmd: "npm test", cwd: ".", covers: ["**"] }]); + assert.deepEqual(r.checks, [ + { kind: "lint", cmd: "npm run lint" }, + { kind: "types", cmd: "npm run typecheck" }, + { kind: "build", cmd: "npm run build" }, + ]); + assert.deepEqual(r.coverage, [{ runner: "node", cmd: "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=", cwd: ".", reads: "lcov" }]); + assert.equal(r.builtin, null); +}); + +test("the lockfile picks the package manager; tsconfig gives npx tsc", () => { + const r = discover({ "package.json": { scripts: { test: "jest", lint: "eslint ." } }, "pnpm-lock.yaml": "", "tsconfig.json": "{}" }); + assert.equal(r.suite.cmd, "pnpm test"); + assert.deepEqual(r.checks, [ + { kind: "lint", cmd: "pnpm run lint" }, + { kind: "types", cmd: "npx tsc --noEmit" }, + ]); + assert.equal(discover({ "package.json": { scripts: { test: "x" } }, "yarn.lock": "" }).suite.cmd, "yarn test"); + assert.equal(discover({ "package.json": { scripts: { test: "x" } }, "bun.lock": "" }).suite.cmd, "bun test"); +}); + +test("npm's placeholder test script is no suite", () => { + const r = discover({ "package.json": { scripts: { test: 'echo "Error: no test specified" && exit 1' } } }); + assert.equal(r.suite, null); + assert.deepEqual(r.runners, []); + assert.deepEqual(r.builtin, { language: "node", cmd: "node --test" }); +}); + +test("npm workspaces array: one runner per package with a test script", () => { + const r = discover({ + "package.json": { workspaces: ["packages/*"], scripts: { test: "npm test --workspaces" } }, + "packages/a/package.json": { scripts: { test: "node --test" } }, + "packages/b/package.json": { scripts: { test: "vitest" }, devDependencies: { vitest: "1", "@vitest/coverage-v8": "1" } }, + "packages/c/package.json": { scripts: {} }, + }); + assert.deepEqual(r.runners, [ + { cmd: "npm test", cwd: ".", covers: ["**"] }, + { cmd: "npm test", cwd: "packages/a", covers: ["packages/a/**"] }, + { cmd: "npm test", cwd: "packages/b", covers: ["packages/b/**"] }, + ]); + assert.deepEqual(r.coverage.map((c) => [c.runner, c.cwd]), [["node", "packages/a"], ["vitest", "packages/b"]]); +}); + +test("pnpm workspace: packages lines and the { packages } form", () => { + const r = discover({ + "package.json": { name: "root" }, + "pnpm-lock.yaml": "", + "pnpm-workspace.yaml": "packages:\n - 'apps/*'\n - \"libs/core\"\n - '!apps/skip'\n", + "apps/web/package.json": { scripts: { test: "vitest" } }, + "apps/skip/package.json": { scripts: { test: "vitest" } }, + "libs/core/package.json": { scripts: { test: "jest" } }, + }); + assert.equal(r.suite, null); + assert.deepEqual(r.runners, [ + { cmd: "pnpm test", cwd: "apps/web", covers: ["apps/web/**"] }, + { cmd: "pnpm test", cwd: "libs/core", covers: ["libs/core/**"] }, + ], "a ! line excludes its folders"); + assert.deepEqual(r.coverage, [{ runner: "jest", cmd: "npx jest --coverage --coverageReporters=lcov", cwd: "libs/core", reads: "lcov" }]); + const obj = discover({ "package.json": { workspaces: { packages: ["pkgs/*"] } }, "pkgs/x/package.json": { scripts: { test: "t" } } }); + assert.deepEqual(obj.runners, [{ cmd: "npm test", cwd: "pkgs/x", covers: ["pkgs/x/**"] }]); +}); + +test("Go module with a nested second module", () => { + const r = discover({ "go.mod": "module example.com/a\n", "main.go": "package main\n", "tools/go.mod": "module example.com/tools\n" }); + assert.deepEqual(r.runners, [ + { cmd: "go test ./...", cwd: ".", covers: ["**"] }, + { cmd: "go test ./...", cwd: "tools", covers: ["tools/**"] }, + ]); + assert.deepEqual(r.coverage[0], { runner: "go", cmd: "go test -coverprofile= ./... && go tool cover -func=", cwd: ".", reads: "go-func" }); + assert.equal(r.builtin, null); +}); + +test("Cargo workspace: one runner at the root, members run from it", () => { + const r = discover({ + "Cargo.toml": '[workspace]\nmembers = ["crates/a"]\n', + "crates/a/Cargo.toml": '[package]\nname = "a"\n', + "crates/a/src/lib.rs": "", + }); + assert.deepEqual(r.runners, [{ cmd: "cargo test", cwd: ".", covers: ["**"] }]); +}); + +test("pytest project and plain Python project", () => { + const py = discover({ "pyproject.toml": "[tool.pytest.ini_options]\naddopts = \"-q\"\n", "pkg/a.py": "" }); + assert.deepEqual(py.runners, [{ cmd: "python -m pytest", cwd: ".", covers: ["**"] }]); + assert.deepEqual(py.coverage, [], "no pytest-cov, no coverage"); + const plain = discover({ "setup.cfg": "[metadata]\nname = x\n", "x/a.py": "" }); + assert.deepEqual(plain.runners, [{ cmd: "python -m unittest", cwd: ".", covers: ["**"] }]); + const req = discover({ "tox.ini": "[tox]\n", "requirements-dev.txt": "pytest==8\n" }); + assert.equal(req.runners[0].cmd, "python -m pytest"); +}); + +test(".NET project", () => { + const r = discover({ "App.sln": "", "src/App/App.csproj": "", "tests/App.Tests/App.Tests.csproj": "" }); + assert.deepEqual(r.runners, [{ cmd: "dotnet test", cwd: ".", covers: ["**"] }]); + assert.deepEqual(r.coverage, []); +}); + +test("Makefile-only project: make test is the suite and the root runner", () => { + const r = discover({ Makefile: "build:\n\tcc a.c\n\ntest: build\n\t./run-tests\n" }); + assert.deepEqual(r.suite, { cmd: "make test", why: "Makefile test target" }); + assert.deepEqual(r.runners, [{ cmd: "make test", cwd: ".", covers: ["**"] }]); + assert.equal(discover({ Makefile: "build:\n\tcc a.c\n" }).suite, null); +}); + +test("coverage is listed only with its add-on present", () => { + const vitest = (extra) => discover({ "package.json": { scripts: { test: "vitest" }, devDependencies: { vitest: "1", ...extra } } }); + assert.deepEqual(vitest({ "@vitest/coverage-v8": "1" }).coverage, [{ runner: "vitest", cmd: "npx vitest run --coverage --coverage.reporter=lcov", cwd: ".", reads: "lcov" }]); + assert.deepEqual(vitest({}).coverage, []); + + const pytest = (deps) => discover({ "pyproject.toml": `[project]\ndependencies = [${deps}]\n` }); + assert.deepEqual(pytest('"pytest", "pytest-cov"').coverage, [{ runner: "pytest", cmd: "python -m pytest --cov --cov-report=lcov:", cwd: ".", reads: "lcov" }]); + assert.deepEqual(pytest('"pytest"').coverage, []); + const nested = discover({ "requirements-dev.txt": "pytest-cov\n", "svc/setup.cfg": "[tool:pytest]\n" }); + assert.deepEqual(nested.coverage, [{ runner: "pytest", cmd: "python -m pytest --cov --cov-report=lcov:", cwd: "svc", reads: "lcov" }], "pytest-cov listed in a folder above the runner"); + + const dotnet = (ref) => discover({ "App.Tests/App.Tests.csproj": `${ref}` }); + assert.deepEqual(dotnet('').coverage, [{ runner: "dotnet", cmd: 'dotnet test --collect:"XPlat Code Coverage"', cwd: "App.Tests", reads: "lcov" }]); + assert.deepEqual(dotnet("").coverage, []); +}); + +test("builtin: an empty Node project, a Python-only folder, and others in a mixed folder", () => { + assert.deepEqual(discover({ "package.json": { name: "x" } }).builtin, { language: "node", cmd: "node --test" }); + assert.deepEqual(discover({ "a.py": "", "lib/b.py": "" }).builtin, { language: "python", cmd: "python -m unittest" }); + const mixed = discover({ "package.json": { name: "x" }, "index.js": "", "a.py": "", "b.py": "", "c.py": "" }); + assert.deepEqual(mixed.builtin, { language: "python", cmd: "python -m unittest", others: [{ language: "node", cmd: "node --test" }] }); + assert.equal(discover({ "README.md": "hi" }).builtin, null); +}); + +test("skipped folders are never walked; bad input is a usage error", () => { + const r = discover({ "node_modules/x/go.mod": "module x\n", "vendor/y/Cargo.toml": "[package]\n", "a.txt": "" }); + assert.deepEqual(r.runners, []); + const dir = tmp("cmds-bad"); + assert.equal(runScript("plan2code-init", "test-commands.mjs", ["list"], { cwd: dir }).code, 2); + assert.equal(runScript("plan2code-init", "test-commands.mjs", ["discover", "--root", "nope"], { cwd: dir }).code, 3); +}); diff --git a/scripts/test-skill-test-runs.mjs b/scripts/test-skill-test-runs.mjs index 7bd17eb..beb7666 100644 --- a/scripts/test-skill-test-runs.mjs +++ b/scripts/test-skill-test-runs.mjs @@ -123,9 +123,19 @@ test("test-runs.md says to ask when a check or its tests are unclear", () => { test("Init and Init-update keep a test map beside the single-test command", () => { const init = src("plan2code-init.md"); - assert.match(init, /run single test \(and beside it a short \*\*Test map\*\*: `area -> tests or command`/); - assert.match(init, /which checks are quick/); + assert.match(init, /run single test \(and beside it a note of which checks are quick\)/); + assert.ok(init.includes("## Test map"), "Init names the ## Test map section"); + assert.match(init, /test-commands\.mjs" discover/); + assert.match(init, /write it only on a yes with `test-map\.mjs write --rows `/); + assert.doesNotMatch(init, /area -> tests or command/, "the old line format is gone"); const upd = src("plan2code-init-update.md"); - assert.match(upd, /\*\*4\. Testing\*\* - .*the test map and quick checks/); - assert.match(upd, /\| Testing \| Commands\? Test map and quick checks stale\?/); + assert.match(upd, /\*\*4\. Testing\*\* - .*the `## Test map` table and quick checks/); + assert.match(upd, /\| Testing \| Commands\? Test map `Paths` still match real files and `Run` commands still exist \(`test-map\.mjs read` problems, `test-commands\.mjs discover`\)\?/); + assert.match(upd, /written only on a yes through `test-map\.mjs write`/); +}); + +test("test-runs.md reads the ## Test map table and treats Shared rows as shared ground", () => { + const rule = src("shared-references", "test-runs.md"); + assert.ok(rule.includes("`## Test map`"), "names the ## Test map table"); + assert.ok(rule.includes("`Shared: yes`"), "a Shared: yes row is shared ground"); }); diff --git a/src/plan2code-init-update.md b/src/plan2code-init-update.md index c360e63..a1566ad 100644 --- a/src/plan2code-init-update.md +++ b/src/plan2code-init-update.md @@ -24,6 +24,7 @@ The FIRST thing you do, before anything else in this file — before the Awarene - `node "/agents-md.mjs" filename "
"`: a new section file's name, link line and header. - `node "/agent-files.mjs" detect` / `apply ...`: Step 7. - `node "/commit-msg.mjs" --subject ""`: the Session End commit. +- `node "/test-map.mjs" read` / `write --rows ` and `node "/test-commands.mjs" discover`: the `## Test map` (Testing, Step 4); the rows file goes in the session folder or the OS temp folder, never the project. --- @@ -138,7 +139,7 @@ Present the user with update options: > - **1. Commands** - Build, test, run, lint, how to run and view the app, or other CLI commands > - **2. Architecture** - How components interact, data flow, key patterns > - **3. Gotchas/Pitfalls** - Traps to avoid, non-obvious behaviors -> - **4. Testing** - Test patterns, how to run specific tests, the test map and quick checks, fixtures +> - **4. Testing** - Test patterns, how to run specific tests, the `## Test map` table and quick checks, fixtures > - **5. Environment/Config** - Setup quirks, env variables, configuration > - **6. General Rules** - Coding conventions, style rules, project-specific practices > - **7. Failure Log** - Record a mistake or correction in the Failure log @@ -160,7 +161,7 @@ Present the user with update options: | Commands | Purpose? Flags? Prerequisites? How to run and view the app? | | Architecture | Components? Interactions? Pattern? | | Gotchas | What was unexpected? Workaround? | -| Testing | Commands? Test map and quick checks stale? Fixtures? Mocking? | +| Testing | Commands? Test map `Paths` still match real files and `Run` commands still exist (`test-map.mjs read` problems, `test-commands.mjs discover`)? Quick checks stale? Fixtures? Mocking? No map but tests exist: offer to create one, written only on a yes through `test-map.mjs write`. | | Environment | Local/CI/deploy? Env vars/files? | | Rules | Project-wide or specific? Why? | | Failure Log | What went wrong? Correct behavior (imperative, one line)? Merge into an entry on the same topic if one exists; over 17 entries, propose what to prune (rule 7). | diff --git a/src/plan2code-init.md b/src/plan2code-init.md index 3d3df31..d0c18b9 100644 --- a/src/plan2code-init.md +++ b/src/plan2code-init.md @@ -26,7 +26,7 @@ The FIRST thing you do, before anything else in this file — before exploring t ## Content -1. **Commands**: Build, lint, test, run single test (and beside it a short **Test map**: `area -> tests or command` lines saying which tests cover which areas, plus a note of which checks are quick; omit it when the project has no tests), other common development tasks, and how to start the app and where to view it (URL or command), when it has one +1. **Commands**: Build, lint, test, run single test (and beside it a note of which checks are quick), other common development tasks, and how to start the app and where to view it (URL or command), when it has one. Then the **`## Test map`** section: run `node "/test-commands.mjs" discover` and `node "/test-map.mjs" read`. When test commands are found and no map exists, propose a `## Test map` table (`| Area | Paths | Run | Shared |`, one row per area with its own run command, `Shared: yes` for helper, fixture and config areas) for the person to approve, and write it only on a yes with `test-map.mjs write --rows ` (the rows file in the session folder or the OS temp folder, never the project). In a progressive `AGENTS.md`, the heading holds one link to `.agents-docs/AGENTS-test-map.md` and the table goes there. Omit the map when the project has no tests. 2. **Architecture**: High-level "big picture" structure requiring multi-file context to understand 3. **How to Use This File**: A short paragraph explaining that sections below contain brief summaries and agents should follow the markdown links to `.agents-docs/` for full details — only read what's relevant to the current task. 4. **Failure Log**: running list of mistakes and/or corrections that went wrong via the AI Agent at least once. diff --git a/src/shared-references/test-runs.md b/src/shared-references/test-runs.md index f04cfc6..c6ee83a 100644 --- a/src/shared-references/test-runs.md +++ b/src/shared-references/test-runs.md @@ -11,8 +11,8 @@ In this order: -1. `AGENTS.md`'s test map and its "run single test" command. -2. Otherwise match by name, folder and imports of the changed files. +1. `AGENTS.md`'s `## Test map` table (`Area`, `Paths`, `Run`, `Shared`; it may sit behind one link to `.agents-docs/AGENTS-test-map.md`), read with `test-map.mjs read` when the skill ships it, plus the "run single test" command. +2. Otherwise match by name, folder and imports of the changed files; `test-match.mjs` does this when the skill ships it. A runner's own related-tests flag may be used when `AGENTS.md` records it. It is never required. @@ -22,7 +22,7 @@ Lint, a type check, a size check: run them at phase end only when the project or ## Shared ground -A test helper or fixture, build or test config, a widely imported module, a shared type or schema. Touching any of these is the moment to offer a wider run: say what was touched and why it reaches wide, and let the person choose. +A test helper or fixture, build or test config, a widely imported module, a shared type or schema. A test map row marked `Shared: yes` is shared ground by definition. Touching any of these is the moment to offer a wider run: say what was touched and why it reaches wide, and let the person choose. ## Ask when unsure diff --git a/src/skill-scripts/run-check.mjs b/src/skill-scripts/run-check.mjs new file mode 100644 index 0000000..f106c12 --- /dev/null +++ b/src/skill-scripts/run-check.mjs @@ -0,0 +1,149 @@ +#!/usr/bin/env node +// run-check.mjs: run one check command with a timeout and classify the result. +// +// Agent shells differ in their timeouts, none kill a Windows process tree, and +// telling "the command is not there" from "the command failed" needs the exit +// code and the spawn error. So one check runs here: passed, failed, timeout or +// not-started, with the output's last lines. The full output goes only to +// --log; nothing else is written. + +import fs from "node:fs"; +import path from "node:path"; +import { spawn, execFileSync } from "node:child_process"; +import { fileURLToPath } from "node:url"; +import { EXIT, fail, out, parseArgs, run, isDir } from "./common.mjs"; + +const USAGE = ` +run-check.mjs: run one command and classify its outcome. JSON on stdout. + + node run-check.mjs --cmd [--cwd ] [--timeout ] [--log ] + Runs the command through the shell in --cwd (relative to the current + folder; default "."), stops it and every process it started after + --timeout seconds (default 600, at most 86400), and prints + { ok, cmd, cwd, outcome, exit, signal, seconds, tail, log }. + outcome passed (exit 0) · failed (any other exit) · timeout · + not-started (with reason: spawn-error, command-not-found, + cwd-missing) + tail the output's last 60 lines, stdout and stderr interleaved, + each cut at 400 characters + log the file holding the full output, when --log was given + +Exit: 0 whenever an outcome was classified · 2 bad arguments. +`; + +// One day: a longer timer overflows Node's 32-bit limit and fires at once. +const MAX_TIMEOUT = 86400; +const TAIL_LINES = 60; +const LINE_CAP = 400; +const NOT_RECOGNIZED = /is not recognized as an internal or external command/; + +function killTree(child) { + if (process.platform === "win32") { + try { + execFileSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore", windowsHide: true }); + } catch {} + } else { + try { + process.kill(-child.pid, "SIGKILL"); + } catch {} + } +} + +function tailOf(text) { + const lines = text.split(/\r?\n/); + if (lines.length && lines[lines.length - 1] === "") lines.pop(); + return lines.slice(-TAIL_LINES).map((l) => (l.length > LINE_CAP ? l.slice(0, LINE_CAP) : l)); +} + +export function runCheck({ cmd, cwd, timeout, log }) { + const shownCwd = cwd || "."; + const abs = path.resolve(shownCwd); + const logFile = log ? path.resolve(log) : null; + const base = { ok: true, cmd, cwd: shownCwd }; + if (!isDir(abs)) { + return Promise.resolve({ ...base, outcome: "not-started", reason: "cwd-missing", exit: null, signal: null, seconds: 0, tail: [], log: null }); + } + return new Promise((resolve) => { + const started = Date.now(); + let output = ""; + let stderrHead = ""; + let spawnError = null; + let timedOut = false; + let done = false; + let logStream = null; + if (logFile) { + fs.mkdirSync(path.dirname(logFile), { recursive: true }); + logStream = fs.createWriteStream(logFile); + } + const child = spawn(cmd, { cwd: abs, shell: true, windowsHide: true, detached: process.platform !== "win32", stdio: ["ignore", "pipe", "pipe"] }); + const take = (chunk, isErr) => { + const s = chunk.toString("utf8"); + output += s; + // Keep only what the tail can still need, plus the start of stderr for + // the Windows "not recognized" check. + if (output.length > 1_000_000) output = output.slice(-TAIL_LINES * LINE_CAP * 4); + if (isErr && stderrHead.length < 65536) stderrHead += s; + if (logStream) logStream.write(chunk); + }; + child.stdout.on("data", (c) => take(c, false)); + child.stderr.on("data", (c) => take(c, true)); + + const finish = (code, signal) => { + if (done) return; + done = true; + clearTimeout(timer); + clearTimeout(grace); + // A process the kill missed must not keep this one alive through its pipes. + child.stdout.destroy(); + child.stderr.destroy(); + child.unref(); + const seconds = Math.round((Date.now() - started) / 100) / 10; + let outcome; + let reason; + if (spawnError) [outcome, reason] = ["not-started", "spawn-error"]; + else if (timedOut) outcome = "timeout"; + else if (code === 0) outcome = "passed"; + else if (code === 127 || code === 9009 || NOT_RECOGNIZED.test(stderrHead)) [outcome, reason] = ["not-started", "command-not-found"]; + else outcome = "failed"; + const result = { ...base, outcome, ...(reason ? { reason } : {}), exit: typeof code === "number" ? code : null, signal: signal || null, seconds, tail: tailOf(output), log: logFile }; + if (logStream) logStream.end(() => resolve(result)); + else resolve(result); + }; + + let grace = null; + const timer = setTimeout(() => { + timedOut = true; + killTree(child); + // Wait for close, but never more than 5 seconds after the kill. + grace = setTimeout(() => finish(null, "SIGKILL"), 5000); + }, timeout * 1000); + + child.on("error", (err) => { + spawnError = err; + // close may not follow a spawn error; give it a moment, then report. + setTimeout(() => finish(null, null), 200); + }); + child.on("close", (code, signal) => finish(code, signal)); + }); +} + +async function main(argv) { + const args = parseArgs(argv, { values: ["cmd", "cwd", "timeout", "log"] }); + if (args._.length) fail(EXIT.USAGE, "unexpected-argument", `Unexpected argument "${args._[0]}".`, "Pass the command with --cmd \"\"."); + if (!args.cmd || !args.cmd.trim()) fail(EXIT.USAGE, "missing-cmd", "run-check needs --cmd .", "Run with --help for usage."); + const timeout = args.timeout === undefined ? 600 : Number(args.timeout); + if (!Number.isFinite(timeout) || timeout <= 0 || timeout > MAX_TIMEOUT) { + fail(EXIT.USAGE, "bad-timeout", `--timeout must be a number of seconds above 0 and at most ${MAX_TIMEOUT}, not "${args.timeout}".`, "Pass --timeout ."); + } + out(await runCheck({ cmd: args.cmd, cwd: args.cwd, timeout, log: args.log })); +} + +// Run as a script; imported, do nothing. +const isMain = (() => { + try { + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) run(USAGE, main); diff --git a/src/skill-scripts/test-commands.mjs b/src/skill-scripts/test-commands.mjs new file mode 100644 index 0000000..7bd1021 --- /dev/null +++ b/src/skill-scripts/test-commands.mjs @@ -0,0 +1,267 @@ +#!/usr/bin/env node +// test-commands.mjs: find a project's test and check commands from its config. +// +// Reads package.json, workspace files and runner configs (go.mod, Cargo.toml, +// pyproject.toml, *.csproj, Makefile, ...) and reports the whole-suite +// command, one runner per folder, lint/type/build checks, the coverage +// commands the project can already run, and the built-in runner to offer when +// it has none. It only reads files: it never runs a command, installs anything +// or touches the network. Which of these to run stays the agent's call, and +// every command is shown to the person before it runs. + +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { EXIT, fail, out, parseArgs, run, readText, isDir } from "./common.mjs"; +import { matchGlob } from "./glob.mjs"; + +const USAGE = ` +test-commands.mjs: a project's test and check commands. JSON on stdout. + + node test-commands.mjs discover [--root ] + { ok, suite, runners, checks, coverage, builtin } + suite { cmd, why } the whole-suite command, or null + runners [{ cmd, cwd, covers[] }] one per folder with its own runner + (workspaces, go.mod, Cargo.toml, Python configs, .NET, Makefile); + the root folder's is cwd "." and covers "**" + checks [{ kind, cmd }] kind is lint, types or build + coverage [{ runner, cmd, cwd, reads }] only for tools already present; + in cmd is a placeholder for the output file; reads is + lcov or go-func + builtin { language, cmd, others? } the language's own runner, only when + there is no suite and no runner; otherwise null + +Only reads files. Exit: 0 ok · 2 bad arguments · 3 no such folder. +`; + +const SKIP = new Set(["node_modules", ".git", "vendor", "dist", "build", "target", ".venv"]); +const MAX_DEPTH = 4; +const PLACEHOLDER = /no test specified/; +const LANG_EXT = { + node: [".js", ".mjs", ".cjs", ".jsx", ".ts", ".mts", ".cts", ".tsx"], + python: [".py"], + go: [".go"], + rust: [".rs"], +}; +const BUILTIN = { node: "node --test", python: "python -m unittest", go: "go test ./...", rust: "cargo test" }; + +function readJson(file) { + const text = readText(file); + if (text === null) return null; + try { + return JSON.parse(text); + } catch { + return null; + } +} + +/** Every folder to depth 4 (forward-slash, "." for the root) with its file names, and source counts. */ +function walk(root) { + const dirs = new Map(); + const counts = { node: 0, python: 0, go: 0, rust: 0 }; + const extLang = new Map(Object.entries(LANG_EXT).flatMap(([lang, exts]) => exts.map((e) => [e, lang]))); + const visit = (abs, rel, depth) => { + let entries; + try { + entries = fs.readdirSync(abs, { withFileTypes: true }); + } catch { + return; + } + const files = []; + dirs.set(rel, files); + for (const e of entries) { + if (e.isDirectory()) { + if (depth < MAX_DEPTH && !SKIP.has(e.name)) visit(path.join(abs, e.name), rel === "." ? e.name : `${rel}/${e.name}`, depth + 1); + } else if (e.isFile()) { + files.push(e.name); + const lang = extLang.get(path.extname(e.name).toLowerCase()); + if (lang) counts[lang]++; + } + } + }; + visit(root, ".", 0); + return { dirs, counts }; +} + +/** The package manager the lockfile names. */ +function packageManager(files) { + if (files.includes("pnpm-lock.yaml")) return "pnpm"; + if (files.includes("yarn.lock")) return "yarn"; + if (files.includes("bun.lockb") || files.includes("bun.lock")) return "bun"; + return "npm"; +} + +const realTest = (pkg) => { + const t = pkg && pkg.scripts && pkg.scripts.test; + return typeof t === "string" && t.trim() && !PLACEHOLDER.test(t) ? t : null; +}; +const deps = (pkg) => ({ ...(pkg && pkg.dependencies), ...(pkg && pkg.devDependencies), ...(pkg && pkg.peerDependencies), ...(pkg && pkg.optionalDependencies) }); +const hasMakeTest = (text) => text !== null && /^test\s*:/m.test(text); +const covers = (cwd) => (cwd === "." ? ["**"] : [`${cwd}/**`]); +const ancestors = (rel) => { + const list = rel === "." ? [] : ["."]; + const parts = rel === "." ? [] : rel.split("/"); + for (let i = 1; i < parts.length; i++) list.push(parts.slice(0, i).join("/")); + return list; +}; + +/** Workspace folders from package.json workspaces and pnpm-workspace.yaml, matched against the folders on disk. */ +function workspaceFolders(root, pkg, dirs) { + let globs = []; + const ws = pkg && pkg.workspaces; + if (Array.isArray(ws)) globs = ws; + else if (ws && Array.isArray(ws.packages)) globs = ws.packages; + const pnpm = readText(path.join(root, "pnpm-workspace.yaml")); + if (pnpm !== null) { + let inPackages = false; + for (const line of pnpm.split(/\r?\n/)) { + if (/^packages\s*:/.test(line)) inPackages = true; + else if (/^\S/.test(line)) inPackages = false; + else if (inPackages) { + const m = line.match(/^\s*-\s*['"]?([^'"#]+?)['"]?\s*(?:#.*)?$/); + if (m) globs.push(m[1]); + } + } + } + globs = globs.filter((g) => typeof g === "string" && g.trim()).map((g) => g.trim().replace(/\/+$/, "")); + const include = globs.filter((g) => !g.startsWith("!")); + const exclude = globs.filter((g) => g.startsWith("!")).map((g) => g.slice(1)); + return [...dirs.keys()].filter((d) => d !== "." && include.some((g) => matchGlob(g, d)) && !exclude.some((g) => matchGlob(g, d))).sort(); +} + +/** pytest is configured or listed in this folder's Python files. */ +function usesPytest(abs, files) { + if (files.includes("pytest.ini")) return true; + const pyproject = readText(path.join(abs, "pyproject.toml")); + if (pyproject !== null && (/\[tool\.pytest/.test(pyproject) || /\bpytest\b/i.test(pyproject))) return true; + const setup = readText(path.join(abs, "setup.cfg")); + if (setup !== null && /\bpytest\b/i.test(setup)) return true; + const tox = readText(path.join(abs, "tox.ini")); + if (tox !== null && /\bpytest\b/i.test(tox)) return true; + return files.some((f) => /^requirements.*\.txt$/i.test(f) && /^\s*pytest\b/im.test(readText(path.join(abs, f)) || "")); +} + +/** The Python files of this folder or a folder above it mention pytest-cov. */ +function usesPytestCov(root, cwd, dirs) { + return [cwd, ...ancestors(cwd)].some((d) => { + const files = dirs.get(d) || []; + return ["pyproject.toml", "setup.cfg", "tox.ini", ...files.filter((f) => /^requirements.*\.txt$/i.test(f))] + .some((f) => files.includes(f) && /pytest-cov/i.test(readText(path.join(root, d, f)) || "")); + }); +} + +export function discover(root) { + const { dirs, counts } = walk(root); + const rootFiles = dirs.get(".") || []; + const pkg = readJson(path.join(root, "package.json")); + const pm = packageManager(rootFiles); + const runners = []; + const coverage = []; + const checks = []; + const add = (list, item) => { + if (!list.some((x) => x.cmd === item.cmd && x.cwd === item.cwd)) list.push(item); + }; + + let suite = null; + if (realTest(pkg)) suite = { cmd: `${pm} test`, why: "package.json scripts.test" }; + else if (hasMakeTest(readText(path.join(root, "Makefile")))) suite = { cmd: "make test", why: "Makefile test target" }; + + if (pkg && pkg.scripts) { + const s = pkg.scripts; + if (s.lint) checks.push({ kind: "lint", cmd: `${pm} run lint` }); + const types = ["typecheck", "type-check", "types", "tsc"].find((n) => s[n]); + if (types) checks.push({ kind: "types", cmd: `${pm} run ${types}` }); + else if (rootFiles.includes("tsconfig.json")) checks.push({ kind: "types", cmd: "npx tsc --noEmit" }); + if (s.build) checks.push({ kind: "build", cmd: `${pm} run build` }); + } else if (rootFiles.includes("tsconfig.json")) checks.push({ kind: "types", cmd: "npx tsc --noEmit" }); + + // Node: the root package and each workspace with a test script. + const nodeFolders = [{ cwd: ".", pkg }, ...workspaceFolders(root, pkg, dirs).map((cwd) => ({ cwd, pkg: readJson(path.join(root, cwd, "package.json")) }))]; + for (const { cwd, pkg: p } of nodeFolders) { + const test = realTest(p); + if (!test) continue; + add(runners, { cmd: `${pm} test`, cwd, covers: covers(cwd) }); + const d = deps(p); + if (/\bnode\s+--test\b/.test(test)) { + add(coverage, { runner: "node", cmd: "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=", cwd, reads: "lcov" }); + } + if (d.jest || /\bjest\b/.test(test)) add(coverage, { runner: "jest", cmd: "npx jest --coverage --coverageReporters=lcov", cwd, reads: "lcov" }); + if (Object.keys(d).some((k) => k.startsWith("@vitest/coverage-"))) add(coverage, { runner: "vitest", cmd: "npx vitest run --coverage --coverage.reporter=lcov", cwd, reads: "lcov" }); + } + + // Nested runner configs, walked in folder order. + const cargoWorkspaces = new Set(); + const solutions = new Set(); + for (const [cwd, files] of [...dirs].sort(([a], [b]) => (a === "." ? -1 : b === "." ? 1 : a.localeCompare(b)))) { + const abs = path.join(root, cwd); + const before = runners.length; + if (files.includes("go.mod")) { + add(runners, { cmd: "go test ./...", cwd, covers: covers(cwd) }); + add(coverage, { runner: "go", cmd: "go test -coverprofile= ./... && go tool cover -func=", cwd, reads: "go-func" }); + } + if (files.includes("Cargo.toml")) { + const cargo = readText(path.join(abs, "Cargo.toml")) || ""; + const isWorkspace = /^\[workspace\]/m.test(cargo); + // A member of a Cargo workspace already runs from the workspace root. + const inWorkspace = ancestors(cwd).some((a) => cargoWorkspaces.has(a)); + if (isWorkspace) cargoWorkspaces.add(cwd); + if (!inWorkspace && (isWorkspace || /^\[package\]/m.test(cargo))) add(runners, { cmd: "cargo test", cwd, covers: covers(cwd) }); + } + if (["pyproject.toml", "setup.cfg", "pytest.ini", "tox.ini"].some((f) => files.includes(f))) { + if (usesPytest(abs, files)) { + add(runners, { cmd: "python -m pytest", cwd, covers: covers(cwd) }); + if (usesPytestCov(root, cwd, dirs)) add(coverage, { runner: "pytest", cmd: "python -m pytest --cov --cov-report=lcov:", cwd, reads: "lcov" }); + } else add(runners, { cmd: "python -m unittest", cwd, covers: covers(cwd) }); + } + const dotnet = files.some((f) => /\.(csproj|sln)$/i.test(f)); + // Projects under a solution already run from the solution's folder. + if (dotnet && !ancestors(cwd).some((a) => solutions.has(a))) { + if (files.some((f) => /\.sln$/i.test(f))) solutions.add(cwd); + add(runners, { cmd: "dotnet test", cwd, covers: covers(cwd) }); + const prefix = cwd === "." ? "" : `${cwd}/`; + const coverlet = [...dirs].some(([d, fs2]) => (cwd === "." || d === cwd || d.startsWith(prefix)) && fs2.some((f) => /\.csproj$/i.test(f) && /coverlet\.collector/i.test(readText(path.join(root, d, f)) || ""))); + if (coverlet) add(coverage, { runner: "dotnet", cmd: 'dotnet test --collect:"XPlat Code Coverage"', cwd, reads: "lcov" }); + } + // make test is the folder's runner only when nothing else runs there. + const hasNode = runners.slice(0, before).some((r) => r.cwd === cwd); + if (runners.length === before && !hasNode && files.includes("Makefile") && hasMakeTest(readText(path.join(abs, "Makefile")))) { + add(runners, { cmd: "make test", cwd, covers: covers(cwd) }); + } + } + + let builtin = null; + if (!suite && !runners.length) { + const present = []; + if (pkg || rootFiles.includes("package.json")) present.push("node"); + if (counts.python) present.push("python"); + if ([...dirs.values()].some((f) => f.includes("go.mod"))) present.push("go"); + if ([...dirs.values()].some((f) => f.includes("Cargo.toml"))) present.push("rust"); + if (present.length) { + present.sort((a, b) => counts[b] - counts[a]); + const [first, ...rest] = present; + builtin = { language: first, cmd: BUILTIN[first] }; + if (rest.length) builtin.others = rest.map((language) => ({ language, cmd: BUILTIN[language] })); + } + } + + return { ok: true, suite, runners, checks, coverage, builtin }; +} + +async function main(argv) { + const args = parseArgs(argv, { values: ["root"] }); + const cmd = args._[0]; + if (cmd !== "discover") fail(EXIT.USAGE, "unknown-command", cmd ? `Unknown command "${cmd}".` : "No command given.", "Commands: discover. Run with --help."); + const root = path.resolve(args.root || "."); + if (!isDir(root)) fail(EXIT.NOT_FOUND, "no-root", `No folder at ${root}.`, "Pass the project folder with --root, or run from the project root."); + out(discover(root)); +} + +// Run as a script; imported, do nothing. +const isMain = (() => { + try { + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) run(USAGE, main);