-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbroadcaster.test.ts
More file actions
89 lines (72 loc) · 2.18 KB
/
Copy pathbroadcaster.test.ts
File metadata and controls
89 lines (72 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { appendEvent, __resetActivityEventLog } from './log';
import { broadcast, subscribe } from './broadcaster';
import type { ActivityEvent } from './types';
beforeEach(() => {
__resetActivityEventLog();
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('broadcaster', () => {
it('delivers manually broadcast events to subscribers', () => {
const handler = vi.fn();
const unsubscribe = subscribe(handler);
const event: ActivityEvent = {
seq: 99,
at: '2026-01-01T00:00:00.000Z',
kind: 'partner_webhook_received',
actor: { kind: 'partner', slug: 'demo' },
offer_id: 'offer-1',
event_name: 'attribution_verified',
idempotency_key: 'key-1',
};
broadcast(event);
expect(handler).toHaveBeenCalledTimes(1);
expect(handler.mock.calls[0][0]).toEqual(event);
unsubscribe();
});
it('stops delivering to unsubscribed handlers', () => {
const handler = vi.fn();
const unsubscribe = subscribe(handler);
unsubscribe();
broadcast({
seq: 1,
at: '2026-01-01T00:00:00.000Z',
kind: 'offer_deleted',
actor: { kind: 'system' },
offer_id: 'x',
});
expect(handler).not.toHaveBeenCalled();
});
it('fans out appendEvent calls through the broadcaster', () => {
const handler = vi.fn();
const unsubscribe = subscribe(handler);
appendEvent({
kind: 'offer_deleted',
actor: { kind: 'system' },
offer_id: 'o-fanout',
});
expect(handler).toHaveBeenCalledTimes(1);
const received = handler.mock.calls[0][0] as ActivityEvent;
expect(received.kind).toBe('offer_deleted');
expect(received.offer_id).toBe('o-fanout');
expect(received.seq).toBe(1);
unsubscribe();
});
it('supports multiple concurrent subscribers', () => {
const a = vi.fn();
const b = vi.fn();
const unsubA = subscribe(a);
const unsubB = subscribe(b);
appendEvent({
kind: 'offer_deleted',
actor: { kind: 'system' },
offer_id: 'o-two',
});
expect(a).toHaveBeenCalledTimes(1);
expect(b).toHaveBeenCalledTimes(1);
unsubA();
unsubB();
});
});