|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { resolveInstallerUrl } from "./newAppInstaller.ts"; |
| 5 | + |
| 6 | +const RELEASE = "https://github.com/block/buzz-app/releases/download/v1.0.0"; |
| 7 | +const DOWNLOADS = { |
| 8 | + "macos-arm64": `${RELEASE}/Buzz_1.0.0_aarch64.dmg`, |
| 9 | + "macos-x64": `${RELEASE}/Buzz_1.0.0_x64.dmg`, |
| 10 | + "windows-x64": `${RELEASE}/Buzz_1.0.0_x64_unsigned.exe`, |
| 11 | + "linux-appimage": `${RELEASE}/Buzz_1.0.0_x86_64.AppImage`, |
| 12 | + "linux-deb": `${RELEASE}/Buzz_1.0.0_amd64.deb`, |
| 13 | +}; |
| 14 | + |
| 15 | +function manifest(overrides = {}) { |
| 16 | + return { |
| 17 | + schemaVersion: 1, |
| 18 | + channel: "stable", |
| 19 | + version: "1.0.0", |
| 20 | + release: "v1.0.0", |
| 21 | + downloads: DOWNLOADS, |
| 22 | + ...overrides, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function withDownload(url) { |
| 27 | + return manifest({ downloads: { ...DOWNLOADS, "macos-arm64": url } }); |
| 28 | +} |
| 29 | + |
| 30 | +test("resolves the installer for every shipped target", () => { |
| 31 | + for (const [target, url] of Object.entries(DOWNLOADS)) { |
| 32 | + assert.equal(resolveInstallerUrl(manifest(), target), url, target); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +test("rejects every manifest or entry that is not a buzz-app release asset", () => { |
| 37 | + const cases = [ |
| 38 | + ["manifest is not an object", "not json", "macos-arm64"], |
| 39 | + ["manifest is null", null, "macos-arm64"], |
| 40 | + ["newer schema version", manifest({ schemaVersion: 2 }), "macos-arm64"], |
| 41 | + [ |
| 42 | + "schema version as a string", |
| 43 | + manifest({ schemaVersion: "1" }), |
| 44 | + "macos-arm64", |
| 45 | + ], |
| 46 | + ["other channel", manifest({ channel: "beta" }), "macos-arm64"], |
| 47 | + ["downloads missing", manifest({ downloads: undefined }), "macos-arm64"], |
| 48 | + [ |
| 49 | + "target missing from downloads", |
| 50 | + manifest({ downloads: { "macos-x64": DOWNLOADS["macos-x64"] } }), |
| 51 | + "macos-arm64", |
| 52 | + ], |
| 53 | + ["target unknown to this app", manifest(), "linux-arm64"], |
| 54 | + ["target that is an inherited property", manifest(), "constructor"], |
| 55 | + ["entry is not a string", withDownload(42), "macos-arm64"], |
| 56 | + ["entry is not a URL", withDownload("Buzz.dmg"), "macos-arm64"], |
| 57 | + [ |
| 58 | + "plain http", |
| 59 | + withDownload(DOWNLOADS["macos-arm64"].replace("https:", "http:")), |
| 60 | + "macos-arm64", |
| 61 | + ], |
| 62 | + [ |
| 63 | + "another host", |
| 64 | + withDownload( |
| 65 | + "https://example.org/block/buzz-app/releases/download/v1.0.0/Buzz.dmg", |
| 66 | + ), |
| 67 | + "macos-arm64", |
| 68 | + ], |
| 69 | + [ |
| 70 | + "host hidden behind userinfo", |
| 71 | + withDownload( |
| 72 | + "https://[email protected]/block/buzz-app/releases/download/v1.0.0/Buzz.dmg", |
| 73 | + ), |
| 74 | + "macos-arm64", |
| 75 | + ], |
| 76 | + [ |
| 77 | + "credentials in the URL", |
| 78 | + withDownload( |
| 79 | + "https://user:[email protected]/block/buzz-app/releases/download/v1.0.0/Buzz.dmg", |
| 80 | + ), |
| 81 | + "macos-arm64", |
| 82 | + ], |
| 83 | + [ |
| 84 | + "non-default port", |
| 85 | + withDownload( |
| 86 | + "https://github.com:8443/block/buzz-app/releases/download/v1.0.0/Buzz.dmg", |
| 87 | + ), |
| 88 | + "macos-arm64", |
| 89 | + ], |
| 90 | + [ |
| 91 | + "another repository", |
| 92 | + withDownload( |
| 93 | + "https://github.com/example-org/buzz-app/releases/download/v1.0.0/Buzz.dmg", |
| 94 | + ), |
| 95 | + "macos-arm64", |
| 96 | + ], |
| 97 | + [ |
| 98 | + "dot segments that leave the repository", |
| 99 | + withDownload( |
| 100 | + "https://github.com/block/buzz-app/releases/download/../../../../example-org/other/releases/download/v1/Buzz.dmg", |
| 101 | + ), |
| 102 | + "macos-arm64", |
| 103 | + ], |
| 104 | + [ |
| 105 | + "encoded dot segments that leave the repository", |
| 106 | + withDownload( |
| 107 | + "https://github.com/block/buzz-app/releases/download/%2e%2e/%2e%2e/%2e%2e/%2e%2e/example-org/other/Buzz.dmg", |
| 108 | + ), |
| 109 | + "macos-arm64", |
| 110 | + ], |
| 111 | + [ |
| 112 | + "wrong file type for the target", |
| 113 | + withDownload(DOWNLOADS["windows-x64"]), |
| 114 | + "macos-arm64", |
| 115 | + ], |
| 116 | + [ |
| 117 | + "expected extension only in the query", |
| 118 | + withDownload(`${RELEASE}/Buzz_1.0.0_x64_unsigned.exe?name=Buzz.dmg`), |
| 119 | + "macos-arm64", |
| 120 | + ], |
| 121 | + ]; |
| 122 | + for (const [name, input, target] of cases) { |
| 123 | + assert.equal(resolveInstallerUrl(input, target), null, name); |
| 124 | + } |
| 125 | +}); |
0 commit comments