BlueGit

timfa filemanager

Files

filemanager / tests / search-index.test.js

search-index.test.js

Cursor Optimisation pass
timfa Tim Falken · pushed by timfa · 5 months ago
7e6dfc52 History
search-index.test.js 4.2 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const os = require('os');
5const path = require('path');
6
7let mockPaths = [];
8
9jest.mock('../server/db', () =>
10{
11 const { Database } = require('node-sqlite3-wasm');
12 let db;
13 function getDb ()
14 {
15 if (!db)
16 {
17 db = new Database(':memory:');
18 db.exec(`
19 CREATE TABLE IF NOT EXISTS file_search_index (
20 file_path TEXT PRIMARY KEY,
21 root_path TEXT NOT NULL,
22 name TEXT NOT NULL,
23 name_lower TEXT NOT NULL,
24 relative_path TEXT NOT NULL,
25 relative_lower TEXT NOT NULL,
26 size INTEGER,
27 mtime_ms INTEGER,
28 indexed_at INTEGER NOT NULL
29 );
30 CREATE INDEX IF NOT EXISTS idx_file_search_root ON file_search_index(root_path);
31 `);
32 }
33 return db;
34 }
35 return { getDb, initDb: getDb };
36});
37
38jest.mock('../server/config', () => ({
39 loadConfig: () => ({ paths: [...mockPaths] }),
40 getEffectivePaths: (paths) => paths,
41}));
42
43describe('search index', () =>
44{
45 let tempDir;
46 let sourceRoot;
47
48 function loadModule ()
49 {
50 jest.resetModules();
51 return require('../server/search-index');
52 }
53
54 beforeEach(() =>
55 {
56 tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-search-index-'));
57 sourceRoot = path.join(tempDir, 'root');
58 fs.mkdirSync(sourceRoot, { recursive: true });
59 mockPaths = [sourceRoot];
60 });
61
62 afterEach(() =>
63 {
64 mockPaths = [];
65 fs.rmSync(tempDir, { recursive: true, force: true });
66 });
67
68 test('syncSearchIndexOnce indexes files and removes deleted entries', async () =>
69 {
70 const keepPath = path.join(sourceRoot, 'dragon-ball.mkv');
71 const removePath = path.join(sourceRoot, 'old.txt');
72 fs.writeFileSync(keepPath, 'vid');
73 fs.writeFileSync(removePath, 'txt');
74
75 const searchIndex = loadModule();
76 await searchIndex.syncSearchIndexOnce([sourceRoot]);
77 expect(searchIndex.countIndexedFilesForRoots([sourceRoot])).toBe(2);
78
79 fs.unlinkSync(removePath);
80 await searchIndex.syncSearchIndexOnce([sourceRoot]);
81 expect(searchIndex.countIndexedFilesForRoots([sourceRoot])).toBe(1);
82
83 const results = await searchIndex.searchFiles('dragon', [sourceRoot], 20);
84 expect(results.length).toBe(1);
85 expect(results[0].name).toBe('dragon-ball.mkv');
86 });
87
88 test('searchFiles removes stale ghost hits and rescans for new external files', async () =>
89 {
90 const searchIndex = loadModule();
91 const ghostPath = path.join(sourceRoot, 'ghost.txt');
92 const livePath = path.join(sourceRoot, 'fresh-notes.txt');
93
94 const { getDb } = require('../server/db');
95 const db = getDb();
96 db.prepare(`
97 INSERT INTO file_search_index (
98 file_path, root_path, name, name_lower, relative_path, relative_lower, size, mtime_ms, indexed_at
99 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
100 `).run([
101 ghostPath,
102 path.resolve(sourceRoot),
103 'ghost.txt',
104 'ghost.txt',
105 'ghost.txt',
106 'ghost.txt',
107 3,
108 Date.now(),
109 Date.now(),
110 ]);
111
112 fs.writeFileSync(livePath, 'hello');
113
114 const ghostResults = await searchIndex.searchFiles('ghost', [sourceRoot], 20);
115 expect(ghostResults.some((row) => row.name === 'ghost.txt')).toBe(false);
116 const ghostRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([ghostPath]);
117 expect(ghostRow).toBeFalsy();
118
119 const freshResults = await searchIndex.searchFiles('fresh', [sourceRoot], 20);
120 expect(freshResults.some((row) => row.name === 'fresh-notes.txt')).toBe(true);
121 });
122
123 test('rewriteIndexPaths moves indexed paths', async () =>
124 {
125 const from = path.join(sourceRoot, 'old-name.txt');
126 const to = path.join(sourceRoot, 'new-name.txt');
127 fs.writeFileSync(from, 'data');
128
129 const searchIndex = loadModule();
130 await searchIndex.syncSearchIndexOnce([sourceRoot]);
131 searchIndex.rewriteIndexPaths(from, to);
132
133 const { getDb } = require('../server/db');
134 const db = getDb();
135 const oldRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([from]);
136 const newRow = db.prepare('SELECT file_path FROM file_search_index WHERE file_path = ?').get([to]);
137 expect(oldRow).toBeFalsy();
138 expect(newRow).toBeTruthy();
139 });
140});