|
| 1 | +import { expect, test, describe } from 'vitest'; |
| 2 | +import sqlite3InitModule from '../bin/sqlite3-bundler-friendly.mjs'; |
| 3 | + |
| 4 | +describe('kvvfs', () => { |
| 5 | + test('kvvfs basic sanity check (browser)', async () => { |
| 6 | + const sqlite3 = await sqlite3InitModule(); |
| 7 | + const { kvvfs } = sqlite3; |
| 8 | + |
| 9 | + expect(kvvfs).toBeDefined(); |
| 10 | + expect(kvvfs.exists('.')).toBe(true); |
| 11 | + |
| 12 | + if (typeof window !== 'undefined' && window.localStorage) { |
| 13 | + expect(kvvfs.exists('local')).toBe(true); |
| 14 | + } |
| 15 | + if (typeof window !== 'undefined' && window.sessionStorage) { |
| 16 | + expect(kvvfs.exists('session')).toBe(true); |
| 17 | + } |
| 18 | + |
| 19 | + const db = new sqlite3.oo1.DB('file:temp?vfs=kvvfs', 'c'); |
| 20 | + try { |
| 21 | + db.exec('CREATE TABLE t(a,b); INSERT INTO t(a,b) VALUES(1,2),(3,4);'); |
| 22 | + const rows = db.selectArrays('SELECT * FROM t ORDER BY a'); |
| 23 | + expect(rows).toEqual([ |
| 24 | + [1, 2], |
| 25 | + [3, 4], |
| 26 | + ]); |
| 27 | + |
| 28 | + const size = kvvfs.estimateSize('temp'); |
| 29 | + expect(size).toBeGreaterThan(0); |
| 30 | + } finally { |
| 31 | + db.close(); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + test('kvvfs persistence check (session storage)', async () => { |
| 36 | + const sqlite3 = await sqlite3InitModule(); |
| 37 | + const { kvvfs } = sqlite3; |
| 38 | + |
| 39 | + const dbName = 'file:persisTest?vfs=kvvfs'; |
| 40 | + |
| 41 | + // Clean up if it already exists |
| 42 | + kvvfs.unlink('persisTest'); |
| 43 | + |
| 44 | + let db = new sqlite3.oo1.DB(dbName, 'c'); |
| 45 | + try { |
| 46 | + db.exec('CREATE TABLE t(a); INSERT INTO t(a) VALUES(100);'); |
| 47 | + } finally { |
| 48 | + db.close(); |
| 49 | + } |
| 50 | + |
| 51 | + // Re-open |
| 52 | + db = new sqlite3.oo1.DB(dbName, 'c'); |
| 53 | + try { |
| 54 | + const val = db.selectValue('SELECT a FROM t'); |
| 55 | + expect(val).toBe(100); |
| 56 | + } finally { |
| 57 | + db.close(); |
| 58 | + kvvfs.unlink('persisTest'); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + test('kvvfs utility methods', async () => { |
| 63 | + const sqlite3 = await sqlite3InitModule(); |
| 64 | + const { kvvfs } = sqlite3; |
| 65 | + |
| 66 | + const name = 'utilTest'; |
| 67 | + const dbName = `file:${name}?vfs=kvvfs`; |
| 68 | + |
| 69 | + kvvfs.unlink(name); |
| 70 | + const db = new sqlite3.oo1.DB(dbName, 'c'); |
| 71 | + try { |
| 72 | + db.exec('CREATE TABLE t(a); INSERT INTO t(a) VALUES(1);'); |
| 73 | + |
| 74 | + const size = kvvfs.estimateSize(name); |
| 75 | + expect(size).toBeGreaterThan(0); |
| 76 | + |
| 77 | + expect(kvvfs.exists(name)).toBe(true); |
| 78 | + } finally { |
| 79 | + db.close(); |
| 80 | + kvvfs.unlink(name); |
| 81 | + expect(kvvfs.exists(name)).toBe(false); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + test('repro issue 146: kvvfs xFileControl [TypeError: Cannot read properties of undefined (reading "disablePageSizeChange")]', async () => { |
| 86 | + const sqlite3 = await sqlite3InitModule(); |
| 87 | + const { kvvfs } = sqlite3; |
| 88 | + const db = new sqlite3.oo1.DB('file:repro146?vfs=kvvfs', 'c'); |
| 89 | + try { |
| 90 | + // The issue was that kvvfs.internal was only defined if kvvfs.log was defined. |
| 91 | + // However, xFileControl unconditionally accessed kvvfs.internal.disablePageSizeChange. |
| 92 | + |
| 93 | + // Trigger xFileControl with SQLITE_FCNTL_PRAGMA for page_size |
| 94 | + db.exec('PRAGMA page_size;'); |
| 95 | + |
| 96 | + // This should also trigger it |
| 97 | + db.exec('PRAGMA page_size = 4096;'); |
| 98 | + |
| 99 | + // VACUUM often triggers various file controls |
| 100 | + db.exec('VACUUM;'); |
| 101 | + |
| 102 | + // Verify no errors were captured in kvvfs cache |
| 103 | + const lastError = kvvfs.internal.cache.popError(); |
| 104 | + expect(lastError).toBeUndefined(); |
| 105 | + } finally { |
| 106 | + db.close(); |
| 107 | + sqlite3.kvvfs.unlink('repro146'); |
| 108 | + } |
| 109 | + }); |
| 110 | +}); |
0 commit comments