Files
piik/tests/release-mirror-ready.test.ts
T
TNTcraft ca2834dda3 fix(release): authenticate mirror preparation reads
## Problem and outcome / 问题与结果

Preparing the missing v2.7.0 mirror fetched AtomGit repository metadata
anonymously and failed with HTTP 401 before reaching tag creation.
Publication preparation now uses the configured token for its GET
requests, including the repository endpoint, while the public read-only
readiness helper remains anonymous.

GET 404 still means absent. Preparation uses one HTTP owner, and
mutation errors are not retried.

## Verification / 验证

- The new mocked endpoint regression reproduces the original 401 before
the change and passes afterward; it also checks that public readiness
remains anonymous.
- Publisher, preparation/readiness and source tests: 65 passed.
TypeScript, diff and repository hygiene checks passed.
- Actual changed paths are release tooling and tests only:
`hasProductChanges(e25bd009, HEAD)` is false. With the published v2.7.0
tag fetched, the planner returns `publish: false`; the existing v2.7.0
packages are not rebuilt or replaced.

## Impact / 影响

No App/Server behavior, dependencies, wire or data format changes.
Recovery continues from the original v2.7.0 CI artifacts and source
marker. AtomGit's separate Pull-mirror tag restriction may remain the
next platform blocker; this repair does not bypass it.
2026-11-15 19:08:16 -04:00

130 lines
7.6 KiB
TypeScript

import { expect, test, vi } from "vitest";
import { mirrorReadiness, readMirror } from "../scripts/release-mirror-ready.mjs";
import { prepareMirror, prepareMirrorRelease } from "../scripts/release-mirror-prepare.mjs";
const revision = "a".repeat(40), version = "v2.6.1";
const asset = { name: "piik-app-fixture.zip", state: "uploaded", size: 1024, digest: `sha256:${"b".repeat(64)}` };
const source = { tag_name: version, target_commitish: revision, draft: false, prerelease: false, assets: [asset] };
const mirror = { tag_name: version, prerelease: false, body: `<!-- piik-source: ${revision} -->`, assets: [{
type: "attach", name: asset.name, size: asset.size, browser_download_url: `https://atomgit.com/TNTcraftHIM/Piik/releases/download/${version}/${asset.name}`,
}, ...["zip", "tar.gz", "tar.bz2", "tar"].map(extension => ({ type: "source", name: `${version}.${extension}` }))] };
test("a completed stable mirror needs only its metadata and no source search or package download", async () => {
const read = vi.fn(async () => mirror);
expect(await mirrorReadiness(source, read)).toEqual({ status: "complete", version, revision });
expect(read.mock.calls).toEqual([[`/releases/tags/${version}`]]);
});
test("a pending mirror resumes without querying mirrored commits or tags", async () => {
const read = vi.fn(async () => ({ ...mirror, prerelease: true, assets: [] }));
expect(await mirrorReadiness(source, read)).toEqual({ status: "ready", version, revision });
expect(read.mock.calls).toEqual([[`/releases/tags/${version}`]]);
});
test("read-only readiness never creates a missing release", async () => {
const read = vi.fn(async () => null);
expect(await mirrorReadiness(source, read)).toEqual({ status: "ready", version, revision });
expect(read.mock.calls).toEqual([[`/releases/tags/${version}`]]);
});
test("publication preparation uses the mirror branch and preserves the independent GitHub source marker", async () => {
const read = vi.fn(async (path: string, method?: string, body?: Record<string, unknown>) => {
if (path === `/releases/tags/${version}`) return null;
if (path === "") return { private: false, default_branch: "main" };
expect(path).toBe("/releases");
expect(method).toBe("POST");
expect(body).toMatchObject({ tag_name: version, target_commitish: "main", release_status: "pre" });
expect(body!.body).toContain(`<!-- piik-source: ${revision} -->`);
expect(body!.body).toContain(`https://github.com/TNTcraftHIM/Piik/commit/${revision}`);
return { ...mirror, prerelease: true, body: body!.body, assets: [] };
});
expect(await prepareMirror(source, read)).toEqual({ status: "ready", version, revision });
expect(read).toHaveBeenCalledTimes(4);
});
test("a platform tag rejection fails readiness before artifact downloads", async () => {
const read = vi.fn(async (_path: string, method?: string) => {
if (method === "POST") throw new Error("image repository");
if (_path === "") return { private: false, default_branch: "main" };
return null;
});
await expect(prepareMirror(source, read)).rejects.toThrow("image repository");
expect(read).toHaveBeenCalledTimes(4);
});
test.each([mirror, { ...mirror, prerelease: true, assets: [] }])("preparation reuses existing releases without writes", async existing => {
const request = vi.fn(async (_path: string, _method?: string) => existing);
await prepareMirror(source, request);
expect(request.mock.calls.every(call => call.length === 1)).toBe(true);
});
test("preparation rejects an unknown source and an unexpected create result", async () => {
const request = vi.fn(async (path: string) => path === "" ? { private: false, default_branch: "main" } : null);
await expect(prepareMirrorRelease({ ...source, target_commitish: "main" }, request)).rejects.toThrow("full source SHA");
expect(request).not.toHaveBeenCalled();
await expect(prepareMirrorRelease(source, request)).rejects.toThrow("did not confirm the prepared prerelease");
});
test("preparation reports the platform rejection without logging its token or retrying the write", async () => {
vi.stubEnv("ATOMGIT_TOKEN", "fixture-private-token");
const fetch = vi.fn(async (url: string, options?: RequestInit) => {
if (options?.method === "POST") return Response.json({ message: "image repository fixture-private-token" }, { status: 400 });
if (url.endsWith(`/releases/tags/${version}`)) return new Response(null, { status: 404 });
return Response.json({ private: false, default_branch: "main" });
});
vi.stubGlobal("fetch", fetch);
try {
await expect(prepareMirror(source)).rejects.toThrow("image repository [redacted]");
const writes = fetch.mock.calls.filter(([, options]) => options?.method === "POST");
expect(writes).toHaveLength(1);
const body = JSON.parse(writes[0][1]!.body as string);
expect(body).toMatchObject({ target_commitish: "main", release_status: "pre" });
expect(body.body).toContain(`<!-- piik-source: ${revision} -->`);
} finally { vi.unstubAllGlobals(); vi.unstubAllEnvs(); }
});
test("preparation authenticates the repository GET while public readiness stays anonymous", async () => {
vi.stubEnv("ATOMGIT_TOKEN", "fixture-private-token");
const origin = "https://api.atomgit.com/api/v5/repos/TNTcraftHIM/Piik";
const requests: { url: string; token: string | null }[] = [];
vi.stubGlobal("fetch", vi.fn(async (url: string, options?: RequestInit) => {
const token = new Headers(options?.headers).get("PRIVATE-TOKEN");
requests.push({ url, token });
if (url === origin) {
return token === "fixture-private-token" ? Response.json({ private: false, default_branch: "main" })
: Response.json({ message: "Authentication required" }, { status: 401 });
}
if (options?.method === "POST") return Response.json({ ...mirror, prerelease: true, assets: [] });
return new Response(null, { status: 404 });
}));
try {
expect(await mirrorReadiness(source)).toEqual({ status: "ready", version, revision });
expect(requests).toEqual([{ url: `${origin}/releases/tags/${version}`, token: null }]);
expect(await prepareMirror(source)).toEqual({ status: "ready", version, revision });
expect(requests.find(request => request.url === origin)?.token).toBe("fixture-private-token");
} finally { vi.unstubAllGlobals(); vi.unstubAllEnvs(); }
});
test("a missing attachment or conflicting source marker fails rather than claiming a completed mirror", async () => {
await expect(mirrorReadiness(source, async () => ({ ...mirror, assets: [] }))).rejects.toThrow("incomplete");
await expect(mirrorReadiness(source, async () => ({ ...mirror, body: `<!-- piik-source: ${"c".repeat(40)} -->` }))).rejects.toThrow("different or unknown source revision");
});
test("a provider source archive cannot stand in for a published package", async () => {
await expect(mirrorReadiness(source, async () => ({ ...mirror,
assets: mirror.assets.map(asset => ({ ...asset, type: "source" })),
}))).rejects.toThrow("incomplete");
});
test.each([{ ...source, draft: true }, { ...source, prerelease: true }, { ...source, target_commitish: "main" }])("GitHub published source identity remains required before any mirror lookup", async invalid => {
const read = vi.fn();
await expect(mirrorReadiness(invalid, read)).rejects.toThrow("published GitHub release");
expect(read).not.toHaveBeenCalled();
});
test("readiness treats an unavailable API as failure, not an absent or completed mirror", async () => {
vi.stubGlobal("fetch", vi.fn(async () => new Response(null, { status: 503 })));
try { await expect(mirrorReadiness(source, readMirror)).rejects.toThrow("HTTP 503"); }
finally { vi.unstubAllGlobals(); }
});