|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #31473 — the SDK's data subscriptions ask for the event types the spec |
| 5 | + * declares, read from the spec, never from a hand list. |
| 6 | + * |
| 7 | + * `subscribeData` hand-listed created / updated / deleted, so when #31220 |
| 8 | + * added `data.record.restored` to `DataEventType` (a row restored from the |
| 9 | + * recycle bin, published by ObjectQL's restore), the subscription filtered it |
| 10 | + * out and a subscriber's live list never saw the row come back. |
| 11 | + * |
| 12 | + * - ⭐ a `data.record.restored` event reaches the callback, and a list the |
| 13 | + * callback keeps shows the restored row again; |
| 14 | + * - ⭐ every member of the spec's `DataEventType` reaches `subscribeData`, |
| 15 | + * and every member of `BulkDataEventType` reaches `subscribeBulkData` |
| 16 | + * (read from the enums, so a type added later is covered without an edit |
| 17 | + * here); |
| 18 | + * - ⭐ `realtime-api.ts` spells no data event type as a string literal: the |
| 19 | + * lists come from the spec enums. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 23 | +import { readFileSync } from 'node:fs'; |
| 24 | +import { dirname, join } from 'node:path'; |
| 25 | +import { fileURLToPath } from 'node:url'; |
| 26 | +import type { RealtimeEventPayload } from '@objectstack/spec/contracts'; |
| 27 | +import { BulkDataEventType, DataEventType, type DataEvent } from '@objectstack/spec/api'; |
| 28 | +import { RealtimeAPI } from './realtime-api'; |
| 29 | + |
| 30 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 31 | +const AT = '2026-11-08T12:00:00.000Z'; |
| 32 | + |
| 33 | +function recordEvent(type: string, recordId: string, after?: Record<string, unknown>): RealtimeEventPayload { |
| 34 | + const payload = { |
| 35 | + id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479', |
| 36 | + type, |
| 37 | + object: 'project_task', |
| 38 | + recordId, |
| 39 | + ...(after ? { after } : {}), |
| 40 | + timestamp: AT, |
| 41 | + }; |
| 42 | + return { type, object: 'project_task', payload, timestamp: AT }; |
| 43 | +} |
| 44 | + |
| 45 | +function bulkEvent(type: string): RealtimeEventPayload { |
| 46 | + const payload = { id: '3f2504e0-4f89-41d3-9a0c-0305e82c3301', type, object: 'project_task', matched: 3, timestamp: AT }; |
| 47 | + return { type, object: 'project_task', payload, timestamp: AT }; |
| 48 | +} |
| 49 | + |
| 50 | +describe('#31473 — the data subscriptions follow the spec event enums', () => { |
| 51 | + let api: RealtimeAPI; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + vi.useFakeTimers(); |
| 55 | + api = new RealtimeAPI('http://localhost:3000'); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + api.disconnect(); |
| 60 | + vi.useRealTimers(); |
| 61 | + vi.restoreAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + function deliver(envelope: RealtimeEventPayload): void { |
| 65 | + api._bufferEvent(envelope); |
| 66 | + vi.advanceTimersByTime(2000); // the poll interval drains the buffer |
| 67 | + } |
| 68 | + |
| 69 | + it('⭐ a row restored from the recycle bin reappears in the subscriber\'s list', () => { |
| 70 | + const rows = new Map<string, Record<string, unknown>>(); |
| 71 | + const apply = (event: DataEvent): void => { |
| 72 | + if (event.type === 'data.record.deleted') rows.delete(event.recordId); |
| 73 | + else if (event.after) rows.set(event.recordId, event.after); |
| 74 | + }; |
| 75 | + api.subscribeData('project_task', apply); |
| 76 | + |
| 77 | + deliver(recordEvent('data.record.created', 'task_1', { id: 'task_1', title: 'Ship it' })); |
| 78 | + deliver(recordEvent('data.record.deleted', 'task_1')); |
| 79 | + expect([...rows.keys()]).toEqual([]); |
| 80 | + |
| 81 | + deliver(recordEvent('data.record.restored', 'task_1', { id: 'task_1', title: 'Ship it' })); |
| 82 | + expect([...rows.keys()]).toEqual(['task_1']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('⭐ every DataEventType the spec declares reaches subscribeData', () => { |
| 86 | + const seen: string[] = []; |
| 87 | + api.subscribeData('project_task', (event) => seen.push(event.type)); |
| 88 | + |
| 89 | + for (const type of DataEventType.options) deliver(recordEvent(type, 'task_1')); |
| 90 | + |
| 91 | + expect(seen).toEqual([...DataEventType.options]); |
| 92 | + expect(seen).toContain('data.record.restored'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('⭐ every BulkDataEventType the spec declares reaches subscribeBulkData', () => { |
| 96 | + const seen: string[] = []; |
| 97 | + api.subscribeBulkData('project_task', (event) => seen.push(event.type)); |
| 98 | + |
| 99 | + for (const type of BulkDataEventType.options) deliver(bulkEvent(type)); |
| 100 | + |
| 101 | + expect(seen).toEqual([...BulkDataEventType.options]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('⭐ realtime-api.ts keeps no hand list of data event types', () => { |
| 105 | + const source = readFileSync(join(HERE, 'realtime-api.ts'), 'utf8'); |
| 106 | + expect(source).not.toMatch(/'data\.records?\.[a-z_]+'/); |
| 107 | + expect(source).toMatch(/DataEventType\.options/); |
| 108 | + expect(source).toMatch(/BulkDataEventType\.options/); |
| 109 | + }); |
| 110 | +}); |
0 commit comments