atagen

malice

public
Star0
Watch0
06339d2de37a2adbd7c9697702da5360fad61c36
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: web/test/coalesce.test.ts

a802a95814e6atagen2026-10-26 03:48 UTC1
import assert from 'node:assert/strict';
a802a95814e6atagen2026-10-26 03:48 UTC2
import { test } from 'node:test';
a802a95814e6atagen2026-10-26 03:48 UTC3
import { EventCoalescer, replacementKey } from '../src/worker/coalesce.ts';
a802a95814e6atagen2026-10-26 03:48 UTC4
a802a95814e6atagen2026-10-26 03:48 UTC5
function setup() {
a802a95814e6atagen2026-10-26 03:48 UTC6
  const delivered: string[] = [];
a802a95814e6atagen2026-10-26 03:48 UTC7
  const queue: Array<() => void> = [];
a802a95814e6atagen2026-10-26 03:48 UTC8
  const coalescer = new EventCoalescer(
a802a95814e6atagen2026-10-26 03:48 UTC9
    (account, event) => delivered.push(`${account}|${event}`),
a802a95814e6atagen2026-10-26 03:48 UTC10
    (flush) => queue.push(flush)
a802a95814e6atagen2026-10-26 03:48 UTC11
  );
a802a95814e6atagen2026-10-26 03:48 UTC12
  return { delivered, coalescer, run: () => queue.splice(0).forEach((flush) => flush()), queue };
a802a95814e6atagen2026-10-26 03:48 UTC13
}
a802a95814e6atagen2026-10-26 03:48 UTC14
a802a95814e6atagen2026-10-26 03:48 UTC15
const timeline = (room: string, n: number) => `{"type":"timeline","room_id":"${room}","items":[${n}]}`;
a802a95814e6atagen2026-10-26 03:48 UTC16
a802a95814e6atagen2026-10-26 03:48 UTC17
test('keys cover full-state events only', () => {
a802a95814e6atagen2026-10-26 03:48 UTC18
  assert.equal(replacementKey(timeline('!a', 1)), 'timeline:"!a"');
a802a95814e6atagen2026-10-26 03:48 UTC19
  assert.equal(replacementKey('{"type":"rooms","rooms":[]}'), 'rooms');
a802a95814e6atagen2026-10-26 03:48 UTC20
  assert.equal(replacementKey('{"type":"thread","room_id":"!a","root_event_id":"$r","items":[]}'), 'thread:"!a":"$r"');
a802a95814e6atagen2026-10-26 03:48 UTC21
  assert.equal(replacementKey('{"type":"thread_list","room_id":"!a","entries":[]}'), 'thread_list:"!a"');
a802a95814e6atagen2026-10-26 03:48 UTC22
  assert.equal(replacementKey('{"type":"pinned","room_id":"!a","items":[]}'), 'pinned:"!a"');
a802a95814e6atagen2026-10-26 03:48 UTC23
  assert.equal(replacementKey('{"type":"thread_closed","room_id":"!a","root_event_id":"$r"}'), null);
a802a95814e6atagen2026-10-26 03:48 UTC24
  assert.equal(replacementKey('{"type":"thread_list_history","room_id":"!a","state":"Idle"}'), null);
a802a95814e6atagen2026-10-26 03:48 UTC25
  assert.equal(replacementKey('{"type":"timeline_closed","room_id":"!a"}'), null);
a802a95814e6atagen2026-10-26 03:48 UTC26
  assert.equal(replacementKey('{"type":"snapshot"}'), null);
a802a95814e6atagen2026-10-26 03:48 UTC27
  assert.equal(replacementKey('{"type":"connection","state":"online"}'), null);
a802a95814e6atagen2026-10-26 03:48 UTC28
});
a802a95814e6atagen2026-10-26 03:48 UTC29
a802a95814e6atagen2026-10-26 03:48 UTC30
test('a burst for one key delivers only the latest, once, on the scheduled flush', () => {
a802a95814e6atagen2026-10-26 03:48 UTC31
  const { delivered, coalescer, run, queue } = setup();
a802a95814e6atagen2026-10-26 03:48 UTC32
  coalescer.push('acc', timeline('!a', 1));
a802a95814e6atagen2026-10-26 03:48 UTC33
  coalescer.push('acc', timeline('!a', 2));
a802a95814e6atagen2026-10-26 03:48 UTC34
  coalescer.push('acc', timeline('!a', 3));
a802a95814e6atagen2026-10-26 03:48 UTC35
  assert.equal(queue.length, 1);
a802a95814e6atagen2026-10-26 03:48 UTC36
  assert.deepEqual(delivered, []);
a802a95814e6atagen2026-10-26 03:48 UTC37
  run();
a802a95814e6atagen2026-10-26 03:48 UTC38
  assert.deepEqual(delivered, [`acc|${timeline('!a', 3)}`]);
a802a95814e6atagen2026-10-26 03:48 UTC39
});
a802a95814e6atagen2026-10-26 03:48 UTC40
a802a95814e6atagen2026-10-26 03:48 UTC41
test('other events flush pending ones first and keep their order', () => {
a802a95814e6atagen2026-10-26 03:48 UTC42
  const { delivered, coalescer, run } = setup();
a802a95814e6atagen2026-10-26 03:48 UTC43
  coalescer.push('acc', timeline('!a', 1));
a802a95814e6atagen2026-10-26 03:48 UTC44
  coalescer.push('acc', timeline('!b', 1));
a802a95814e6atagen2026-10-26 03:48 UTC45
  coalescer.push('acc', '{"type":"timeline_closed","room_id":"!a"}');
a802a95814e6atagen2026-10-26 03:48 UTC46
  coalescer.push('acc', timeline('!a', 2));
a802a95814e6atagen2026-10-26 03:48 UTC47
  run();
a802a95814e6atagen2026-10-26 03:48 UTC48
  assert.deepEqual(delivered, [
a802a95814e6atagen2026-10-26 03:48 UTC49
    `acc|${timeline('!a', 1)}`,
a802a95814e6atagen2026-10-26 03:48 UTC50
    `acc|${timeline('!b', 1)}`,
a802a95814e6atagen2026-10-26 03:48 UTC51
    'acc|{"type":"timeline_closed","room_id":"!a"}',
a802a95814e6atagen2026-10-26 03:48 UTC52
    `acc|${timeline('!a', 2)}`
a802a95814e6atagen2026-10-26 03:48 UTC53
  ]);
a802a95814e6atagen2026-10-26 03:48 UTC54
});
a802a95814e6atagen2026-10-26 03:48 UTC55
a802a95814e6atagen2026-10-26 03:48 UTC56
test('different keys and accounts are kept apart', () => {
a802a95814e6atagen2026-10-26 03:48 UTC57
  const { delivered, coalescer, run } = setup();
a802a95814e6atagen2026-10-26 03:48 UTC58
  coalescer.push('x', timeline('!a', 1));
a802a95814e6atagen2026-10-26 03:48 UTC59
  coalescer.push('y', timeline('!a', 2));
a802a95814e6atagen2026-10-26 03:48 UTC60
  coalescer.push('x', timeline('!a', 3));
a802a95814e6atagen2026-10-26 03:48 UTC61
  run();
a802a95814e6atagen2026-10-26 03:48 UTC62
  assert.deepEqual(delivered, [`y|${timeline('!a', 2)}`, `x|${timeline('!a', 3)}`]);
a802a95814e6atagen2026-10-26 03:48 UTC63
});
a802a95814e6atagen2026-10-26 03:48 UTC64
a802a95814e6atagen2026-10-26 03:48 UTC65
test('a flush with nothing pending after an explicit flush delivers nothing twice', () => {
a802a95814e6atagen2026-10-26 03:48 UTC66
  const { delivered, coalescer, run } = setup();
a802a95814e6atagen2026-10-26 03:48 UTC67
  coalescer.push('acc', timeline('!a', 1));
a802a95814e6atagen2026-10-26 03:48 UTC68
  coalescer.flush();
a802a95814e6atagen2026-10-26 03:48 UTC69
  run();
a802a95814e6atagen2026-10-26 03:48 UTC70
  assert.equal(delivered.length, 1);
a802a95814e6atagen2026-10-26 03:48 UTC71
});