import { describe, expect, it, vi } from "vitest";
import { didOf } from "#lib/api/syntax.ts";
import type { OAuthUserAgent } from "@atcute/oauth-browser-client";
import type { Fetch } from "#lib/fetch.ts";
vi.mock("#lib/auth/agent.ts", () => ({
createClient: () => ({}),
mintServiceAuth: async () => "token",
serviceDidForHost: (host: string) => `did:web:${host}`,
hostForServiceDid: () => null
}));
vi.mock("#lib/api/knot.ts", () => ({
knotCapabilities: async () => null
}));
const { approvePipeline, retryPipeline } = await import("#lib/api/pipelineActions.ts");
const agent = { sub: "did:plc:nel" } as unknown as OAuthUserAgent;
const target = { repoDid: didOf("did:plc:repoproof") };
const spindle = new URL("https://spindle.test");
const answer = (status: number, body?: unknown) => {
const fetch = vi.fn<Fetch>(
async () =>
new Response(body === undefined ? null : JSON.stringify(body), {
status,
headers: { "content-type": "application/json" }
})
);
vi.stubGlobal("fetch", fetch);
return fetch;
};
describe("pipeline actions", () => {
it("approves a pipeline on the repo's spindle", async () => {
const fetch = answer(200);
await approvePipeline(agent, spindle, target, "3mypipeline");
const [url, init] = fetch.mock.calls[0];
expect(String(url)).toBe("https://spindle.test/xrpc/org.tangled.ci.approvePipeline");
expect(new Headers(init?.headers).get("authorization")).toBe("Bearer token");
expect(JSON.parse(String(init?.body))).toEqual({
repo: target.repoDid,
pipeline: "3mypipeline"
});
});
it("rejects with the spindle's reason", async () => {
answer(400, {
error: "PipelineNotBlocked",
message: "the pipeline has no blocked workflow"
});
await expect(approvePipeline(agent, spindle, target, "3mypipeline")).rejects.toThrow(
"the pipeline has no blocked workflow"
);
});
it("retries a pipeline and hands back the new run", async () => {
const fetch = answer(200, { pipeline: "3mretried" });
await expect(retryPipeline(agent, spindle, target, "3mpipeline")).resolves.toBe(
"3mretried"
);
const [url, init] = fetch.mock.calls[0];
expect(String(url)).toBe("https://spindle.test/xrpc/org.tangled.ci.retryPipeline");
expect(JSON.parse(String(init?.body))).toEqual({
repo: target.repoDid,
pipeline: "3mpipeline"
});
});
});