import { describe, expect, it, vi } from "vite-plus/test"; import { parseAndroidApkReleaseTag, resolveAndroidApkRepository, } from "./lib/android-apk-release.ts"; import { publishForkApk } from "./publish-fork-apk.ts"; describe("APK release version codes", () => { it("accepts Android version codes, including the platform maximum", () => { expect(parseAndroidApkReleaseTag("apk-42")).toBe(42); expect(parseAndroidApkReleaseTag("apk-2100000000")).toBe(2100000000); }); it.each([ "v1.2.3", "apk-0", "apk-01", "apk--1", "apk-1.5", "apk-2100000001", "apk-1/../../secret", ])("rejects invalid release tag %s", (tag) => expect(() => parseAndroidApkReleaseTag(tag)).toThrow("APK release tags"), ); }); describe("fork APK publishing", () => { const options = { tag: "apk-42", commit: "abc123", token: "test-token", repositoryUrl: "https://git.example.com/new-owner/renamed-repo", apk: new Blob(["apk"]), checksum: new Blob(["checksum"]), }; it("keeps the release in draft until both authenticated uploads finish", async () => { const request = vi.fn().mockResolvedValue(new Response("{}")); request.mockResolvedValueOnce(Response.json({ id: 123 })); await publishForkApk({ ...options, request }); expect(request).toHaveBeenCalledTimes(4); const [create, apk, checksum, publish] = request.mock.calls; expect(create?.[0]).toBe( "https://git.example.com/api/v1/repos/new-owner/renamed-repo/releases", ); expect(JSON.parse(String(create?.[1]?.body))).toMatchObject({ draft: true, tag_name: "apk-42", target_commitish: "abc123", }); expect(apk?.[0]).toContain("/123/assets?name=t3code-apk-42.apk"); const attachment = apk?.[1]?.body; expect(attachment).toBeInstanceOf(FormData); if (!(attachment instanceof FormData)) throw new Error("Missing upload form"); expect(attachment.get("attachment")).toBeInstanceOf(Blob); expect(checksum?.[0]).toContain("t3code-apk-42.apk.sha256"); expect(publish?.[1]?.method).toBe("PATCH"); expect(JSON.parse(String(publish?.[1]?.body))).toEqual({ draft: false }); for (const [, init] of request.mock.calls) { expect(init?.headers).toMatchObject({ Authorization: "token test-token" }); expect(init?.redirect).toBe("error"); } }); it("passes a configured release body to the repository release", async () => { const request = vi.fn().mockResolvedValue(new Response("{}")); request.mockResolvedValueOnce(Response.json({ id: 123 })); await publishForkApk({ ...options, body: "Local release notes", request }); expect(JSON.parse(String(request.mock.calls[0]?.[1]?.body))).toMatchObject({ body: "Local release notes", }); }); it("does not publish an incomplete release after an upload failure", async () => { const request = vi .fn() .mockResolvedValueOnce(Response.json({ id: 123 })) .mockResolvedValueOnce(new Response("", { status: 500 })); await expect(publishForkApk({ ...options, request })).rejects.toThrow("HTTP 500"); expect(request).toHaveBeenCalledTimes(2); }); it("reports authentication failures without exposing credentials", async () => { const request = vi.fn().mockResolvedValue(new Response("", { status: 401 })); await expect(publishForkApk({ ...options, request })).rejects.toThrow("HTTP 401"); expect(request).toHaveBeenCalledTimes(1); }); it("does not upload without a valid release ID", async () => { const request = vi.fn().mockResolvedValue(Response.json({ id: "../other" })); await expect(publishForkApk({ ...options, request })).rejects.toThrow("valid release ID"); expect(request).toHaveBeenCalledTimes(1); }); }); describe("APK repository location", () => { it.each(["original-owner", "new-owner"])("derives release locations for %s", (owner) => { expect(resolveAndroidApkRepository(`https://git.example.com/${owner}/app.git/`)).toEqual({ releasesUrl: `https://git.example.com/${owner}/app/releases`, apiUrl: `https://git.example.com/api/v1/repos/${owner}/app/releases`, }); }); it("supports a Git server hosted under a subpath", () => { expect(resolveAndroidApkRepository("https://example.com/git/owner/app").apiUrl).toBe( "https://example.com/git/api/v1/repos/owner/app/releases", ); }); it.each([ "http://example.com/owner/app", "https://token@example.com/owner/app", "https://example.com/owner", "https://example.com/owner/app?token=secret", ])("rejects invalid repository URL %s", (url) => { expect(() => resolveAndroidApkRepository(url)).toThrow(); }); });