-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathsingle-output.test.ts
More file actions
83 lines (70 loc) · 2.91 KB
/
single-output.test.ts
File metadata and controls
83 lines (70 loc) · 2.91 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
import assert from "node:assert/strict";
import { afterEach, describe, it } from "node:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { finalizeSingleOutput, injectSingleOutputInstruction, resolveSingleOutputPath } from "./single-output.ts";
const tempDirs: string[] = [];
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (!dir) continue;
fs.rmSync(dir, { recursive: true, force: true });
}
});
describe("resolveSingleOutputPath", () => {
it("keeps absolute paths unchanged", () => {
const absolutePath = path.join(os.tmpdir(), "pi-subagents-abs", "report.md");
const resolved = resolveSingleOutputPath(absolutePath, "/repo", "/override");
assert.equal(resolved, absolutePath);
});
it("resolves relative paths against requested cwd", () => {
const resolved = resolveSingleOutputPath("reviews/report.md", "/runtime", "/requested");
assert.equal(resolved, path.resolve("/requested", "reviews/report.md"));
});
it("resolves relative paths against runtime cwd when requested cwd is absent", () => {
const resolved = resolveSingleOutputPath("reviews/report.md", "/runtime");
assert.equal(resolved, path.resolve("/runtime", "reviews/report.md"));
});
it("resolves relative requested cwd from runtime cwd before resolving output", () => {
const resolved = resolveSingleOutputPath("reviews/report.md", "/runtime", "nested/work");
assert.equal(resolved, path.resolve("/runtime", "nested/work", "reviews/report.md"));
});
});
describe("injectSingleOutputInstruction", () => {
it("appends output instruction with resolved path", () => {
const output = injectSingleOutputInstruction("Analyze this", "/tmp/report.md");
assert.match(output, /Write your findings to: \/tmp\/report.md/);
});
});
describe("finalizeSingleOutput", () => {
it("persists full output while displaying truncated output", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagents-output-test-"));
tempDirs.push(dir);
const outputPath = path.join(dir, "review.md");
const fullOutput = "line 1\nline 2\nline 3";
const truncatedOutput = "[TRUNCATED]\nline 1";
const result = finalizeSingleOutput({
fullOutput,
truncatedOutput,
outputPath,
exitCode: 0,
});
assert.match(result.displayOutput, /^\[TRUNCATED\]\nline 1/);
assert.match(result.displayOutput, /📄 Output saved to:/);
assert.equal(fs.readFileSync(outputPath, "utf-8"), fullOutput);
});
it("does not write output file on failed runs", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-subagents-output-test-"));
tempDirs.push(dir);
const outputPath = path.join(dir, "review.md");
const result = finalizeSingleOutput({
fullOutput: "full output",
truncatedOutput: "truncated output",
outputPath,
exitCode: 1,
});
assert.equal(result.displayOutput, "truncated output");
assert.equal(fs.existsSync(outputPath), false);
});
});