|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #4626 — subscribeData delivers TRUE `DataEvent`s, validated at the boundary |
| 5 | + * (the data-side twin of #4602's metadata pins). |
| 6 | + * |
| 7 | + * The callback is typed `(event: DataEvent) => void` — top-level `id` (uuid), |
| 8 | + * `type`, `object`, `recordId` (required), `changes?`, `before?`, `after?`, |
| 9 | + * `userId?`, `timestamp`. Before this fix the handler delivered the raw |
| 10 | + * `RealtimeEventPayload` envelope via `callback(event as any as DataEvent)`, |
| 11 | + * so `event.recordId` / `event.changes` / `event.id` were `undefined` at |
| 12 | + * runtime while the types said otherwise. |
| 13 | + * |
| 14 | + * Pins: |
| 15 | + * - the subscriber receives the top-level fields (fails on the pre-fix |
| 16 | + * envelope passthrough); |
| 17 | + * - an off-contract payload — including the pre-fix producer's |
| 18 | + * `{ recordId, after }` shape — is rejected LOUDLY: callback never invoked, |
| 19 | + * error surfaced, nothing coerced; |
| 20 | + * - the `recordId` filter narrows on the FULFILLED event. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 24 | +import type { RealtimeEventPayload } from '@objectstack/spec/contracts'; |
| 25 | +import { RealtimeAPI } from './realtime-api'; |
| 26 | + |
| 27 | +const VALID_EVENT = { |
| 28 | + id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479', |
| 29 | + type: 'data.record.updated', |
| 30 | + object: 'project_task', |
| 31 | + recordId: 'task_1', |
| 32 | + changes: { status: 'done' }, |
| 33 | + after: { id: 'task_1', title: 'Ship it', status: 'done' }, |
| 34 | + userId: 'usr_123', |
| 35 | + timestamp: '2026-08-02T12:00:00.000Z', |
| 36 | +} as const; |
| 37 | + |
| 38 | +function envelopeOf( |
| 39 | + payload: Record<string, unknown>, |
| 40 | + type = 'data.record.updated', |
| 41 | + object = 'project_task', |
| 42 | +): RealtimeEventPayload { |
| 43 | + return { type, object, payload, timestamp: '2026-08-02T12:00:00.000Z' }; |
| 44 | +} |
| 45 | + |
| 46 | +describe('#4626 — RealtimeAPI.subscribeData contract boundary', () => { |
| 47 | + let api: RealtimeAPI; |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + vi.useFakeTimers(); |
| 51 | + api = new RealtimeAPI('http://localhost:3000'); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + api.disconnect(); |
| 56 | + vi.useRealTimers(); |
| 57 | + vi.restoreAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + function deliver(envelope: RealtimeEventPayload): void { |
| 61 | + api._bufferEvent(envelope); |
| 62 | + vi.advanceTimersByTime(2000); // poll interval drains the buffer |
| 63 | + } |
| 64 | + |
| 65 | + it('delivers the DataEvent with top-level fields to the callback', () => { |
| 66 | + const seen: unknown[] = []; |
| 67 | + api.subscribeData('project_task', (event) => seen.push(event)); |
| 68 | + |
| 69 | + deliver(envelopeOf({ ...VALID_EVENT })); |
| 70 | + |
| 71 | + expect(seen).toHaveLength(1); |
| 72 | + const event = seen[0] as typeof VALID_EVENT; |
| 73 | + // Top-level, as the type declares — NOT nested under `payload`. |
| 74 | + expect(event.id).toBe(VALID_EVENT.id); |
| 75 | + expect(event.type).toBe('data.record.updated'); |
| 76 | + expect(event.object).toBe('project_task'); |
| 77 | + expect(event.recordId).toBe('task_1'); |
| 78 | + expect(event.changes).toEqual({ status: 'done' }); |
| 79 | + expect(event.after).toEqual({ id: 'task_1', title: 'Ship it', status: 'done' }); |
| 80 | + expect(event.userId).toBe('usr_123'); |
| 81 | + expect(event.timestamp).toBe(VALID_EVENT.timestamp); |
| 82 | + }); |
| 83 | + |
| 84 | + it('rejects the pre-fix producer shape LOUDLY instead of passing it through', () => { |
| 85 | + // The old engine payload: `{ recordId, after }` with no id/type/object/ |
| 86 | + // timestamp. The envelope carried those — which is precisely why the |
| 87 | + // double-cast compiled and lied. |
| 88 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); |
| 89 | + const callback = vi.fn(); |
| 90 | + api.subscribeData('project_task', callback); |
| 91 | + |
| 92 | + deliver(envelopeOf({ recordId: 'task_1', after: { id: 'task_1', title: 'Ship it' } })); |
| 93 | + |
| 94 | + expect(callback).not.toHaveBeenCalled(); |
| 95 | + expect(errorSpy).toHaveBeenCalled(); |
| 96 | + const logged = String(errorSpy.mock.calls.map((c) => c.join(' ')).join('\n')); |
| 97 | + expect(logged).toContain('realtime event handler'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('rejects a payload with a wrong field type instead of coercing it', () => { |
| 101 | + const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined); |
| 102 | + const callback = vi.fn(); |
| 103 | + api.subscribeData('project_task', callback); |
| 104 | + |
| 105 | + deliver(envelopeOf({ ...VALID_EVENT, id: 'not-a-uuid' })); |
| 106 | + expect(callback).not.toHaveBeenCalled(); |
| 107 | + |
| 108 | + // A numeric recordId is a producer bug, not something to String() here. |
| 109 | + deliver(envelopeOf({ ...VALID_EVENT, recordId: 42 })); |
| 110 | + expect(callback).not.toHaveBeenCalled(); |
| 111 | + expect(errorSpy).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('filters by recordId on the fulfilled event', () => { |
| 115 | + const callback = vi.fn(); |
| 116 | + api.subscribeData('project_task', callback, { recordId: 'task_2' }); |
| 117 | + |
| 118 | + deliver(envelopeOf({ ...VALID_EVENT })); |
| 119 | + expect(callback).not.toHaveBeenCalled(); |
| 120 | + |
| 121 | + deliver(envelopeOf({ ...VALID_EVENT, recordId: 'task_2', after: { id: 'task_2' } })); |
| 122 | + expect(callback).toHaveBeenCalledTimes(1); |
| 123 | + expect(callback.mock.calls[0][0].recordId).toBe('task_2'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('ignores events for another object', () => { |
| 127 | + const callback = vi.fn(); |
| 128 | + api.subscribeData('project_task', callback); |
| 129 | + |
| 130 | + deliver(envelopeOf( |
| 131 | + { ...VALID_EVENT, object: 'account', recordId: 'acc_1' }, |
| 132 | + 'data.record.updated', |
| 133 | + 'account', |
| 134 | + )); |
| 135 | + |
| 136 | + expect(callback).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('delivers created and deleted events too', () => { |
| 140 | + const seen: any[] = []; |
| 141 | + api.subscribeData('project_task', (event) => seen.push(event)); |
| 142 | + |
| 143 | + deliver(envelopeOf({ |
| 144 | + id: '9f8b0f6e-1c2d-4a3b-8c9d-0e1f2a3b4c5d', |
| 145 | + type: 'data.record.created', |
| 146 | + object: 'project_task', |
| 147 | + recordId: 'task_9', |
| 148 | + after: { id: 'task_9', title: 'New' }, |
| 149 | + timestamp: '2026-08-02T12:00:01.000Z', |
| 150 | + }, 'data.record.created')); |
| 151 | + |
| 152 | + deliver(envelopeOf({ |
| 153 | + id: '1b2c3d4e-5f60-4718-9a2b-3c4d5e6f7081', |
| 154 | + type: 'data.record.deleted', |
| 155 | + object: 'project_task', |
| 156 | + recordId: 'task_9', |
| 157 | + timestamp: '2026-08-02T12:00:02.000Z', |
| 158 | + }, 'data.record.deleted')); |
| 159 | + |
| 160 | + expect(seen.map((e) => e.type)).toEqual(['data.record.created', 'data.record.deleted']); |
| 161 | + expect(seen.map((e) => e.recordId)).toEqual(['task_9', 'task_9']); |
| 162 | + expect(seen[1].after).toBeUndefined(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('unsubscribe stops delivery', () => { |
| 166 | + const callback = vi.fn(); |
| 167 | + const off = api.subscribeData('project_task', callback); |
| 168 | + |
| 169 | + deliver(envelopeOf({ ...VALID_EVENT })); |
| 170 | + expect(callback).toHaveBeenCalledTimes(1); |
| 171 | + |
| 172 | + off(); |
| 173 | + deliver(envelopeOf({ ...VALID_EVENT })); |
| 174 | + expect(callback).toHaveBeenCalledTimes(1); |
| 175 | + }); |
| 176 | +}); |
0 commit comments