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