Files
Void-Homelab/tests/cron/sync_source_docs.test.js
root 13fac102dd feat(cron): daily sync.source_doc enqueue
node-cron schedules runSync at 03:00 local time; runSync enqueues
sync.source_doc for every source_docs row with sync_source='url'.
Started from server.js's CLI gate alongside the job queue.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 10:14:07 +10:00

52 lines
2.0 KiB
JavaScript

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);
});
});