-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlint-transcript.test.mjs
More file actions
159 lines (159 loc) · 7.53 KB
/
lint-transcript.test.mjs
File metadata and controls
159 lines (159 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, it } from "node:test";
import { fileURLToPath } from "node:url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const LINTER = join(__dirname, "..", "bin", "lint-transcript.mjs");
function lintEvents(events, flags = []) {
const dir = join(tmpdir(), `ci-lint-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(dir, { recursive: true });
const file = join(dir, "transcript.jsonl");
writeFileSync(file, events.map((event) => JSON.stringify(event)).join("\n") + "\n");
try {
return execFileSync("node", [LINTER, file, ...flags], { encoding: "utf8" });
}
finally {
rmSync(dir, { recursive: true, force: true });
}
}
describe("lint-transcript.mjs", () => {
it("shows help with --help", () => {
const output = execFileSync("node", [LINTER, "--help"], { encoding: "utf8" });
assert.match(output, /Agent Transcript Linter/);
assert.match(output, /--stdin/);
assert.match(output, /--strict/);
assert.match(output, /--json/);
});
it("exits 0 with no events from stdin", () => {
const dir = join(tmpdir(), `ci-lint-empty-${Date.now()}`);
mkdirSync(dir, { recursive: true });
const file = join(dir, "empty.jsonl");
writeFileSync(file, "\n");
try {
const output = execFileSync("node", [LINTER, file], { encoding: "utf8" });
assert.match(output, /No events found/);
}
finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("detects Law 1 violation (writes without research)", () => {
const events = [
{ tool: "Write", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Write", event: "tool_start", tool_input: { file_path: "/tmp/b.ts" } },
];
const output = lintEvents(events);
assert.match(output, /Law 1/);
assert.match(output, /Research Before Executing/);
});
it("no Law 1 violation when research precedes writes", () => {
const events = [
{ tool: "Grep", event: "tool_start", tool_input: { pattern: "test" } },
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Write", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
];
const result = JSON.parse(lintEvents(events, ["--json"]));
const law1Violations = result.violations.filter((violation) => violation.law === 1);
assert.equal(law1Violations.length, 0, "Should have no Law 1 violations");
});
it("detects Law 4 violation (no verification)", () => {
const events = [
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Edit", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
];
const output = lintEvents(events);
assert.match(output, /Law 4/);
assert.match(output, /Verify Before Reporting/);
});
it("no Law 4 violation when tests are run", () => {
const events = [
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Edit", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Bash", event: "tool_start", tool_input: { command: "npm test" } },
];
const result = JSON.parse(lintEvents(events, ["--json"]));
const law4Violations = result.violations.filter((violation) => violation.law === 4);
assert.equal(law4Violations.length, 0, "Should have no Law 4 violations");
});
it("detects Law 3 violation (too many consecutive edits)", () => {
const events = [
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
...Array.from({ length: 7 }, (_, index) => ({
tool: "Edit",
event: "tool_start",
tool_input: { file_path: `/tmp/file${index}.ts` },
})),
];
const output = lintEvents(events);
assert.match(output, /Law 3/);
});
it("outputs JSON with --json flag", () => {
const events = [{ tool: "Read", event: "tool_start", tool_input: {} }];
const result = JSON.parse(lintEvents(events, ["--json"]));
assert.ok("violations" in result);
assert.ok("stats" in result);
assert.ok("score" in result);
assert.ok(typeof result.score === "number");
});
it("calculates discipline score", () => {
const events = [
{ tool: "Grep", event: "tool_start", tool_input: { pattern: "test" } },
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Edit", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Bash", event: "tool_start", tool_input: { command: "npm test" } },
];
const result = JSON.parse(lintEvents(events, ["--json"]));
assert.equal(result.score, 100, "Perfect discipline should score 100");
});
it("reads from file path", () => {
const tempDir = join(tmpdir(), `ci-lint-test-${Date.now()}`);
mkdirSync(tempDir, { recursive: true });
const filePath = join(tempDir, "transcript.jsonl");
writeFileSync(filePath, '{"tool":"Read","event":"tool_start","tool_input":{}}\n');
const output = execFileSync("node", [LINTER, filePath], { encoding: "utf8" });
assert.match(output, /Agent Discipline Report/);
rmSync(tempDir, { recursive: true, force: true });
});
it("--strict exits 1 on violations", () => {
const events = [
{ tool: "Write", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
];
let exitCode = 0;
try {
lintEvents(events, ["--strict"]);
}
catch (error) {
exitCode = typeof error.status === "number"
? error.status
: 0;
}
assert.equal(exitCode, 1, "Should exit 1 with --strict and violations");
});
it("--strict exits 0 on clean transcript", () => {
const events = [
{ tool: "Grep", event: "tool_start", tool_input: { pattern: "x" } },
{ tool: "Read", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Edit", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Bash", event: "tool_start", tool_input: { command: "npm test" } },
];
const output = lintEvents(events, ["--strict"]);
assert.match(output, /No law violations/);
});
it("tracks stats correctly", () => {
const events = [
{ tool: "Grep", event: "tool_start", tool_input: {} },
{ tool: "Read", event: "tool_start", tool_input: {} },
{ tool: "Glob", event: "tool_start", tool_input: {} },
{ tool: "Write", event: "tool_start", tool_input: { file_path: "/tmp/a.ts" } },
{ tool: "Edit", event: "tool_start", tool_input: { file_path: "/tmp/b.ts" } },
{ tool: "Bash", event: "tool_start", tool_input: { command: "jest" } },
];
const result = JSON.parse(lintEvents(events, ["--json"]));
assert.equal(result.stats.researchTools, 3);
assert.equal(result.stats.writeTools, 2);
assert.equal(result.stats.verifyTools, 1);
});
});