import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
import { aiAssistantSlice, createDefaultSessionState, } from './ai-assistant-slice';
// GENC-1351 §5.6 / Phase 1: the new bulk loadSession + resetSession reducers.
const { loadSession, resetSession, setMessages, setState, setSessionMenuOpen, setShowToolCalls, setShowThinkingSteps, setShowNarration, setShowAgentSwitchIndicator, setEnabledAnimations, } = aiAssistantSlice.actions;
const reduce = aiAssistantSlice.reducer;
const Suite = createLogicSuite('ai-assistant-slice-persistence');
Suite('loadSession hydrates the restorable fields and forces idle', () => {
    let start = createDefaultSessionState();
    start = reduce(start, setState('loading')); // prove it overrides any prior state
    const payload = {
        // Usage rides on the message: `loadSession` derives the session totals from the
        // restored transcript rather than from a stored counter, so the cost has to be in
        // the messages for the assertion below to see it.
        messages: [
            { role: 'user', content: 'hi', cost: 1.25, inputTokens: 100, outputTokens: 20 },
        ],
        pinnedAgentName: 'Guided Booking',
        flowOwnerAgentName: 'Guided Booking',
        contextTokens: 1200,
        contextLimit: 200000,
        activeModel: 'claude-sonnet-4-6',
        activeProviderName: 'anthropic',
    };
    const next = reduce(start, loadSession(payload));
    assert.is(next.messages.length, 1);
    assert.is(next.pinnedAgentName, 'Guided Booking');
    assert.is(next.flowOwnerAgentName, 'Guided Booking');
    assert.is(next.sessionUsage.costUsd, 1.25);
    // Derived from the same walk: 100 prompt tokens with no cache split reported, so all
    // of it is uncached input, plus the 20 generated.
    assert.is(next.sessionUsage.uncachedInputTokens, 100);
    assert.is(next.sessionUsage.outputTokens, 20);
    assert.is(next.contextTokens, 1200);
    assert.is(next.contextLimit, 200000);
    assert.is(next.activeModel, 'claude-sonnet-4-6');
    assert.is(next.activeProviderName, 'anthropic');
    assert.is(next.state, 'idle');
});
Suite('resetSession wipes back to a fresh default', () => {
    let state = createDefaultSessionState();
    state = reduce(state, setMessages([{ role: 'user', content: 'x' }]));
    state = reduce(state, loadSession({
        messages: [],
        pinnedAgentName: 'A',
        flowOwnerAgentName: 'A',
        contextTokens: 5,
        contextLimit: 10,
        activeModel: 'm',
        activeProviderName: 'p',
    }));
    const cleared = reduce(state, resetSession());
    assert.is(cleared.messages.length, 0);
    assert.is(cleared.sessionUsage.costUsd, 0);
    assert.is(cleared.pinnedAgentName, null);
    assert.is(cleared.flowOwnerAgentName, null);
    assert.is(cleared.contextTokens, undefined);
    assert.is(cleared.state, 'idle');
});
Suite('resetSession preserves the user display preferences (they survive Clear)', () => {
    // Display prefs are settings, not conversation state — Clear/New-chat must keep them
    // (GENC-1351 #10). Turn several on, plus an animation, then reset.
    let state = createDefaultSessionState();
    state = reduce(state, setShowToolCalls(true));
    state = reduce(state, setShowThinkingSteps(true));
    state = reduce(state, setShowNarration(true));
    state = reduce(state, setShowAgentSwitchIndicator(true));
    state = reduce(state, setEnabledAnimations(['halo']));
    state = reduce(state, setMessages([{ role: 'user', content: 'x' }]));
    const cleared = reduce(state, resetSession());
    // Conversation wiped…
    assert.is(cleared.messages.length, 0, 'conversation cleared');
    // …but the display prefs kept.
    assert.is(cleared.showToolCalls, true);
    assert.is(cleared.showThinkingSteps, true);
    assert.is(cleared.showNarration, true);
    assert.is(cleared.showAgentSwitchIndicator, true);
    assert.equal(cleared.enabledAnimations, ['halo']);
});
Suite('createDefaultSessionState returns independent collections', () => {
    // No aliasing between instances — resetSession must hand back a clean slate.
    assert.ok(createDefaultSessionState().messages !== createDefaultSessionState().messages);
});
// GENC-1351 Phase 3: the lifecycle (session) menu open/close flag.
Suite('setSessionMenuOpen toggles the lifecycle-menu flag; resetSession clears it', () => {
    let state = createDefaultSessionState();
    assert.is(state.sessionMenuOpen, false);
    state = reduce(state, setSessionMenuOpen(true));
    assert.is(state.sessionMenuOpen, true);
    const cleared = reduce(state, resetSession());
    assert.is(cleared.sessionMenuOpen, false);
});
Suite.run();
