import test from 'node:test';
import assert from 'node:assert/strict';
import { extractChatMessageParts, parseChatMessageParts } from './chat-message-parts.js';

test('extractChatMessageParts normalizes absolute legacy dot-codex prompt preview urls to api chat resource urls', () => {
  const input = [
    '문서 미리보기',
    '[[prompt:{"title":"확인","options":[{"label":"legacy","value":"legacy","preview":{"type":"resource","url":"https://preview.sm-home.cloud/public/.codex_chat/chat-room/resource/source/chat-room-reference.md"}}]}]]',
  ].join('\n');

  const parsed = extractChatMessageParts(input);
  const prompt = parsed.parts.find((part): part is Extract<(typeof parsed.parts)[number], { type: 'prompt' }> => part.type === 'prompt');

  assert.ok(prompt);
  assert.equal(
    prompt.options[0]?.preview?.url,
    '/api/chat/resources/.codex_chat/chat-room/resource/source/chat-room-reference.md',
  );
});

test('parseChatMessageParts normalizes absolute legacy link card urls to api chat resource urls', () => {
  const parsed = parseChatMessageParts([
    {
      type: 'link_card',
      title: 'legacy resource',
      url: 'https://preview.sm-home.cloud/.codex_chat/chat-room/resource/uploads/spec.png',
      actionLabel: '열기',
    },
  ]);

  assert.deepEqual(parsed, [
    {
      type: 'link_card',
      title: 'legacy resource',
      url: '/api/chat/resources/.codex_chat/chat-room/resource/uploads/spec.png',
      actionLabel: '열기',
    },
  ]);
});
