jev-test-filter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Every test in the repository, whatever language it is written in.
3
+ *
4
+ * ECMAScript and Go come out of the source and cost a parse. Rust has to be
5
+ * asked for: its names come from `cargo test -- --list`, which builds the test
6
+ * targets, and a tool that triggers a compile nobody asked for is a tool that
7
+ * gets removed from the workflow. `--format rust` is that asking.
8
+ */
9
+ import { readFile } from "node:fs/promises";
10
+ import { join } from "node:path";
11
+ import { detectFramework, findTestFiles, isGoTestFile } from "./framework.js";
12
+ import { extractTests } from "./extract.js";
13
+ import { extractGoTests } from "./extract-go.js";
14
+ import { listRustTests } from "./cargo.js";
15
+ export async function discoverTests(cwd, paths, format) {
16
+ const out = [];
17
+ for (const file of await findTestFiles(cwd, paths)) {
18
+ const source = await readFile(join(cwd, file), "utf8");
19
+ if (isGoTestFile(file)) {
20
+ if (format === null || format === "go")
21
+ out.push(...extractGoTests(source, file));
22
+ continue;
23
+ }
24
+ const framework = detectFramework(source, file);
25
+ if (format !== null && framework !== format)
26
+ continue;
27
+ out.push(...extractTests(source, file, framework));
28
+ }
29
+ if (format === "rust")
30
+ out.push(...(await listRustTests(cwd)));
31
+ return out;
32
+ }
@@ -0,0 +1,4 @@
1
+ import type { TestCase } from "./types.ts";
2
+ /** A Go string literal's text, or null when the argument is not one. */
3
+ export declare function goLiteral(raw: string): string | null;
4
+ export declare function extractGoTests(source: string, file: string): TestCase[];
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Go's tests, read out of the source.
3
+ *
4
+ * `go test -list` prints only top-level functions, so it cannot supply what a
5
+ * per-subtest judgment needs. The source can: `func TestXxx` is a leaf when it
6
+ * holds no `t.Run`, and a suite when it does.
7
+ *
8
+ * A `t.Run` whose name is not a literal is the table-driven idiom, and it is
9
+ * `dynamic` for exactly the reason an interpolated Vitest title is -- there is
10
+ * no name to put in a pattern. Go compounds it: a subtest's spaces become
11
+ * underscores, and two subtests that collide after that rewrite get `#01`
12
+ * appended and cannot be told apart by name at all. The filter layer answers
13
+ * both by selecting whole top-level functions.
14
+ */
15
+ import { parse } from "@ast-grep/napi";
16
+ import { registerLanguages } from "./languages.js";
17
+ /**
18
+ * `func TestXxx(...)`. Go's own rule is that what follows `Test` must not
19
+ * start with a lowercase letter, which is what separates `TestFoo` from
20
+ * `Testing`. `Benchmark`, `Fuzz` and `Example` are not what `-run` selects.
21
+ */
22
+ const TEST_FUNC = { kind: "function_declaration", has: { field: "name", regex: "^Test($|[^a-z])" } };
23
+ /** `t.Run(name, fn)`, by the method's name rather than the receiver's. */
24
+ const SUBTEST = {
25
+ kind: "call_expression",
26
+ has: { field: "function", kind: "selector_expression", has: { field: "field", regex: "^Run$" } },
27
+ };
28
+ /** A Go string literal's text, or null when the argument is not one. */
29
+ export function goLiteral(raw) {
30
+ if (raw.startsWith("`") && raw.endsWith("`"))
31
+ return raw.slice(1, -1);
32
+ if (raw.startsWith('"') && raw.endsWith('"'))
33
+ return raw.slice(1, -1).replace(/\\(.)/g, "$1");
34
+ return null;
35
+ }
36
+ export function extractGoTests(source, file) {
37
+ registerLanguages();
38
+ const root = parse("go", source).root();
39
+ const out = [];
40
+ for (const fn of root.findAll({ rule: TEST_FUNC })) {
41
+ const name = fn.field("name")?.text() ?? "";
42
+ const range = fn.range();
43
+ const subs = fn.findAll({ rule: SUBTEST });
44
+ if (subs.length === 0) {
45
+ out.push({
46
+ file,
47
+ titlePath: [name],
48
+ line: range.start.line + 1,
49
+ endLine: range.end.line + 1,
50
+ framework: "go",
51
+ dynamic: false,
52
+ });
53
+ continue;
54
+ }
55
+ for (const sub of subs) {
56
+ // child(0) is the opening parenthesis; the name is the first argument.
57
+ const raw = sub.field("arguments")?.child(1)?.text() ?? "";
58
+ const title = goLiteral(raw);
59
+ const at = sub.range();
60
+ out.push({
61
+ file,
62
+ titlePath: [name, title ?? ""],
63
+ line: at.start.line + 1,
64
+ endLine: at.end.line + 1,
65
+ framework: "go",
66
+ dynamic: title === null,
67
+ });
68
+ }
69
+ }
70
+ return out;
71
+ }
@@ -0,0 +1,15 @@
1
+ import type { Framework, TestCase } from "./types.ts";
2
+ /**
3
+ * The text of a string literal, or null when the title is not statically
4
+ * knowable -- a template with an interpolation. A caller that gets null must
5
+ * treat the test as dynamic rather than drop it.
6
+ */
7
+ export declare function literalTitle(raw: string): string | null;
8
+ /**
9
+ * Every test in one file.
10
+ *
11
+ * The suite chain is taken by range containment rather than by walking
12
+ * ancestors, so a suite shape the ancestor walk would not recognise still
13
+ * contributes its title as long as the suite matcher found it.
14
+ */
15
+ export declare function extractTests(source: string, file: string, framework: Framework): TestCase[];
@@ -0,0 +1,146 @@
1
+ /**
2
+ * The test inventory, read out of the source.
3
+ *
4
+ * The matchers are jev-lint's, which were written once for every ECMAScript
5
+ * test framework rather than per framework: `test("x", { timeout }, fn)` from
6
+ * node:test, `test.describe` from Playwright, `Deno.test({ name, fn })` and
7
+ * `test.if(cond)("x", fn)` from bun are all shapes a jest-shaped matcher
8
+ * misses, and a matcher that misses produces no error -- only a test that
9
+ * quietly never runs.
10
+ */
11
+ import { parse } from "@ast-grep/napi";
12
+ import { langFor } from "./framework.js";
13
+ /** `describe(...)` and its spellings; Playwright's `test.describe` with its own modifiers. */
14
+ const SUITE_CALLEE = "^(x|f)?(describe|suite|context)(\\.(only|skip|todo|concurrent|sequential|shuffle|skipIf|runIf|if|each|for)(\\([^)]*\\))?)*$" +
15
+ "|^test\\.describe(\\.(serial|parallel|only|skip|fixme|configure)(\\([^)]*\\))?)*$";
16
+ /** The names node:test's context goes by, for `t.test("subtest", fn)`. */
17
+ const SUBTEST_CONTEXT = "(t|ctx|context)";
18
+ /** A subtest call, wherever it sits inside the parent. */
19
+ const SUBTEST_INSIDE = {
20
+ stopBy: "end",
21
+ kind: "call_expression",
22
+ has: { field: "function", regex: `^${SUBTEST_CONTEXT}\\.test$` },
23
+ };
24
+ /** `it`/`test` with modifiers; `Deno.test`; node:test's subtest on the context. */
25
+ const TEST_CALLEE = "^(x|f)?(it|test)(\\.(only|skip|todo|concurrent|sequential|fails|fixme|slow|skipIf|runIf|if|todoIf|failsIf|each|for)(\\([^)]*\\))?)*$" +
26
+ "|^Deno\\.test(\\.(only|ignore))?$" +
27
+ `|^${SUBTEST_CONTEXT}\\.test$`;
28
+ const FUNCTION_KINDS = [{ kind: "arrow_function" }, { kind: "function_expression" }, { kind: "generator_function" }];
29
+ /** The title, by whichever of the three shapes carries it. */
30
+ const TITLE_ARG = {
31
+ field: "arguments",
32
+ any: [
33
+ { has: { nthChild: 1, any: [{ kind: "string" }, { kind: "template_string" }], pattern: "$TITLE" } },
34
+ {
35
+ has: {
36
+ nthChild: 1,
37
+ kind: "object",
38
+ has: {
39
+ kind: "pair",
40
+ all: [{ has: { field: "key", regex: "^name$" } }, { has: { field: "value", pattern: "$TITLE" } }],
41
+ },
42
+ },
43
+ },
44
+ { has: { nthChild: 1, kind: "function_expression", has: { field: "name", pattern: "$TITLE" } } },
45
+ ],
46
+ };
47
+ /**
48
+ * A test call: a test callee, a title, a body to run, and -- for node:test --
49
+ * no subtests inside it, because a parent that only opens subtests is their
50
+ * suite and is matched as one below.
51
+ */
52
+ const TEST_RULE = {
53
+ kind: "call_expression",
54
+ all: [
55
+ { has: { field: "function", regex: TEST_CALLEE } },
56
+ { has: TITLE_ARG },
57
+ { not: { has: SUBTEST_INSIDE } },
58
+ {
59
+ has: {
60
+ field: "arguments",
61
+ any: [
62
+ { has: { any: FUNCTION_KINDS, pattern: "$BODY" } },
63
+ {
64
+ has: {
65
+ kind: "object",
66
+ has: { any: [{ kind: "method_definition" }, { kind: "pair", has: { any: FUNCTION_KINDS } }], pattern: "$BODY" },
67
+ },
68
+ },
69
+ ],
70
+ },
71
+ },
72
+ ],
73
+ };
74
+ /** A suite call, or a node:test parent that holds subtests. */
75
+ const SUITE_RULE = {
76
+ kind: "call_expression",
77
+ all: [
78
+ {
79
+ any: [
80
+ { has: { field: "function", regex: SUITE_CALLEE } },
81
+ { all: [{ has: { field: "function", regex: TEST_CALLEE } }, { has: SUBTEST_INSIDE }] },
82
+ ],
83
+ },
84
+ { has: { field: "arguments", has: { nthChild: 1, any: [{ kind: "string" }, { kind: "template_string" }], pattern: "$TITLE" } } },
85
+ { has: { field: "arguments", has: { any: FUNCTION_KINDS, pattern: "$BODY" } } },
86
+ ],
87
+ };
88
+ /**
89
+ * The text of a string literal, or null when the title is not statically
90
+ * knowable -- a template with an interpolation. A caller that gets null must
91
+ * treat the test as dynamic rather than drop it.
92
+ */
93
+ export function literalTitle(raw) {
94
+ const q = raw[0];
95
+ if (q !== "'" && q !== '"' && q !== "`")
96
+ return null;
97
+ const body = raw.slice(1, -1);
98
+ if (q === "`" && /\$\{/.test(body))
99
+ return null;
100
+ return body.replace(/\\(.)/g, "$1");
101
+ }
102
+ /** `.each(...)` / `.for(...)` generate one test per row, so no static title names them. */
103
+ const GENERATED = /\.(each|for)\b/;
104
+ function collect(root, rule) {
105
+ return root.findAll({ rule: rule }).map((node) => {
106
+ const range = node.range();
107
+ const raw = node.getMatch("TITLE")?.text() ?? null;
108
+ return {
109
+ start: range.start.index,
110
+ end: range.end.index,
111
+ node,
112
+ title: raw === null ? null : literalTitle(raw),
113
+ };
114
+ });
115
+ }
116
+ /**
117
+ * Every test in one file.
118
+ *
119
+ * The suite chain is taken by range containment rather than by walking
120
+ * ancestors, so a suite shape the ancestor walk would not recognise still
121
+ * contributes its title as long as the suite matcher found it.
122
+ */
123
+ export function extractTests(source, file, framework) {
124
+ const root = parse(langFor(file), source).root();
125
+ const tests = collect(root, TEST_RULE);
126
+ const suites = collect(root, SUITE_RULE);
127
+ return tests.map((t) => {
128
+ const chain = suites
129
+ .filter((s) => s.start <= t.start && s.end >= t.end && !(s.start === t.start && s.end === t.end))
130
+ .sort((a, b) => a.start - b.start || b.end - a.end);
131
+ const parts = [...chain, t];
132
+ const dynamic = parts.some((p) => p.title === null) || GENERATED.test(calleeText(t.node));
133
+ const range = t.node.range();
134
+ return {
135
+ file,
136
+ titlePath: parts.map((p) => p.title ?? ""),
137
+ line: range.start.line + 1,
138
+ endLine: range.end.line + 1,
139
+ framework,
140
+ dynamic,
141
+ };
142
+ });
143
+ }
144
+ function calleeText(node) {
145
+ return node.field("function")?.text() ?? "";
146
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * A selection, as the runner's own arguments.
3
+ *
4
+ * Three shapes, one per framework family, and the differences between them
5
+ * were measured rather than assumed:
6
+ *
7
+ * - Vitest joins a full name with `" > "`; node:test joins it with a single
8
+ * space. A pattern in the wrong spelling selects nothing and says nothing.
9
+ * - node:test ORs repeated `--test-name-pattern` flags, and a pattern that
10
+ * matches a SUITE name runs every test under it. One flag, one
11
+ * alternation of anchored full names, is the only safe shape.
12
+ * - Playwright's `--grep` matches `"<project> <file> <chain> <title>"`, so an
13
+ * anchored pattern would have to know the project name and would break
14
+ * when a project is added. `file:line` is exact and needs no escaping.
15
+ */
16
+ import type { Framework, Selection, TestCase } from "./types.ts";
17
+ export type FilterMode =
18
+ /** Everything was selected: pass no arguments and let the runner run. */
19
+ "all"
20
+ /** Nothing was selected: there is nothing to run. */
21
+ | "none"
22
+ /** Whole files, because a name pattern cannot express the selection. */
23
+ | "files"
24
+ /** Files plus an anchored alternation of full names. */
25
+ | "pattern"
26
+ /** `file:line` positionals. */
27
+ | "locations"
28
+ /** Exact names, for a runner that does not take a pattern. */
29
+ | "exact";
30
+ export interface FilterArgs {
31
+ mode: FilterMode;
32
+ /** Arguments to append to the runner command, in order. */
33
+ argv: string[];
34
+ }
35
+ export interface FilterOptions {
36
+ /**
37
+ * Above this fraction of the suite, the name pattern is dropped and whole
38
+ * files are selected instead. A run that keeps most of the suite gains
39
+ * nothing from an alternation of a thousand names.
40
+ */
41
+ fileThreshold?: number;
42
+ }
43
+ export declare function escapeRegExp(s: string): string;
44
+ /** The full name as this test's own runner spells it. */
45
+ export declare function fullName(t: TestCase): string;
46
+ export declare function buildFilter(sel: Selection, framework: Framework, { fileThreshold }?: FilterOptions): FilterArgs;
package/dist/filter.js ADDED
@@ -0,0 +1,81 @@
1
+ export function escapeRegExp(s) {
2
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3
+ }
4
+ /** The full name as this test's own runner spells it. */
5
+ export function fullName(t) {
6
+ if (t.framework === "rust")
7
+ return t.titlePath.join("::");
8
+ if (t.framework === "go")
9
+ return t.titlePath.join("/");
10
+ const sep = t.framework === "node" ? " " : " > ";
11
+ return t.titlePath.join(sep);
12
+ }
13
+ /** The flag that carries a name pattern, per framework. */
14
+ function patternFlag(framework) {
15
+ if (framework === "node")
16
+ return "--test-name-pattern";
17
+ if (framework === "vitest" || framework === "jest")
18
+ return "-t";
19
+ return null;
20
+ }
21
+ function uniqueFiles(tests) {
22
+ return [...new Set(tests.map((t) => t.file))];
23
+ }
24
+ /** `./pkg` for each directory a selected Go test lives in. */
25
+ function goPackages(tests) {
26
+ const dirs = tests.map((t) => {
27
+ const at = t.file.lastIndexOf("/");
28
+ return at === -1 ? "./" : `./${t.file.slice(0, at)}`;
29
+ });
30
+ return [...new Set(dirs)].sort();
31
+ }
32
+ export function buildFilter(sel, framework, { fileThreshold = 0.8 } = {}) {
33
+ const { selected, all } = sel;
34
+ // Both of these come before the per-framework dispatch on purpose. When
35
+ // every test is selected, no arguments is not merely shorter than naming
36
+ // them all -- it is the same run, and for Playwright it is the difference
37
+ // between one command and one argument per test. When none is selected
38
+ // there is nothing any framework could be asked to run.
39
+ if (selected.length === 0)
40
+ return { mode: "none", argv: [] };
41
+ if (selected.length === all.length)
42
+ return { mode: "all", argv: [] };
43
+ if (framework === "playwright") {
44
+ return { mode: "locations", argv: selected.map((t) => `${t.file}:${t.line}`) };
45
+ }
46
+ if (framework === "rust") {
47
+ // `--exact` takes several names in one invocation, and every name came
48
+ // from cargo's own listing rather than from a path this tool assembled --
49
+ // a module path one segment wrong selects nothing and says nothing.
50
+ // Targets are not narrowed: a name that exists in two of them runs in
51
+ // both, which costs time and cannot lose a test.
52
+ return { mode: "exact", argv: ["--", "--exact", ...selected.map(fullName)] };
53
+ }
54
+ if (framework === "go") {
55
+ // Top-level functions, never subtests. `-run` takes one hierarchical
56
+ // pattern and a second `-run` replaces the first, so "all of TestA, but
57
+ // only x and y of TestB" cannot be said; the shape that covers TestB
58
+ // would silently drop TestA's other subtests. Scoring stays per subtest,
59
+ // which is what `--json` reports.
60
+ const parents = [...new Set(selected.map((t) => t.titlePath[0] ?? ""))];
61
+ const pattern = `^(?:${parents.map(escapeRegExp).join("|")})$`;
62
+ return { mode: "pattern", argv: ["-run", pattern, ...goPackages(selected)] };
63
+ }
64
+ const files = uniqueFiles(selected);
65
+ const flag = patternFlag(framework);
66
+ // A dynamic test has no name a pattern can hold, and a pattern applies to
67
+ // the whole run rather than to one file -- so one of them costs the run its
68
+ // name-level filtering. Files are still a large saving.
69
+ const hasDynamic = selected.some((t) => t.dynamic);
70
+ const rate = selected.length / all.length;
71
+ if (!flag || hasDynamic || rate > fileThreshold) {
72
+ return { mode: "files", argv: files };
73
+ }
74
+ const names = [...new Set(selected.map(fullName))];
75
+ const pattern = `^(?:${names.map(escapeRegExp).join("|")})$`;
76
+ // The flag goes BEFORE the files, and that is not a style choice. Node's
77
+ // test runner silently ignores `--test-name-pattern` when it follows a
78
+ // positional: `node --test a.test.js --test-name-pattern X` runs the whole
79
+ // file and exits 0. Vitest accepts either order, so one order serves both.
80
+ return { mode: "pattern", argv: [flag, pattern, ...files] };
81
+ }
@@ -0,0 +1,21 @@
1
+ import { Lang } from "@ast-grep/napi";
2
+ import type { Framework } from "./types.ts";
3
+ /** The grammar to parse a file under, by extension. */
4
+ export declare function langFor(file: string): Lang;
5
+ export declare function isGoTestFile(file: string): boolean;
6
+ export declare function isTestFile(file: string): boolean;
7
+ /**
8
+ * Playwright wins a tie because a spec that imports `expect` from elsewhere
9
+ * is still a Playwright spec, and selecting a Playwright test by name is the
10
+ * one thing that does not work.
11
+ *
12
+ * `file` only picks the grammar; the default suits a bare source string.
13
+ */
14
+ export declare function detectFramework(source: string, file?: string): Framework;
15
+ /**
16
+ * The test files to consider.
17
+ *
18
+ * `git ls-files` rather than a directory walk: it honours `.gitignore` for
19
+ * free, and a file git does not track is not one a CI run would execute.
20
+ */
21
+ export declare function findTestFiles(cwd: string, paths?: string[]): Promise<string[]>;
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Which runner a file's tests belong to, and which files to look in.
3
+ *
4
+ * Detection is by import source rather than by configuration, because the
5
+ * configuration says what the project runs and the import says what this file
6
+ * is. A repository with Vitest unit tests and Playwright specs has both, and
7
+ * a per-run answer would be wrong for half of it.
8
+ */
9
+ import { execFile } from "node:child_process";
10
+ import { promisify } from "node:util";
11
+ import { parse, Lang } from "@ast-grep/napi";
12
+ const execFileAsync = promisify(execFile);
13
+ /** The grammar to parse a file under, by extension. */
14
+ export function langFor(file) {
15
+ if (/\.tsx$/.test(file))
16
+ return Lang.Tsx;
17
+ if (/\.jsx$/.test(file))
18
+ return Lang.Tsx;
19
+ if (/\.[cm]?ts$/.test(file))
20
+ return Lang.TypeScript;
21
+ return Lang.JavaScript;
22
+ }
23
+ /** `foo.test.ts`, `foo.spec.tsx`, `foo.test.mjs`, and the rest of the family. */
24
+ const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/;
25
+ /** Go's own convention, and the only one `go test` compiles into a test binary. */
26
+ const GO_TEST_FILE = /_test\.go$/;
27
+ export function isGoTestFile(file) {
28
+ return GO_TEST_FILE.test(file);
29
+ }
30
+ export function isTestFile(file) {
31
+ return TEST_FILE.test(file) || GO_TEST_FILE.test(file);
32
+ }
33
+ /** `require("x")`, with the specifier captured. */
34
+ const REQUIRE_RULE = {
35
+ kind: "call_expression",
36
+ all: [
37
+ { has: { field: "function", regex: "^require$" } },
38
+ { has: { field: "arguments", has: { nthChild: 1, kind: "string", pattern: "$SPEC" } } },
39
+ ],
40
+ };
41
+ /** A string literal's text, without its quotes. */
42
+ function unquote(raw) {
43
+ return raw.slice(1, -1);
44
+ }
45
+ /**
46
+ * Every module specifier the file imports or requires.
47
+ *
48
+ * A parse, not a regular expression. The first version of this matched
49
+ * specifiers in the text, which reads an import written inside a string
50
+ * literal as a real one -- and this tool's own test files carry fixture
51
+ * sources as template literals, so its own repository looked like it held
52
+ * Playwright specs it does not have. There is no way to tell code from a
53
+ * string without parsing, so it parses.
54
+ *
55
+ * The file is parsed once here and once more by the extractor. Tree-sitter is
56
+ * fast enough that the second parse does not show up next to reading the file
57
+ * off disk, and threading a parsed tree between the two would put the grammar
58
+ * choice in the caller.
59
+ */
60
+ function specifiers(source, file) {
61
+ const root = parse(langFor(file), source).root();
62
+ const out = [];
63
+ for (const kind of ["import_statement", "export_statement"]) {
64
+ for (const node of root.findAll({ rule: { kind } })) {
65
+ const src = node.field("source")?.text();
66
+ if (src)
67
+ out.push(unquote(src));
68
+ }
69
+ }
70
+ for (const node of root.findAll({ rule: REQUIRE_RULE })) {
71
+ const spec = node.getMatch("SPEC")?.text();
72
+ if (spec)
73
+ out.push(unquote(spec));
74
+ }
75
+ return out;
76
+ }
77
+ /**
78
+ * Playwright wins a tie because a spec that imports `expect` from elsewhere
79
+ * is still a Playwright spec, and selecting a Playwright test by name is the
80
+ * one thing that does not work.
81
+ *
82
+ * `file` only picks the grammar; the default suits a bare source string.
83
+ */
84
+ export function detectFramework(source, file = "a.ts") {
85
+ let vitest = false;
86
+ let node = false;
87
+ let jest = false;
88
+ for (const spec of specifiers(source, file)) {
89
+ if (spec === "@playwright/test" || spec.startsWith("@playwright/test/"))
90
+ return "playwright";
91
+ if (spec === "vitest" || spec.startsWith("vitest/"))
92
+ vitest = true;
93
+ else if (spec === "node:test")
94
+ node = true;
95
+ else if (spec === "@jest/globals")
96
+ jest = true;
97
+ }
98
+ if (vitest)
99
+ return "vitest";
100
+ if (node)
101
+ return "node";
102
+ if (jest)
103
+ return "jest";
104
+ return "unknown";
105
+ }
106
+ /**
107
+ * The test files to consider.
108
+ *
109
+ * `git ls-files` rather than a directory walk: it honours `.gitignore` for
110
+ * free, and a file git does not track is not one a CI run would execute.
111
+ */
112
+ export async function findTestFiles(cwd, paths = []) {
113
+ const args = ["ls-files", "-z", "--", ...(paths.length > 0 ? paths : ["."])];
114
+ const { stdout } = await execFileAsync("git", args, { cwd, maxBuffer: 64 * 1024 * 1024 });
115
+ return stdout.split("\0").filter((f) => f !== "" && isTestFile(f));
116
+ }
package/dist/gate.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Answer, Selection, TestCase, Verdict } from "./types.ts";
2
+ /**
3
+ * The boundary between "this change cannot alter the outcome" (level 1) and
4
+ * "it might" (level 2). It is a level boundary, not a tuned number, and it is
5
+ * the one default worth defending.
6
+ */
7
+ export declare const DEFAULT_CUTOFF = 2;
8
+ /** Under this, the model's own ranking is not worth acting on. */
9
+ export declare const DEFAULT_UNSURE_BELOW = 0.5;
10
+ /** How far under the cutoff an unsure answer is still rescued. */
11
+ export declare const DEFAULT_UNSURE_MARGIN = 1;
12
+ export interface GateOptions {
13
+ cutoff?: number;
14
+ unsureBelow?: number;
15
+ unsureMargin?: number;
16
+ }
17
+ /** Decide one test. Never returns null: every test gets a side and a reason. */
18
+ export declare function decide(id: string, t: TestCase, answer: Answer | null, touched: boolean, { cutoff, unsureBelow, unsureMargin }?: GateOptions): Verdict;
19
+ /**
20
+ * Decide every test.
21
+ *
22
+ * `answers` is keyed by question id, which is the test's index in `tests`;
23
+ * `touched` is keyed by `testId`, because it is computed from the diff before
24
+ * any question exists.
25
+ */
26
+ export declare function gate(tests: TestCase[], answers: Map<string, Answer | null>, touched: Set<string>, opts?: GateOptions): Selection;
package/dist/gate.js ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Answers in, a selection out.
3
+ *
4
+ * Everything here is pure and runs offline, and that separation is the point:
5
+ * verdicts are what cost money and thresholds are what you will change twenty
6
+ * times, so re-gating a recorded run has to be free. That is what `--replay`
7
+ * is.
8
+ *
9
+ * Two rules, each of them the same one under different names:
10
+ *
11
+ * 1. **Confidence routes, it does not gate.** A verdict under the cutoff that
12
+ * the model was unsure about is selected, because running a test that did
13
+ * not need to run costs seconds and skipping one that did costs a release.
14
+ * 2. **No answer is not a passing grade.** A missing or malformed answer
15
+ * selects the test, so a run whose requests failed cannot read as a small
16
+ * selection.
17
+ */
18
+ import { questionId } from "./questions.js";
19
+ import { testId } from "./types.js";
20
+ /**
21
+ * The boundary between "this change cannot alter the outcome" (level 1) and
22
+ * "it might" (level 2). It is a level boundary, not a tuned number, and it is
23
+ * the one default worth defending.
24
+ */
25
+ export const DEFAULT_CUTOFF = 2.0;
26
+ /** Under this, the model's own ranking is not worth acting on. */
27
+ export const DEFAULT_UNSURE_BELOW = 0.5;
28
+ /** How far under the cutoff an unsure answer is still rescued. */
29
+ export const DEFAULT_UNSURE_MARGIN = 1.0;
30
+ /** Decide one test. Never returns null: every test gets a side and a reason. */
31
+ export function decide(id, t, answer, touched, { cutoff = DEFAULT_CUTOFF, unsureBelow = DEFAULT_UNSURE_BELOW, unsureMargin = DEFAULT_UNSURE_MARGIN } = {}) {
32
+ if (touched)
33
+ return { id, test: t, answer, reason: "touched", selected: true };
34
+ if (t.dynamic)
35
+ return { id, test: t, answer, reason: "dynamic", selected: true };
36
+ if (!answer)
37
+ return { id, test: t, answer: null, reason: "missing", selected: true };
38
+ if (answer.value >= cutoff)
39
+ return { id, test: t, answer, reason: "scored", selected: true };
40
+ if (answer.confidence !== null && answer.confidence < unsureBelow && answer.value >= cutoff - unsureMargin) {
41
+ return { id, test: t, answer, reason: "unsure", selected: true };
42
+ }
43
+ return { id, test: t, answer, reason: "below", selected: false };
44
+ }
45
+ /**
46
+ * Decide every test.
47
+ *
48
+ * `answers` is keyed by question id, which is the test's index in `tests`;
49
+ * `touched` is keyed by `testId`, because it is computed from the diff before
50
+ * any question exists.
51
+ */
52
+ export function gate(tests, answers, touched, opts = {}) {
53
+ const verdicts = tests.map((t, i) => {
54
+ const id = questionId(i);
55
+ return decide(id, t, answers.get(id) ?? null, touched.has(testId(t)), opts);
56
+ });
57
+ return {
58
+ verdicts,
59
+ selected: verdicts.filter((v) => v.selected).map((v) => v.test),
60
+ all: tests,
61
+ fallback: null,
62
+ };
63
+ }