mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-08-22 03:18:54 +08:00
fix: enforce max turns from claude args (#1607)
This commit is contained in:
@@ -128,4 +128,71 @@ describe("runClaudeWithSdk", () => {
|
||||
coreErrorSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
test("fails closed when a successful result exceeds maxTurns", async () => {
|
||||
const consoleErrorSpy = spyOn(console, "error").mockImplementation(
|
||||
() => {},
|
||||
);
|
||||
const consoleLogSpy = spyOn(console, "log").mockImplementation(() => {});
|
||||
const coreErrorSpy = spyOn(
|
||||
await import("@actions/core"),
|
||||
"error",
|
||||
).mockImplementation(() => {});
|
||||
|
||||
tempDir = await mkdtemp(join(tmpdir(), "claude-sdk-"));
|
||||
process.env.RUNNER_TEMP = tempDir;
|
||||
|
||||
const promptPath = join(tempDir, "prompt.txt");
|
||||
await writeFile(promptPath, "test prompt");
|
||||
|
||||
const initMessage = {
|
||||
type: "system",
|
||||
subtype: "init",
|
||||
session_id: "session-123",
|
||||
model: "claude-opus-4-7",
|
||||
};
|
||||
|
||||
const successResultMessage = {
|
||||
type: "result",
|
||||
subtype: "success",
|
||||
is_error: false,
|
||||
duration_ms: 960000,
|
||||
num_turns: 73,
|
||||
total_cost_usd: 0,
|
||||
permission_denials: [],
|
||||
};
|
||||
|
||||
mock.module("@anthropic-ai/claude-agent-sdk", () => ({
|
||||
query: async function* () {
|
||||
yield initMessage;
|
||||
yield successResultMessage;
|
||||
},
|
||||
}));
|
||||
|
||||
try {
|
||||
const { runClaudeWithSdk } = await import("../src/run-claude-sdk");
|
||||
|
||||
await expect(
|
||||
runClaudeWithSdk(promptPath, {
|
||||
sdkOptions: { maxTurns: 60 },
|
||||
showFullOutput: false,
|
||||
hasJsonSchema: false,
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
"Claude reported a successful result after 73 turns, exceeding the configured maximum of 60",
|
||||
);
|
||||
|
||||
const executionFile = join(tempDir, "claude-execution-output.json");
|
||||
await expect(readFile(executionFile, "utf-8")).resolves.toBe(
|
||||
JSON.stringify([initMessage, successResultMessage], null, 2),
|
||||
);
|
||||
expect(coreErrorSpy).toHaveBeenCalledWith(
|
||||
"Claude reported a successful result after 73 turns, exceeding the configured maximum of 60",
|
||||
);
|
||||
} finally {
|
||||
consoleErrorSpy.mockRestore();
|
||||
consoleLogSpy.mockRestore();
|
||||
coreErrorSpy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user