|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import path from 'node:path'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import os from 'node:os'; |
| 6 | +import { createRequire } from 'node:module'; |
| 7 | + |
| 8 | +const require = createRequire(import.meta.url); |
| 9 | +const initSqlJs = require('sql.js'); |
| 10 | +const Module = require('node:module'); |
| 11 | +const { DB_FILENAME } = require('../dist-electron/appConstants.js'); |
| 12 | + |
| 13 | +const worktreeRoot = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..'); |
| 14 | +const repoRoot = path.resolve(worktreeRoot, '..', '..'); |
| 15 | +const sqlWasmPath = path.join(repoRoot, 'node_modules', 'sql.js', 'dist', 'sql-wasm.wasm'); |
| 16 | + |
| 17 | +async function createSqlDatabase() { |
| 18 | + const SQL = await initSqlJs({ |
| 19 | + locateFile: () => sqlWasmPath, |
| 20 | + }); |
| 21 | + return new SQL.Database(); |
| 22 | +} |
| 23 | + |
| 24 | +function getColumns(db, tableName) { |
| 25 | + const result = db.exec(`PRAGMA table_info(${tableName})`); |
| 26 | + return (result[0]?.values || []).map((row) => String(row[1])); |
| 27 | +} |
| 28 | + |
| 29 | +function getIndexNames(db, tableName) { |
| 30 | + const result = db.exec(`PRAGMA index_list(${tableName})`); |
| 31 | + return (result[0]?.values || []).map((row) => String(row[1])); |
| 32 | +} |
| 33 | + |
| 34 | +test('SqliteStore.create() upgrades legacy user_memories scope columns before creating scoped indexes', async () => { |
| 35 | + const legacyDb = await createSqlDatabase(); |
| 36 | + legacyDb.run(` |
| 37 | + CREATE TABLE kv ( |
| 38 | + key TEXT PRIMARY KEY, |
| 39 | + value TEXT NOT NULL, |
| 40 | + updated_at INTEGER NOT NULL |
| 41 | + ); |
| 42 | + `); |
| 43 | + legacyDb.run(` |
| 44 | + CREATE TABLE metabots ( |
| 45 | + id INTEGER PRIMARY KEY, |
| 46 | + name TEXT, |
| 47 | + avatar TEXT, |
| 48 | + metabot_type TEXT |
| 49 | + ); |
| 50 | + `); |
| 51 | + legacyDb.run(` |
| 52 | + INSERT INTO metabots (id, name, avatar, metabot_type) |
| 53 | + VALUES (1, 'Twin', NULL, 'twin'); |
| 54 | + `); |
| 55 | + legacyDb.run(` |
| 56 | + CREATE TABLE user_memories ( |
| 57 | + id TEXT PRIMARY KEY, |
| 58 | + text TEXT NOT NULL, |
| 59 | + fingerprint TEXT NOT NULL, |
| 60 | + confidence REAL NOT NULL DEFAULT 0.75, |
| 61 | + is_explicit INTEGER NOT NULL DEFAULT 0, |
| 62 | + status TEXT NOT NULL DEFAULT 'created', |
| 63 | + created_at INTEGER NOT NULL, |
| 64 | + updated_at INTEGER NOT NULL, |
| 65 | + last_used_at INTEGER |
| 66 | + ); |
| 67 | + `); |
| 68 | + |
| 69 | + const userDataPath = fs.mkdtempSync(path.join(os.tmpdir(), 'idbots-sqlitestore-memory-scope-')); |
| 70 | + const dbPath = path.join(userDataPath, DB_FILENAME); |
| 71 | + fs.writeFileSync(dbPath, Buffer.from(legacyDb.export())); |
| 72 | + |
| 73 | + const originalLoad = Module._load; |
| 74 | + Module._load = function patchedModuleLoad(request, parent, isMain) { |
| 75 | + if (request === 'electron') { |
| 76 | + return { |
| 77 | + app: { |
| 78 | + isPackaged: false, |
| 79 | + getAppPath: () => repoRoot, |
| 80 | + getPath: () => userDataPath, |
| 81 | + }, |
| 82 | + }; |
| 83 | + } |
| 84 | + return originalLoad(request, parent, isMain); |
| 85 | + }; |
| 86 | + |
| 87 | + try { |
| 88 | + const { SqliteStore } = require('../dist-electron/sqliteStore.js'); |
| 89 | + const sqliteStore = await SqliteStore.create(userDataPath); |
| 90 | + const db = sqliteStore.getDatabase(); |
| 91 | + |
| 92 | + const columns = getColumns(db, 'user_memories'); |
| 93 | + assert(columns.includes('scope_kind')); |
| 94 | + assert(columns.includes('scope_key')); |
| 95 | + assert(columns.includes('usage_class')); |
| 96 | + assert(columns.includes('visibility')); |
| 97 | + |
| 98 | + const indexNames = getIndexNames(db, 'user_memories'); |
| 99 | + assert(indexNames.includes('idx_user_memories_scope_status_updated')); |
| 100 | + assert(indexNames.includes('idx_user_memories_scope_fingerprint')); |
| 101 | + assert(indexNames.includes('idx_user_memories_usage_visibility')); |
| 102 | + } finally { |
| 103 | + Module._load = originalLoad; |
| 104 | + fs.rmSync(userDataPath, { recursive: true, force: true }); |
| 105 | + } |
| 106 | +}); |
0 commit comments