import { describe, expect, test } from "vitest";
import { observeToolResult, resetToolWatchdog } from "../tool-watchdog.js";

function failingBash(command, text = "command failed") {
	return {
		toolName: "Bash",
		input: { command },
		isError: true,
		content: [{ type: "text", text }],
	};
}

describe("tool watchdog loop signal", () => {
	test("observeToolResult_when_same_bash_command_fails_twice_returns_loop_signal", () => {
		resetToolWatchdog();

		const first = observeToolResult(failingBash("npm run missing", "first"));
		const second = observeToolResult(failingBash("npm run missing", "second"));

		expect(first).toBeUndefined();
		expect(second).toMatchObject({
			key: "repeat-failure:npm run missing",
			severity: "warning",
			loopSignal: {
				scope: "tool",
				kind: "tool-result:repeated-failure",
				severity: "warning",
				recommendedAction: "continue",
				source: "tool-watchdog",
				evidence: {
					toolName: "Bash",
					command: "npm run missing",
					count: 2,
					windowMs: 600000,
					excerpt: "second",
				},
			},
		});
	});
});
