|
| 1 | +import { mockLogger } from '../interfaces/utils.js'; |
| 2 | +import { AztecSQLiteOPFSStore } from './index.js'; |
| 3 | +import { deleteStore, listStores, storePoolDirectory } from './manage.js'; |
| 4 | + |
| 5 | +const openByName = (name: string) => AztecSQLiteOPFSStore.open(mockLogger, name, false, storePoolDirectory(name)); |
| 6 | + |
| 7 | +describe('sqlite-opfs store management', () => { |
| 8 | + it('round-trips data for a store reopened by name', async () => { |
| 9 | + const store = await openByName('mech_roundtrip'); |
| 10 | + await store.openSingleton<string>('payload').set('data'); |
| 11 | + await store.close(); |
| 12 | + |
| 13 | + const reopened = await openByName('mech_roundtrip'); |
| 14 | + expect(await reopened.openSingleton<string>('payload').getAsync()).toEqual('data'); |
| 15 | + await reopened.close(); |
| 16 | + await deleteStore('mech_roundtrip'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('opens two different stores concurrently in the same tab', async () => { |
| 20 | + const a = await openByName('mech_concurrent_a'); |
| 21 | + const b = await openByName('mech_concurrent_b'); |
| 22 | + |
| 23 | + await a.openSingleton<string>('k').set('a'); |
| 24 | + await b.openSingleton<string>('k').set('b'); |
| 25 | + expect(await a.openSingleton<string>('k').getAsync()).toEqual('a'); |
| 26 | + expect(await b.openSingleton<string>('k').getAsync()).toEqual('b'); |
| 27 | + |
| 28 | + await a.close(); |
| 29 | + await b.close(); |
| 30 | + await deleteStore('mech_concurrent_a'); |
| 31 | + await deleteStore('mech_concurrent_b'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('lists created stores and deletes them', async () => { |
| 35 | + const store = await openByName('mech_managed'); |
| 36 | + await store.openSingleton<string>('k').set('v'); |
| 37 | + await store.close(); |
| 38 | + |
| 39 | + expect(await listStores()).toContain('mech_managed'); |
| 40 | + await deleteStore('mech_managed'); |
| 41 | + expect(await listStores()).not.toContain('mech_managed'); |
| 42 | + |
| 43 | + // Recreating after deletion starts empty. |
| 44 | + const fresh = await openByName('mech_managed'); |
| 45 | + expect(await fresh.openSingleton<string>('k').getAsync()).toBeUndefined(); |
| 46 | + await fresh.close(); |
| 47 | + await deleteStore('mech_managed'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('refuses to delete a store that is currently open', async () => { |
| 51 | + const store = await openByName('mech_locked'); |
| 52 | + await expect(deleteStore('mech_locked')).rejects.toThrow(); |
| 53 | + await store.close(); |
| 54 | + await deleteStore('mech_locked'); |
| 55 | + }); |
| 56 | +}); |
0 commit comments