feat(TRUEREF-0023): add sqlite-vec search pipeline
This commit is contained in:
@@ -11,6 +11,8 @@ import Database from 'better-sqlite3';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { RepositoryService } from './repository.service';
|
||||
import { loadSqliteVec, sqliteVecRowidTableName, sqliteVecTableName } from '$lib/server/db/sqlite-vec.js';
|
||||
import { SqliteVecStore } from '$lib/server/search/sqlite-vec.store.js';
|
||||
import {
|
||||
AlreadyExistsError,
|
||||
InvalidInputError,
|
||||
@@ -25,6 +27,7 @@ import {
|
||||
function createTestDb(): Database.Database {
|
||||
const client = new Database(':memory:');
|
||||
client.pragma('foreign_keys = ON');
|
||||
loadSqliteVec(client);
|
||||
|
||||
const migrationsFolder = join(import.meta.dirname, '../db/migrations');
|
||||
|
||||
@@ -33,7 +36,9 @@ function createTestDb(): Database.Database {
|
||||
'0001_quick_nighthawk.sql',
|
||||
'0002_silky_stellaris.sql',
|
||||
'0003_multiversion_config.sql',
|
||||
'0004_complete_sentry.sql'
|
||||
'0004_complete_sentry.sql',
|
||||
'0005_fix_stage_defaults.sql',
|
||||
'0006_yielding_centennial.sql'
|
||||
]) {
|
||||
const statements = readFileSync(join(migrationsFolder, migration), 'utf-8')
|
||||
.split('--> statement-breakpoint')
|
||||
@@ -331,6 +336,41 @@ describe('RepositoryService.remove()', () => {
|
||||
it('throws NotFoundError when the repository does not exist', () => {
|
||||
expect(() => service.remove('/not/found')).toThrow(NotFoundError);
|
||||
});
|
||||
|
||||
it('removes derived vec rows before the repository cascade deletes snippets', () => {
|
||||
const docId = crypto.randomUUID();
|
||||
const snippetId = crypto.randomUUID();
|
||||
const embedding = Float32Array.from([1, 0, 0]);
|
||||
const vecStore = new SqliteVecStore((service as unknown as { db: Database.Database }).db);
|
||||
const db = (service as unknown as { db: Database.Database }).db;
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
db.prepare(
|
||||
`INSERT INTO documents (id, repository_id, version_id, file_path, checksum, indexed_at)
|
||||
VALUES (?, '/facebook/react', NULL, 'README.md', 'repo-doc', ?)`
|
||||
).run(docId, now);
|
||||
db.prepare(
|
||||
`INSERT INTO snippets (id, document_id, repository_id, version_id, type, content, created_at)
|
||||
VALUES (?, ?, '/facebook/react', NULL, 'info', 'repo snippet', ?)`
|
||||
).run(snippetId, docId, now);
|
||||
db.prepare(
|
||||
`INSERT INTO snippet_embeddings (snippet_id, profile_id, model, dimensions, embedding, created_at)
|
||||
VALUES (?, 'local-default', 'test-model', 3, ?, ?)`
|
||||
).run(snippetId, Buffer.from(embedding.buffer), now);
|
||||
vecStore.upsertEmbedding('local-default', snippetId, embedding);
|
||||
|
||||
service.remove('/facebook/react');
|
||||
|
||||
const vecTable = sqliteVecTableName('local-default');
|
||||
const rowidTable = sqliteVecRowidTableName('local-default');
|
||||
const vecCount = db.prepare(`SELECT COUNT(*) as n FROM "${vecTable}"`).get() as { n: number };
|
||||
const rowidCount = db.prepare(`SELECT COUNT(*) as n FROM "${rowidTable}"`).get() as {
|
||||
n: number;
|
||||
};
|
||||
|
||||
expect(vecCount.n).toBe(0);
|
||||
expect(rowidCount.n).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user