"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const anthropic_1 = require("../../src/clients/anthropic");
function createClient() {
    return new anthropic_1.GenericAnthropicClient("fake-key");
}
describe("GenericAnthropicClient.transformMessages", () => {
    let client;
    beforeEach(() => {
        client = createClient();
    });
    it("should handle a simple user message", () => {
        const messages = [
            { role: "user", content: "Hello" },
        ];
        const result = client.transformMessages(messages);
        expect(result).toHaveLength(1);
        expect(result[0].role).toBe("user");
        expect(result[0].content).toBe("Hello");
    });
    it("should filter out system messages", () => {
        const messages = [
            { role: "system", content: "You are helpful" },
            { role: "user", content: "Hello" },
        ];
        const result = client.transformMessages(messages);
        expect(result).toHaveLength(1);
        expect(result[0].role).toBe("user");
    });
    it("should inject tool_use assistant block when processing tool result", () => {
        const messages = [
            { role: "user", content: "Use a tool" },
            {
                role: "assistant",
                content: "",
                tool_calls: [
                    {
                        id: "toolu_abc123",
                        type: "function",
                        function: {
                            name: "listAvailableTools",
                            arguments: "{}",
                        },
                    },
                ],
            },
            {
                role: "tool",
                tool_call_id: "toolu_abc123",
                name: "listAvailableTools",
                content: '{"enabled": ["finalAnswer"], "disabled": []}',
            },
        ];
        const result = client.transformMessages(messages);
        expect(result.length).toBeGreaterThanOrEqual(2);
        const assistantMsg = result.find((m) => m.role === "assistant" &&
            Array.isArray(m.content) &&
            m.content.some((c) => c.type === "tool_use"));
        expect(assistantMsg).toBeDefined();
        const toolUseBlock = assistantMsg.content.find((c) => c.type === "tool_use");
        expect(toolUseBlock.id).toBe("toolu_abc123");
        expect(toolUseBlock.name).toBe("listAvailableTools");
        const userToolResult = result.find((m) => m.role === "user" &&
            Array.isArray(m.content) &&
            m.content.some((c) => c.type === "tool_result"));
        expect(userToolResult).toBeDefined();
        const toolResultBlock = userToolResult.content.find((c) => c.type === "tool_result");
        expect(toolResultBlock.tool_use_id).toBe("toolu_abc123");
    });
    it("should not have undefined tool_use_id when assistant message has empty content with tool_calls", () => {
        const messages = [
            { role: "user", content: "Use a tool" },
            {
                role: "assistant",
                content: "",
                tool_calls: [
                    {
                        id: "toolu_abc123",
                        type: "function",
                        function: {
                            name: "listAvailableTools",
                            arguments: "{}",
                        },
                    },
                ],
            },
            {
                role: "tool",
                tool_call_id: "toolu_abc123",
                name: "listAvailableTools",
                content: '{"enabled": ["finalAnswer"]}',
            },
        ];
        const result = client.transformMessages(messages);
        const userToolResult = result.find((m) => m.role === "user" &&
            Array.isArray(m.content) &&
            m.content.some((c) => c.type === "tool_result"));
        expect(userToolResult).toBeDefined();
        const toolResultBlock = userToolResult.content.find((c) => c.type === "tool_result");
        expect(toolResultBlock.tool_use_id).toBe("toolu_abc123");
        expect(toolResultBlock.tool_use_id).not.toBeUndefined();
    });
    it("should handle multiple sequential tool calls", () => {
        const messages = [
            { role: "user", content: "Do two things" },
            {
                role: "assistant",
                content: "",
                tool_calls: [
                    {
                        id: "toolu_111",
                        type: "function",
                        function: { name: "toolOne", arguments: "{}" },
                    },
                ],
            },
            {
                role: "tool",
                tool_call_id: "toolu_111",
                name: "toolOne",
                content: "result one",
            },
            {
                role: "assistant",
                content: "",
                tool_calls: [
                    {
                        id: "toolu_222",
                        type: "function",
                        function: { name: "toolTwo", arguments: "{}" },
                    },
                ],
            },
            {
                role: "tool",
                tool_call_id: "toolu_222",
                name: "toolTwo",
                content: "result two",
            },
        ];
        const result = client.transformMessages(messages);
        const toolResults = result
            .filter((m) => m.role === "user" && Array.isArray(m.content))
            .flatMap((m) => m.content.filter((c) => c.type === "tool_result"));
        expect(toolResults).toHaveLength(2);
        const ids = toolResults.map((r) => r.tool_use_id);
        expect(ids).toContain("toolu_111");
        expect(ids).toContain("toolu_222");
        expect(ids).not.toContain(undefined);
    });
    it("should not crash when response is undefined (Cannot use in operator bug)", () => {
        const undefinedLike = undefined;
        expect(() => {
            if (undefinedLike != null && "response" in undefinedLike) {
            }
        }).not.toThrow();
    });
});
//# sourceMappingURL=anthropic.test.js.map