Files
streamline/control-server/test/security_artifact_gate.test.ts
T
2026-11-16 17:26:44 -08:00

59 lines
2.5 KiB
TypeScript

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<string, unknown> = {};
let predicate: unknown;
(db as any).update = () => ({ set: (v: Record<string, unknown>) => {
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<string, unknown> = {};
let predicate: unknown;
(db as any).update = () => ({ set: (v: Record<string, unknown>) => {
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);
});