tdd-enforcer 0.1.4 → 0.1.5
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.
- package/adapters/pi/log.test.ts +73 -0
- package/adapters/pi/log.ts +2 -2
- package/adapters/pi/tools.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { mkdirSync, writeFileSync, rmSync, readFileSync, existsSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { tddLog } from "./log.js";
|
|
6
|
+
|
|
7
|
+
function setup() {
|
|
8
|
+
const dir = join(tmpdir(), `tdd-log-test-${Date.now()}`);
|
|
9
|
+
mkdirSync(dir, { recursive: true });
|
|
10
|
+
return dir;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function teardown(dir: string) {
|
|
14
|
+
rmSync(dir, { recursive: true, force: true });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe("tddLog", () => {
|
|
18
|
+
it("creates the log file if it doesn't exist", () => {
|
|
19
|
+
const dir = setup();
|
|
20
|
+
tddLog(dir, "INFO", "hello");
|
|
21
|
+
expect(existsSync(join(dir, "tdd.log"))).toBe(true);
|
|
22
|
+
teardown(dir);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("appends multiple lines", () => {
|
|
26
|
+
const dir = setup();
|
|
27
|
+
tddLog(dir, "INFO", "line one");
|
|
28
|
+
tddLog(dir, "DEBUG", "line two");
|
|
29
|
+
const content = readFileSync(join(dir, "tdd.log"), "utf-8");
|
|
30
|
+
const lines = content.trim().split("\n");
|
|
31
|
+
expect(lines).toHaveLength(2);
|
|
32
|
+
expect(lines[0]).toContain("[INFO]");
|
|
33
|
+
expect(lines[0]).toContain("line one");
|
|
34
|
+
expect(lines[1]).toContain("[DEBUG]");
|
|
35
|
+
expect(lines[1]).toContain("line two");
|
|
36
|
+
teardown(dir);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("includes data as JSON when provided", () => {
|
|
40
|
+
const dir = setup();
|
|
41
|
+
tddLog(dir, "INFO", "with data", { key: "val", num: 42 });
|
|
42
|
+
const content = readFileSync(join(dir, "tdd.log"), "utf-8");
|
|
43
|
+
expect(content).toContain('{"key":"val","num":42}');
|
|
44
|
+
teardown(dir);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("trims to last 1000 lines when exceeded", () => {
|
|
48
|
+
const dir = setup();
|
|
49
|
+
for (let i = 0; i < 1005; i++) {
|
|
50
|
+
tddLog(dir, "DEBUG", `line ${i}`);
|
|
51
|
+
}
|
|
52
|
+
const content = readFileSync(join(dir, "tdd.log"), "utf-8");
|
|
53
|
+
const lines = content.trim().split("\n");
|
|
54
|
+
expect(lines).toHaveLength(1000);
|
|
55
|
+
expect(lines[0]).toContain("line 5");
|
|
56
|
+
expect(lines[999]).toContain("line 1004");
|
|
57
|
+
teardown(dir);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("does not throw on invalid tddDir", () => {
|
|
61
|
+
expect(() => tddLog("/nonexistent/path/tdd", "INFO", "fail")).not.toThrow();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("handles missing data field gracefully", () => {
|
|
65
|
+
const dir = setup();
|
|
66
|
+
tddLog(dir, "WARN", "no data");
|
|
67
|
+
const content = readFileSync(join(dir, "tdd.log"), "utf-8");
|
|
68
|
+
expect(content).toContain("[WARN]");
|
|
69
|
+
expect(content).toContain("no data");
|
|
70
|
+
expect(content).not.toContain("{}");
|
|
71
|
+
teardown(dir);
|
|
72
|
+
});
|
|
73
|
+
});
|
package/adapters/pi/log.ts
CHANGED
|
@@ -19,8 +19,8 @@ export function tddLog(
|
|
|
19
19
|
|
|
20
20
|
// Trim to last MAX_LINES
|
|
21
21
|
const content = readFileSync(logPath, "utf-8");
|
|
22
|
-
const lines = content.split("\n");
|
|
23
|
-
if (lines.length > MAX_LINES
|
|
22
|
+
const lines = content.trimEnd().split("\n");
|
|
23
|
+
if (lines.length > MAX_LINES) {
|
|
24
24
|
const trimmed = lines.slice(-MAX_LINES).join("\n") + "\n";
|
|
25
25
|
writeFileSync(logPath, trimmed, "utf-8");
|
|
26
26
|
}
|
package/adapters/pi/tools.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
1
2
|
import { Type } from "typebox";
|
|
2
3
|
import { execSync } from "node:child_process";
|
|
3
4
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
@@ -15,7 +16,7 @@ import {
|
|
|
15
16
|
import type { TestRunner, Phase } from "../../engine/index.js";
|
|
16
17
|
import { getNudgePrompt } from "./prompts.js";
|
|
17
18
|
import { loadTddState } from "./helpers.js";
|
|
18
|
-
|
|
19
|
+
import { tddLog } from "./log.js";
|
|
19
20
|
|
|
20
21
|
export function registerTools(pi: ExtensionAPI): void {
|
|
21
22
|
pi.registerTool({
|