Skip to content

Commit b0dd596

Browse files
cleanup tests
1 parent feca863 commit b0dd596

File tree

3 files changed

+13
-182
lines changed

3 files changed

+13
-182
lines changed

packages/web/tests/multiple_tabs_iframe.test.ts

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ interface IframeClient {
1313
iframe: HTMLIFrameElement;
1414
cleanup: () => Promise<void>;
1515
executeQuery: (query: string, parameters?: unknown[]) => Promise<unknown[]>;
16-
getSyncStatus: () => Promise<{
17-
connected: boolean;
18-
connecting: boolean;
19-
downloading: boolean;
20-
uploading: boolean;
21-
lastSyncedAt?: string;
22-
hasSynced?: boolean;
23-
}>;
2416
getCredentialsFetchCount: () => Promise<number>;
2517
}
2618

@@ -67,15 +59,6 @@ async function createIframeWithPowerSyncClient(
6759
font-size: 12px;
6860
background: #f5f5f5;
6961
}
70-
#status {
71-
padding: 8px;
72-
background: #fff;
73-
border: 1px solid #ddd;
74-
border-radius: 4px;
75-
margin-bottom: 8px;
76-
font-weight: 500;
77-
transition: color 0.3s, border-color 0.3s;
78-
}
7962
#info {
8063
padding: 8px;
8164
background: #e7f3ff;
@@ -91,7 +74,6 @@ async function createIframeWithPowerSyncClient(
9174
</style>
9275
</head>
9376
<body>
94-
<div id="status">Initializing...</div>
9577
<div id="info">
9678
<div><span class="label">ID:</span>${identifier}</div>
9779
<div><span class="label">DB:</span>${dbFilename}</div>
@@ -166,44 +148,6 @@ async function createIframeWithPowerSyncClient(
166148
}, 30000);
167149
});
168150
},
169-
getSyncStatus: (): Promise<{
170-
connected: boolean;
171-
connecting: boolean;
172-
downloading: boolean;
173-
uploading: boolean;
174-
lastSyncedAt?: string;
175-
hasSynced?: boolean;
176-
}> => {
177-
return new Promise((resolveStatus, rejectStatus) => {
178-
const requestId = `status-${identifier}-${++requestIdCounter}`;
179-
pendingRequests.set(requestId, {
180-
resolve: resolveStatus,
181-
reject: rejectStatus
182-
});
183-
184-
const iframeWindow = iframe.contentWindow;
185-
if (!iframeWindow) {
186-
rejectStatus(new Error('Iframe window not available'));
187-
return;
188-
}
189-
190-
iframeWindow.postMessage(
191-
{
192-
type: 'get-sync-status',
193-
requestId
194-
},
195-
'*'
196-
);
197-
198-
// Cleanup after timeout to prevent memory leaks
199-
setTimeout(() => {
200-
if (pendingRequests.has(requestId)) {
201-
pendingRequests.delete(requestId);
202-
rejectStatus(new Error('Status request timeout'));
203-
}
204-
}, 10000);
205-
});
206-
},
207151
getCredentialsFetchCount: (): Promise<number> => {
208152
return new Promise((resolveCount, rejectCount) => {
209153
const requestId = `credentials-count-${identifier}-${++requestIdCounter}`;
@@ -253,16 +197,6 @@ async function createIframeWithPowerSyncClient(
253197
pending.reject(new Error(data.error || 'Query failed'));
254198
}
255199
}
256-
} else if (data?.type === 'sync-status-result' && data.identifier === identifier) {
257-
const pending = pendingRequests.get(data.requestId);
258-
if (pending) {
259-
pendingRequests.delete(data.requestId);
260-
if (data.success) {
261-
pending.resolve(data.status);
262-
} else {
263-
pending.reject(new Error(data.error || 'Status request failed'));
264-
}
265-
}
266200
} else if (data?.type === 'credentials-count-result' && data.identifier === identifier) {
267201
const pending = pendingRequests.get(data.requestId);
268202
if (pending) {
@@ -447,17 +381,14 @@ function createMultipleTabsTest(vfs?: WASQLiteVFS) {
447381

448382
// Wait for the new tab's credentials to be fetched (indicating the shared sync worker is active)
449383
// The mocked remote always returns 401, so the shared sync worker should try and fetch credentials again.
450-
await vi.waitFor(
451-
async () => {
452-
const credentialsFetchCount = await newTab.getCredentialsFetchCount();
453-
expect(
454-
credentialsFetchCount,
455-
'The new client should have been asked for credentials by the shared sync worker. ' +
456-
'This indicates the shared sync worker may be stuck or not processing new connections.'
457-
).toBeGreaterThanOrEqual(1);
458-
},
459-
{ timeout: 10000 }
460-
);
384+
await vi.waitFor(async () => {
385+
const credentialsFetchCount = await newTab.getCredentialsFetchCount();
386+
expect(
387+
credentialsFetchCount,
388+
'The new client should have been asked for credentials by the shared sync worker. ' +
389+
'This indicates the shared sync worker may be stuck or not processing new connections.'
390+
).toBeGreaterThanOrEqual(1);
391+
});
461392
});
462393
});
463394
}

packages/web/tests/utils/iframeInitializer.ts

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
LogLevel,
3-
Schema,
4-
SyncStatus,
5-
SyncStreamConnectionMethod,
6-
TableV2,
7-
column,
8-
createBaseLogger
9-
} from '@powersync/common';
1+
import { LogLevel, Schema, SyncStreamConnectionMethod, TableV2, column, createBaseLogger } from '@powersync/common';
102
import { PowerSyncDatabase, WASQLiteOpenFactory, WASQLiteVFS } from '@powersync/web';
113

124
/**
@@ -28,21 +20,8 @@ export async function setupPowerSyncInIframe(dbFilename: string, identifier: str
2820
async uploadData() {}
2921
};
3022

31-
// Create the same schema as used in tests
23+
// Create a simple schema for testing
3224
const schema = new Schema({
33-
assets: new TableV2(
34-
{
35-
created_at: column.text,
36-
make: column.text,
37-
model: column.text,
38-
serial_number: column.text,
39-
quantity: column.integer,
40-
user_id: column.text,
41-
customer_id: column.text,
42-
description: column.text
43-
},
44-
{ indexes: { makemodel: ['make', 'model'] } }
45-
),
4625
customers: new TableV2({
4726
name: column.text,
4827
email: column.text
@@ -70,41 +49,12 @@ export async function setupPowerSyncInIframe(dbFilename: string, identifier: str
7049
logger
7150
});
7251

73-
// Register a listener for sync status updates
74-
const updateStatusDisplay = (status: SyncStatus) => {
75-
const statusEl = document.getElementById('status');
76-
if (statusEl) {
77-
const connected = status.connected ? 'Connected' : 'Disconnected';
78-
const syncing = status.dataFlowStatus.downloading ? ' (Syncing...)' : '';
79-
statusEl.textContent = `${connected}${syncing}`;
80-
81-
// Update color based on connection status
82-
if (status.connected) {
83-
statusEl.style.color = '#28a745';
84-
statusEl.style.borderColor = '#28a745';
85-
} else {
86-
statusEl.style.color = '#dc3545';
87-
statusEl.style.borderColor = '#dc3545';
88-
}
89-
}
90-
};
91-
92-
// Register listener for status changes
93-
db.registerListener({
94-
statusChanged: (status) => {
95-
updateStatusDisplay(status);
96-
}
97-
});
98-
9952
// Connect to PowerSync
10053
await db.connect(connector, { connectionMethod: SyncStreamConnectionMethod.HTTP });
10154

10255
// Store reference for cleanup
10356
(window as any).powersyncClient = db;
10457

105-
// Update initial status
106-
updateStatusDisplay(db.currentStatus);
107-
10858
// Set up message handlers for test operations
10959
window.addEventListener('message', async (event: MessageEvent) => {
11060
// Only handle messages from parent window
@@ -140,38 +90,6 @@ export async function setupPowerSyncInIframe(dbFilename: string, identifier: str
14090
'*'
14191
);
14292
}
143-
} else if (type === 'get-sync-status' && requestId) {
144-
try {
145-
const status = db.currentStatus;
146-
window.parent.postMessage(
147-
{
148-
type: 'sync-status-result',
149-
requestId,
150-
identifier,
151-
success: true,
152-
status: {
153-
connected: status.connected,
154-
connecting: status.connecting,
155-
downloading: status.dataFlowStatus.downloading,
156-
uploading: status.dataFlowStatus.uploading,
157-
lastSyncedAt: status.lastSyncedAt?.toISOString(),
158-
hasSynced: status.hasSynced
159-
}
160-
},
161-
'*'
162-
);
163-
} catch (error) {
164-
window.parent.postMessage(
165-
{
166-
type: 'sync-status-result',
167-
requestId,
168-
identifier,
169-
success: false,
170-
error: (error as Error).message
171-
},
172-
'*'
173-
);
174-
}
17593
} else if (type === 'get-credentials-count' && requestId) {
17694
try {
17795
window.parent.postMessage(
@@ -208,12 +126,6 @@ export async function setupPowerSyncInIframe(dbFilename: string, identifier: str
208126
'*'
209127
);
210128
} catch (error) {
211-
const statusEl = document.getElementById('status');
212-
if (statusEl) {
213-
statusEl.textContent = 'Error: ' + (error as Error).message;
214-
statusEl.style.color = '#dc3545';
215-
statusEl.style.borderColor = '#dc3545';
216-
}
217129
console.error('PowerSync initialization error:', error);
218130
window.parent.postMessage(
219131
{

packages/web/vitest.config.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ const config: UserConfigExport = {
2222
// https://jira.mongodb.org/browse/NODE-5773
2323
bson: require.resolve('bson'),
2424
// Mock WebRemote to throw 401 errors for all HTTP requests in tests
25-
'../../db/sync/WebRemote': path.resolve(__dirname, './tests/mocks/MockWebRemote.ts'),
26-
// Also handle the case where it's imported from the worker context
27-
'@powersync/web/src/db/sync/WebRemote': path.resolve(__dirname, './tests/mocks/MockWebRemote.ts')
25+
'../../db/sync/WebRemote': path.resolve(__dirname, './tests/mocks/MockWebRemote.ts')
2826
}
2927
},
3028
worker: {
@@ -35,7 +33,7 @@ const config: UserConfigExport = {
3533
// Don't optimise these packages as they contain web workers and WASM files.
3634
// https://github.com/vitejs/vite/issues/11672#issuecomment-1415820673
3735
exclude: ['@journeyapps/wa-sqlite', '@powersync/web'],
38-
include: ['bson', 'comlink', 'async-mutex']
36+
include: []
3937
},
4038
plugins: [wasm(), topLevelAwait()],
4139
test: {
@@ -66,17 +64,7 @@ const config: UserConfigExport = {
6664
// {
6765
// browser: 'webkit'
6866
// }
69-
],
70-
// Disable private browsing mode for WebKit
71-
// This allows persistent storage (IndexedDB, localStorage, etc.) to work properly
72-
providerOptions: {
73-
webkit: {
74-
launch: {
75-
// WebKit-specific launch options
76-
},
77-
context: {}
78-
}
79-
}
67+
]
8068
}
8169
}
8270
};

0 commit comments

Comments
 (0)