import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import test from "node:test"; import { startPostgresFixture } from "./helpers/postgres-fixture.ts"; // BUG-2418: one person may queue several reports; the worker runs one of // theirs at a time, in order, and never two at once even from two lanes. const runnerPath = new URL("../scripts/db-migrate.mjs", import.meta.url).pathname; const USER_A = "a1a1a1a1-a1a1-4a1a-8a1a-a1a1a1a1a1a1"; const USER_B = "b2b2b2b2-b2b2-4b2b-8b2b-b2b2b2b2b2b2"; const REPORTS_A = [ ["c3c3c3c3-c3c3-4c3c-8c3c-c3c3c3c3c301", "d4d4d4d4-d4d4-4d4d-8d4d-d4d4d4d4d401", "1".repeat(64)], ["c3c3c3c3-c3c3-4c3c-8c3c-c3c3c3c3c302", "d4d4d4d4-d4d4-4d4d-8d4d-d4d4d4d4d402", "2".repeat(64)], ["c3c3c3c3-c3c3-4c3c-8c3c-c3c3c3c3c303", "d4d4d4d4-d4d4-4d4d-8d4d-d4d4d4d4d403", "3".repeat(64)], ] as const; const REPORT_B = ["e5e5e5e5-e5e5-4e5e-8e5e-e5e5e5e5e501", "f6f6f6f6-f6f6-4f6f-8f6f-f6f6f6f6f601", "4".repeat(64)] as const; function insertGeneratingReport(userId: string, [reportId, requestId, fingerprint]: readonly [string, string, string]): string { return ` insert into public.personal_reports ( id, user_id, request_id, request_fingerprint, report_type, status, schema_version, presentation_mode, requested_themes, depth, skill_name, skill_version, skill_source_commit, skill_snapshot_sha256 ) values ( '${reportId}', '${userId}', '${requestId}', '${fingerprint}', 'personal_full', 'generating', 'report_document.v2', 'default', array['general']::text[], 'standard', 'jyotisha-personal-report', '1.0.0', '${"5".repeat(40)}', '${"6".repeat(64)}' ); `; } test("BUG-2418: a person's second and third reports queue; one runs at a time, in order", () => { const fixture = startPostgresFixture(); try { const migration = spawnSync(process.execPath, [runnerPath], { encoding: "utf8", env: { ...process.env, SCHEMA_DATABASE_URL: fixture.connectionUrl("schema_owner", "schema-owner-test-password") }, }); assert.equal(migration.status, 0, migration.stderr); assert.match(migration.stdout, /applied 20261126040000_report_queue_per_user\.sql/); fixture.psql(` insert into identity.users (id, name, email, email_verified, email_verified_at) values ('${USER_A}', 'Queue A', 'queue-a@example.com', true, now()), ('${USER_B}', 'Queue B', 'queue-b@example.com', true, now()); `); // Three reports in flight for one person no longer violate any index. for (const report of REPORTS_A) fixture.psql(insertGeneratingReport(USER_A, report)); assert.equal(fixture.psql(`select count(*) from public.personal_report_jobs where user_id = '${USER_A}' and status = 'queued'`), "3"); const claim = (fn: string) => fixture.psqlAs( "service_runtime", "service-runtime-test-password", `set role service_role; select coalesce((select request_id::text from ${fn}), 'none');`, ).split("\n").at(-1)!; // The first one runs; the person's others wait, from either claim function. assert.equal(claim(`public.claim_personal_report_job('lane-1', 60, null)`), REPORTS_A[0][1]); assert.equal(claim(`public.claim_personal_report_job('lane-2', 60, null)`), "none"); assert.equal(claim(`public.claim_personal_report_job_for_types('lane-2', 60, array['personal_full'])`), "none"); assert.equal(claim(`public.claim_personal_report_job('lane-2', 60, (select id from public.personal_report_jobs where request_id = '${REPORTS_A[1][1]}'))`), "none"); // Someone else is not held up by that person's line. fixture.psql(insertGeneratingReport(USER_B, REPORT_B)); assert.equal(claim(`public.claim_personal_report_job('lane-2', 60, null)`), REPORT_B[1]); // The database itself refuses a second running job for one person. assert.throws(() => fixture.psql(` update public.personal_report_jobs set status = 'running', attempt_count = attempt_count + 1, lease_token = gen_random_uuid(), lease_owner = 'manual', lease_acquired_at = clock_timestamp(), lease_expires_at = clock_timestamp() + interval '60 seconds', heartbeat_at = clock_timestamp(), started_at = clock_timestamp() where request_id = '${REPORTS_A[1][1]}'; `), /personal_report_jobs_one_running_per_user/); // The queue view tells the third report how many of the person's own come first. const mineAhead = fixture.psqlAs( "app_runtime", "app-runtime-test-password", `set role authenticated; select set_config('request.jwt.claim.sub', '${USER_A}', true); select (public.personal_report_queue_view( (select id from public.personal_report_jobs where request_id = '${REPORTS_A[2][1]}') ) ->> 'mineAhead');`, ).split("\n").at(-1)!; assert.equal(mineAhead, "2"); } finally { fixture.stop(); } });