import { describe, expect, it } from "vitest"; import { spindleUrl, toPipelineSummary } from "./spindle"; import type * as Pipeline from "./lexicons/types/sh/tangled/ci/pipeline"; describe("spindleUrl", () => { it("gives bare hosts https and localhost plain http", () => { expect(spindleUrl("spindle.tangled.org")).toBe("https://spindle.tangled.org"); expect(spindleUrl("localhost:6555")).toBe("http://localhost:6555"); }); it("keeps an explicit scheme and drops the trailing slash", () => { expect(spindleUrl("http://spindle.test/")).toBe("http://spindle.test"); }); }); const workflow = (over: Partial = {}): Pipeline.Workflow => ({ id: "build", name: "build", status: "success", ...over }); const pipeline = (over: Partial): Pipeline.Main => ({ id: "3lz", commit: "9f2c1ab4d8e7350f6b1c2d9e4a5b6c7d8e9f0a1b", trigger: { $type: "sh.tangled.ci.trigger#manual", sha: "9f2c1ab" }, workflows: [workflow()], ...over }) as Pipeline.Main; describe("toPipelineSummary", () => { it("strips the ref prefix off a push trigger", () => { const summary = toPipelineSummary( pipeline({ trigger: { $type: "sh.tangled.ci.trigger#push", ref: "refs/heads/master", newSha: "a".repeat(40), oldSha: "b".repeat(40) } }) ); expect(summary.trigger).toEqual({ kind: "push", targetRef: "master" }); }); it("reads a pull request's target and source branches", () => { const summary = toPipelineSummary( pipeline({ trigger: { $type: "sh.tangled.ci.trigger#pullRequest", targetBranch: "master", sourceBranch: "fix-timeout", sourceSha: "c".repeat(40) } }) ); expect(summary.trigger).toEqual({ kind: "pull_request", targetRef: "master", sourceLabel: "fix-timeout" }); }); it("keeps a fork's repo did so the page can upgrade the label", () => { const summary = toPipelineSummary( pipeline({ trigger: { $type: "sh.tangled.ci.trigger#pullRequest", targetBranch: "master", sourceBranch: "fix-timeout", sourceSha: "c".repeat(40), sourceRepo: "did:plc:forkowner" } }), "did:plc:thisrepo" ); expect(summary.trigger).toEqual({ kind: "pull_request", targetRef: "master", sourceLabel: "fix-timeout", sourceRepo: "did:plc:forkowner" }); }); it("drops the did when the pull came from the same repo", () => { const summary = toPipelineSummary( pipeline({ trigger: { $type: "sh.tangled.ci.trigger#pullRequest", targetBranch: "master", sourceBranch: "fix-timeout", sourceSha: "c".repeat(40), sourceRepo: "did:plc:thisrepo" } }), "did:plc:thisrepo" ); expect(summary.trigger).toEqual({ kind: "pull_request", targetRef: "master", sourceLabel: "fix-timeout" }); }); it("times a workflow from its own start and finish stamps", () => { const summary = toPipelineSummary( pipeline({ workflows: [ workflow({ startedAt: "2026-08-04T09:00:00.000Z", finishedAt: "2026-08-04T09:01:12.000Z" }), workflow({ name: "test", status: "running", startedAt: "2026-08-04T09:00:00.000Z" }) ] }) ); expect(summary.workflows.map((w) => w.duration)).toEqual([72_000, 0]); }); });