|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { spawn, spawnSync } from "node:child_process"; |
| 4 | +import { createServer } from "node:http"; |
| 5 | +import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { dirname, join, resolve } from "node:path"; |
| 8 | +import { fileURLToPath } from "node:url"; |
| 9 | + |
| 10 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../.."); |
| 11 | + |
| 12 | +test("self-update check prefers the official upstream release repo in fork checkouts", async () => { |
| 13 | + const dir = mkdtempSync(join(tmpdir(), "arrakis-self-update-")); |
| 14 | + mkdirSync(join(dir, "runtime", "scripts"), { recursive: true }); |
| 15 | + copyFileSync(join(repoRoot, "runtime", "scripts", "self-update.sh"), join(dir, "runtime", "scripts", "self-update.sh")); |
| 16 | + chmodSync(join(dir, "runtime", "scripts", "self-update.sh"), 0o700); |
| 17 | + writeFileSync(join(dir, "VERSION"), "v1.3.37\n"); |
| 18 | + |
| 19 | + assert.equal(spawnSync("git", ["init", "-q"], { cwd: dir }).status, 0); |
| 20 | + assert.equal(spawnSync("git", ["remote", "add", "origin", "git@github.com:yacketrj/dune-awakening-selfhost-docker-WSL.git"], { cwd: dir }).status, 0); |
| 21 | + assert.equal(spawnSync("git", ["remote", "add", "upstream", "https://github.com/Red-Blink/dune-awakening-selfhost-docker.git"], { cwd: dir }).status, 0); |
| 22 | + |
| 23 | + const requests = []; |
| 24 | + const server = createServer((req, res) => { |
| 25 | + requests.push(req.url || ""); |
| 26 | + if (req.url === "/repos/Red-Blink/dune-awakening-selfhost-docker/releases/latest") { |
| 27 | + res.writeHead(200, { "content-type": "application/json" }); |
| 28 | + res.end(JSON.stringify({ tag_name: "v1.3.37" })); |
| 29 | + return; |
| 30 | + } |
| 31 | + res.writeHead(404, { "content-type": "application/json" }); |
| 32 | + res.end(JSON.stringify({ message: "not found" })); |
| 33 | + }); |
| 34 | + await new Promise((resolveListen) => server.listen(0, "127.0.0.1", resolveListen)); |
| 35 | + |
| 36 | + try { |
| 37 | + const address = server.address(); |
| 38 | + const apiBase = `http://127.0.0.1:${address.port}`; |
| 39 | + const result = await runProcess("bash", ["runtime/scripts/self-update.sh", "check"], { |
| 40 | + cwd: dir, |
| 41 | + timeout: 15000, |
| 42 | + env: { ...process.env, DUNE_SELF_UPDATE_API_BASE: apiBase, NO_PROXY: "127.0.0.1,localhost", no_proxy: "127.0.0.1,localhost" } |
| 43 | + }); |
| 44 | + |
| 45 | + assert.equal(result.status, 0, result.stderr || result.stdout); |
| 46 | + assert.match(result.stdout, /GitHub repo:\s+Red-Blink\/dune-awakening-selfhost-docker/); |
| 47 | + assert(!result.stdout.includes("yacketrj/dune-awakening-selfhost-docker-WSL")); |
| 48 | + assert.deepEqual(requests, ["/repos/Red-Blink/dune-awakening-selfhost-docker/releases/latest"]); |
| 49 | + } finally { |
| 50 | + server.closeAllConnections(); |
| 51 | + await new Promise((resolveClose) => server.close(resolveClose)); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +function runProcess(command, args, options = {}) { |
| 56 | + return new Promise((resolve, reject) => { |
| 57 | + const { timeout = 15000, ...spawnOptions } = options; |
| 58 | + const child = spawn(command, args, spawnOptions); |
| 59 | + let stdout = ""; |
| 60 | + let stderr = ""; |
| 61 | + const timer = setTimeout(() => { |
| 62 | + child.kill("SIGKILL"); |
| 63 | + reject(new Error(`${command} ${args.join(" ")} timed out\n${stdout}\n${stderr}`)); |
| 64 | + }, timeout); |
| 65 | + |
| 66 | + child.stdout?.on("data", (chunk) => { |
| 67 | + stdout += chunk.toString(); |
| 68 | + }); |
| 69 | + child.stderr?.on("data", (chunk) => { |
| 70 | + stderr += chunk.toString(); |
| 71 | + }); |
| 72 | + child.on("error", (error) => { |
| 73 | + clearTimeout(timer); |
| 74 | + reject(error); |
| 75 | + }); |
| 76 | + child.on("close", (status, signal) => { |
| 77 | + clearTimeout(timer); |
| 78 | + resolve({ status, signal, stdout, stderr }); |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
0 commit comments