|
| 1 | +import { beforeEach, describe, expect, it, jest } from "@jest/globals"; |
| 2 | + |
| 3 | +const mockInfo = jest.fn(); |
| 4 | +const mockWarning = jest.fn(); |
| 5 | + |
| 6 | +jest.mock("@actions/core", () => ({ |
| 7 | + debug: jest.fn(), |
| 8 | + info: mockInfo, |
| 9 | + warning: mockWarning, |
| 10 | +})); |
| 11 | + |
| 12 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 13 | +const mockDownloadTool = jest.fn<any>(); |
| 14 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 15 | +const mockExtractTar = jest.fn<any>(); |
| 16 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 17 | +const mockExtractZip = jest.fn<any>(); |
| 18 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 19 | +const mockCacheDir = jest.fn<any>(); |
| 20 | + |
| 21 | +jest.mock("@actions/tool-cache", () => { |
| 22 | + const actual = jest.requireActual("@actions/tool-cache") as Record< |
| 23 | + string, |
| 24 | + unknown |
| 25 | + >; |
| 26 | + |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + cacheDir: mockCacheDir, |
| 30 | + downloadTool: mockDownloadTool, |
| 31 | + extractTar: mockExtractTar, |
| 32 | + extractZip: mockExtractZip, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 37 | +const mockGetLatestVersionFromNdjson = jest.fn<any>(); |
| 38 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 39 | +const mockGetAllVersionsFromNdjson = jest.fn<any>(); |
| 40 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 41 | +const mockGetArtifactFromNdjson = jest.fn<any>(); |
| 42 | + |
| 43 | +jest.mock("../../src/download/versions-client", () => ({ |
| 44 | + getAllVersions: mockGetAllVersionsFromNdjson, |
| 45 | + getArtifact: mockGetArtifactFromNdjson, |
| 46 | + getLatestVersion: mockGetLatestVersionFromNdjson, |
| 47 | +})); |
| 48 | + |
| 49 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 50 | +const mockGetAllManifestVersions = jest.fn<any>(); |
| 51 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 52 | +const mockGetLatestVersionInManifest = jest.fn<any>(); |
| 53 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 54 | +const mockGetManifestArtifact = jest.fn<any>(); |
| 55 | + |
| 56 | +jest.mock("../../src/download/version-manifest", () => ({ |
| 57 | + getAllVersions: mockGetAllManifestVersions, |
| 58 | + getLatestKnownVersion: mockGetLatestVersionInManifest, |
| 59 | + getManifestArtifact: mockGetManifestArtifact, |
| 60 | +})); |
| 61 | + |
| 62 | +// biome-ignore lint/suspicious/noExplicitAny: Mock requires flexible typing in tests. |
| 63 | +const mockValidateChecksum = jest.fn<any>(); |
| 64 | + |
| 65 | +jest.mock("../../src/download/checksum/checksum", () => ({ |
| 66 | + validateChecksum: mockValidateChecksum, |
| 67 | +})); |
| 68 | + |
| 69 | +import { |
| 70 | + downloadVersionFromManifest, |
| 71 | + downloadVersionFromNdjson, |
| 72 | + resolveVersion, |
| 73 | +} from "../../src/download/download-version"; |
| 74 | + |
| 75 | +describe("download-version", () => { |
| 76 | + beforeEach(() => { |
| 77 | + mockInfo.mockReset(); |
| 78 | + mockWarning.mockReset(); |
| 79 | + mockDownloadTool.mockReset(); |
| 80 | + mockExtractTar.mockReset(); |
| 81 | + mockExtractZip.mockReset(); |
| 82 | + mockCacheDir.mockReset(); |
| 83 | + mockGetLatestVersionFromNdjson.mockReset(); |
| 84 | + mockGetAllVersionsFromNdjson.mockReset(); |
| 85 | + mockGetArtifactFromNdjson.mockReset(); |
| 86 | + mockGetAllManifestVersions.mockReset(); |
| 87 | + mockGetLatestVersionInManifest.mockReset(); |
| 88 | + mockGetManifestArtifact.mockReset(); |
| 89 | + mockValidateChecksum.mockReset(); |
| 90 | + |
| 91 | + mockDownloadTool.mockResolvedValue("/tmp/downloaded"); |
| 92 | + mockExtractTar.mockResolvedValue("/tmp/extracted"); |
| 93 | + mockExtractZip.mockResolvedValue("/tmp/extracted"); |
| 94 | + mockCacheDir.mockResolvedValue("/tmp/cached"); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("resolveVersion", () => { |
| 98 | + it("uses astral-sh/versions to resolve latest", async () => { |
| 99 | + mockGetLatestVersionFromNdjson.mockResolvedValue("0.9.26"); |
| 100 | + |
| 101 | + const version = await resolveVersion("latest", undefined); |
| 102 | + |
| 103 | + expect(version).toBe("0.9.26"); |
| 104 | + expect(mockGetLatestVersionFromNdjson).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + |
| 107 | + it("uses astral-sh/versions to resolve available versions", async () => { |
| 108 | + mockGetAllVersionsFromNdjson.mockResolvedValue(["0.9.26", "0.9.25"]); |
| 109 | + |
| 110 | + const version = await resolveVersion("^0.9.0", undefined); |
| 111 | + |
| 112 | + expect(version).toBe("0.9.26"); |
| 113 | + expect(mockGetAllVersionsFromNdjson).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it("does not fall back when astral-sh/versions fails", async () => { |
| 117 | + mockGetLatestVersionFromNdjson.mockRejectedValue( |
| 118 | + new Error("NDJSON unavailable"), |
| 119 | + ); |
| 120 | + |
| 121 | + await expect(resolveVersion("latest", undefined)).rejects.toThrow( |
| 122 | + "NDJSON unavailable", |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it("uses manifest-file when provided", async () => { |
| 127 | + mockGetAllManifestVersions.mockResolvedValue(["0.9.26", "0.9.25"]); |
| 128 | + |
| 129 | + const version = await resolveVersion( |
| 130 | + "^0.9.0", |
| 131 | + "https://example.com/custom.ndjson", |
| 132 | + ); |
| 133 | + |
| 134 | + expect(version).toBe("0.9.26"); |
| 135 | + expect(mockGetAllManifestVersions).toHaveBeenCalledWith( |
| 136 | + "https://example.com/custom.ndjson", |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("downloadVersionFromNdjson", () => { |
| 142 | + it("fails when NDJSON metadata lookup fails", async () => { |
| 143 | + mockGetArtifactFromNdjson.mockRejectedValue( |
| 144 | + new Error("NDJSON unavailable"), |
| 145 | + ); |
| 146 | + |
| 147 | + await expect( |
| 148 | + downloadVersionFromNdjson( |
| 149 | + "unknown-linux-gnu", |
| 150 | + "x86_64", |
| 151 | + "0.9.26", |
| 152 | + undefined, |
| 153 | + "token", |
| 154 | + ), |
| 155 | + ).rejects.toThrow("NDJSON unavailable"); |
| 156 | + |
| 157 | + expect(mockDownloadTool).not.toHaveBeenCalled(); |
| 158 | + expect(mockValidateChecksum).not.toHaveBeenCalled(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("fails when no matching artifact exists in NDJSON metadata", async () => { |
| 162 | + mockGetArtifactFromNdjson.mockResolvedValue(undefined); |
| 163 | + |
| 164 | + await expect( |
| 165 | + downloadVersionFromNdjson( |
| 166 | + "unknown-linux-gnu", |
| 167 | + "x86_64", |
| 168 | + "0.9.26", |
| 169 | + undefined, |
| 170 | + "token", |
| 171 | + ), |
| 172 | + ).rejects.toThrow( |
| 173 | + "Could not find artifact for version 0.9.26, arch x86_64, platform unknown-linux-gnu in https://raw.githubusercontent.com/astral-sh/versions/main/v1/uv.ndjson .", |
| 174 | + ); |
| 175 | + |
| 176 | + expect(mockDownloadTool).not.toHaveBeenCalled(); |
| 177 | + expect(mockValidateChecksum).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + |
| 180 | + it("uses built-in checksums for default NDJSON downloads", async () => { |
| 181 | + mockGetArtifactFromNdjson.mockResolvedValue({ |
| 182 | + archiveFormat: "tar.gz", |
| 183 | + sha256: "ndjson-checksum-that-should-be-ignored", |
| 184 | + url: "https://example.com/uv.tar.gz", |
| 185 | + }); |
| 186 | + |
| 187 | + await downloadVersionFromNdjson( |
| 188 | + "unknown-linux-gnu", |
| 189 | + "x86_64", |
| 190 | + "0.9.26", |
| 191 | + undefined, |
| 192 | + "token", |
| 193 | + ); |
| 194 | + |
| 195 | + expect(mockValidateChecksum).toHaveBeenCalledWith( |
| 196 | + undefined, |
| 197 | + "/tmp/downloaded", |
| 198 | + "x86_64", |
| 199 | + "unknown-linux-gnu", |
| 200 | + "0.9.26", |
| 201 | + ); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + describe("downloadVersionFromManifest", () => { |
| 206 | + it("uses manifest-file checksum metadata when checksum input is unset", async () => { |
| 207 | + mockGetManifestArtifact.mockResolvedValue({ |
| 208 | + archiveFormat: "tar.gz", |
| 209 | + checksum: "manifest-checksum", |
| 210 | + downloadUrl: "https://example.com/custom-uv.tar.gz", |
| 211 | + }); |
| 212 | + |
| 213 | + await downloadVersionFromManifest( |
| 214 | + "https://example.com/custom.ndjson", |
| 215 | + "unknown-linux-gnu", |
| 216 | + "x86_64", |
| 217 | + "0.9.26", |
| 218 | + "", |
| 219 | + "token", |
| 220 | + ); |
| 221 | + |
| 222 | + expect(mockValidateChecksum).toHaveBeenCalledWith( |
| 223 | + "manifest-checksum", |
| 224 | + "/tmp/downloaded", |
| 225 | + "x86_64", |
| 226 | + "unknown-linux-gnu", |
| 227 | + "0.9.26", |
| 228 | + ); |
| 229 | + }); |
| 230 | + |
| 231 | + it("prefers checksum input over manifest-file checksum metadata", async () => { |
| 232 | + mockGetManifestArtifact.mockResolvedValue({ |
| 233 | + archiveFormat: "tar.gz", |
| 234 | + checksum: "manifest-checksum", |
| 235 | + downloadUrl: "https://example.com/custom-uv.tar.gz", |
| 236 | + }); |
| 237 | + |
| 238 | + await downloadVersionFromManifest( |
| 239 | + "https://example.com/custom.ndjson", |
| 240 | + "unknown-linux-gnu", |
| 241 | + "x86_64", |
| 242 | + "0.9.26", |
| 243 | + "user-checksum", |
| 244 | + "token", |
| 245 | + ); |
| 246 | + |
| 247 | + expect(mockValidateChecksum).toHaveBeenCalledWith( |
| 248 | + "user-checksum", |
| 249 | + "/tmp/downloaded", |
| 250 | + "x86_64", |
| 251 | + "unknown-linux-gnu", |
| 252 | + "0.9.26", |
| 253 | + ); |
| 254 | + }); |
| 255 | + }); |
| 256 | +}); |
0 commit comments