import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { usePersistedModel } from "../usePersistedModel";
import { DEFAULT_MODEL_ID, DEFAULT_CURSOR_MODEL_ID } from "../../models/registry";
const STORAGE_KEY_NATIVE = "stigmer:session:model";
const STORAGE_KEY_CURSOR = "stigmer:session:model:cursor";
describe("usePersistedModel", () => {
    beforeEach(() => {
        localStorage.clear();
    });
    afterEach(() => {
        localStorage.clear();
    });
    describe("basic persistence", () => {
        it("returns undefined when localStorage is empty", () => {
            const { result } = renderHook(() => usePersistedModel({ harness: "native" }));
            expect(result.current[0]).toBeUndefined();
        });
        it("restores a valid model from localStorage", () => {
            localStorage.setItem(STORAGE_KEY_NATIVE, DEFAULT_MODEL_ID);
            const { result } = renderHook(() => usePersistedModel({ harness: "native" }));
            expect(result.current[0]).toBe(DEFAULT_MODEL_ID);
        });
        it("returns undefined for an invalid model in localStorage", () => {
            localStorage.setItem(STORAGE_KEY_NATIVE, "nonexistent-model-xyz");
            const { result } = renderHook(() => usePersistedModel({ harness: "native" }));
            expect(result.current[0]).toBeUndefined();
        });
        it("persists model on change", () => {
            const { result } = renderHook(() => usePersistedModel({ harness: "native" }));
            act(() => result.current[1](DEFAULT_MODEL_ID));
            expect(localStorage.getItem(STORAGE_KEY_NATIVE)).toBe(DEFAULT_MODEL_ID);
            expect(result.current[0]).toBe(DEFAULT_MODEL_ID);
        });
        it("uses cursor-specific key for cursor harness", () => {
            localStorage.setItem(STORAGE_KEY_CURSOR, DEFAULT_CURSOR_MODEL_ID);
            const { result } = renderHook(() => usePersistedModel({ harness: "cursor" }));
            expect(result.current[0]).toBe(DEFAULT_CURSOR_MODEL_ID);
        });
    });
    describe("compound key handling", () => {
        it("extracts plain modelId from compound key in localStorage", () => {
            localStorage.setItem(STORAGE_KEY_CURSOR, "cursor/default");
            const { result } = renderHook(() => usePersistedModel({ harness: "cursor" }));
            expect(result.current[0]).toBe(DEFAULT_CURSOR_MODEL_ID);
        });
        it("extracts plain modelId from native compound key", () => {
            localStorage.setItem(STORAGE_KEY_NATIVE, `native/${DEFAULT_MODEL_ID}`);
            const { result } = renderHook(() => usePersistedModel({ harness: "native" }));
            expect(result.current[0]).toBe(DEFAULT_MODEL_ID);
        });
        it("handles non-compound values unchanged", () => {
            localStorage.setItem(STORAGE_KEY_CURSOR, DEFAULT_CURSOR_MODEL_ID);
            const { result } = renderHook(() => usePersistedModel({ harness: "cursor" }));
            expect(result.current[0]).toBe(DEFAULT_CURSOR_MODEL_ID);
        });
    });
    describe("harness transition (key change re-sync)", () => {
        it("re-reads from new localStorage key when harness changes", () => {
            localStorage.setItem(STORAGE_KEY_NATIVE, DEFAULT_MODEL_ID);
            localStorage.setItem(STORAGE_KEY_CURSOR, DEFAULT_CURSOR_MODEL_ID);
            const { result, rerender } = renderHook(({ harness }) => usePersistedModel({ harness }), { initialProps: { harness: "native" } });
            expect(result.current[0]).toBe(DEFAULT_MODEL_ID);
            rerender({ harness: "cursor" });
            expect(result.current[0]).toBe(DEFAULT_CURSOR_MODEL_ID);
        });
        it("returns undefined after harness change when new key has no stored value", () => {
            localStorage.setItem(STORAGE_KEY_NATIVE, DEFAULT_MODEL_ID);
            const { result, rerender } = renderHook(({ harness }) => usePersistedModel({ harness }), { initialProps: { harness: "native" } });
            expect(result.current[0]).toBe(DEFAULT_MODEL_ID);
            rerender({ harness: "cursor" });
            expect(result.current[0]).toBeUndefined();
        });
        it("handles compound key in new storage key after harness transition", () => {
            localStorage.setItem(STORAGE_KEY_CURSOR, "cursor/default");
            const { result, rerender } = renderHook(({ harness }) => usePersistedModel({ harness }), { initialProps: { harness: "native" } });
            rerender({ harness: "cursor" });
            expect(result.current[0]).toBe(DEFAULT_CURSOR_MODEL_ID);
        });
    });
});
//# sourceMappingURL=usePersistedModel.test.js.map