Skip to content

Commit fcd3737

Browse files
ClaudiaFangclaude
andcommitted
test: update embedding backfill mock and BulkAnalyzeButton tests for useAnalysis context
- embedding.test.ts: mock the DB client singleton so backfill integration tests run against an in-memory DB instead of the real data.db, and seed the insights table schema needed by the zero-pending-rows case. - BulkAnalyzeButton.test.tsx: update tests to mock useAnalysis (AnalysisContext) instead of analyzeSession directly, matching the component's current state-management approach; adjusts progress-count assertions accordingly. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 4989677 commit fcd3737

2 files changed

Lines changed: 129 additions & 70 deletions

File tree

cli/src/embeddings/__tests__/embedding.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
// Unit tests for the embedding pipeline.
22
// Mocks Ollama HTTP responses; tests batching, store operations, and backfill logic.
33

4-
import { describe, it, expect, vi, beforeEach } from 'vitest';
4+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
55
import Database from 'better-sqlite3';
66
import * as sqliteVec from 'sqlite-vec';
77

88
// ─── Mock Ollama before importing the client ───────────────────────
99
const mockFetch = vi.fn();
1010
global.fetch = mockFetch;
1111

12+
// ─── Mock the DB singleton so backfill integration tests run against an
13+
// in-memory database instead of the real ~/.code-insights/data.db ──────
14+
let mockBackfillDb: Database.Database | null = null;
15+
vi.mock('../../db/client.js', () => ({
16+
getDb: () => mockBackfillDb,
17+
}));
18+
1219
function setupMockOllama(dim: number = 768) {
1320
mockFetch.mockImplementation(async (url: string, opts: any) => {
1421
const body = JSON.parse(opts.body);
@@ -518,19 +525,45 @@ describe('backfill integration', () => {
518525
setupMockOllama(768);
519526
});
520527

528+
afterEach(() => {
529+
mockBackfillDb?.close();
530+
mockBackfillDb = null;
531+
});
532+
521533
it('backfillEmbeddings skips when no pending rows', async () => {
522534
const { backfillEmbeddings } = await import('../backfill.js');
523535
const db = freshDb();
536+
mockBackfillDb = db;
524537

525538
// Insert schema tables needed by backfill
526539
db.exec(`
527540
CREATE TABLE IF NOT EXISTS schema_version (version INTEGER PRIMARY KEY, applied_at TEXT);
528541
INSERT INTO schema_version VALUES (1, datetime('now'));
542+
543+
CREATE TABLE IF NOT EXISTS insights (
544+
id TEXT PRIMARY KEY,
545+
session_id TEXT NOT NULL,
546+
project_id TEXT NOT NULL,
547+
project_name TEXT NOT NULL,
548+
type TEXT NOT NULL,
549+
title TEXT NOT NULL,
550+
content TEXT NOT NULL,
551+
summary TEXT NOT NULL,
552+
bullets TEXT NOT NULL,
553+
confidence REAL NOT NULL,
554+
source TEXT NOT NULL,
555+
metadata TEXT,
556+
timestamp TEXT NOT NULL,
557+
created_at TEXT NOT NULL,
558+
scope TEXT NOT NULL,
559+
analysis_version TEXT NOT NULL,
560+
embedding_status TEXT NOT NULL DEFAULT 'pending'
561+
);
529562
`);
563+
// No rows inserted — table exists but has zero pending insights.
530564

531565
const stats = await backfillEmbeddings(TEST_CONFIG, 'insight');
532566
expect(stats.total).toBe(0);
533567
expect(stats.computed).toBe(0);
534-
db.close();
535568
});
536569
});

0 commit comments

Comments
 (0)