atagen

malice

public
Star0
Watch0
81e91c6568a294819a15f19b1e5d9c51639610d6
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
web/test/coalesce.test.tsTypescript71 lines3.0 KB
1
import assert from 'node:assert/strict';
2
import { test } from 'node:test';
3
import { EventCoalescer, replacementKey } from '../src/worker/coalesce.ts';
4
5
function setup() {
6
  const delivered: string[] = [];
7
  const queue: Array<() => void> = [];
8
  const coalescer = new EventCoalescer(
9
    (account, event) => delivered.push(`${account}|${event}`),
10
    (flush) => queue.push(flush)
11
  );
12
  return { delivered, coalescer, run: () => queue.splice(0).forEach((flush) => flush()), queue };
13
}
14
15
const timeline = (room: string, n: number) => `{"type":"timeline","room_id":"${room}","items":[${n}]}`;
16
17
test('keys cover full-state events only', () => {
18
  assert.equal(replacementKey(timeline('!a', 1)), 'timeline:"!a"');
19
  assert.equal(replacementKey('{"type":"rooms","rooms":[]}'), 'rooms');
20
  assert.equal(replacementKey('{"type":"thread","room_id":"!a","root_event_id":"$r","items":[]}'), 'thread:"!a":"$r"');
21
  assert.equal(replacementKey('{"type":"thread_list","room_id":"!a","entries":[]}'), 'thread_list:"!a"');
22
  assert.equal(replacementKey('{"type":"pinned","room_id":"!a","items":[]}'), 'pinned:"!a"');
23
  assert.equal(replacementKey('{"type":"thread_closed","room_id":"!a","root_event_id":"$r"}'), null);
24
  assert.equal(replacementKey('{"type":"thread_list_history","room_id":"!a","state":"Idle"}'), null);
25
  assert.equal(replacementKey('{"type":"timeline_closed","room_id":"!a"}'), null);
26
  assert.equal(replacementKey('{"type":"snapshot"}'), null);
27
  assert.equal(replacementKey('{"type":"connection","state":"online"}'), null);
28
});
29
30
test('a burst for one key delivers only the latest, once, on the scheduled flush', () => {
31
  const { delivered, coalescer, run, queue } = setup();
32
  coalescer.push('acc', timeline('!a', 1));
33
  coalescer.push('acc', timeline('!a', 2));
34
  coalescer.push('acc', timeline('!a', 3));
35
  assert.equal(queue.length, 1);
36
  assert.deepEqual(delivered, []);
37
  run();
38
  assert.deepEqual(delivered, [`acc|${timeline('!a', 3)}`]);
39
});
40
41
test('other events flush pending ones first and keep their order', () => {
42
  const { delivered, coalescer, run } = setup();
43
  coalescer.push('acc', timeline('!a', 1));
44
  coalescer.push('acc', timeline('!b', 1));
45
  coalescer.push('acc', '{"type":"timeline_closed","room_id":"!a"}');
46
  coalescer.push('acc', timeline('!a', 2));
47
  run();
48
  assert.deepEqual(delivered, [
49
    `acc|${timeline('!a', 1)}`,
50
    `acc|${timeline('!b', 1)}`,
51
    'acc|{"type":"timeline_closed","room_id":"!a"}',
52
    `acc|${timeline('!a', 2)}`
53
  ]);
54
});
55
56
test('different keys and accounts are kept apart', () => {
57
  const { delivered, coalescer, run } = setup();
58
  coalescer.push('x', timeline('!a', 1));
59
  coalescer.push('y', timeline('!a', 2));
60
  coalescer.push('x', timeline('!a', 3));
61
  run();
62
  assert.deepEqual(delivered, [`y|${timeline('!a', 2)}`, `x|${timeline('!a', 3)}`]);
63
});
64
65
test('a flush with nothing pending after an explicit flush delivers nothing twice', () => {
66
  const { delivered, coalescer, run } = setup();
67
  coalescer.push('acc', timeline('!a', 1));
68
  coalescer.flush();
69
  run();
70
  assert.equal(delivered.length, 1);
71
});