|
| 1 | +import { describe, vi, expect, beforeEach } from 'vitest'; |
| 2 | +import { SyncClientImplementation, SyncStreamConnectionMethod } from '@powersync/common'; |
| 3 | +import Logger from 'js-logger'; |
| 4 | +import { bucket, checkpoint, mockSyncServiceTest, nextStatus, stream, TestConnector } from './utils'; |
| 5 | + |
| 6 | +Logger.useDefaults({ defaultLevel: Logger.WARN }); |
| 7 | + |
| 8 | +describe('Sync streams', () => { |
| 9 | + const defaultOptions = { |
| 10 | + clientImplementation: SyncClientImplementation.RUST, |
| 11 | + connectionMethod: SyncStreamConnectionMethod.HTTP |
| 12 | + }; |
| 13 | + |
| 14 | + mockSyncServiceTest('can disable default streams', async ({ syncService }) => { |
| 15 | + const database = await syncService.createDatabase(); |
| 16 | + database.connect(new TestConnector(), { |
| 17 | + includeDefaultStreams: false, |
| 18 | + ...defaultOptions |
| 19 | + }); |
| 20 | + |
| 21 | + await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1)); |
| 22 | + expect(syncService.connectedListeners[0]).toMatchObject({ |
| 23 | + streams: { |
| 24 | + include_defaults: false, |
| 25 | + subscriptions: [] |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + mockSyncServiceTest('subscribes with streams', async ({ syncService }) => { |
| 31 | + const database = await syncService.createDatabase(); |
| 32 | + const a = await database.syncStream('stream', { foo: 'a' }).subscribe(); |
| 33 | + const b = await database.syncStream('stream', { foo: 'b' }).subscribe({ priority: 1 }); |
| 34 | + |
| 35 | + database.connect(new TestConnector(), defaultOptions); |
| 36 | + await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1)); |
| 37 | + |
| 38 | + expect(syncService.connectedListeners[0]).toMatchObject({ |
| 39 | + streams: { |
| 40 | + include_defaults: true, |
| 41 | + subscriptions: [ |
| 42 | + { |
| 43 | + stream: 'stream', |
| 44 | + parameters: { foo: 'a' }, |
| 45 | + override_priority: null |
| 46 | + }, |
| 47 | + { |
| 48 | + stream: 'stream', |
| 49 | + parameters: { foo: 'b' }, |
| 50 | + override_priority: 1 |
| 51 | + } |
| 52 | + ] |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + let statusPromise = nextStatus(database); |
| 57 | + syncService.pushLine( |
| 58 | + checkpoint({ |
| 59 | + last_op_id: 0, |
| 60 | + buckets: [ |
| 61 | + bucket('a', 0, { priority: 3, subscriptions: [{ sub: 0 }] }), |
| 62 | + bucket('b', 0, { priority: 1, subscriptions: [{ sub: 1 }] }) |
| 63 | + ], |
| 64 | + streams: [stream('stream', false)] |
| 65 | + }) |
| 66 | + ); |
| 67 | + let status = await statusPromise; |
| 68 | + for (const subscription of [a, b]) { |
| 69 | + expect(status.statusFor(subscription).subscription.active).toBeTruthy(); |
| 70 | + expect(status.statusFor(subscription).subscription.lastSyncedAt).toBeNull(); |
| 71 | + expect(status.statusFor(subscription).subscription.hasExplicitSubscription).toBeTruthy(); |
| 72 | + } |
| 73 | + |
| 74 | + statusPromise = nextStatus(database); |
| 75 | + syncService.pushLine({ partial_checkpoint_complete: { last_op_id: '0', priority: 1 } }); |
| 76 | + status = await statusPromise; |
| 77 | + expect(status.statusFor(a).subscription.lastSyncedAt).toBeNull(); |
| 78 | + expect(status.statusFor(b).subscription.lastSyncedAt).not.toBeNull(); |
| 79 | + await b.waitForFirstSync(); |
| 80 | + |
| 81 | + syncService.pushLine({ checkpoint_complete: { last_op_id: '0' } }); |
| 82 | + await a.waitForFirstSync(); |
| 83 | + }); |
| 84 | + |
| 85 | + mockSyncServiceTest('reports default streams', async ({ syncService }) => { |
| 86 | + const database = await syncService.createDatabase(); |
| 87 | + database.connect(new TestConnector(), defaultOptions); |
| 88 | + await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1)); |
| 89 | + |
| 90 | + let statusPromise = nextStatus(database); |
| 91 | + syncService.pushLine( |
| 92 | + checkpoint({ |
| 93 | + last_op_id: 0, |
| 94 | + buckets: [], |
| 95 | + streams: [stream('default_stream', true)] |
| 96 | + }) |
| 97 | + ); |
| 98 | + let status = await statusPromise; |
| 99 | + |
| 100 | + expect(status.subscriptions).toHaveLength(1); |
| 101 | + expect(status.subscriptions[0]).toMatchObject({ |
| 102 | + subscription: { |
| 103 | + name: 'default_stream', |
| 104 | + parameters: null, |
| 105 | + isDefault: true, |
| 106 | + hasExplicitSubscription: false |
| 107 | + } |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + mockSyncServiceTest('changes subscriptions dynamically', async ({ syncService }) => { |
| 112 | + const database = await syncService.createDatabase(); |
| 113 | + database.connect(new TestConnector(), defaultOptions); |
| 114 | + await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1)); |
| 115 | + |
| 116 | + syncService.pushLine( |
| 117 | + checkpoint({ |
| 118 | + last_op_id: 0, |
| 119 | + buckets: [] |
| 120 | + }) |
| 121 | + ); |
| 122 | + const subscription = await database.syncStream('a').subscribe(); |
| 123 | + |
| 124 | + await vi.waitFor(() => |
| 125 | + expect(syncService.connectedListeners[0]).toMatchObject({ |
| 126 | + streams: { |
| 127 | + include_defaults: true, |
| 128 | + subscriptions: [ |
| 129 | + { |
| 130 | + stream: 'a', |
| 131 | + parameters: null, |
| 132 | + override_priority: null |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + }) |
| 137 | + ); |
| 138 | + |
| 139 | + // Given that the subscription has a TTL, dropping the handle should not re-subscribe. |
| 140 | + subscription.unsubscribe(); |
| 141 | + await new Promise((r) => setTimeout(r, 100)); |
| 142 | + expect(syncService.connectedListeners[0].streams.subscriptions).toHaveLength(1); |
| 143 | + }); |
| 144 | + |
| 145 | + mockSyncServiceTest('subscriptions update while offline', async ({ syncService }) => { |
| 146 | + const database = await syncService.createDatabase(); |
| 147 | + |
| 148 | + let statusPromise = nextStatus(database); |
| 149 | + const subscription = await database.syncStream('foo').subscribe(); |
| 150 | + let status = await statusPromise; |
| 151 | + expect(status.statusFor(subscription)).not.toBeNull(); |
| 152 | + }); |
| 153 | +}); |
0 commit comments