import "./setup_env"; import { afterEach, expect, test } from "bun:test"; import { PgDialect } from "drizzle-orm/pg-core"; import { db } from "../src/db_client"; import { completeSecurityArtifactEvaluation, releaseSecurityArtifact } from "../src/db"; import { normalizeSecurityArtifacts } from "@bbbug/shared/security_artifacts"; const originalUpdate = db.update; afterEach(() => { db.update = originalUpdate; }); test("artifact release atomically saves the file list and queues only the waiting task", async () => { let values: Record = {}; let predicate: unknown; (db as any).update = () => ({ set: (v: Record) => { values = v; return { where: (condition: unknown) => { predicate = condition; return { returning: async () => [] }; } }; } }); const files = normalizeSecurityArtifacts([ { name: "app.apk", url: "https://example.com/app.apk" }, { name: "mapping.txt", url: "https://example.com/mapping" }, ]); expect(await releaseSecurityArtifact("task-1", files)).toBe(false); expect(JSON.parse(String(values.artifacts))).toEqual(files); expect(values.status).toBe("awaiting_security"); const query = new PgDialect().sqlToQuery(predicate as any); expect(query.params).toContain("task-1"); expect(query.params).toContain("awaiting_artifacts"); }); test("artifact input accepts legacy URL and rejects conflicting or unsafe filenames", () => { expect(normalizeSecurityArtifacts(undefined, "https://example.com/app")).toEqual([ { name: "prebuilt.apk", url: "https://example.com/app" }, ]); expect(() => normalizeSecurityArtifacts([{ name: "../app.apk", url: "https://example.com/a" }])).toThrow(); expect(normalizeSecurityArtifacts([{ name: "mapping.txt", url: "https://example.com/m" }])).toHaveLength(1); expect(normalizeSecurityArtifacts([])).toEqual([]); }); test("late evaluation cannot move a non-initializing task back to the gate", async () => { let values: Record = {}; let predicate: unknown; (db as any).update = () => ({ set: (v: Record) => { values = v; return { where: (condition: unknown) => { predicate = condition; return { returning: async () => [] }; } }; } }); expect(await completeSecurityArtifactEvaluation("task-2", "TestSpec", true)).toBe(false); expect(values.status).toBe("awaiting_artifacts"); expect(values.executorNodeId).toBeNull(); const query = new PgDialect().sqlToQuery(predicate as any); expect(query.params).toContain("initializing"); expect(query.params).toContain(1); });