import { describe, expect, test } from "bun:test"; import { execFileSync, spawnSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { releaseVersion } from "./release-version.cjs"; import { artifactRecordKey, MAX_ARTIFACT_BYTES, publishArtifact, validateArtifactSize } from "./android-release"; describe("Android releases", () => { test("stable tags produce increasing Android versions and reject ambiguous tags", () => { expect(releaseVersion("v0.1.1")).toEqual({ version: "0.1.1", versionCode: 1001 }); expect(releaseVersion("v1.0.0").versionCode).toBeGreaterThan(releaseVersion("v0.999.999").versionCode); for (const tag of ["main", "v01.2.3", "v0.1.1-beta", "v0.0.0", "v1.1000.0", "v2101.0.0", "v1.0.0\n"]) expect(() => releaseVersion(tag)).toThrow(); }); test("only artifacts within Tangled's size limit are accepted", () => { expect(() => validateArtifactSize(MAX_ARTIFACT_BYTES)).not.toThrow(); for (const size of [0, -1, MAX_ARTIFACT_BYTES + 1]) expect(() => validateArtifactSize(size)).toThrow(); }); test("retries share a record key, while separate tags and names stay separate", () => { const key = artifactRecordKey("did:plc:repo", "a".repeat(40), "co.apk"); expect(key).toMatch(/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/); expect(key).toBe(artifactRecordKey("did:plc:repo", "a".repeat(40), "co.apk")); expect(key).not.toBe(artifactRecordKey("did:plc:repo", "b".repeat(40), "co.apk")); expect(key).not.toBe(artifactRecordKey("did:plc:repo", "a".repeat(40), "different.apk")); }); }); const base = { pds: "https://pds.example", identifier: "owner.example", password: "test-secret", did: "did:plc:owner", repo: "did:plc:repo", tagHash: "a".repeat(40), name: "co-v0.1.1-arm64.apk", bytes: new Uint8Array([1, 2, 3]) }; function mock(responses: Array<[number, object]>) { const calls: Array<{ url: string; options: RequestInit }> = []; const fetcher = (async (url: string | URL | Request, options: RequestInit = {}) => { calls.push({ url: String(url), options }); const next = responses.shift(); if (!next) throw new Error("Unexpected request"); return Response.json(next[1], { status: next[0] }); }) as typeof fetch; return { calls, fetcher }; } test("publishes an uploaded APK with the repository DID and annotated tag bytes", async () => { const { calls, fetcher } = mock([ [200, { did: base.did, accessJwt: "test-jwt" }], [400, { error: "RecordNotFound" }], [200, { blob: { $type: "blob", ref: { $link: "bafytest" }, size: 3, mimeType: "application/vnd.android.package-archive" } }], [200, { uri: "at://owner/artifact/record" }], ]); expect(await publishArtifact({ ...base, fetcher })).toEqual({ uri: "at://owner/artifact/record", existing: false }); const created = JSON.parse(calls[3]!.options.body as string); expect(created.repo).toBe(base.did); expect(created.record.repoDid).toBe(base.repo); expect(created.record.tag.$bytes).toBe(Buffer.from(base.tagHash, "hex").toString("base64").replace(/=+$/, "")); expect(created.record.artifact.ref.$link).toBe("bafytest"); expect(calls[2]!.options.body).toEqual(base.bytes); }); test("a repeated pipeline keeps the published artifact immutable", async () => { const { calls, fetcher } = mock([ [200, { did: base.did, accessJwt: "test-jwt" }], [200, { uri: "at://owner/artifact/existing", value: { repoDid: base.repo, name: base.name, tag: { $bytes: Buffer.from(base.tagHash, "hex").toString("base64").replace(/=+$/, "") } } }], ]); expect((await publishArtifact({ ...base, fetcher })).existing).toBe(true); expect(calls).toHaveLength(2); }); test("wrong-account authentication never uploads or publishes", async () => { const { calls, fetcher } = mock([[200, { did: "did:plc:someone-else", accessJwt: "test-jwt" }]]); await expect(publishArtifact({ ...base, fetcher })).rejects.toThrow("does not own"); expect(calls).toHaveLength(1); }); test("a failed upload does not create an artifact or expose credentials", async () => { const { calls, fetcher } = mock([ [200, { did: base.did, accessJwt: "test-jwt" }], [400, { error: "RecordNotFound" }], [500, { error: "Oops", message: base.password }], ]); await expect(publishArtifact({ ...base, fetcher })).rejects.toThrow("com.atproto.repo.uploadBlob failed (HTTP 500)."); expect(calls).toHaveLength(3); }); test("an incomplete upload is never published", async () => { const { fetcher } = mock([[200, { did: base.did, accessJwt: "test-jwt" }], [400, { error: "RecordNotFound" }], [200, { blob: { ref: { $link: "bafytest" }, size: 1 } }]]); await expect(publishArtifact({ ...base, fetcher })).rejects.toThrow("complete APK upload"); }); test("preflight requires an annotated tag at HEAD and rejects branch and manual runs", () => { const directory = mkdtempSync(join(tmpdir(), "co-release-test-")); const script = resolve(import.meta.dir, "android-release.ts"); const environment = { ...process.env, GIT_AUTHOR_NAME: "Release test", GIT_AUTHOR_EMAIL: "test@example.com", GIT_COMMITTER_NAME: "Release test", GIT_COMMITTER_EMAIL: "test@example.com", TANGLED_REF_NAME: "v0.1.1", TANGLED_REF_TYPE: "tag", TANGLED_PIPELINE_KIND: "push", CO_REPO_DID: base.repo, CO_OWNER_DID: base.did, TANGLED_REPO_REPO_DID: base.repo, ANDROID_KEYSTORE_BASE64: "test", ANDROID_KEYSTORE_PASSWORD: "test", ANDROID_KEY_ALIAS: "test", ATP_APP_PASSWORD: "test", ATP_IDENTIFIER: base.identifier, ATP_PDS_URL: base.pds }; const git = (...args: string[]) => execFileSync("git", args, { cwd: directory, env: environment, stdio: "pipe" }); const check = (overrides = {}) => spawnSync(process.execPath, [script, "preflight"], { cwd: directory, env: { ...environment, ...overrides }, encoding: "utf8" }); try { git("init"); git("commit", "--allow-empty", "-m", "Initial"); git("tag", "-a", "v0.1.1", "-m", "Release"); expect(check().status).toBe(0); expect(check({ TANGLED_REF_TYPE: "branch" }).status).not.toBe(0); expect(check({ TANGLED_PIPELINE_KIND: "manual" }).status).not.toBe(0); git("tag", "v0.1.2"); expect(check({ TANGLED_REF_NAME: "v0.1.2" }).status).not.toBe(0); git("commit", "--allow-empty", "-m", "Later"); expect(check().stderr).toContain("does not point at"); } finally { rmSync(directory, { recursive: true, force: true }); } });