/*
 * Copyright OpenSearch Contributors
 * SPDX-License-Identifier: Apache-2.0
 */
/**
 * Build an {@link AgentFixture} around a runner-supplied {@link AgentInvokeFn}.
 *
 * Enforces the one-invocation-per-test rule and resolves the effective
 * prompt/context (per-call argument wins over the test's `defaultPrompt` /
 * `defaultContext`). The `invoke` callback does the real work (connector +
 * RunResult wrapping) and is only ever called at most once.
 */
export function createAgentFixture(invoke, options = {}) {
    let invoked = false;
    return {
        get invoked() {
            return invoked;
        },
        async run(prompt, runOptions) {
            if (invoked) {
                throw new Error('agent.run() may be called at most once per test. Benchmarking ' +
                    'requires exactly one agent invocation per test so trajectories ' +
                    'stay comparable across runs. For multi-turn conversations, model ' +
                    'the turns inside a single run via your connector.');
            }
            const effectivePrompt = prompt ?? options.defaultPrompt;
            if (effectivePrompt == null || effectivePrompt === '') {
                throw new Error('agent.run() requires a prompt — none was passed and the test has ' +
                    'no `defaultPrompt`. Pass a prompt: `await agent.run("...")`.');
            }
            // Mark invoked BEFORE awaiting so a concurrent/re-entrant second call
            // (e.g. forgetting to await) still trips the guard deterministically.
            invoked = true;
            const mergedContext = [
                ...(options.defaultContext ?? []),
                ...(runOptions?.context ?? []),
            ];
            const context = mergedContext.length > 0 ? mergedContext : undefined;
            return invoke(effectivePrompt, { ...runOptions, context });
        },
    };
}
//# sourceMappingURL=agentFixture.js.map