"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const act_js_1 = require("../../lib/v3/agent/tools/act.js");
const extract_js_1 = require("../../lib/v3/agent/tools/extract.js");
const fillform_js_1 = require("../../lib/v3/agent/tools/fillform.js");
/**
 * Minimal mock of V3 that captures how tools pass `model` options
 * into v3.act(), v3.extract(), and v3.observe().
 */
function createMockV3() {
    const calls = [];
    const mock = {
        logger: vitest_1.vi.fn(),
        recordAgentReplayStep: vitest_1.vi.fn(),
        act: vitest_1.vi.fn(async (_instruction, options) => {
            calls.push({ method: "act", model: options?.model });
            return {
                success: true,
                message: "ok",
                actionDescription: "clicked",
                actions: [],
            };
        }),
        extract: vitest_1.vi.fn(async (_instruction, _schema, options) => {
            calls.push({ method: "extract", model: options?.model });
            return { extraction: "data" };
        }),
        observe: vitest_1.vi.fn(async (_instruction, options) => {
            calls.push({ method: "observe", model: options?.model });
            return [];
        }),
        calls,
    };
    return mock;
}
(0, vitest_1.describe)("agent tools pass full executionModel config to v3 methods", () => {
    const modelConfig = {
        modelName: "openai/gpt-4o-mini",
        apiKey: "sk-test-key",
        baseURL: "https://custom.api",
    };
    (0, vitest_1.it)("actTool passes AgentModelConfig object to v3.act()", async () => {
        const v3 = createMockV3();
        const tool = (0, act_js_1.actTool)(v3, modelConfig);
        await tool.execute({ action: "click the button" }, {
            toolCallId: "t1",
            messages: [],
            abortSignal: new AbortController().signal,
        });
        (0, vitest_1.expect)(v3.calls).toHaveLength(1);
        (0, vitest_1.expect)(v3.calls[0].method).toBe("act");
        (0, vitest_1.expect)(v3.calls[0].model).toBe(modelConfig);
    });
    (0, vitest_1.it)("extractTool passes AgentModelConfig object to v3.extract()", async () => {
        const v3 = createMockV3();
        const tool = (0, extract_js_1.extractTool)(v3, modelConfig);
        await tool.execute({ instruction: "get the title", schema: undefined }, {
            toolCallId: "t2",
            messages: [],
            abortSignal: new AbortController().signal,
        });
        (0, vitest_1.expect)(v3.calls).toHaveLength(1);
        (0, vitest_1.expect)(v3.calls[0].method).toBe("extract");
        (0, vitest_1.expect)(v3.calls[0].model).toBe(modelConfig);
    });
    (0, vitest_1.it)("fillFormTool passes AgentModelConfig object to v3.observe()", async () => {
        const v3 = createMockV3();
        const tool = (0, fillform_js_1.fillFormTool)(v3, modelConfig);
        await tool.execute({ fields: [{ action: "type hello into name", value: "hello" }] }, {
            toolCallId: "t3",
            messages: [],
            abortSignal: new AbortController().signal,
        });
        (0, vitest_1.expect)(v3.calls).toHaveLength(1);
        (0, vitest_1.expect)(v3.calls[0].method).toBe("observe");
        (0, vitest_1.expect)(v3.calls[0].model).toBe(modelConfig);
    });
    (0, vitest_1.it)("actTool passes undefined when no executionModel is set", async () => {
        const v3 = createMockV3();
        const tool = (0, act_js_1.actTool)(v3, undefined);
        await tool.execute({ action: "click the button" }, {
            toolCallId: "t4",
            messages: [],
            abortSignal: new AbortController().signal,
        });
        (0, vitest_1.expect)(v3.calls).toHaveLength(1);
        (0, vitest_1.expect)(v3.calls[0].model).toBeUndefined();
    });
    (0, vitest_1.it)("actTool passes plain string executionModel to v3.act()", async () => {
        const v3 = createMockV3();
        const tool = (0, act_js_1.actTool)(v3, "openai/gpt-4o-mini");
        await tool.execute({ action: "click the button" }, {
            toolCallId: "t5",
            messages: [],
            abortSignal: new AbortController().signal,
        });
        (0, vitest_1.expect)(v3.calls).toHaveLength(1);
        (0, vitest_1.expect)(v3.calls[0].model).toBe("openai/gpt-4o-mini");
    });
});
(0, vitest_1.describe)("executionModel fallback logic", () => {
    // This mirrors the resolution in V3.prepareAgentExecution (v3.ts:1682):
    //   const resolvedExecutionModel = options?.executionModel ?? options?.model;
    function resolveExecutionModel(options) {
        return options?.executionModel ?? options?.model;
    }
    (0, vitest_1.it)("prefers explicit executionModel over model", () => {
        const result = resolveExecutionModel({
            executionModel: "openai/gpt-4o-mini",
            model: "anthropic/claude-sonnet-4-20250514",
        });
        (0, vitest_1.expect)(result).toBe("openai/gpt-4o-mini");
    });
    (0, vitest_1.it)("falls back to model when executionModel is not set", () => {
        const modelConfig = {
            modelName: "anthropic/claude-sonnet-4-20250514",
            apiKey: "sk-test",
        };
        const result = resolveExecutionModel({ model: modelConfig });
        (0, vitest_1.expect)(result).toBe(modelConfig);
    });
    (0, vitest_1.it)("returns undefined when neither is set", () => {
        (0, vitest_1.expect)(resolveExecutionModel({})).toBeUndefined();
        (0, vitest_1.expect)(resolveExecutionModel(undefined)).toBeUndefined();
    });
});
//# sourceMappingURL=agent-execution-model.test.js.map