Skip to content

Commit 0e6d380

Browse files
committed
chore: update tests
1 parent c332fdd commit 0e6d380

File tree

2 files changed

+202
-86
lines changed

2 files changed

+202
-86
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import { describe, it, expect, vi } from 'vitest';
2+
import { AbstractPowerSyncDatabase, DEFAULT_RETRY_DELAY_MS, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, BucketStorageAdapter, DBAdapter, PowerSyncBackendConnector, PowerSyncDatabaseOptionsWithSettings, RequiredAdditionalConnectionOptions, StreamingSyncImplementation } from '@powersync/common';
3+
import { testSchema } from '../../utils/testDb';
4+
5+
class TestPowerSyncDatabase extends AbstractPowerSyncDatabase {
6+
protected openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter {
7+
return {} as any
8+
}
9+
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: RequiredAdditionalConnectionOptions): StreamingSyncImplementation {
10+
return undefined as any;
11+
}
12+
protected generateBucketStorageAdapter(): BucketStorageAdapter {
13+
return {
14+
init: vi.fn()
15+
} as any
16+
}
17+
_initialize(): Promise<void> {
18+
return Promise.resolve();
19+
}
20+
21+
get database() {
22+
return {
23+
get: vi.fn().mockResolvedValue({ version: '0.3.0'}),
24+
execute: vi.fn(),
25+
refreshSchema: vi.fn(),
26+
} as any
27+
}
28+
// Expose protected method for testing
29+
public testResolvedConnectionOptions(options?: any) {
30+
return this.resolvedConnectionOptions(options);
31+
}
32+
}
33+
34+
describe('AbstractPowerSyncDatabase', () => {
35+
describe('resolvedConnectionOptions', () => {
36+
it('should use connect options when provided', () => {
37+
const db = new TestPowerSyncDatabase({
38+
schema: testSchema,
39+
database: { dbFilename: 'test.db' }
40+
});
41+
42+
const result = db.testResolvedConnectionOptions({
43+
retryDelayMs: 1000,
44+
crudUploadThrottleMs: 2000
45+
});
46+
47+
expect(result).toEqual({
48+
retryDelayMs: 1000,
49+
crudUploadThrottleMs: 2000
50+
});
51+
});
52+
53+
it('should fallback to constructor options when connect options not provided', () => {
54+
const db = new TestPowerSyncDatabase({
55+
schema: testSchema,
56+
database: { dbFilename: 'test.db' },
57+
retryDelayMs: 3000,
58+
crudUploadThrottleMs: 4000
59+
});
60+
61+
const result = db.testResolvedConnectionOptions();
62+
63+
expect(result).toEqual({
64+
retryDelayMs: 3000,
65+
crudUploadThrottleMs: 4000
66+
});
67+
});
68+
69+
it('should convert retryDelay to retryDelayMs', () => {
70+
const db = new TestPowerSyncDatabase({
71+
schema: testSchema,
72+
database: { dbFilename: 'test.db' },
73+
retryDelay: 5000
74+
});
75+
76+
const result = db.testResolvedConnectionOptions();
77+
78+
expect(result).toEqual({
79+
retryDelayMs: 5000,
80+
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
81+
});
82+
});
83+
84+
it('should prioritize retryDelayMs over retryDelay in constructor options', () => {
85+
const db = new TestPowerSyncDatabase({
86+
schema: testSchema,
87+
database: { dbFilename: 'test.db' },
88+
retryDelay: 5000,
89+
retryDelayMs: 6000
90+
});
91+
92+
const result = db.testResolvedConnectionOptions();
93+
94+
expect(result).toEqual({
95+
retryDelayMs: 6000,
96+
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
97+
});
98+
});
99+
100+
it('should prioritize connect options over constructor options', () => {
101+
const db = new TestPowerSyncDatabase({
102+
schema: testSchema,
103+
database: { dbFilename: 'test.db' },
104+
retryDelayMs: 5000,
105+
crudUploadThrottleMs: 6000
106+
});
107+
108+
const result = db.testResolvedConnectionOptions({
109+
retryDelayMs: 7000,
110+
crudUploadThrottleMs: 8000
111+
});
112+
113+
expect(result).toEqual({
114+
retryDelayMs: 7000,
115+
crudUploadThrottleMs: 8000
116+
});
117+
});
118+
119+
it('should use default values when no options provided', () => {
120+
const db = new TestPowerSyncDatabase({
121+
schema: testSchema,
122+
database: { dbFilename: 'test.db' }
123+
});
124+
125+
const result = db.testResolvedConnectionOptions();
126+
127+
expect(result).toEqual({
128+
retryDelayMs: DEFAULT_RETRY_DELAY_MS,
129+
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
130+
});
131+
});
132+
133+
it('should handle partial connect options', () => {
134+
const db = new TestPowerSyncDatabase({
135+
schema: testSchema,
136+
database: { dbFilename: 'test.db' },
137+
retryDelayMs: 5000,
138+
crudUploadThrottleMs: 6000
139+
});
140+
141+
const result = db.testResolvedConnectionOptions({
142+
retryDelayMs: 7000
143+
});
144+
145+
expect(result).toEqual({
146+
retryDelayMs: 7000,
147+
crudUploadThrottleMs: 6000
148+
});
149+
});
150+
});
151+
});
Lines changed: 51 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,65 @@
1-
import { describe, it, expect, beforeEach, vi } from 'vitest'
2-
import { PowerSyncDatabase, SharedWebStreamingSyncImplementation, WebStreamingSyncImplementation } from '../../../src'
3-
import { SSRStreamingSyncImplementation } from '../../../src/db/sync/SSRWebStreamingSyncImplementation'
4-
import { testSchema } from '../../utils/testDb'
1+
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
2+
import { AbstractPowerSyncDatabase, PowerSyncDatabase, SyncStreamConnectionMethod } from '../../../src';
3+
import { testSchema } from '../../utils/testDb';
54

6-
vi.mock('../../../src/db/sync/WebStreamingSyncImplementation')
7-
vi.mock('../../../src/db/sync/SharedWebStreamingSyncImplementation')
8-
vi.mock('../../../src/db/sync/SSRWebStreamingSyncImplementation')
9-
10-
describe('PowerSyncDatabase - generateSyncStreamImplementation', () => {
11-
const mockConnector = {
12-
uploadData: vi.fn(),
13-
fetchCredentials: vi.fn()
14-
}
5+
describe('PowerSyncDatabase', () => {
6+
let db: PowerSyncDatabase;
7+
let mockConnector: any;
8+
let mockLogger: any;
9+
let mockSyncImplementation: any;
1510

1611
beforeEach(() => {
17-
vi.resetAllMocks()
18-
})
19-
20-
it('uses SSRStreamingSyncImplementation when ssrMode is true', async () => {
21-
// This is to prevent a false positive from the unhandled rejection
22-
// of using SSR in this test.
23-
const handler = (event: PromiseRejectionEvent) => {
24-
event.preventDefault()
25-
}
26-
window.addEventListener('unhandledrejection', handler)
12+
mockLogger = {
13+
debug: vi.fn(),
14+
};
2715

28-
const db = new PowerSyncDatabase({
16+
// Initialize with minimal required options
17+
db = new PowerSyncDatabase({
2918
schema: testSchema,
3019
database: {
3120
dbFilename: 'test.db'
3221
},
33-
flags: {
34-
ssrMode: true,
35-
},
36-
retryDelayMs: 1000,
37-
crudUploadThrottleMs: 2000
38-
})
22+
logger: mockLogger
23+
});
3924

40-
db['generateSyncStreamImplementation'](mockConnector, { retryDelayMs: 1000, crudUploadThrottleMs: 2000 })
41-
expect(SSRStreamingSyncImplementation).toHaveBeenCalled()
25+
vi.spyOn(db as any, 'runExclusive').mockImplementation((cb: any) => cb());
4226

43-
await setTimeout(() => window.removeEventListener('unhandledrejection', handler), 1)
44-
})
27+
vi.spyOn(AbstractPowerSyncDatabase.prototype, 'connect')
28+
.mockResolvedValue(undefined);
29+
});
4530

46-
it('uses SharedWebStreamingSyncImplementation when enableMultiTabs is true', () => {
47-
const db = new PowerSyncDatabase({
48-
schema: testSchema,
49-
database: { dbFilename: 'test.db' },
50-
flags: { enableMultiTabs: true }
51-
})
52-
db['generateSyncStreamImplementation'](mockConnector, { retryDelayMs: 1000, crudUploadThrottleMs: 2000 })
53-
expect(SharedWebStreamingSyncImplementation).toHaveBeenCalled()
54-
})
31+
afterEach(() => {
32+
vi.clearAllMocks();
33+
});
5534

56-
it('handles option overrides', () => {
57-
const db = new PowerSyncDatabase({
58-
schema: testSchema,
59-
database: {
60-
dbFilename: 'test.db'
61-
},
62-
flags: {
63-
ssrMode: false,
64-
enableMultiTabs: false,
65-
},
66-
retryDelayMs: 1000,
67-
crudUploadThrottleMs: 1000
68-
})
69-
70-
db['generateSyncStreamImplementation'](mockConnector, { crudUploadThrottleMs: 20000, retryDelayMs: 50000 })
71-
expect(WebStreamingSyncImplementation).toHaveBeenCalledWith(
72-
expect.objectContaining({
73-
retryDelayMs: 50000,
74-
crudUploadThrottleMs: 20000
75-
})
76-
)
77-
})
35+
describe('connect', () => {
36+
it('should log debug message when attempting to connect', async () => {
37+
await db.connect(mockConnector);
38+
expect(mockLogger.debug).toHaveBeenCalledWith('Attempting to connect to PowerSync instance');
39+
expect(db['runExclusive']).toHaveBeenCalled();
40+
});
7841

79-
// This test can be removed once retryDelay is removed and entirely replaced with retryDelayMs
80-
it('works when using deprecated retryDelay instead of retryDelayMs', async () => {
81-
const db = new PowerSyncDatabase({
82-
schema: testSchema,
83-
database: {
84-
dbFilename: 'test.db'
85-
},
86-
flags: {
87-
ssrMode: false,
88-
enableMultiTabs: false,
89-
},
90-
retryDelay: 11100,
91-
})
42+
it('should use connect with correct options', async () => {
43+
await db.connect(mockConnector, {
44+
retryDelayMs: 1000,
45+
crudUploadThrottleMs: 2000,
46+
params: {
47+
param1: 1
48+
},
49+
connectionMethod: SyncStreamConnectionMethod.HTTP
50+
});
9251

93-
db['generateSyncStreamImplementation'](mockConnector, { crudUploadThrottleMs: 2000, retryDelayMs: 50000 })
94-
expect(WebStreamingSyncImplementation).toHaveBeenCalledWith(
95-
expect.objectContaining({
96-
retryDelay: 11100,
97-
})
98-
)
99-
})
100-
})
52+
expect(AbstractPowerSyncDatabase.prototype.connect).toHaveBeenCalledWith(
53+
mockConnector,
54+
{
55+
retryDelayMs: 1000,
56+
crudUploadThrottleMs: 2000,
57+
connectionMethod: "http",
58+
params: {
59+
param1: 1,
60+
},
61+
}
62+
);
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)