import type { HttpDispatchRow } from '../supabase/functions/_backend/utils/http_dispatch.ts' import { createPgliteEngine, Database } from 'tinbase' import { loadSupabaseProject } from 'tinbase/node' import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest' import { HTTP_DISPATCH_CLAIM_SQL } from '../supabase/functions/_backend/utils/http_dispatch.ts' // public.queue_do_targets is the per-queue switch the QueueDispatcher reads // when it claims outbox wakeups (20261202145317_queue_do_targets). const WAKEUP_URL = 'http://localhost/functions/v1/triggers/queue_consumer/sync' describe('queue_do_targets switch', () => { let database: Database async function writeWakeup(target: string): Promise { await database.query(` INSERT INTO public.http_dispatch_outbox (target, url, body, timeout_ms, calls) VALUES ($1, $2, pg_catalog.jsonb_build_object('queue_name', $1::text, 'batch_size', 100), 8000, 2) `, [target, WAKEUP_URL]) } async function claim(): Promise { const { rows } = await database.query(HTTP_DISPATCH_CLAIM_SQL, [100]) return rows } beforeAll(async () => { const project = await loadSupabaseProject(process.cwd()) database = await Database.create(await createPgliteEngine()) await database.runMigrations(project.migrations) }, 300_000) beforeEach(async () => { await database.exec('TRUNCATE public.http_dispatch_outbox; DELETE FROM public.queue_do_targets;') }) afterAll(async () => { await database?.close?.() }) it('is off for every queue after the migration', async () => { await writeWakeup('on_app_update') expect((await claim()).map(row => [row.target, row.queue_do])).toEqual([['on_app_update', false]]) }) it('flags only the listed queues when the dispatcher claims their wakeups', async () => { await database.exec(`INSERT INTO public.queue_do_targets (queue_name) VALUES ('cron_email')`) await writeWakeup('cron_email') await writeWakeup('on_app_update') const rows = await claim() expect(rows.map(row => [row.target, row.queue_do])).toEqual([['cron_email', true], ['on_app_update', false]]) expect(rows[0]).toMatchObject({ url: WAKEUP_URL, body: { queue_name: 'cron_email', batch_size: 100 }, timeout_ms: 8000, calls: 2 }) // The claim still deletes what it returns. expect(await claim()).toEqual([]) }) it('switches a queue back by deleting its row', async () => { await database.exec(`INSERT INTO public.queue_do_targets (queue_name) VALUES ('cron_email')`) await database.exec(`DELETE FROM public.queue_do_targets WHERE queue_name = 'cron_email'`) await writeWakeup('cron_email') expect((await claim()).map(row => row.queue_do)).toEqual([false]) }) it('rejects an empty queue name', async () => { await expect(database.exec(`INSERT INTO public.queue_do_targets (queue_name) VALUES ('')`)).rejects.toThrow() }) })