|
| 1 | +import Logger from 'js-logger'; |
| 2 | +import p from 'p-defer'; |
| 3 | +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { ConnectedDatabaseUtils, generateConnectedDatabase } from './stream.test'; |
| 5 | + |
| 6 | +describe('CRUD Uploads', () => { |
| 7 | + let connectedUtils: ConnectedDatabaseUtils; |
| 8 | + const logger = Logger.get('crud-logger'); |
| 9 | + |
| 10 | + beforeAll(() => Logger.useDefaults()); |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + connectedUtils = await generateConnectedDatabase({ |
| 14 | + powerSyncOptions: { |
| 15 | + logger, |
| 16 | + /** |
| 17 | + * The timeout here is set to longer than the default test timeout |
| 18 | + * A retry wil cause tests to fail. |
| 19 | + */ |
| 20 | + crudUploadThrottleMs: 10_000, |
| 21 | + flags: { |
| 22 | + enableMultiTabs: false |
| 23 | + } |
| 24 | + } |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(async () => { |
| 29 | + connectedUtils.remote.streamController?.close(); |
| 30 | + await connectedUtils.powersync.disconnectAndClear(); |
| 31 | + await connectedUtils.powersync.close(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should warn for missing upload operations in uploadData', async () => { |
| 35 | + const { powersync, uploadSpy } = connectedUtils; |
| 36 | + const loggerSpy = vi.spyOn(logger, 'warn'); |
| 37 | + |
| 38 | + const deferred = p(); |
| 39 | + |
| 40 | + uploadSpy.mockImplementation(async (db) => { |
| 41 | + // This upload method does not perform an upload |
| 42 | + deferred.resolve(); |
| 43 | + }); |
| 44 | + |
| 45 | + // Create something with CRUD in it. |
| 46 | + await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven']); |
| 47 | + |
| 48 | + // The empty upload handler should have been called |
| 49 | + // Timeouts seem to be weird in Vitest Browser mode. |
| 50 | + // This makes the check below more stable. |
| 51 | + await deferred.promise; |
| 52 | + |
| 53 | + await vi.waitFor( |
| 54 | + () => { |
| 55 | + expect( |
| 56 | + loggerSpy.mock.calls.find((logArgs) => |
| 57 | + logArgs[0].includes('Potentially previously uploaded CRUD entries are still present') |
| 58 | + ) |
| 59 | + ).exist; |
| 60 | + }, |
| 61 | + { |
| 62 | + timeout: 500 |
| 63 | + } |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should immediately upload sequential transactions', async () => { |
| 68 | + const { powersync, uploadSpy } = connectedUtils; |
| 69 | + const deferred = p(); |
| 70 | + |
| 71 | + uploadSpy.mockImplementation(async (db) => { |
| 72 | + // This upload method does not perform an upload |
| 73 | + const nextTransaction = await db.getNextCrudTransaction(); |
| 74 | + console.log('uploading trans', nextTransaction); |
| 75 | + if (!nextTransaction) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Mockingly delete the crud op in order to progress through the CRUD queue |
| 80 | + for (const op of nextTransaction.crud) { |
| 81 | + await db.execute(`DELETE FROM ps_crud WHERE id = ?`, [op.clientId]); |
| 82 | + } |
| 83 | + |
| 84 | + deferred.resolve(); |
| 85 | + }); |
| 86 | + |
| 87 | + // Create the first item |
| 88 | + await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven']); |
| 89 | + |
| 90 | + // Modify the first item in a new transaction |
| 91 | + await powersync.execute(` |
| 92 | + UPDATE |
| 93 | + users |
| 94 | + SET |
| 95 | + name = 'Mugi' |
| 96 | + WHERE |
| 97 | + name = 'steven'`); |
| 98 | + |
| 99 | + // Create a second item |
| 100 | + await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven2']); |
| 101 | + |
| 102 | + // The empty upload handler should have been called |
| 103 | + // Timeouts seem to be weird in Vitest Browser mode. |
| 104 | + // This makes the check below more stable. |
| 105 | + await deferred.promise; |
| 106 | + |
| 107 | + await vi.waitFor( |
| 108 | + () => { |
| 109 | + expect(uploadSpy.mock.calls.length).eq(3); |
| 110 | + }, |
| 111 | + { |
| 112 | + timeout: 5_000 |
| 113 | + } |
| 114 | + ); |
| 115 | + }); |
| 116 | +}); |
0 commit comments