import test from "node:test";
import assert from "node:assert/strict";
import { coalesceEvents } from "./event-stream.js";

test("combines consecutive assistant deltas into one event", () => {
  const events = [
    event("session.output", "Hello", "assistant_delta"),
    event("session.output", ", nice", "assistant_delta"),
    event("session.output", " to meet you.", "assistant_delta")
  ];

  const result = coalesceEvents(events);

  assert.equal(result.length, 1);
  assert.equal(result[0].data.text, "Hello, nice to meet you.");
});

test("keeps status output separate from assistant text", () => {
  const events = [
    event("session.output", "Hello.", "assistant_delta"),
    event("session.output", "Codex turn completed.", "status")
  ];

  const result = coalesceEvents(events);

  assert.equal(result.length, 2);
});

function event(name, text, kind) {
  return {
    id: crypto.randomUUID(),
    event: name,
    data: { text, kind },
    createdAt: new Date().toISOString()
  };
}
