Skip to content

Commit ec765f5

Browse files
committed
fix(client): subscribeData asks for the data event types the spec declares
The SDK's data subscriptions read their event types from the spec's DataEventType and BulkDataEventType enums instead of hand lists, so a row restored from the recycle bin (data.record.restored) reaches subscribers. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5mbAyTfp7gbjowRA48
1 parent 2ae3e30 commit ec765f5

2 files changed

Lines changed: 119 additions & 6 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
});

‎packages/client/src/realtime-api.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
MetadataEventSchema,
1313
DataEventSchema,
1414
BulkDataEventSchema,
15+
DataEventType,
16+
BulkDataEventType,
1517
type MetadataEvent,
1618
type MetadataEventSubject,
1719
type MetadataEventType,
@@ -142,11 +144,11 @@ export class RealtimeAPI {
142144
filter: {
143145
type: object,
144146
recordId: options?.recordId,
145-
eventTypes: [
146-
'data.record.created',
147-
'data.record.updated',
148-
'data.record.deleted'
149-
]
147+
// [#31473] Read from the spec's enum, never hand-listed: a hand list
148+
// of created / updated / deleted silently dropped
149+
// `data.record.restored` when #31220 added it, so a row restored from
150+
// the recycle bin never reached the subscriber.
151+
eventTypes: [...DataEventType.options]
150152
},
151153
handler: (event) => {
152154
if (!event.type.startsWith('data.') || event.object !== object) return;
@@ -224,7 +226,8 @@ export class RealtimeAPI {
224226
this.subscriptions.set(subscriptionId, {
225227
filter: {
226228
type: object,
227-
eventTypes: ['data.records.updated', 'data.records.deleted']
229+
// [#31473] Read from the spec's enum, like subscribeData's list.
230+
eventTypes: [...BulkDataEventType.options]
228231
},
229232
handler: (event) => {
230233
if (!event.type.startsWith('data.records.') || event.object !== object) return;

0 commit comments

Comments
 (0)