| 1 | const assert = require("node:assert/strict"); |
| 2 | const crypto = require("node:crypto"); |
| 3 | const fs = require("node:fs"); |
| 4 | const http = require("node:http"); |
| 5 | const os = require("node:os"); |
| 6 | const path = require("node:path"); |
| 7 | const test = require("node:test"); |
| 8 | |
| 9 | const { |
| 10 | CHECKSUM_MANIFEST, |
| 11 | cnbReleaseBaseUrl, |
| 12 | explicitReleaseBase, |
| 13 | firstPartyReleaseSources, |
| 14 | githubReleaseBaseUrl, |
| 15 | hasExplicitReleaseBase, |
| 16 | shouldRaceFirstPartyMirrors, |
| 17 | } = require("../scripts/artifacts"); |
| 18 | const { run, _internal } = require("../scripts/install"); |
| 19 | |
| 20 | const VERSION = "0.9.10"; |
| 21 | const REPO = "Hmbown/CodeWhale"; |
| 22 | const CODEWHALE_ASSET = "codewhale-linux-x64"; |
| 23 | const CODEW_ASSET = "codew-linux-x64"; |
| 24 | const REQUIRED_ASSETS = [CODEWHALE_ASSET, CODEW_ASSET]; |
| 25 | |
| 26 | function sha256(content) { |
| 27 | return crypto.createHash("sha256").update(content).digest("hex"); |
| 28 | } |
| 29 | |
| 30 | function hasExactHostname(value, expectedHostname) { |
| 31 | return new URL(value).hostname === expectedHostname; |
| 32 | } |
| 33 | |
| 34 | function manifestFor(files) { |
| 35 | return Object.entries(files) |
| 36 | .map(([name, body]) => `${sha256(body)} ${name}`) |
| 37 | .join("\n"); |
| 38 | } |
| 39 | |
| 40 | function abortError() { |
| 41 | const err = new Error("The operation was aborted"); |
| 42 | err.name = "AbortError"; |
| 43 | err.code = "ABORT_ERR"; |
| 44 | err.nonRetryable = true; |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | function hangUntilAbort(signal) { |
| 49 | return new Promise((_, reject) => { |
| 50 | const fail = () => reject(abortError()); |
| 51 | if (!signal) { |
| 52 | return; |
| 53 | } |
| 54 | if (signal.aborted) { |
| 55 | fail(); |
| 56 | return; |
| 57 | } |
| 58 | signal.addEventListener("abort", fail, { once: true }); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | function deferred() { |
| 63 | let resolve; |
| 64 | let reject; |
| 65 | const promise = new Promise((resolvePromise, rejectPromise) => { |
| 66 | resolve = resolvePromise; |
| 67 | reject = rejectPromise; |
| 68 | }); |
| 69 | return { promise, reject, resolve }; |
| 70 | } |
| 71 | |
| 72 | async function makeTempDir(t) { |
| 73 | const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "codewhale-source-")); |
| 74 | t.after(() => fs.promises.rm(dir, { force: true, recursive: true })); |
| 75 | return dir; |
| 76 | } |
| 77 | |
| 78 | function linuxSelectOptions(overrides = {}) { |
| 79 | return { |
| 80 | version: VERSION, |
| 81 | repo: REPO, |
| 82 | requiredAssets: REQUIRED_ASSETS, |
| 83 | context: "runtime", |
| 84 | env: {}, |
| 85 | platform: "linux", |
| 86 | arch: "x64", |
| 87 | ...overrides, |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | async function withoutForcedDownload(callback) { |
| 92 | const previousCodewhale = process.env.CODEWHALE_FORCE_DOWNLOAD; |
| 93 | const previousTui = process.env.DEEPSEEK_TUI_FORCE_DOWNLOAD; |
| 94 | const previousLegacy = process.env.DEEPSEEK_FORCE_DOWNLOAD; |
| 95 | delete process.env.CODEWHALE_FORCE_DOWNLOAD; |
| 96 | delete process.env.DEEPSEEK_TUI_FORCE_DOWNLOAD; |
| 97 | delete process.env.DEEPSEEK_FORCE_DOWNLOAD; |
| 98 | try { |
| 99 | return await callback(); |
| 100 | } finally { |
| 101 | if (previousCodewhale === undefined) { |
| 102 | delete process.env.CODEWHALE_FORCE_DOWNLOAD; |
| 103 | } else { |
| 104 | process.env.CODEWHALE_FORCE_DOWNLOAD = previousCodewhale; |
| 105 | } |
| 106 | if (previousTui === undefined) { |
| 107 | delete process.env.DEEPSEEK_TUI_FORCE_DOWNLOAD; |
| 108 | } else { |
| 109 | process.env.DEEPSEEK_TUI_FORCE_DOWNLOAD = previousTui; |
| 110 | } |
| 111 | if (previousLegacy === undefined) { |
| 112 | delete process.env.DEEPSEEK_FORCE_DOWNLOAD; |
| 113 | } else { |
| 114 | process.env.DEEPSEEK_FORCE_DOWNLOAD = previousLegacy; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | test("Linux x64 races first-party sources unless an explicit override is set", () => { |
| 120 | assert.equal(shouldRaceFirstPartyMirrors({}, "linux", "x64"), true); |
| 121 | assert.equal(shouldRaceFirstPartyMirrors({}, "openharmony", "x64"), true); |
| 122 | assert.equal(shouldRaceFirstPartyMirrors({}, "linux", "arm64"), false); |
| 123 | assert.equal(shouldRaceFirstPartyMirrors({}, "darwin", "arm64"), false); |
| 124 | assert.equal( |
| 125 | shouldRaceFirstPartyMirrors({ CODEWHALE_USE_CNB_MIRROR: "1" }, "linux", "x64"), |
| 126 | false, |
| 127 | ); |
| 128 | assert.equal( |
| 129 | shouldRaceFirstPartyMirrors({ CODEWHALE_USE_CNB_MIRROR: "0" }, "linux", "x64"), |
| 130 | true, |
| 131 | ); |
| 132 | assert.equal( |
| 133 | shouldRaceFirstPartyMirrors( |
| 134 | { CODEWHALE_RELEASE_BASE_URL: "https://mirror.example/v0.9.10/" }, |
| 135 | "linux", |
| 136 | "x64", |
| 137 | ), |
| 138 | false, |
| 139 | ); |
| 140 | assert.equal( |
| 141 | hasExplicitReleaseBase({ DEEPSEEK_TUI_RELEASE_BASE_URL: "https://legacy.example/" }), |
| 142 | true, |
| 143 | ); |
| 144 | assert.equal( |
| 145 | hasExplicitReleaseBase({ |
| 146 | CODEWHALE_RELEASE_BASE_URL: " ", |
| 147 | DEEPSEEK_TUI_RELEASE_BASE_URL: "https://legacy.example/", |
| 148 | }), |
| 149 | true, |
| 150 | ); |
| 151 | assert.equal( |
| 152 | explicitReleaseBase({ |
| 153 | CODEWHALE_RELEASE_BASE_URL: " ", |
| 154 | DEEPSEEK_TUI_RELEASE_BASE_URL: "https://legacy.example/releases", |
| 155 | }), |
| 156 | "https://legacy.example/releases/", |
| 157 | ); |
| 158 | }); |
| 159 | |
| 160 | test("first-party source URLs stay pinned to the exact package version", () => { |
| 161 | assert.deepEqual(firstPartyReleaseSources(VERSION, REPO), [ |
| 162 | { |
| 163 | id: "github", |
| 164 | label: "GitHub Releases", |
| 165 | baseUrl: githubReleaseBaseUrl(VERSION, REPO), |
| 166 | }, |
| 167 | { |
| 168 | id: "cnb", |
| 169 | label: "CNB first-party mirror", |
| 170 | baseUrl: cnbReleaseBaseUrl(VERSION), |
| 171 | }, |
| 172 | ]); |
| 173 | assert.equal( |
| 174 | githubReleaseBaseUrl(VERSION, REPO), |
| 175 | `https://github.com/${REPO}/releases/download/v${VERSION}/`, |
| 176 | ); |
| 177 | assert.equal( |
| 178 | cnbReleaseBaseUrl(VERSION), |
| 179 | `https://cnb.cool/codewhale.net/codewhale/-/releases/download/v${VERSION}/`, |
| 180 | ); |
| 181 | }); |
| 182 | |
| 183 | test("CNB wins when its checksum manifest validates first", async () => { |
| 184 | const githubBody = Buffer.from("github-codewhale"); |
| 185 | const cnbBody = Buffer.from("cnb-codewhale"); |
| 186 | const fetched = []; |
| 187 | let githubSignal; |
| 188 | |
| 189 | const source = await _internal.selectReleaseSource( |
| 190 | linuxSelectOptions({ |
| 191 | fetchText: async (url, opts) => { |
| 192 | fetched.push(url); |
| 193 | if (hasExactHostname(url, "github.com")) { |
| 194 | githubSignal = opts && opts.signal; |
| 195 | return hangUntilAbort(opts && opts.signal); |
| 196 | } |
| 197 | return manifestFor({ |
| 198 | [CODEWHALE_ASSET]: cnbBody, |
| 199 | [CODEW_ASSET]: Buffer.from("cnb-codew"), |
| 200 | }); |
| 201 | }, |
| 202 | }), |
| 203 | ); |
| 204 | |
| 205 | assert.equal(source.id, "cnb"); |
| 206 | assert.match(source.label, /CNB first-party mirror/); |
| 207 | assert.equal(source.baseUrl, cnbReleaseBaseUrl(VERSION)); |
| 208 | assert.equal(source.checksums.get(CODEWHALE_ASSET), sha256(cnbBody)); |
| 209 | assert.notEqual(source.checksums.get(CODEWHALE_ASSET), sha256(githubBody)); |
| 210 | assert.equal(githubSignal && githubSignal.aborted, true); |
| 211 | assert.ok(fetched.some((url) => hasExactHostname(url, "cnb.cool"))); |
| 212 | assert.ok(fetched.some((url) => hasExactHostname(url, "github.com"))); |
| 213 | assert.ok(fetched.every((url) => url.endsWith(CHECKSUM_MANIFEST))); |
| 214 | }); |
| 215 | |
| 216 | test("GitHub wins when its checksum manifest validates first", async () => { |
| 217 | const githubBody = Buffer.from("github-codewhale"); |
| 218 | let cnbSignal; |
| 219 | |
| 220 | const source = await _internal.selectReleaseSource( |
| 221 | linuxSelectOptions({ |
| 222 | fetchText: async (url, opts) => { |
| 223 | if (hasExactHostname(url, "cnb.cool")) { |
| 224 | cnbSignal = opts && opts.signal; |
| 225 | return hangUntilAbort(opts && opts.signal); |
| 226 | } |
| 227 | return manifestFor({ |
| 228 | [CODEWHALE_ASSET]: githubBody, |
| 229 | [CODEW_ASSET]: Buffer.from("github-codew"), |
| 230 | }); |
| 231 | }, |
| 232 | }), |
| 233 | ); |
| 234 | |
| 235 | assert.equal(source.id, "github"); |
| 236 | assert.equal(source.baseUrl, githubReleaseBaseUrl(VERSION, REPO)); |
| 237 | assert.equal(source.checksums.get(CODEWHALE_ASSET), sha256(githubBody)); |
| 238 | assert.equal(cnbSignal && cnbSignal.aborted, true); |
| 239 | }); |
| 240 | |
| 241 | test("one unavailable first-party source does not block the valid source", async () => { |
| 242 | const githubBody = Buffer.from("github-only"); |
| 243 | const fetched = []; |
| 244 | |
| 245 | const source = await _internal.selectReleaseSource( |
| 246 | linuxSelectOptions({ |
| 247 | fetchText: async (url) => { |
| 248 | fetched.push(url); |
| 249 | if (hasExactHostname(url, "cnb.cool")) { |
| 250 | const err = new Error("Request failed with status 404: " + url); |
| 251 | err.name = "HttpStatusError"; |
| 252 | err.status = 404; |
| 253 | err.nonRetryable = true; |
| 254 | throw err; |
| 255 | } |
| 256 | return manifestFor({ |
| 257 | [CODEWHALE_ASSET]: githubBody, |
| 258 | [CODEW_ASSET]: Buffer.from("github-codew"), |
| 259 | }); |
| 260 | }, |
| 261 | }), |
| 262 | ); |
| 263 | |
| 264 | assert.equal(source.id, "github"); |
| 265 | assert.equal(source.checksums.get(CODEWHALE_ASSET), sha256(githubBody)); |
| 266 | assert.equal(fetched.length, 2); |
| 267 | }); |
| 268 | |
| 269 | test("both invalid manifests fail closed", async () => { |
| 270 | await assert.rejects( |
| 271 | () => |
| 272 | _internal.selectReleaseSource( |
| 273 | linuxSelectOptions({ |
| 274 | fetchText: async () => "not-a-checksum-manifest", |
| 275 | }), |
| 276 | ), |
| 277 | (err) => { |
| 278 | assert.match(String(err.message), /No usable first-party release source/); |
| 279 | assert.match(String(err.message), /GitHub Releases/); |
| 280 | assert.match(String(err.message), /CNB first-party mirror/); |
| 281 | assert.equal(err.nonRetryable, true); |
| 282 | return true; |
| 283 | }, |
| 284 | ); |
| 285 | }); |
| 286 | |
| 287 | test("aggregate source failure preserves retryable optional-install behavior", async () => { |
| 288 | let failure; |
| 289 | try { |
| 290 | await _internal.selectReleaseSource( |
| 291 | linuxSelectOptions({ |
| 292 | fetchText: async (url) => { |
| 293 | const error = new Error(`getaddrinfo ENOTFOUND ${new URL(url).hostname}`); |
| 294 | error.code = "ENOTFOUND"; |
| 295 | throw error; |
| 296 | }, |
| 297 | }), |
| 298 | ); |
| 299 | } catch (error) { |
| 300 | failure = error; |
| 301 | } |
| 302 | |
| 303 | assert.ok(failure); |
| 304 | assert.equal(failure.retryable, true); |
| 305 | assert.equal(failure.nonRetryable, undefined); |
| 306 | assert.equal( |
| 307 | _internal.shouldIgnoreInstallFailure( |
| 308 | "install", |
| 309 | failure, |
| 310 | ["--optional"], |
| 311 | {}, |
| 312 | ), |
| 313 | true, |
| 314 | ); |
| 315 | }); |
| 316 | |
| 317 | test("explicit release base and CNB override take precedence over the race", async () => { |
| 318 | const overrideBase = "https://mirror.example/v0.9.10/"; |
| 319 | const overrideBody = Buffer.from("override-binary"); |
| 320 | const fetched = []; |
| 321 | |
| 322 | const overrideSource = await _internal.selectReleaseSource( |
| 323 | linuxSelectOptions({ |
| 324 | env: { |
| 325 | CODEWHALE_RELEASE_BASE_URL: overrideBase, |
| 326 | CODEWHALE_USE_CNB_MIRROR: "1", |
| 327 | }, |
| 328 | fetchText: async (url) => { |
| 329 | fetched.push(url); |
| 330 | if (!url.startsWith(overrideBase)) { |
| 331 | throw new Error(`unexpected fetch ${url}`); |
| 332 | } |
| 333 | return manifestFor({ |
| 334 | [CODEWHALE_ASSET]: overrideBody, |
| 335 | [CODEW_ASSET]: Buffer.from("override-codew"), |
| 336 | }); |
| 337 | }, |
| 338 | }), |
| 339 | ); |
| 340 | |
| 341 | assert.equal(overrideSource.id, "override"); |
| 342 | assert.equal(overrideSource.baseUrl, overrideBase); |
| 343 | assert.deepEqual(fetched, [`${overrideBase}${CHECKSUM_MANIFEST}`]); |
| 344 | |
| 345 | fetched.length = 0; |
| 346 | const cnbSource = await _internal.selectReleaseSource( |
| 347 | linuxSelectOptions({ |
| 348 | env: { CODEWHALE_USE_CNB_MIRROR: "1" }, |
| 349 | fetchText: async (url) => { |
| 350 | fetched.push(url); |
| 351 | if (!url.startsWith(cnbReleaseBaseUrl(VERSION))) { |
| 352 | throw new Error(`unexpected fetch ${url}`); |
| 353 | } |
| 354 | return manifestFor({ |
| 355 | [CODEWHALE_ASSET]: Buffer.from("cnb-forced"), |
| 356 | [CODEW_ASSET]: Buffer.from("cnb-forced-codew"), |
| 357 | }); |
| 358 | }, |
| 359 | }), |
| 360 | ); |
| 361 | |
| 362 | assert.equal(cnbSource.id, "cnb"); |
| 363 | assert.deepEqual(fetched, [`${cnbReleaseBaseUrl(VERSION)}${CHECKSUM_MANIFEST}`]); |
| 364 | }); |
| 365 | |
| 366 | test("locked source downloads each required binary once from the winner", async (t) => { |
| 367 | const dir = await makeTempDir(t); |
| 368 | const cnbWhale = Buffer.from("cnb-codewhale-bytes"); |
| 369 | const cnbCodew = Buffer.from("cnb-codew-bytes"); |
| 370 | const githubWhale = Buffer.from("github-codewhale-bytes"); |
| 371 | const githubCodew = Buffer.from("github-codew-bytes"); |
| 372 | const binaryDownloads = []; |
| 373 | const manifestFetches = []; |
| 374 | |
| 375 | const source = await _internal.selectReleaseSource( |
| 376 | linuxSelectOptions({ |
| 377 | fetchText: async (url, opts) => { |
| 378 | manifestFetches.push(url); |
| 379 | if (hasExactHostname(url, "github.com")) { |
| 380 | return hangUntilAbort(opts && opts.signal); |
| 381 | } |
| 382 | return manifestFor({ |
| 383 | [CODEWHALE_ASSET]: cnbWhale, |
| 384 | [CODEW_ASSET]: cnbCodew, |
| 385 | }); |
| 386 | }, |
| 387 | }), |
| 388 | ); |
| 389 | |
| 390 | assert.equal(source.id, "cnb"); |
| 391 | |
| 392 | const fakeDownload = async (url, destination) => { |
| 393 | binaryDownloads.push(url); |
| 394 | const body = url.endsWith(CODEW_ASSET) ? cnbCodew : cnbWhale; |
| 395 | await fs.promises.mkdir(path.dirname(destination), { recursive: true }); |
| 396 | await fs.promises.writeFile(destination, body); |
| 397 | }; |
| 398 | |
| 399 | await withoutForcedDownload(async () => { |
| 400 | await _internal.ensureBinary( |
| 401 | path.join(dir, "codewhale"), |
| 402 | CODEWHALE_ASSET, |
| 403 | VERSION, |
| 404 | REPO, |
| 405 | async () => source.checksums, |
| 406 | { |
| 407 | baseUrl: source.baseUrl, |
| 408 | sourceId: source.id, |
| 409 | sourceLabel: source.label, |
| 410 | download: fakeDownload, |
| 411 | }, |
| 412 | ); |
| 413 | await _internal.ensureBinary( |
| 414 | path.join(dir, "codew"), |
| 415 | CODEW_ASSET, |
| 416 | VERSION, |
| 417 | REPO, |
| 418 | async () => source.checksums, |
| 419 | { |
| 420 | baseUrl: source.baseUrl, |
| 421 | sourceId: source.id, |
| 422 | sourceLabel: source.label, |
| 423 | download: fakeDownload, |
| 424 | }, |
| 425 | ); |
| 426 | }); |
| 427 | |
| 428 | assert.deepEqual(binaryDownloads, [ |
| 429 | `${cnbReleaseBaseUrl(VERSION)}${CODEWHALE_ASSET}`, |
| 430 | `${cnbReleaseBaseUrl(VERSION)}${CODEW_ASSET}`, |
| 431 | ]); |
| 432 | assert.ok(manifestFetches.every((url) => url.endsWith(CHECKSUM_MANIFEST))); |
| 433 | assert.ok(!binaryDownloads.some((url) => hasExactHostname(url, "github.com"))); |
| 434 | assert.equal(await fs.promises.readFile(path.join(dir, "codewhale"), "utf8"), cnbWhale.toString()); |
| 435 | assert.equal( |
| 436 | await fs.promises.readFile(path.join(dir, "codewhale.source"), "utf8"), |
| 437 | [ |
| 438 | `source=${source.id}`, |
| 439 | `label=${source.label}`, |
| 440 | `base=${source.baseUrl}`, |
| 441 | `version=${VERSION}`, |
| 442 | "", |
| 443 | ].join("\n"), |
| 444 | ); |
| 445 | }); |
| 446 | |
| 447 | test("run locks both binaries to the first valid manifest source", async (t) => { |
| 448 | const dir = await makeTempDir(t); |
| 449 | const cnbWhale = Buffer.from("run-cnb-codewhale"); |
| 450 | const cnbCodew = Buffer.from("run-cnb-codew"); |
| 451 | const bothStarted = deferred(); |
| 452 | const releaseCnbManifest = deferred(); |
| 453 | const binaryDownloads = []; |
| 454 | let manifestStarts = 0; |
| 455 | let githubSignal; |
| 456 | |
| 457 | const sources = [ |
| 458 | { |
| 459 | id: "github", |
| 460 | label: "GitHub Releases", |
| 461 | baseUrl: "https://github.invalid/releases/download/v0.9.10/", |
| 462 | }, |
| 463 | { |
| 464 | id: "cnb", |
| 465 | label: "CNB first-party mirror", |
| 466 | baseUrl: "https://cnb.invalid/releases/download/v0.9.10/", |
| 467 | }, |
| 468 | ]; |
| 469 | const paths = { |
| 470 | codewhale: { |
| 471 | asset: CODEWHALE_ASSET, |
| 472 | target: path.join(dir, "codewhale"), |
| 473 | }, |
| 474 | codew: { |
| 475 | asset: CODEW_ASSET, |
| 476 | target: path.join(dir, "codew"), |
| 477 | }, |
| 478 | }; |
| 479 | |
| 480 | const install = run({ |
| 481 | context: "runtime", |
| 482 | env: { |
| 483 | CODEWHALE_FORCE_DOWNLOAD: "1", |
| 484 | CODEWHALE_QUIET_INSTALL: "1", |
| 485 | CODEWHALE_VERSION: VERSION, |
| 486 | }, |
| 487 | platform: "linux", |
| 488 | arch: "x64", |
| 489 | paths, |
| 490 | releaseDir: dir, |
| 491 | sources, |
| 492 | fetchText: (url, options) => { |
| 493 | manifestStarts += 1; |
| 494 | if (manifestStarts === sources.length) { |
| 495 | bothStarted.resolve(); |
| 496 | } |
| 497 | if (url.startsWith(sources[0].baseUrl)) { |
| 498 | githubSignal = options.signal; |
| 499 | return hangUntilAbort(options.signal); |
| 500 | } |
| 501 | return releaseCnbManifest.promise; |
| 502 | }, |
| 503 | download: async (url, destination) => { |
| 504 | binaryDownloads.push(url); |
| 505 | const body = url.endsWith(CODEW_ASSET) ? cnbCodew : cnbWhale; |
| 506 | await fs.promises.writeFile(destination, body); |
| 507 | }, |
| 508 | }); |
| 509 | |
| 510 | await bothStarted.promise; |
| 511 | releaseCnbManifest.resolve( |
| 512 | manifestFor({ |
| 513 | [CODEWHALE_ASSET]: cnbWhale, |
| 514 | [CODEW_ASSET]: cnbCodew, |
| 515 | }), |
| 516 | ); |
| 517 | await install; |
| 518 | |
| 519 | assert.equal(githubSignal.aborted, true); |
| 520 | assert.deepEqual(binaryDownloads.sort(), [ |
| 521 | `${sources[1].baseUrl}${CODEW_ASSET}`, |
| 522 | `${sources[1].baseUrl}${CODEWHALE_ASSET}`, |
| 523 | ]); |
| 524 | assert.equal( |
| 525 | await fs.promises.readFile(`${paths.codewhale.target}.source`, "utf8"), |
| 526 | [ |
| 527 | "source=cnb", |
| 528 | "label=CNB first-party mirror", |
| 529 | `base=${sources[1].baseUrl}`, |
| 530 | `version=${VERSION}`, |
| 531 | "", |
| 532 | ].join("\n"), |
| 533 | ); |
| 534 | assert.equal( |
| 535 | await fs.promises.readFile(`${paths.codew.target}.source`, "utf8"), |
| 536 | await fs.promises.readFile(`${paths.codewhale.target}.source`, "utf8"), |
| 537 | ); |
| 538 | }); |
| 539 | |
| 540 | test("run fails before binary download when neither manifest validates", async (t) => { |
| 541 | const dir = await makeTempDir(t); |
| 542 | const binaryDownloads = []; |
| 543 | |
| 544 | await assert.rejects( |
| 545 | () => |
| 546 | run({ |
| 547 | context: "runtime", |
| 548 | env: { |
| 549 | CODEWHALE_FORCE_DOWNLOAD: "1", |
| 550 | CODEWHALE_QUIET_INSTALL: "1", |
| 551 | CODEWHALE_VERSION: VERSION, |
| 552 | }, |
| 553 | platform: "linux", |
| 554 | arch: "x64", |
| 555 | releaseDir: dir, |
| 556 | paths: { |
| 557 | codewhale: { |
| 558 | asset: CODEWHALE_ASSET, |
| 559 | target: path.join(dir, "codewhale"), |
| 560 | }, |
| 561 | codew: { |
| 562 | asset: CODEW_ASSET, |
| 563 | target: path.join(dir, "codew"), |
| 564 | }, |
| 565 | }, |
| 566 | fetchText: async () => "invalid manifest", |
| 567 | download: async (url) => { |
| 568 | binaryDownloads.push(url); |
| 569 | }, |
| 570 | }), |
| 571 | /No usable first-party release source/, |
| 572 | ); |
| 573 | |
| 574 | assert.deepEqual(binaryDownloads, []); |
| 575 | assert.equal( |
| 576 | await fs.promises.access(path.join(dir, "codewhale")).then(() => true, () => false), |
| 577 | false, |
| 578 | ); |
| 579 | assert.equal( |
| 580 | await fs.promises.access(path.join(dir, "codew")).then(() => true, () => false), |
| 581 | false, |
| 582 | ); |
| 583 | }); |
| 584 | |
| 585 | test("run sends manifest and binaries only to an explicit release base", async (t) => { |
| 586 | const dir = await makeTempDir(t); |
| 587 | const baseUrl = "https://mirror.invalid/releases/download/v0.9.10/"; |
| 588 | const codewhaleBody = Buffer.from("override-codewhale"); |
| 589 | const codewBody = Buffer.from("override-codew"); |
| 590 | const manifestFetches = []; |
| 591 | const binaryDownloads = []; |
| 592 | |
| 593 | await run({ |
| 594 | context: "runtime", |
| 595 | env: { |
| 596 | CODEWHALE_FORCE_DOWNLOAD: "1", |
| 597 | CODEWHALE_QUIET_INSTALL: "1", |
| 598 | CODEWHALE_RELEASE_BASE_URL: baseUrl, |
| 599 | CODEWHALE_USE_CNB_MIRROR: "1", |
| 600 | CODEWHALE_VERSION: VERSION, |
| 601 | }, |
| 602 | platform: "linux", |
| 603 | arch: "x64", |
| 604 | releaseDir: dir, |
| 605 | paths: { |
| 606 | codewhale: { |
| 607 | asset: CODEWHALE_ASSET, |
| 608 | target: path.join(dir, "codewhale"), |
| 609 | }, |
| 610 | codew: { |
| 611 | asset: CODEW_ASSET, |
| 612 | target: path.join(dir, "codew"), |
| 613 | }, |
| 614 | }, |
| 615 | fetchText: async (url) => { |
| 616 | manifestFetches.push(url); |
| 617 | return manifestFor({ |
| 618 | [CODEWHALE_ASSET]: codewhaleBody, |
| 619 | [CODEW_ASSET]: codewBody, |
| 620 | }); |
| 621 | }, |
| 622 | download: async (url, destination) => { |
| 623 | binaryDownloads.push(url); |
| 624 | await fs.promises.writeFile( |
| 625 | destination, |
| 626 | url.endsWith(CODEW_ASSET) ? codewBody : codewhaleBody, |
| 627 | ); |
| 628 | }, |
| 629 | }); |
| 630 | |
| 631 | assert.deepEqual(manifestFetches, [`${baseUrl}${CHECKSUM_MANIFEST}`]); |
| 632 | assert.deepEqual(binaryDownloads.sort(), [ |
| 633 | `${baseUrl}${CODEW_ASSET}`, |
| 634 | `${baseUrl}${CODEWHALE_ASSET}`, |
| 635 | ]); |
| 636 | }); |
| 637 | |
| 638 | test("checksum mismatch against the locked source fails closed", async (t) => { |
| 639 | const dir = await makeTempDir(t); |
| 640 | const expected = Buffer.from("expected-cnb-bytes"); |
| 641 | const actual = Buffer.from("tampered-cnb-bytes"); |
| 642 | const downloads = []; |
| 643 | const source = { |
| 644 | id: "cnb", |
| 645 | label: "CNB first-party mirror", |
| 646 | baseUrl: cnbReleaseBaseUrl(VERSION), |
| 647 | checksums: new Map([[CODEWHALE_ASSET, sha256(expected)]]), |
| 648 | }; |
| 649 | |
| 650 | await assert.rejects( |
| 651 | () => |
| 652 | _internal.ensureBinary( |
| 653 | path.join(dir, "codewhale"), |
| 654 | CODEWHALE_ASSET, |
| 655 | VERSION, |
| 656 | REPO, |
| 657 | async () => source.checksums, |
| 658 | { |
| 659 | baseUrl: source.baseUrl, |
| 660 | sourceId: source.id, |
| 661 | sourceLabel: source.label, |
| 662 | download: async (url, destination) => { |
| 663 | downloads.push(url); |
| 664 | await fs.promises.mkdir(path.dirname(destination), { recursive: true }); |
| 665 | await fs.promises.writeFile(destination, actual); |
| 666 | }, |
| 667 | }, |
| 668 | ), |
| 669 | /Checksum mismatch for codewhale-linux-x64 from CNB first-party mirror/, |
| 670 | ); |
| 671 | assert.deepEqual(downloads, [`${cnbReleaseBaseUrl(VERSION)}${CODEWHALE_ASSET}`]); |
| 672 | assert.equal(await fs.promises.access(path.join(dir, "codewhale")).then(() => true, () => false), false); |
| 673 | }); |
| 674 | |
| 675 | test("source selection is visible in progress output", async () => { |
| 676 | const previousWrite = process.stderr.write; |
| 677 | const previousQuiet = process.env.DEEPSEEK_TUI_QUIET_INSTALL; |
| 678 | let stderr = ""; |
| 679 | process.stderr.write = (chunk) => { |
| 680 | stderr += String(chunk); |
| 681 | return true; |
| 682 | }; |
| 683 | delete process.env.DEEPSEEK_TUI_QUIET_INSTALL; |
| 684 | |
| 685 | try { |
| 686 | await _internal.selectReleaseSource( |
| 687 | linuxSelectOptions({ |
| 688 | fetchText: async (url, opts) => { |
| 689 | if (hasExactHostname(url, "github.com")) { |
| 690 | return hangUntilAbort(opts && opts.signal); |
| 691 | } |
| 692 | return manifestFor({ |
| 693 | [CODEWHALE_ASSET]: Buffer.from("cnb"), |
| 694 | [CODEW_ASSET]: Buffer.from("cnb-codew"), |
| 695 | }); |
| 696 | }, |
| 697 | }), |
| 698 | ); |
| 699 | assert.match(stderr, /probing GitHub Releases and CNB first-party mirror/); |
| 700 | assert.match(stderr, /selected CNB first-party mirror/); |
| 701 | } finally { |
| 702 | process.stderr.write = previousWrite; |
| 703 | if (previousQuiet === undefined) { |
| 704 | delete process.env.DEEPSEEK_TUI_QUIET_INSTALL; |
| 705 | } else { |
| 706 | process.env.DEEPSEEK_TUI_QUIET_INSTALL = previousQuiet; |
| 707 | } |
| 708 | } |
| 709 | }); |
| 710 | |
| 711 | test("aborting a losing manifest probe closes a body already in progress", async (t) => { |
| 712 | const responseStarted = deferred(); |
| 713 | const responseClosed = deferred(); |
| 714 | const server = http.createServer((_req, res) => { |
| 715 | res.on("close", () => responseClosed.resolve()); |
| 716 | res.writeHead(200, { "Content-Type": "text/plain" }); |
| 717 | res.write(`${"a".repeat(64)} ${CODEWHALE_ASSET}\n`); |
| 718 | responseStarted.resolve(); |
| 719 | }); |
| 720 | await new Promise((resolve, reject) => { |
| 721 | server.listen(0, "127.0.0.1", resolve); |
| 722 | server.once("error", reject); |
| 723 | }); |
| 724 | t.after( |
| 725 | () => |
| 726 | new Promise((resolve) => { |
| 727 | server.close(() => resolve()); |
| 728 | }), |
| 729 | ); |
| 730 | |
| 731 | const address = server.address(); |
| 732 | const controller = new AbortController(); |
| 733 | const fetching = _internal.downloadText( |
| 734 | `http://127.0.0.1:${address.port}/${CHECKSUM_MANIFEST}`, |
| 735 | { |
| 736 | signal: controller.signal, |
| 737 | stallMs: 10_000, |
| 738 | totalTimeoutMs: 10_000, |
| 739 | }, |
| 740 | ); |
| 741 | await responseStarted.promise; |
| 742 | controller.abort(); |
| 743 | |
| 744 | await assert.rejects(fetching, (error) => { |
| 745 | assert.equal(error.name, "AbortError"); |
| 746 | assert.equal(error.code, "ABORT_ERR"); |
| 747 | return true; |
| 748 | }); |
| 749 | await responseClosed.promise; |
| 750 | }); |
| 751 | |
| 752 | test("HTTP fixture never starts the losing source's full binary download", async (t) => { |
| 753 | const dir = await makeTempDir(t); |
| 754 | const cnbWhale = Buffer.from("fixture-cnb-whale"); |
| 755 | const cnbCodew = Buffer.from("fixture-cnb-codew"); |
| 756 | const githubWhale = Buffer.from("fixture-github-whale"); |
| 757 | const githubCodew = Buffer.from("fixture-github-codew"); |
| 758 | const hits = { githubManifest: 0, githubBinary: 0, cnbManifest: 0, cnbBinary: 0 }; |
| 759 | |
| 760 | const github = await listenFixture({ |
| 761 | manifest: manifestFor({ |
| 762 | [CODEWHALE_ASSET]: githubWhale, |
| 763 | [CODEW_ASSET]: githubCodew, |
| 764 | }), |
| 765 | files: { |
| 766 | [CODEWHALE_ASSET]: githubWhale, |
| 767 | [CODEW_ASSET]: githubCodew, |
| 768 | }, |
| 769 | onManifest: () => { |
| 770 | hits.githubManifest += 1; |
| 771 | }, |
| 772 | onBinary: () => { |
| 773 | hits.githubBinary += 1; |
| 774 | }, |
| 775 | }); |
| 776 | const cnb = await listenFixture({ |
| 777 | manifest: manifestFor({ |
| 778 | [CODEWHALE_ASSET]: cnbWhale, |
| 779 | [CODEW_ASSET]: cnbCodew, |
| 780 | }), |
| 781 | files: { |
| 782 | [CODEWHALE_ASSET]: cnbWhale, |
| 783 | [CODEW_ASSET]: cnbCodew, |
| 784 | }, |
| 785 | onManifest: () => { |
| 786 | hits.cnbManifest += 1; |
| 787 | }, |
| 788 | onBinary: () => { |
| 789 | hits.cnbBinary += 1; |
| 790 | }, |
| 791 | }); |
| 792 | t.after(() => github.close()); |
| 793 | t.after(() => cnb.close()); |
| 794 | |
| 795 | const source = await _internal.selectReleaseSource( |
| 796 | linuxSelectOptions({ |
| 797 | sources: [ |
| 798 | { |
| 799 | id: "github", |
| 800 | label: "GitHub Releases", |
| 801 | baseUrl: github.baseUrl, |
| 802 | }, |
| 803 | { |
| 804 | id: "cnb", |
| 805 | label: "CNB first-party mirror", |
| 806 | baseUrl: cnb.baseUrl, |
| 807 | }, |
| 808 | ], |
| 809 | fetchText: (url, opts) => |
| 810 | url.startsWith(github.baseUrl) |
| 811 | ? hangUntilAbort(opts && opts.signal) |
| 812 | : _internal.downloadText(url, opts), |
| 813 | }), |
| 814 | ); |
| 815 | |
| 816 | assert.equal(source.id, "cnb"); |
| 817 | await withoutForcedDownload(() => |
| 818 | _internal.ensureBinary( |
| 819 | path.join(dir, "codewhale"), |
| 820 | CODEWHALE_ASSET, |
| 821 | VERSION, |
| 822 | REPO, |
| 823 | async () => source.checksums, |
| 824 | { baseUrl: source.baseUrl, sourceId: source.id, sourceLabel: source.label }, |
| 825 | ), |
| 826 | ); |
| 827 | |
| 828 | assert.equal(hits.githubBinary, 0); |
| 829 | assert.equal(hits.cnbBinary, 1); |
| 830 | assert.equal(hits.cnbManifest, 1); |
| 831 | assert.equal(await fs.promises.readFile(path.join(dir, "codewhale"), "utf8"), cnbWhale.toString()); |
| 832 | }); |
| 833 | |
| 834 | function listenFixture(spec) { |
| 835 | return new Promise((resolve, reject) => { |
| 836 | const server = http.createServer((req, res) => { |
| 837 | const urlPath = String(req.url || "").split("?")[0]; |
| 838 | const name = path.posix.basename(urlPath); |
| 839 | if (name === CHECKSUM_MANIFEST) { |
| 840 | if (spec.onManifest) spec.onManifest(); |
| 841 | res.writeHead(200, { "Content-Type": "text/plain" }); |
| 842 | res.end(spec.manifest); |
| 843 | return; |
| 844 | } |
| 845 | if (spec.files[name]) { |
| 846 | if (spec.onBinary) spec.onBinary(); |
| 847 | res.writeHead(200, { "Content-Type": "application/octet-stream" }); |
| 848 | res.end(spec.files[name]); |
| 849 | return; |
| 850 | } |
| 851 | res.writeHead(404); |
| 852 | res.end("missing"); |
| 853 | }); |
| 854 | server.listen(0, "127.0.0.1", () => { |
| 855 | const address = server.address(); |
| 856 | resolve({ |
| 857 | baseUrl: `http://127.0.0.1:${address.port}/releases/download/v${VERSION}/`, |
| 858 | close: () => |
| 859 | new Promise((done) => { |
| 860 | server.close(() => done()); |
| 861 | }), |
| 862 | }); |
| 863 | }); |
| 864 | server.once("error", reject); |
| 865 | }); |
| 866 | } |
| 867 |