import { describe, expect, it } from 'vitest';
import { OllamaRunState } from '../runner';
describe('OllamaRunState', () => {
    const sampleConversation = [
        {
            role: 'system',
            content: 'You are helpful.'
        },
        {
            role: 'user',
            content: 'Hello there!'
        },
        {
            role: 'assistant',
            content: '',
            tool_calls: [
                {
                    id: 'call-1',
                    type: 'function',
                    function: {
                        name: 'mcp_server__tool_a',
                        arguments: '{"foo":"bar"}'
                    }
                }
            ]
        }
    ];
    const pendingCalls = [
        {
            callId: 'call-1',
            toolUniqueName: 'mcp_server__tool_a',
            toolDisplayName: 'tool_a',
            serverId: 'server',
            serverName: 'Test Server',
            arguments: '{"foo":"bar"}',
            status: 'pending'
        }
    ];
    it('serializes and deserializes run state', () => {
        const state = new OllamaRunState({ conversation: sampleConversation, calls: pendingCalls });
        const serialized = state.toString();
        expect(typeof serialized).toBe('string');
        const restored = OllamaRunState.deserialize(serialized);
        const interruptions = restored.getInterruptions();
        expect(interruptions).toHaveLength(1);
        const [interruption] = interruptions;
        expect(interruption?.rawItem).toMatchObject({
            id: 'call-1',
            name: 'tool_a',
            serverName: 'Test Server'
        });
    });
    it('tracks approval decisions across consumeResolvedCalls', () => {
        const state = new OllamaRunState({ conversation: sampleConversation, calls: pendingCalls });
        const [interruption] = state.getInterruptions();
        expect(interruption).toBeDefined();
        state.approve(interruption);
        const { approved, rejected } = state.consumeResolvedCalls();
        expect(approved).toHaveLength(1);
        expect(rejected).toHaveLength(0);
        expect(approved[0]).toMatchObject({
            callId: 'call-1',
            status: 'consumed'
        });
    });
    it('records rejection reasons', () => {
        const state = new OllamaRunState({ conversation: sampleConversation, calls: pendingCalls });
        const [interruption] = state.getInterruptions();
        expect(interruption).toBeDefined();
        state.reject(interruption, { reason: 'User declined' });
        const { approved, rejected } = state.consumeResolvedCalls();
        expect(approved).toHaveLength(0);
        expect(rejected).toHaveLength(1);
        expect(rejected[0]).toMatchObject({
            reason: 'User declined'
        });
    });
});
