import { describe, expect, it } from "vitest";

const {
  buildSessionsDialogViewModel,
  formatCwdTail,
  formatRelativeTime,
} = await import("../src/dimsdk/ui/components/sessions-dialog/view-model.ts");

describe("dimsdk sessions dialog view model compatibility", () => {
  it("renders cwd, title, and relative time without leaking the session id", () => {
    const now = Date.now();
    const viewModel = buildSessionsDialogViewModel({
      deleteArmed: false,
      h: 20,
      loading: false,
      modelValue: true,
      selectedIndex: 0,
      sessions: [
        {
          cwd: "/a/b/c/d",
          id: "this-id-should-not-be-visible",
          lastMessage: "last",
          messageCount: 1,
          summary: "hello",
          title: "t",
          updatedAt: now - 1000 * 60 * 60,
        },
      ],
      w: 90,
    });

    expect(viewModel.options[0]?.label).toContain("…/b/c/d");
    expect(viewModel.options[0]?.label).toContain("t");
    expect(viewModel.options[0]?.label).not.toContain("this-id-should-not-be-visible");
    expect(viewModel.options[0]?.detail).not.toContain(":");
  });

  it("adds status and queue badges for quick session scanning", () => {
    const now = Date.now();
    const viewModel = buildSessionsDialogViewModel({
      deleteArmed: false,
      h: 20,
      loading: false,
      modelValue: true,
      selectedIndex: 0,
      sessions: [
        {
          cwd: "/repo/app",
          id: "active",
          lastMessage: "queued task",
          lastUpdatedAt: now,
          messageCount: 2,
          queueLength: 3,
          status: "running",
          summary: "queued task",
          title: "Queued work",
          updatedAt: now - 1000 * 60,
        },
      ],
      w: 90,
    });

    expect(viewModel.options[0]?.label).toContain("⟳");
    expect(viewModel.options[0]?.label).toContain("[3]");
  });

  it("keeps cwd tail and relative time formatting aligned with DimSdk", () => {
    expect(formatCwdTail("/Users/Simon/Github/DimSdk", 3)).toBe("…/Simon/Github/DimSdk");
    expect(formatCwdTail("C:\\Users\\me\\proj\\deep", 3)).toBe("C:/…/me/proj/deep");

    const now = 1_700_000_000_000;
    const formatted = formatRelativeTime(now - 1000 * 60 * 60 * 24, now);
    expect(formatted).toBeTruthy();
    expect(formatted).not.toContain(":");
  });
});
