|
| 1 | +import { |
| 2 | + LocalSubscriptionStore, |
| 3 | +} from '../../src/ws/local-subscription-store'; |
| 4 | + |
| 5 | +describe('LocalSubscriptionStore', () => { |
| 6 | + it('stores and retrieves subscription by id', async () => { |
| 7 | + const store = new LocalSubscriptionStore(); |
| 8 | + |
| 9 | + await store.subscribe('conn-1', 'sub-1', { |
| 10 | + message: { method: 'load' }, |
| 11 | + state: { foo: 'bar' } |
| 12 | + }); |
| 13 | + |
| 14 | + const subscription = await store.getSubscription('conn-1', 'sub-1'); |
| 15 | + |
| 16 | + expect(subscription).toBeDefined(); |
| 17 | + expect(subscription?.message).toEqual({ method: 'load' }); |
| 18 | + expect(subscription?.state).toEqual({ foo: 'bar' }); |
| 19 | + expect(subscription?.timestamp).toBeInstanceOf(Date); |
| 20 | + }); |
| 21 | + |
| 22 | + it('stores and retrieves subscription by string id', async () => { |
| 23 | + const store = new LocalSubscriptionStore(); |
| 24 | + |
| 25 | + await store.subscribe('conn-1', '123', { |
| 26 | + message: { method: 'load' }, |
| 27 | + state: { answer: true } |
| 28 | + }); |
| 29 | + |
| 30 | + const result = await store.getSubscription('conn-1', '123'); |
| 31 | + |
| 32 | + expect(result).toBeDefined(); |
| 33 | + expect(result?.state).toEqual({ answer: true }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not create a connection when reading missing subscription', async () => { |
| 37 | + const store = new LocalSubscriptionStore(); |
| 38 | + |
| 39 | + const missing = await store.getSubscription('unknown-conn', 'sub-1'); |
| 40 | + |
| 41 | + expect(missing).toBeUndefined(); |
| 42 | + // eslint-disable-next-line dot-notation |
| 43 | + expect(store['connections'].size).toBe(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not create a connection when unsubscribing unknown connection', async () => { |
| 47 | + const store = new LocalSubscriptionStore(); |
| 48 | + |
| 49 | + await store.unsubscribe('unknown-conn', 'sub-1'); |
| 50 | + |
| 51 | + // eslint-disable-next-line dot-notation |
| 52 | + expect(store['connections'].size).toBe(0); |
| 53 | + }); |
| 54 | + |
| 55 | + it('unsubscribes existing subscription', async () => { |
| 56 | + const store = new LocalSubscriptionStore(); |
| 57 | + |
| 58 | + await store.subscribe('conn-1', 'sub-1', { |
| 59 | + message: { method: 'load' }, |
| 60 | + state: {} |
| 61 | + }); |
| 62 | + |
| 63 | + await store.unsubscribe('conn-1', 'sub-1'); |
| 64 | + |
| 65 | + const subscription = await store.getSubscription('conn-1', 'sub-1'); |
| 66 | + expect(subscription).toBeUndefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns all active subscriptions with connectionId', async () => { |
| 70 | + const store = new LocalSubscriptionStore(); |
| 71 | + |
| 72 | + await store.subscribe('conn-1', 'sub-1', { |
| 73 | + message: { method: 'load' }, |
| 74 | + state: { a: 1 } |
| 75 | + }); |
| 76 | + await store.subscribe('conn-2', 'sub-2', { |
| 77 | + message: { method: 'subscribe' }, |
| 78 | + state: { b: 2 } |
| 79 | + }); |
| 80 | + |
| 81 | + const allSubscriptions = store.getAllSubscriptions(); |
| 82 | + |
| 83 | + expect(allSubscriptions).toHaveLength(2); |
| 84 | + expect(allSubscriptions).toEqual(expect.arrayContaining([ |
| 85 | + expect.objectContaining({ |
| 86 | + connectionId: 'conn-1', |
| 87 | + message: { method: 'load' }, |
| 88 | + state: { a: 1 } |
| 89 | + }), |
| 90 | + expect.objectContaining({ |
| 91 | + connectionId: 'conn-2', |
| 92 | + message: { method: 'subscribe' }, |
| 93 | + state: { b: 2 } |
| 94 | + }) |
| 95 | + ])); |
| 96 | + }); |
| 97 | + |
| 98 | + it('removes stale subscriptions during getAllSubscriptions', async () => { |
| 99 | + const store = new LocalSubscriptionStore({ heartBeatInterval: 1 }); |
| 100 | + |
| 101 | + await store.subscribe('conn-1', 'stale', { |
| 102 | + message: { method: 'load' }, |
| 103 | + state: {} |
| 104 | + }); |
| 105 | + await store.subscribe('conn-1', 'active', { |
| 106 | + message: { method: 'load' }, |
| 107 | + state: {} |
| 108 | + }); |
| 109 | + |
| 110 | + const staleSubscription = await store.getSubscription('conn-1', 'stale'); |
| 111 | + expect(staleSubscription).toBeDefined(); |
| 112 | + if (!staleSubscription) { |
| 113 | + throw new Error('Expected stale subscription to exist'); |
| 114 | + } |
| 115 | + staleSubscription.timestamp = new Date(Date.now() - 5000); |
| 116 | + |
| 117 | + const allSubscriptions = store.getAllSubscriptions(); |
| 118 | + |
| 119 | + expect(allSubscriptions).toHaveLength(1); |
| 120 | + expect(allSubscriptions[0].connectionId).toBe('conn-1'); |
| 121 | + expect(allSubscriptions[0].message).toEqual({ method: 'load' }); |
| 122 | + |
| 123 | + const staleAfterCleanup = await store.getSubscription('conn-1', 'stale'); |
| 124 | + expect(staleAfterCleanup).toBeUndefined(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('stores and retrieves auth context', async () => { |
| 128 | + const store = new LocalSubscriptionStore(); |
| 129 | + |
| 130 | + const authContext = { securityContext: { userId: 42 } }; |
| 131 | + await store.setAuthContext('conn-1', authContext); |
| 132 | + |
| 133 | + await expect(store.getAuthContext('conn-1')).resolves.toEqual(authContext); |
| 134 | + }); |
| 135 | + |
| 136 | + it('removes connection on disconnect', async () => { |
| 137 | + const store = new LocalSubscriptionStore(); |
| 138 | + |
| 139 | + await store.subscribe('conn-1', 'sub-1', { |
| 140 | + message: { method: 'load' }, |
| 141 | + state: {} |
| 142 | + }); |
| 143 | + |
| 144 | + await store.disconnect('conn-1'); |
| 145 | + |
| 146 | + // eslint-disable-next-line dot-notation |
| 147 | + expect(store['connections'].has('conn-1')).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it('clears all connections', async () => { |
| 151 | + const store = new LocalSubscriptionStore(); |
| 152 | + |
| 153 | + await store.subscribe('conn-1', 'sub-1', { |
| 154 | + message: { method: 'load' }, |
| 155 | + state: {} |
| 156 | + }); |
| 157 | + await store.subscribe('conn-2', 'sub-2', { |
| 158 | + message: { method: 'subscribe' }, |
| 159 | + state: {} |
| 160 | + }); |
| 161 | + |
| 162 | + store.clear(); |
| 163 | + |
| 164 | + // eslint-disable-next-line dot-notation |
| 165 | + expect(store['connections'].size).toBe(0); |
| 166 | + }); |
| 167 | +}); |
0 commit comments