/** * stacksjs/stacks#3200: the Queue page's per-queue counts come from GROUP BY * queries and must match the loop over every job they replaced. See * fixtures/queue-stats.ts for what runs. */ import { mkdtemp, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { SQL } from 'bun' import { expect, test } from 'bun:test' for (const dialect of ['sqlite', 'postgres', 'mysql'] as const) { const connection = dialect === 'postgres' ? process.env.STACKS_TEST_POSTGRES_URL : process.env.STACKS_TEST_MYSQL_URL test.skipIf(dialect !== 'sqlite' && !connection)(`${dialect} queue counts are the database's, and match counting every job`, async () => { const url = dialect === 'sqlite' ? undefined : new URL(connection!) if (url && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname)) throw new Error('Queue stats tests require a local disposable database server') const directory = await mkdtemp(join(tmpdir(), 'stacks-queue-stats-')) const name = `stacks_queue_stats_${crypto.randomUUID().replaceAll('-', '')}` const admin = url ? new SQL(url.href) : undefined const quoted = dialect === 'mysql' ? `\`${name}\`` : `"${name}"` let created = false try { const config = join(directory, 'bunfig.toml') // Passing --config at this file replaces the repo bunfig, so the child // inherits none of its preloads. The key is left out rather than set to // an empty array: `preload = []` is rejected by Bun before 1.4, which is // still what a stale local install runs. await writeFile(config, '# no preload\n') if (admin) { await admin.unsafe(`CREATE DATABASE ${quoted}`); created = true } const child = Bun.spawn([process.execPath, `--config=${config}`, '--no-env-file', `${import.meta.dir}/fixtures/queue-stats.ts`], { env: { ...process.env, APP_ENV: 'test', TZ: 'UTC', DB_CONNECTION: dialect, DB_QUERY_LOGGING_ENABLED: 'false', DB_DATABASE_PATH: dialect === 'sqlite' ? join(directory, 'queue-stats.sqlite') : ':memory:', STACKS_QUEUE_STATS_CONFIG: config, ...(url ? { DB_DATABASE: name, DB_HOST: url.hostname, DB_PORT: url.port || (dialect === 'mysql' ? '3306' : '5432'), DB_USERNAME: decodeURIComponent(url.username), DB_PASSWORD: decodeURIComponent(url.password), DB_SSL: url.searchParams.get('ssl') === 'true' ? 'true' : 'false' } : {}), }, stdout: 'pipe', stderr: 'pipe', }) let timedOut = false const watchdog = setTimeout(() => { timedOut = true; child.kill() }, 25_000) try { const [code, stdout, stderr] = await Promise.all([child.exited, new Response(child.stdout).text(), new Response(child.stderr).text()]) expect(timedOut, 'the queue stats fixture must finish without a watchdog kill').toBe(false) expect(code, `${stdout}\n${stderr}`).toBe(0) expect(stdout).toContain('queue stats OK') } finally { clearTimeout(watchdog); child.kill() } } finally { try { if (created) await admin!.unsafe(`DROP DATABASE ${quoted}${dialect === 'postgres' ? ' WITH (FORCE)' : ''}`) } finally { try { await admin?.close() } finally { await rm(directory, { recursive: true, force: true }) } } } }, 30_000) }