import test from "node:test";
import assert from "node:assert/strict";
import { createNewChatMessagePreview, reconcileNewChatMessagePreview } from "./new-chat-transition.js";
import { projectConversation } from "../../conversation.js";

test("new chat message preview is immediately renderable without exposing local files", () => {
  const preview = createNewChatMessagePreview("  Start now  ", [{ name: "secret-local-name.png" }], {
    id: "optimistic-new-chat",
    createdAt: "2026-07-17T00:00:00.000Z"
  });

  assert.deepEqual(preview, {
    id: "optimistic-new-chat",
    event: "message.accepted",
    data: {
      text: "Start now",
      attachmentCount: 1,
      optimistic: true
    },
    createdAt: "2026-07-17T00:00:00.000Z"
  });
  assert.doesNotMatch(JSON.stringify(preview), /secret-local-name/);
});

test("attachment-only new chats still produce a visible optimistic message", () => {
  const preview = createNewChatMessagePreview("", [{}, {}], {
    id: "optimistic-attachments",
    createdAt: "2026-07-17T00:00:00.000Z"
  });

  assert.equal(preview.data.text, "");
  assert.equal(preview.data.attachmentCount, 2);
  assert.equal(projectConversation([preview])[0].userText, "2 images attached");
  assert.equal(projectConversation([preview])[0].status, "sending");
});

test("accepted new chat preview uses the successful partial upload count", () => {
  const preview = createNewChatMessagePreview("Start", [{}, {}], {
    id: "optimistic-partial-upload",
    createdAt: "2026-07-17T00:00:00.000Z"
  });

  const accepted = reconcileNewChatMessagePreview(preview, 1);

  assert.equal(accepted.data.attachmentCount, 1);
  assert.equal(preview.data.attachmentCount, 2);
  assert.equal(accepted.id, preview.id);
  assert.equal(accepted.createdAt, preview.createdAt);
});
