import { jsx as _jsx } from "react/jsx-runtime";
/** @vitest-environment jsdom */
import React from 'react';
import { describe, expect, it } from 'vitest';
import { act, render } from '@testing-library/react';
import { ChatSessionsProvider, useChatSessions } from '../ChatSessionsContext';
import { StorageAdapterProvider } from '../StorageAdapterContext';
const createMemoryAdapter = () => {
    const store = new Map();
    const adapter = {
        getItem: (key) => (store.has(key) ? store.get(key) : null),
        setItem: (key, value) => {
            store.set(key, value);
        },
        removeItem: (key) => {
            store.delete(key);
        }
    };
    return { adapter, store };
};
const Consumer = React.forwardRef((_, ref) => {
    const context = useChatSessions();
    React.useImperativeHandle(ref, () => context, [context]);
    return null;
});
const renderWithProviders = (adapter, ref) => {
    render(_jsx(StorageAdapterProvider, { adapter: adapter, children: _jsx(ChatSessionsProvider, { children: _jsx(Consumer, { ref: ref }) }) }));
};
describe('ChatSessionsContext', () => {
    it('initializes with a default session', () => {
        const { adapter } = createMemoryAdapter();
        const ref = React.createRef();
        renderWithProviders(adapter, ref);
        const api = ref.current;
        expect(api).toBeDefined();
        expect(api?.sessions).toHaveLength(1);
        expect(api?.activeSessionId).toBeTypeOf('string');
        expect(api?.activeSessionId).toEqual(api?.sessions[0]?.id);
    });
    it('creates and archives sessions correctly', () => {
        const { adapter } = createMemoryAdapter();
        const ref = React.createRef();
        renderWithProviders(adapter, ref);
        const getApi = () => {
            const api = ref.current;
            if (!api)
                throw new Error('Chat sessions context not ready');
            return api;
        };
        const initialId = getApi().activeSessionId;
        act(() => {
            getApi().startNewSession();
        });
        const apiAfterNewSession = getApi();
        expect(apiAfterNewSession.sessions).toHaveLength(2);
        const [active, previous] = apiAfterNewSession.sessions;
        expect(active.id).toEqual(apiAfterNewSession.activeSessionId);
        expect(previous.id).toEqual(initialId);
        expect(previous.archivedAt).toBeTypeOf('number');
        expect(previous.archivedAt).toBeGreaterThan(0);
    });
    it('resumes archived sessions and clears archive state', () => {
        const { adapter } = createMemoryAdapter();
        const ref = React.createRef();
        renderWithProviders(adapter, ref);
        const getApi = () => {
            const api = ref.current;
            if (!api)
                throw new Error('Chat sessions context not ready');
            return api;
        };
        const firstId = getApi().activeSessionId;
        act(() => {
            getApi().startNewSession();
        });
        const secondId = getApi().activeSessionId;
        act(() => {
            getApi().resumeSession(firstId);
        });
        const apiAfterResume = getApi();
        expect(apiAfterResume.activeSessionId).toEqual(firstId);
        const resumed = apiAfterResume.sessions.find((session) => session.id === firstId);
        expect(resumed?.archivedAt).toBeUndefined();
        const second = apiAfterResume.sessions.find((session) => session.id === secondId);
        expect(second?.archivedAt).toBeTypeOf('number');
    });
    it('deletes sessions and clears persisted data for that session', () => {
        const { adapter, store } = createMemoryAdapter();
        const ref = React.createRef();
        renderWithProviders(adapter, ref);
        const getApi = () => {
            const api = ref.current;
            if (!api)
                throw new Error('Chat sessions context not ready');
            return api;
        };
        const firstId = getApi().activeSessionId;
        adapter.setItem(`chat-session-${firstId}`, JSON.stringify({ chatHistory: [] }));
        adapter.setItem(`chat-session-${firstId}-attachments`, JSON.stringify({}));
        adapter.setItem(`chat-session-${firstId}-pending-run`, JSON.stringify(null));
        act(() => {
            getApi().startNewSession();
        });
        act(() => {
            getApi().deleteSession(firstId);
        });
        expect(store.has(`chat-session-${firstId}`)).toBe(false);
        expect(store.has(`chat-session-${firstId}-attachments`)).toBe(false);
        expect(store.has(`chat-session-${firstId}-pending-run`)).toBe(false);
    });
});
