|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + PresenceStatus, |
| 4 | + RealtimeRecordAction, |
| 5 | + BasePresenceSchema, |
| 6 | + type BasePresence, |
| 7 | +} from './realtime-shared.zod'; |
| 8 | + |
| 9 | +describe('PresenceStatus (Shared)', () => { |
| 10 | + it('should accept valid presence statuses', () => { |
| 11 | + const statuses = ['online', 'away', 'busy', 'offline']; |
| 12 | + statuses.forEach(status => { |
| 13 | + expect(() => PresenceStatus.parse(status)).not.toThrow(); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should reject invalid presence statuses', () => { |
| 18 | + expect(() => PresenceStatus.parse('idle')).toThrow(); |
| 19 | + expect(() => PresenceStatus.parse('dnd')).toThrow(); |
| 20 | + expect(() => PresenceStatus.parse('')).toThrow(); |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('RealtimeRecordAction (Shared)', () => { |
| 25 | + it('should accept valid record actions', () => { |
| 26 | + const actions = ['created', 'updated', 'deleted']; |
| 27 | + actions.forEach(action => { |
| 28 | + expect(() => RealtimeRecordAction.parse(action)).not.toThrow(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should reject invalid record actions', () => { |
| 33 | + expect(() => RealtimeRecordAction.parse('inserted')).toThrow(); |
| 34 | + expect(() => RealtimeRecordAction.parse('modified')).toThrow(); |
| 35 | + expect(() => RealtimeRecordAction.parse('')).toThrow(); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe('BasePresenceSchema (Shared)', () => { |
| 40 | + it('should accept valid minimal presence', () => { |
| 41 | + const presence: BasePresence = { |
| 42 | + userId: 'user-123', |
| 43 | + status: 'online', |
| 44 | + lastSeen: '2024-01-15T10:30:00Z', |
| 45 | + }; |
| 46 | + |
| 47 | + const result = BasePresenceSchema.parse(presence); |
| 48 | + expect(result.userId).toBe('user-123'); |
| 49 | + expect(result.status).toBe('online'); |
| 50 | + expect(result.metadata).toBeUndefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should accept presence with metadata', () => { |
| 54 | + const presence = { |
| 55 | + userId: 'user-456', |
| 56 | + status: 'away', |
| 57 | + lastSeen: '2024-01-15T10:30:00Z', |
| 58 | + metadata: { |
| 59 | + currentPage: '/dashboard', |
| 60 | + customStatus: 'In a meeting', |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + const result = BasePresenceSchema.parse(presence); |
| 65 | + expect(result.metadata).toBeDefined(); |
| 66 | + expect(result.metadata?.currentPage).toBe('/dashboard'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should accept all presence statuses', () => { |
| 70 | + const statuses: Array<BasePresence['status']> = ['online', 'away', 'busy', 'offline']; |
| 71 | + |
| 72 | + statuses.forEach(status => { |
| 73 | + const presence = { |
| 74 | + userId: 'user-789', |
| 75 | + status, |
| 76 | + lastSeen: '2024-01-15T10:30:00Z', |
| 77 | + }; |
| 78 | + |
| 79 | + const parsed = BasePresenceSchema.parse(presence); |
| 80 | + expect(parsed.status).toBe(status); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should validate datetime format', () => { |
| 85 | + expect(() => BasePresenceSchema.parse({ |
| 86 | + userId: 'user-123', |
| 87 | + status: 'online', |
| 88 | + lastSeen: 'not-a-datetime', |
| 89 | + })).toThrow(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should reject presence without required fields', () => { |
| 93 | + expect(() => BasePresenceSchema.parse({ |
| 94 | + status: 'online', |
| 95 | + lastSeen: '2024-01-15T10:30:00Z', |
| 96 | + })).toThrow(); |
| 97 | + |
| 98 | + expect(() => BasePresenceSchema.parse({ |
| 99 | + userId: 'user-123', |
| 100 | + lastSeen: '2024-01-15T10:30:00Z', |
| 101 | + })).toThrow(); |
| 102 | + |
| 103 | + expect(() => BasePresenceSchema.parse({ |
| 104 | + userId: 'user-123', |
| 105 | + status: 'online', |
| 106 | + })).toThrow(); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('Cross-protocol consistency', () => { |
| 111 | + it('should ensure PresenceStatus is used by both realtime and websocket protocols', async () => { |
| 112 | + // Verify that both realtime.zod and websocket.zod use the shared PresenceStatus |
| 113 | + const realtime = await import('./realtime.zod'); |
| 114 | + const websocket = await import('./websocket.zod'); |
| 115 | + |
| 116 | + // Both should accept the same presence status values |
| 117 | + const testStatuses = ['online', 'away', 'busy', 'offline']; |
| 118 | + |
| 119 | + testStatuses.forEach(status => { |
| 120 | + expect(() => realtime.RealtimePresenceStatus.parse(status)).not.toThrow(); |
| 121 | + expect(() => websocket.WebSocketPresenceStatus.parse(status)).not.toThrow(); |
| 122 | + expect(() => PresenceStatus.parse(status)).not.toThrow(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should ensure RealtimePresenceSchema and BasePresenceSchema are compatible', () => { |
| 127 | + const testPresence = { |
| 128 | + userId: 'user-123', |
| 129 | + status: 'online' as const, |
| 130 | + lastSeen: '2024-01-15T10:30:00Z', |
| 131 | + metadata: { page: '/dashboard' }, |
| 132 | + }; |
| 133 | + |
| 134 | + // Both schemas should parse the same data |
| 135 | + const shared = BasePresenceSchema.parse(testPresence); |
| 136 | + expect(shared.userId).toBe('user-123'); |
| 137 | + expect(shared.status).toBe('online'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments