import assert from 'node:assert/strict';
import test from 'node:test';
import { mergeMessageContent, mergeStreamingChunk } from './message-stream.mjs';

test('mergeStreamingChunk appends deltas', () => {
  assert.equal(mergeStreamingChunk('Hello', ' world'), 'Hello world');
});

test('mergeStreamingChunk accepts cumulative snapshots', () => {
  assert.equal(mergeStreamingChunk('Hello', 'Hello world'), 'Hello world');
});

test('mergeStreamingChunk ignores duplicate chunks', () => {
  assert.equal(mergeStreamingChunk('Hello world', 'world'), 'Hello world');
  assert.equal(mergeStreamingChunk('Done!!', 'Done!!'), 'Done!!');
});

test('mergeStreamingChunk merges overlapping chunks', () => {
  assert.equal(mergeStreamingChunk('Hello wo', 'orld'), 'Hello world');
});

test('mergeMessageContent merges thinking and text separately', () => {
  const first = [
    { type: 'thinking', thinking: 'Let' },
    { type: 'text', text: '搞定' },
  ];
  const second = [
    { type: 'thinking', thinking: 'Let me' },
    { type: 'text', text: '搞定！！' },
  ];
  const merged = mergeMessageContent(first, second);
  assert.equal(merged.length, 2);
  assert.equal(merged[0].thinking, 'Let me');
  assert.equal(merged[1].text, '搞定！！');
});

test('mergeMessageContent avoids duplicate multi-part appends', () => {
  let content = [];
  content = mergeMessageContent(content, [
    { type: 'thinking', thinking: 'Done!! ' },
    { type: 'text', text: '👉 ' },
  ]);
  content = mergeMessageContent(content, [
    { type: 'thinking', thinking: 'Done!! Let me' },
    { type: 'text', text: '👉 [欢迎' },
  ]);
  assert.equal(content.length, 2);
  assert.equal(content[0].thinking, 'Done!! Let me');
  assert.equal(content[1].text, '👉 [欢迎');
});
