|
| 1 | +import { randomUUID as v4 } from 'crypto'; |
| 2 | +import fs from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 5 | +import { DataStore, StoreName } from '../../../src/datastore/DataStore'; |
| 6 | +import { LMDBStoreFactory } from '../../../src/datastore/LMDBStoreFactory'; |
| 7 | + |
| 8 | +describe('LMDB value size handling', () => { |
| 9 | + let lmdbFactory: LMDBStoreFactory; |
| 10 | + let lmdbStore: DataStore; |
| 11 | + const testDir = join(process.cwd(), 'node_modules', '.cache', 'lmdb-valuesize-tests', v4()); |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + fs.mkdirSync(testDir, { recursive: true }); |
| 15 | + lmdbFactory = new LMDBStoreFactory(testDir); |
| 16 | + await lmdbFactory.initialize(); |
| 17 | + lmdbStore = lmdbFactory.get(StoreName.public_schemas); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(async () => { |
| 21 | + await lmdbFactory.close(); |
| 22 | + fs.rmSync(testDir, { recursive: true, force: true }); |
| 23 | + }); |
| 24 | + |
| 25 | + /** |
| 26 | + * Installs a `put` mock that always throws MDB_BAD_VALSIZE. Recovery (triggered by the |
| 27 | + * factory's `onError` handler) replaces the underlying store handle via `updateStore`, |
| 28 | + * so the mock must be re-applied after each such replacement to keep simulating a |
| 29 | + * deterministic (non-transient) size error across retries. |
| 30 | + */ |
| 31 | + function mockPutAlwaysRejectsWithBadValSize(): void { |
| 32 | + const internal = lmdbStore as any; |
| 33 | + const throwBadValSize = () => { |
| 34 | + throw new Error('MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong DUPFIXED size'); |
| 35 | + }; |
| 36 | + internal.store.put = throwBadValSize; |
| 37 | + |
| 38 | + const originalUpdateStore = internal.updateStore.bind(internal); |
| 39 | + internal.updateStore = (newStore: unknown) => { |
| 40 | + originalUpdateStore(newStore); |
| 41 | + internal.store.put = throwBadValSize; |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + it('should skip caching gracefully (not throw) when a value exceeds LMDB size limits', async () => { |
| 46 | + mockPutAlwaysRejectsWithBadValSize(); |
| 47 | + |
| 48 | + const result = await lmdbStore.put('big-key', 'value'); |
| 49 | + expect(result).toBe(false); |
| 50 | + expect(lmdbStore.get('big-key')).toBeUndefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not produce an unhandled promise rejection for MDB_BAD_VALSIZE errors', async () => { |
| 54 | + mockPutAlwaysRejectsWithBadValSize(); |
| 55 | + |
| 56 | + // If put() ever rejects without being awaited/caught here, this would surface as an |
| 57 | + // unhandled rejection in the test process. Awaiting confirms the promise always resolves. |
| 58 | + await expect(lmdbStore.put('key', 'value')).resolves.not.toThrow(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should still throw for errors unrelated to value size', async () => { |
| 62 | + const internal = lmdbStore as any; |
| 63 | + |
| 64 | + // No-op onError override so recovery doesn't replace the mocked store handle, |
| 65 | + // letting the deterministic failure propagate as expected by the generic retry path. |
| 66 | + (lmdbFactory as any).handleError = () => { |
| 67 | + /* no-op: simulate recovery failure so retry hits the same mocked error */ |
| 68 | + }; |
| 69 | + |
| 70 | + internal.store.put = () => { |
| 71 | + throw new Error('MDB_PANIC: unrecoverable'); |
| 72 | + }; |
| 73 | + |
| 74 | + await expect(lmdbStore.put('key', 'value')).rejects.toThrow('MDB_PANIC: unrecoverable'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should put normal-sized values without any change in behavior', async () => { |
| 78 | + const result = await lmdbStore.put('normal-key', { data: 'small value' }); |
| 79 | + expect(result).toBe(true); |
| 80 | + expect(lmdbStore.get('normal-key')).toEqual({ data: 'small value' }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments