import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { resetDb } from '../helpers/db.js'; import { migrateUp } from '../../lib/db/migrate.js'; import { stopBoss, waitForJob } from '../helpers/boss.js'; import { pool } from '../../lib/db/pool.js'; import * as queue from '../../lib/jobs/queue.js'; import { registerWorkers } from '../../lib/jobs/index.js'; import * as spaces from '../../lib/db/repos/spaces.js'; import * as pages from '../../lib/db/repos/pages.js'; import * as refs from '../../lib/db/repos/refs.js'; beforeEach(async () => { await resetDb(); await migrateUp(); await queue.start(); await registerWorkers(); global.fetch = vi.fn(async () => new Response( JSON.stringify({ embedding: new Array(768).fill(0.1) }), { status: 200, headers: { 'content-type': 'application/json' }} )); }); afterEach(async () => { await stopBoss(); vi.restoreAllMocks(); }); describe('repo embed triggers', () => { it('pages.create enqueues embed.text and the worker runs', async () => { const sp = await spaces.create({ slug: 't', name: 'T' }, { kind: 'user', id: null }); const pg = await pages.create( { space_id: sp.id, slug: 'p', title: 'P', body_md: 'b' }, { kind: 'user', id: null } ); // wait until the page row has an embedding for (let i = 0; i < 100; i++) { const { rows: [row] } = await pool.query('SELECT embedding FROM pages WHERE id=$1', [pg.id]); if (row.embedding) return; await new Promise(r => setTimeout(r, 100)); } throw new Error('page was not embedded within 10 s'); }); it('refs.create enqueues embed.text', async () => { const sp = await spaces.create({ slug: 't2', name: 'T2' }, { kind: 'user', id: null }); const r = await refs.create({ space_id: sp.id, kind: 'url', source_url: 'https://example.com/x', title: 'X' }, { kind: 'user', id: null }); for (let i = 0; i < 100; i++) { const { rows: [row] } = await pool.query('SELECT embedding FROM refs WHERE id=$1', [r.id]); if (row.embedding) return; await new Promise(r => setTimeout(r, 100)); } throw new Error('ref was not embedded within 10 s'); }); it('no-op when queue is not running (matches prior server.test behavior)', async () => { // Stop the queue mid-test await stopBoss(); const sp = await spaces.create({ slug: 't3', name: 'T3' }, { kind: 'user', id: null }); // Should not throw const pg = await pages.create( { space_id: sp.id, slug: 'p', title: 'P', body_md: 'b' }, { kind: 'user', id: null } ); expect(pg.id).toBeTruthy(); }); });