Add drag reordering, exam deadlines, frozen shared quizzes, batched OCR and semantic search
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 33s

- Drag-to-reorder for class materials, folders, quiz questions and deck cards. Pointer-based
  so touch works, plus keyboard (arrows/Home/End) with screen-reader announcements.
- Timed exams get a wall-clock deadline. The server refuses late answers, submits overdue
  exams with what was saved, and the countdown keeps running while the page is closed.
- Shared quiz sessions keep a frozen copy of their questions and flag later owner edits.
- Scanned PDFs are read in cached 8-page batches with real page numbers (pdf-lib), removing
  the 30 MB whole-file limit. Compatible providers now receive PDFs as file parts.
- Semantic search: embeddings for OpenAI, Gemini, Ollama and compatible servers, background
  indexing into chunk_vectors (migration 2), hybrid keyword + vector ranking for search, the
  assistant and focused generation, and a Settings -> AI section.

181 tests pass locally and in the Docker test stage on Node 24.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-12 11:48:20 -07:00
parent 3046e46d1c
commit ccdad9728b
39 changed files with 1772 additions and 121 deletions

120
tests/semantic.test.ts Normal file
View file

@ -0,0 +1,120 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import { queueIdle } from '../server/services/documents.ts';
import { hybridSearch, indexingIdle } from '../server/services/semantic.ts';
import { createTestApp, type TestApp } from './helpers.ts';
import { startMockAi, type MockAi } from './mockAi.ts';
let mock: MockAi;
let t: TestApp;
beforeAll(async () => {
mock = await startMockAi();
});
afterAll(() => mock.close());
beforeEach(async () => {
t = await createTestApp();
mock.requests.length = 0;
});
afterEach(async () => {
await indexingIdle();
t.close();
});
const DIURETICS = `# Loop diuretics
Furosemide acts on the ascending limb of the loop of Henle. Watch for hypokalemia: encourage potassium-rich foods and check serum levels before each dose.`;
const DIGOXIN = `# Cardiac glycosides
Digoxin strengthens contraction and slows the heart rate. Toxicity shows as nausea, vomiting and yellow-green halos around lights. Take the apical pulse for one minute first.`;
async function setup() {
const provider = (await t.api('POST', '/api/ai/providers', { name: 'Mock', kind: 'openai_compatible', baseUrl: mock.url, apiKey: 'mock-key' })).body.provider;
const cls = (await t.api('POST', '/api/classes', { name: 'Pharmacology' })).body.class;
for (const [title, text] of [
['Diuretics lecture', DIURETICS],
['Cardiac glycosides lecture', DIGOXIN],
]) {
expect((await t.api('POST', '/api/documents/text', { classId: cls.id, title, text })).status).toBe(201);
}
await queueIdle(t.ctx);
return { provider, cls };
}
async function enable(providerId: string, model = 'mock-embedding-1') {
const res = await t.api('PUT', '/api/ai/embeddings', { providerId, model, dimensions: null });
expect(res.status).toBe(200);
await indexingIdle();
return (await t.api('GET', '/api/ai/embeddings')).body;
}
describe('semantic search', () => {
it('is off by default and search falls back to keywords', async () => {
const { cls } = await setup();
const status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status).toMatchObject({ configured: false, indexed: 0 });
expect(status.total).toBeGreaterThan(0);
const hits = await hybridSearch(t.ctx, 'digoxin', { classId: cls.id }, 3);
expect(hits[0].documentTitle).toBe('Cardiac glycosides lecture');
expect(mock.requests.filter((r) => r.path.endsWith('/embeddings'))).toHaveLength(0);
});
it('indexes every passage once and finds paraphrased questions that share no keywords', async () => {
const { provider, cls } = await setup();
const status = await enable(provider.id);
expect(status).toMatchObject({ configured: true, running: false, lastError: '' });
expect(status.indexed).toBe(status.total);
// No word of this query appears in the passage, so keyword search alone finds nothing.
const query = 'why should lasix patients eat bananas';
const search = (await t.api('GET', `/api/search?q=${encodeURIComponent(query)}&classId=${cls.id}`)).body.results as { kind: string; title: string; semantic?: boolean }[];
const passages = search.filter((r) => r.kind === 'passage');
expect(passages[0]).toMatchObject({ title: 'Diuretics lecture', semantic: true });
const hits = await hybridSearch(t.ctx, query, { classId: cls.id }, 2);
expect(hits[0].documentTitle).toBe('Diuretics lecture');
// Usage is logged, and a second index run embeds nothing new.
const usage = t.ctx.db.all<{ task: string }>("SELECT task FROM ai_usage WHERE task LIKE 'semantic%'");
expect(usage.some((u) => u.task === 'semantic indexing')).toBe(true);
const before = mock.requests.length;
await t.api('POST', '/api/ai/embeddings/resume');
await indexingIdle();
expect(mock.requests.length).toBe(before);
});
it('indexes documents added later and drops vectors when the model changes', async () => {
const { provider, cls } = await setup();
await enable(provider.id);
await t.api('POST', '/api/documents/text', { classId: cls.id, title: 'Insulin notes', text: '# Insulin\n\nRapid-acting insulin starts within 15 minutes; give it with a meal.' });
await queueIdle(t.ctx);
await indexingIdle();
let status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status.indexed).toBe(status.total);
status = await enable(provider.id, 'mock-embedding-2');
expect(status.indexed).toBe(status.total);
const keys = t.ctx.db.all<{ model_key: string }>('SELECT DISTINCT model_key FROM chunk_vectors');
expect(keys).toHaveLength(1);
expect(keys[0].model_key).toContain('mock-embedding-2');
});
it('refuses providers without an embeddings API and clears settings when the provider is deleted', async () => {
const { provider } = await setup();
const claude = (await t.api('POST', '/api/ai/providers', { name: 'Claude', kind: 'anthropic', apiKey: 'sk-ant-test' })).body.provider;
const refused = await t.api('PUT', '/api/ai/embeddings', { providerId: claude.id, model: 'anything', dimensions: null });
expect(refused.status).toBe(400);
await enable(provider.id);
await t.api('DELETE', `/api/ai/providers/${provider.id}`);
const status = (await t.api('GET', '/api/ai/embeddings')).body;
expect(status).toMatchObject({ configured: false, indexed: 0 });
});
it('keeps working on keywords when the embedding provider fails', async () => {
const { provider, cls } = await setup();
await enable(provider.id);
await t.api('PATCH', `/api/ai/providers/${provider.id}`, { apiKey: 'wrong-key' });
const hits = await hybridSearch(t.ctx, 'apical pulse before digoxin', { classId: cls.id }, 2);
expect(hits[0].documentTitle).toBe('Cardiac glycosides lecture');
});
});