import { describe, expect, it } from "vitest";
import { AssistantSessionStore } from "../src/services/assistantSessionStore";

describe("assistant session backward compatibility", () => {
  it("lazy-upgrades legacy session objects without investigation_state", () => {
    const store = new AssistantSessionStore();
    const sessionsMap = (store as unknown as { sessions: Map<string, unknown> }).sessions;
    const sessionId = "legacy-session-1";

    sessionsMap.set(sessionId, {
      session_id: sessionId,
      updated_at: "2026-03-25T10:00:00.000Z",
      items: []
    });

    const session = store.getSession(sessionId);
    expect(session).toBeTruthy();
    expect(session?.session_id).toBe(sessionId);
    expect(Array.isArray(session?.items)).toBe(true);
    expect(session?.investigation_state?.schema_version).toBe("investigation_state_v1");
  });

  it("normalizes malformed legacy sessions with missing items array", () => {
    const store = new AssistantSessionStore();
    const sessionsMap = (store as unknown as { sessions: Map<string, unknown> }).sessions;
    const sessionId = "legacy-session-2";

    sessionsMap.set(sessionId, {
      session_id: sessionId
    });

    const ensured = store.ensureSession(sessionId);
    expect(ensured.session_id).toBe(sessionId);
    expect(Array.isArray(ensured.items)).toBe(true);
    expect(ensured.items.length).toBe(0);
    expect(ensured.investigation_state?.schema_version).toBe("investigation_state_v1");
  });
});
