import { test, expect } from "bun:test"; import { syntheticUserIdentity, assertHTTPStatus } from "./http"; const nonce = "01234567-89ab-4cde-8fab-0123456789ab"; test("human fixture labels never become invalid API usernames", () => { for (const label of ["grafana-alert", "Grafana QA", "Сервис"]) { const identity = syntheticUserIdentity(label, nonce); expect(identity.username).toMatch(/^[a-z0-9_]{3,32}$/); expect(identity.display_name).toBe(label); } }); test("unexpected native HTTP result retains its exact outcome before stable failure", async () => { const saved: any[] = []; await expect( assertHTTPStatus( { method: "POST", path: "/api/v1/users", expected: 201, status: 400, content_type: "application/json", body_base64: Buffer.from('{"error":"invalid username"}').toString( "base64", ), }, async (value) => { saved.push(value); }, ), ).rejects.toThrow("HTTP_POST_201_GOT_400"); expect(saved.length).toBe(1); expect(saved[0].status).toBe(400); expect(Buffer.from(saved[0].body_base64, "base64").toString()).toBe( '{"error":"invalid username"}', ); }); test("diagnostic write failure cannot replace HTTP400", async () => { let writes = 0; await expect( assertHTTPStatus( { method: "POST", path: "/api/v1/users", expected: 201, status: 400, content_type: "text/plain", body_base64: "", }, async () => { writes++; throw Error("EVIDENCE_IO_FAILED"); }, ), ).rejects.toThrow("HTTP_POST_201_GOT_400"); expect(writes).toBe(1); });