'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); let mockPaths = []; jest.mock('../server/db', () => { const { Database } = require('node-sqlite3-wasm'); let db; function getDb () { if (!db) { db = new Database(':memory:'); db.exec(` CREATE TABLE IF NOT EXISTS file_search_index ( file_path TEXT PRIMARY KEY, root_path TEXT NOT NULL, name TEXT NOT NULL, name_lower TEXT NOT NULL, relative_path TEXT NOT NULL, relative_lower TEXT NOT NULL, size INTEGER, mtime_ms INTEGER, indexed_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_file_search_root ON file_search_index(root_path); `); } return db; } return { getDb, initDb: getDb }; }); jest.mock('../server/config', () => ({ loadConfig: () => ({ paths: [...mockPaths] }), getEffectivePaths: (paths) => paths, })); describe('search index', () => { let tempDir; let sourceRoot; function loadModule () { jest.resetModules(); return require('../server/search-index'); } beforeEach(() => { tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-search-index-')); sourceRoot = path.join(tempDir, 'root'); fs.mkdirSync(sourceRoot, { recursive: true }); mockPaths = [sourceRoot]; }); afterEach(() => { mockPaths = []; fs.rmSync(tempDir, { recursive: true, force: true }); }); test('syncSearchIndexOnce indexes files and removes deleted entries', async () => { const keepPath = path.join(sourceRoot, 'dragon-ball.mkv'); const removePath = path.join(sourceRoot, 'old.txt'); fs.writeFileSync(keepPath, 'vid'); fs.writeFileSync(removePath, 'txt'); const searchIndex = loadModule(); await searchIndex.syncSearchIndexOnce([sourceRoot]); expect(searchIndex.countIndexedFilesForRoots([sourceRoot])).toBe(2); fs.unlinkSync(removePath); await searchIndex.syncSearchIndexOnce([sourceRoot]); expect(searchIndex.countIndexedFilesForRoots([sourceRoot])).toBe(1); const results = await searchIndex.searchFiles('dragon', [sourceRoot], 20); expect(results.length).toBe(1); expect(results[0].name).toBe('dragon-ball.mkv'); }); test('searchFiles removes stale ghost hits and rescans for new external files', async () => { const searchIndex = loadModule(); const ghostPath = path.join(sourceRoot, 'ghost.txt'); const livePath = path.join(sourceRoot, 'fresh-notes.txt'); const { getDb } = require('../server/db'); const db = getDb(); db.prepare(` INSERT INTO file_search_index ( file_path, root_path, name, name_lower, relative_path, relative_lower, size, mtime_ms, indexed_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) `).run([ ghostPath, path.resolve(sourceRoot), 'ghost.txt', 'ghost.txt', 'ghost.txt', 'ghost.txt', 3, Date.now(), Date.now(), ]); fs.writeFileSync(livePath, 'hello'); const ghostResults = await searchIndex.searchFiles('ghost', [sourceRoot], 20); expect(ghostResults.some((row) => row.name === 'ghost.txt')).toBe(false); const ghostRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([ghostPath]); expect(ghostRow).toBeFalsy(); const freshResults = await searchIndex.searchFiles('fresh', [sourceRoot], 20); expect(freshResults.some((row) => row.name === 'fresh-notes.txt')).toBe(true); }); test('rewriteIndexPaths moves indexed paths', async () => { const from = path.join(sourceRoot, 'old-name.txt'); const to = path.join(sourceRoot, 'new-name.txt'); fs.writeFileSync(from, 'data'); const searchIndex = loadModule(); await searchIndex.syncSearchIndexOnce([sourceRoot]); searchIndex.rewriteIndexPaths(from, to); const { getDb } = require('../server/db'); const db = getDb(); const oldRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([from]); const newRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([to]); expect(oldRow).toBeFalsy(); expect(newRow).toBeTruthy(); }); });