import test from "node:test";
import assert from "node:assert/strict";
import { createConversationEventBatch } from "./conversation-event-batch.js";

test("conversation event batch flushes a burst in one update", () => {
  const scheduled = [];
  const flushed = [];
  const batch = createConversationEventBatch({
    flush: (entries) => flushed.push(entries),
    schedule: (callback) => { scheduled.push(callback); return scheduled.length; },
    cancel: () => {}
  });

  batch.push({ id: "one" });
  batch.push({ id: "two" });

  assert.equal(scheduled.length, 1);
  scheduled[0]();
  assert.deepEqual(flushed, [[{ id: "one" }, { id: "two" }]]);
});
test("clearing a conversation event batch discards stale session events", () => {
  const scheduled = [];
  const flushed = [];
  const cancelled = [];
  const batch = createConversationEventBatch({
    flush: (entries) => flushed.push(entries),
    schedule: (callback) => { scheduled.push(callback); return "frame-1"; },
    cancel: (handle) => cancelled.push(handle)
  });

  batch.push({ id: "old-session" });
  batch.clear();
  scheduled[0]();

  assert.deepEqual(cancelled, ["frame-1"]);
  assert.deepEqual(flushed, []);
});
