|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + MetadataEventType, |
| 4 | + DataEventType, |
| 5 | + MetadataEventSchema, |
| 6 | + DataEventSchema, |
| 7 | +} from './events.zod'; |
| 8 | + |
| 9 | +// Coverage added with the v17 dual-source cleanup (#4587): ./api is now the |
| 10 | +// SOLE owner of the bare names MetadataEvent(Schema). This vocabulary is the |
| 11 | +// live realtime contract — `MetadataManager` publishes |
| 12 | +// `metadata.{type}.{created|deleted}` events and the client SDK |
| 13 | +// (`@objectstack/client` subscribeMetadata / `@objectstack/client-react`) |
| 14 | +// subscribes against exactly these names. The kernel-side lifecycle envelope |
| 15 | +// (`metadata.registered/…`) that used to share the names was removed — it had |
| 16 | +// no producer and no consumer. |
| 17 | + |
| 18 | +describe('MetadataEventType', () => { |
| 19 | + it('follows the metadata.{type}.{created|updated|deleted} pattern for every member', () => { |
| 20 | + for (const value of MetadataEventType.options) { |
| 21 | + expect(value).toMatch(/^metadata\.[a-z]+\.(created|updated|deleted)$/); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + it('covers the created/updated/deleted triple for each covered metadata type', () => { |
| 26 | + const byType = new Map<string, string[]>(); |
| 27 | + for (const value of MetadataEventType.options) { |
| 28 | + const [, metadataType, action] = value.split('.'); |
| 29 | + const actions = byType.get(metadataType) ?? []; |
| 30 | + actions.push(action); |
| 31 | + byType.set(metadataType, actions); |
| 32 | + } |
| 33 | + for (const [metadataType, actions] of byType) { |
| 34 | + expect(actions, `actions of metadata.${metadataType}.*`).toEqual([ |
| 35 | + 'created', |
| 36 | + 'updated', |
| 37 | + 'deleted', |
| 38 | + ]); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it('includes the event names the client SDK subscribes to', () => { |
| 43 | + // `RealtimeAPI.subscribeMetadata(type)` filters on exactly these three. |
| 44 | + for (const name of [ |
| 45 | + 'metadata.object.created', |
| 46 | + 'metadata.object.updated', |
| 47 | + 'metadata.object.deleted', |
| 48 | + ]) { |
| 49 | + expect(MetadataEventType.options).toContain(name); |
| 50 | + } |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('MetadataEventSchema', () => { |
| 55 | + const base = { |
| 56 | + id: '4b4720e8-97c3-4a12-9b70-b70a3d2314a1', |
| 57 | + type: 'metadata.object.created', |
| 58 | + metadataType: 'object', |
| 59 | + name: 'account', |
| 60 | + timestamp: new Date().toISOString(), |
| 61 | + }; |
| 62 | + |
| 63 | + it('validates a minimal realtime metadata event', () => { |
| 64 | + const event = MetadataEventSchema.parse(base); |
| 65 | + expect(event.type).toBe('metadata.object.created'); |
| 66 | + expect(event.name).toBe('account'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('accepts the optional payload fields', () => { |
| 70 | + const event = MetadataEventSchema.parse({ |
| 71 | + ...base, |
| 72 | + packageId: 'com.acme.crm', |
| 73 | + definition: { label: 'Account' }, |
| 74 | + userId: 'usr_1', |
| 75 | + }); |
| 76 | + expect(event.packageId).toBe('com.acme.crm'); |
| 77 | + expect(event.userId).toBe('usr_1'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('rejects event types outside the vocabulary', () => { |
| 81 | + expect(() => MetadataEventSchema.parse({ |
| 82 | + ...base, |
| 83 | + // The retired kernel-side lifecycle vocabulary must NOT parse here — |
| 84 | + // it was never produced by anything and is not part of this contract. |
| 85 | + type: 'metadata.registered', |
| 86 | + })).toThrow(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('rejects a non-uuid id and a non-datetime timestamp', () => { |
| 90 | + expect(() => MetadataEventSchema.parse({ ...base, id: 'evt-1' })).toThrow(); |
| 91 | + expect(() => MetadataEventSchema.parse({ ...base, timestamp: 'yesterday' })).toThrow(); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe('DataEventSchema', () => { |
| 96 | + it('validates a data record event', () => { |
| 97 | + const event = DataEventSchema.parse({ |
| 98 | + id: '4b4720e8-97c3-4a12-9b70-b70a3d2314a2', |
| 99 | + type: 'data.record.updated', |
| 100 | + object: 'account', |
| 101 | + recordId: 'rec_1', |
| 102 | + changes: { name: 'New Name' }, |
| 103 | + timestamp: new Date().toISOString(), |
| 104 | + }); |
| 105 | + expect(event.object).toBe('account'); |
| 106 | + expect(event.recordId).toBe('rec_1'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('rejects types outside the DataEventType vocabulary', () => { |
| 110 | + expect(DataEventType.options).toEqual([ |
| 111 | + 'data.record.created', |
| 112 | + 'data.record.updated', |
| 113 | + 'data.record.deleted', |
| 114 | + 'data.field.changed', |
| 115 | + ]); |
| 116 | + expect(() => DataEventSchema.parse({ |
| 117 | + id: '4b4720e8-97c3-4a12-9b70-b70a3d2314a3', |
| 118 | + type: 'data.record.upserted', |
| 119 | + object: 'account', |
| 120 | + recordId: 'rec_1', |
| 121 | + timestamp: new Date().toISOString(), |
| 122 | + })).toThrow(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments