|
| 1 | +import { |
| 2 | + describeSqliteEngine, |
| 3 | + probeSqliteEngine, |
| 4 | + sqliteEngineLogLevel, |
| 5 | +} from '../probeSqliteEngine'; |
| 6 | + |
| 7 | +function mockDb(options: { |
| 8 | + dispatcher?: string; |
| 9 | + versionRows?: Record<string, unknown>[]; |
| 10 | + jsonRows?: Record<string, unknown>[] | Error; |
| 11 | + jsiBinding?: boolean; |
| 12 | +}) { |
| 13 | + let queryCount = 0; |
| 14 | + const unsafeFetchRaw = jest.fn(async () => { |
| 15 | + queryCount += 1; |
| 16 | + if (queryCount === 1) { |
| 17 | + return options.versionRows ?? [{ sqlite_version: '3.46.0' }]; |
| 18 | + } |
| 19 | + if (options.jsonRows instanceof Error) { |
| 20 | + throw options.jsonRows; |
| 21 | + } |
| 22 | + return options.jsonRows ?? [{ json_ok: '1' }]; |
| 23 | + }); |
| 24 | + const db = { |
| 25 | + adapter: { |
| 26 | + underlyingAdapter: { |
| 27 | + _dispatcherType: options.dispatcher ?? 'jsi', |
| 28 | + initializingPromise: Promise.resolve(), |
| 29 | + }, |
| 30 | + }, |
| 31 | + get: jest.fn(() => ({ |
| 32 | + query: jest.fn(() => ({ unsafeFetchRaw })), |
| 33 | + })), |
| 34 | + }; |
| 35 | + if (options.jsiBinding) { |
| 36 | + ( |
| 37 | + globalThis as { nativeWatermelonCreateAdapter?: () => unknown } |
| 38 | + ).nativeWatermelonCreateAdapter = () => ({}); |
| 39 | + } else { |
| 40 | + delete (globalThis as { nativeWatermelonCreateAdapter?: () => unknown }) |
| 41 | + .nativeWatermelonCreateAdapter; |
| 42 | + } |
| 43 | + return db; |
| 44 | +} |
| 45 | + |
| 46 | +describe('probeSqliteEngine', () => { |
| 47 | + afterEach(() => { |
| 48 | + delete (globalThis as { nativeWatermelonCreateAdapter?: () => unknown }) |
| 49 | + .nativeWatermelonCreateAdapter; |
| 50 | + }); |
| 51 | + |
| 52 | + it('describes a healthy bundled engine', () => { |
| 53 | + const report = { |
| 54 | + dispatcher: 'jsi', |
| 55 | + sqliteVersion: '3.46.0', |
| 56 | + jsonExtract: true, |
| 57 | + jsiBinding: true, |
| 58 | + }; |
| 59 | + expect(describeSqliteEngine(report)).toBe( |
| 60 | + 'sqlite engine dispatcher=jsi version=3.46.0 json_extract=ok jsiBinding=yes', |
| 61 | + ); |
| 62 | + expect(sqliteEngineLogLevel(report)).toBe('info'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('treats a system-sqlite fallback as a warning even when JSON1 exists', () => { |
| 66 | + expect( |
| 67 | + sqliteEngineLogLevel({ |
| 68 | + dispatcher: 'asynchronous', |
| 69 | + sqliteVersion: '3.32.2', |
| 70 | + jsonExtract: true, |
| 71 | + jsiBinding: false, |
| 72 | + }), |
| 73 | + ).toBe('warn'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('treats missing json_extract as an error', () => { |
| 77 | + expect( |
| 78 | + sqliteEngineLogLevel({ |
| 79 | + dispatcher: 'asynchronous', |
| 80 | + sqliteVersion: '3.22.0', |
| 81 | + jsonExtract: false, |
| 82 | + jsiBinding: false, |
| 83 | + }), |
| 84 | + ).toBe('error'); |
| 85 | + expect( |
| 86 | + describeSqliteEngine({ |
| 87 | + dispatcher: 'asynchronous', |
| 88 | + sqliteVersion: '3.22.0', |
| 89 | + jsonExtract: false, |
| 90 | + jsiBinding: false, |
| 91 | + }), |
| 92 | + ).toContain('json_extract=missing'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('probes dispatcher, version, json_extract, and JSI binding', async () => { |
| 96 | + const db = mockDb({ dispatcher: 'jsi', jsiBinding: true }); |
| 97 | + const report = await probeSqliteEngine(db as never); |
| 98 | + expect(report).toEqual({ |
| 99 | + dispatcher: 'jsi', |
| 100 | + sqliteVersion: '3.46.0', |
| 101 | + jsonExtract: true, |
| 102 | + jsiBinding: true, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('records json_extract as missing when the probe query fails', async () => { |
| 107 | + const db = mockDb({ |
| 108 | + dispatcher: 'asynchronous', |
| 109 | + jsonRows: new Error('no such function: json_extract'), |
| 110 | + jsiBinding: false, |
| 111 | + }); |
| 112 | + const report = await probeSqliteEngine(db as never); |
| 113 | + expect(report.jsonExtract).toBe(false); |
| 114 | + expect(report.dispatcher).toBe('asynchronous'); |
| 115 | + expect(report.jsiBinding).toBe(false); |
| 116 | + }); |
| 117 | +}); |
0 commit comments