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

import type { ConversationRow } from "./conversation";
import { projectConversation } from "./conversation";

const row = (
  overrides: Partial<ConversationRow> &
    Pick<ConversationRow, "messageId" | "role" | "status">
): ConversationRow => ({
  attachments: [],
  error: null,
  rawText: "",
  ...overrides,
});

describe("projectConversation", () => {
  test("keeps queued assistant rows as status instead of duplicate messages", () => {
    const state = projectConversation([
      row({
        messageId: "user-1",
        rawText: "Build it",
        role: "user",
        status: "processing",
      }),
      row({ messageId: "assistant-1", role: "assistant", status: "queued" }),
    ]);
    expect(state.pending).toBe(true);
    expect(state.messages).toHaveLength(1);
  });

  test("projects completed Convex rows into renderable messages", () => {
    const state = projectConversation([
      row({
        messageId: "assistant-1",
        rawText: "Captured the request.",
        role: "assistant",
        status: "completed",
      }),
    ]);
    expect(state.messages[0]?.parts).toEqual([
      { state: "done", text: "Captured the request.", type: "text" },
    ]);
  });
});
