Skip to content

Commit c96998b

Browse files
committed
test: assert error logging in generateAgentRules on CLI failure
Export generateAgentRules and add a test that verifies: - logCommand is called with the binary path, args, and cwd - logResult is called with exit code 1 on failure - The thrown error contains ENOENT context Closes #46 Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
1 parent ccf145c commit c96998b

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/commands/initializeProject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function classifyAgentsFile(existingContent: string | undefined, generate
102102
: "different";
103103
}
104104

105-
async function generateAgentRules(binaryPath: string, cwd: string): Promise<string> {
105+
export async function generateAgentRules(binaryPath: string, cwd: string): Promise<string> {
106106
const log = getPatchloomLog();
107107
const args = ["agent-rules"];
108108
log?.logCommand(binaryPath, args, cwd);

test/unit/initializeProject.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33
import { MINIMUM_SUPPORTED_PATCHLOOM_VERSION } from "../../src/binary/patchloom.js";
4-
import { classifyAgentsFile } from "../../src/commands/initializeProject.js";
4+
import { classifyAgentsFile, generateAgentRules } from "../../src/commands/initializeProject.js";
55
import { buildStatusDetails, preferredStatusAction } from "../../src/commands/showStatus.js";
66
import { buildPatchloomMcpEntry, configureMcpTargets, inspectMcpTargets } from "../../src/mcp/config.js";
7+
import { setPatchloomLog } from "../../src/logging/outputChannel.js";
78
import { formatCliOutput, formatError } from "../../src/util.js";
89

910
test("formatError extracts message from Error instances", () => {
@@ -334,3 +335,32 @@ test("configureMcpTargets creates or updates only the selected target kinds", as
334335
assert.match(writes.get(cursorPath) ?? "", /mcp-server/);
335336
assert.match(writes.get(cursorPath) ?? "", /other/);
336337
});
338+
339+
test("generateAgentRules logs error to output channel on CLI failure", async () => {
340+
const logged: { exitCode: number; stdout: string; stderr: string }[] = [];
341+
const commands: { binary: string; args: readonly string[]; cwd: string }[] = [];
342+
setPatchloomLog({
343+
log() {},
344+
logCommand(binary, args, cwd) { commands.push({ binary, args, cwd }); },
345+
logResult(exitCode, stdout, stderr) { logged.push({ exitCode, stdout, stderr }); },
346+
show() {},
347+
dispose() {}
348+
});
349+
try {
350+
await assert.rejects(
351+
() => generateAgentRules("/nonexistent/patchloom", "/tmp"),
352+
(err: Error) => {
353+
assert.match(err.message, /ENOENT|not found|No such file/i);
354+
return true;
355+
}
356+
);
357+
assert.equal(commands.length, 1, "logCommand should be called once");
358+
assert.equal(commands[0].binary, "/nonexistent/patchloom");
359+
assert.deepEqual(commands[0].args, ["agent-rules"]);
360+
assert.equal(commands[0].cwd, "/tmp");
361+
assert.equal(logged.length, 1, "logResult should be called once on failure");
362+
assert.equal(logged[0].exitCode, 1);
363+
} finally {
364+
setPatchloomLog(undefined);
365+
}
366+
});

0 commit comments

Comments
 (0)