import assert from 'node:assert/strict';
import { test } from 'node:test';
import { EventCoalescer, replacementKey } from '../src/worker/coalesce.ts';

function setup() {
  const delivered: string[] = [];
  const queue: Array<() => void> = [];
  const coalescer = new EventCoalescer(
    (account, event) => delivered.push(`${account}|${event}`),
    (flush) => queue.push(flush)
  );
  return { delivered, coalescer, run: () => queue.splice(0).forEach((flush) => flush()), queue };
}

const timeline = (room: string, n: number) => `{"type":"timeline","room_id":"${room}","items":[${n}]}`;

test('keys cover full-state events only', () => {
  assert.equal(replacementKey(timeline('!a', 1)), 'timeline:"!a"');
  assert.equal(replacementKey('{"type":"rooms","rooms":[]}'), 'rooms');
  assert.equal(replacementKey('{"type":"thread","room_id":"!a","root_event_id":"$r","items":[]}'), 'thread:"!a":"$r"');
  assert.equal(replacementKey('{"type":"thread_list","room_id":"!a","entries":[]}'), 'thread_list:"!a"');
  assert.equal(replacementKey('{"type":"pinned","room_id":"!a","items":[]}'), 'pinned:"!a"');
  assert.equal(replacementKey('{"type":"thread_closed","room_id":"!a","root_event_id":"$r"}'), null);
  assert.equal(replacementKey('{"type":"thread_list_history","room_id":"!a","state":"Idle"}'), null);
  assert.equal(replacementKey('{"type":"timeline_closed","room_id":"!a"}'), null);
  assert.equal(replacementKey('{"type":"snapshot"}'), null);
  assert.equal(replacementKey('{"type":"connection","state":"online"}'), null);
});

test('a burst for one key delivers only the latest, once, on the scheduled flush', () => {
  const { delivered, coalescer, run, queue } = setup();
  coalescer.push('acc', timeline('!a', 1));
  coalescer.push('acc', timeline('!a', 2));
  coalescer.push('acc', timeline('!a', 3));
  assert.equal(queue.length, 1);
  assert.deepEqual(delivered, []);
  run();
  assert.deepEqual(delivered, [`acc|${timeline('!a', 3)}`]);
});

test('other events flush pending ones first and keep their order', () => {
  const { delivered, coalescer, run } = setup();
  coalescer.push('acc', timeline('!a', 1));
  coalescer.push('acc', timeline('!b', 1));
  coalescer.push('acc', '{"type":"timeline_closed","room_id":"!a"}');
  coalescer.push('acc', timeline('!a', 2));
  run();
  assert.deepEqual(delivered, [
    `acc|${timeline('!a', 1)}`,
    `acc|${timeline('!b', 1)}`,
    'acc|{"type":"timeline_closed","room_id":"!a"}',
    `acc|${timeline('!a', 2)}`
  ]);
});

test('different keys and accounts are kept apart', () => {
  const { delivered, coalescer, run } = setup();
  coalescer.push('x', timeline('!a', 1));
  coalescer.push('y', timeline('!a', 2));
  coalescer.push('x', timeline('!a', 3));
  run();
  assert.deepEqual(delivered, [`y|${timeline('!a', 2)}`, `x|${timeline('!a', 3)}`]);
});

test('a flush with nothing pending after an explicit flush delivers nothing twice', () => {
  const { delivered, coalescer, run } = setup();
  coalescer.push('acc', timeline('!a', 1));
  coalescer.flush();
  run();
  assert.equal(delivered.length, 1);
});
