|
1 | | -import { Schema, Table, column } from '@powersync/common'; |
2 | | -import { WebPowerSyncOpenFactoryOptions } from '@powersync/web'; |
3 | 1 | import Logger from 'js-logger'; |
4 | | -import { v4 as uuid } from 'uuid'; |
5 | 2 | import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; |
6 | | -import { MockRemote, MockStreamOpenFactory, TestConnector } from './utils/MockStreamOpenFactory'; |
7 | | - |
8 | | -type UnwrapPromise<T> = T extends Promise<infer U> ? U : T; |
9 | | - |
10 | | -export type ConnectedDatabaseUtils = UnwrapPromise<ReturnType<typeof generateConnectedDatabase>>; |
11 | | -export type GenerateConnectedDatabaseOptions = { |
12 | | - powerSyncOptions: Partial<WebPowerSyncOpenFactoryOptions>; |
13 | | -}; |
14 | | - |
15 | | -// const UPLOAD_TIMEOUT_MS = 3000; |
16 | | -const UPLOAD_TIMEOUT_MS = 30_000; |
17 | | - |
18 | | -export const DEFAULT_CONNECTED_POWERSYNC_OPTIONS = { |
19 | | - powerSyncOptions: { |
20 | | - dbFilename: 'test-stream-connection.db', |
21 | | - flags: { |
22 | | - enableMultiTabs: false, |
23 | | - useWebWorker: true |
24 | | - }, |
25 | | - // Makes tests faster |
26 | | - crudUploadThrottleMs: 0, |
27 | | - schema: new Schema({ |
28 | | - users: new Table({ name: column.text }) |
29 | | - }) |
30 | | - } |
31 | | -}; |
32 | | - |
33 | | -export async function generateConnectedDatabase( |
34 | | - options: GenerateConnectedDatabaseOptions = DEFAULT_CONNECTED_POWERSYNC_OPTIONS |
35 | | -) { |
36 | | - const { powerSyncOptions } = options; |
37 | | - const { powerSyncOptions: defaultPowerSyncOptions } = DEFAULT_CONNECTED_POWERSYNC_OPTIONS; |
38 | | - /** |
39 | | - * Very basic implementation of a listener pattern. |
40 | | - * Required since we cannot extend multiple classes. |
41 | | - */ |
42 | | - const callbacks: Map<string, () => void> = new Map(); |
43 | | - const connector = new TestConnector(); |
44 | | - const uploadSpy = vi.spyOn(connector, 'uploadData'); |
45 | | - const remote = new MockRemote(connector, () => callbacks.forEach((c) => c())); |
46 | | - |
47 | | - const factory = new MockStreamOpenFactory( |
48 | | - { |
49 | | - ...defaultPowerSyncOptions, |
50 | | - ...powerSyncOptions, |
51 | | - flags: { |
52 | | - ...(defaultPowerSyncOptions.flags ?? {}), |
53 | | - ...(powerSyncOptions.flags ?? {}) |
54 | | - } |
55 | | - }, |
56 | | - remote |
57 | | - ); |
58 | | - const powersync = factory.getInstance(); |
59 | | - |
60 | | - const waitForStream = () => |
61 | | - new Promise<void>((resolve) => { |
62 | | - const id = uuid(); |
63 | | - callbacks.set(id, () => { |
64 | | - resolve(); |
65 | | - callbacks.delete(id); |
66 | | - }); |
67 | | - }); |
68 | | - |
69 | | - const connect = async () => { |
70 | | - const streamOpened = waitForStream(); |
71 | | - |
72 | | - const connectedPromise = powersync.connect(connector); |
| 3 | +import { TestConnector } from './utils/MockStreamOpenFactory'; |
| 4 | +import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/generateConnectedDatabase'; |
73 | 5 |
|
74 | | - await streamOpened; |
75 | | - |
76 | | - remote.streamController?.enqueue(new TextEncoder().encode('{"token_expires_in":3426}\n')); |
77 | | - |
78 | | - // Wait for connected to be true |
79 | | - await connectedPromise; |
80 | | - }; |
81 | | - |
82 | | - await connect(); |
83 | | - |
84 | | - return { |
85 | | - connector, |
86 | | - connect, |
87 | | - factory, |
88 | | - powersync, |
89 | | - remote, |
90 | | - uploadSpy, |
91 | | - waitForStream, |
92 | | - dispose: async () => { |
93 | | - remote.streamController?.close(); |
94 | | - await powersync.disconnectAndClear(); |
95 | | - await powersync.close(); |
96 | | - } |
97 | | - }; |
98 | | -} |
| 6 | +const UPLOAD_TIMEOUT_MS = 3000; |
99 | 7 |
|
100 | 8 | describe( |
101 | 9 | 'Streaming', |
|
0 commit comments