import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { resetDb } from '../helpers/db.js'; import { migrateUp } from '../../lib/db/migrate.js'; import { stopBoss } 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 { runSync } from '../../lib/cron/sync_source_docs.js'; import * as jobs from '../../lib/db/repos/jobs.js'; beforeEach(async () => { await resetDb(); await migrateUp(); await queue.start(); await registerWorkers(); }); afterEach(async () => { await stopBoss(); }); describe('cron/sync_source_docs.runSync', () => { it('enqueues sync.source_doc for each url-synced row', async () => { const sp = (await pool.query( `INSERT INTO spaces(slug, name) VALUES('s','S') RETURNING id` )).rows[0].id; const res = (await pool.query( `INSERT INTO resources(space_id, slug, name, runtime_type) VALUES($1,'r','R','lxc') RETURNING id`, [sp] )).rows[0].id; await pool.query( `INSERT INTO source_docs(resource_id, name, upstream_url, sync_source) VALUES($1,'doc','https://example.com/r','url')`, [res] ); const enqueued = await runSync(); expect(enqueued).toBe(1); const queued = await jobs.list({ name: 'sync.source_doc' }); expect(queued.length).toBe(1); }); it('skips rows without sync_source=url', async () => { const sp = (await pool.query( `INSERT INTO spaces(slug, name) VALUES('s2','S2') RETURNING id` )).rows[0].id; const res = (await pool.query( `INSERT INTO resources(space_id, slug, name, runtime_type) VALUES($1,'r','R','lxc') RETURNING id`, [sp] )).rows[0].id; await pool.query( `INSERT INTO source_docs(resource_id, name, upstream_url, sync_source) VALUES($1,'doc','https://example.com/r','manual')`, [res] ); const enqueued = await runSync(); expect(enqueued).toBe(0); }); });