|
| 1 | +// Tests for the Windows zip extraction logic in install.js. |
| 2 | +// Uses Node's built-in test runner (node:test, available since Node 18). |
| 3 | + |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +const { test } = require("node:test"); |
| 7 | +const assert = require("node:assert/strict"); |
| 8 | +const fs = require("fs"); |
| 9 | +const os = require("os"); |
| 10 | +const path = require("path"); |
| 11 | +const { execSync } = require("child_process"); |
| 12 | +const { extractZip } = require("./install"); |
| 13 | + |
| 14 | +// createTestZip builds a real .zip containing a single file named "supermodel.exe" |
| 15 | +// using the system zip/tar command. Skips on platforms where neither is available. |
| 16 | +function createTestZip(t) { |
| 17 | + const src = fs.mkdtempSync(path.join(os.tmpdir(), "supermodel-test-src-")); |
| 18 | + const binary = path.join(src, "supermodel.exe"); |
| 19 | + fs.writeFileSync(binary, "fake binary"); |
| 20 | + |
| 21 | + const archive = path.join(os.tmpdir(), `supermodel-test-${process.pid}.zip`); |
| 22 | + try { |
| 23 | + // Use system zip or tar to build the archive. |
| 24 | + try { |
| 25 | + execSync(`zip -j "${archive}" "${binary}"`, { stdio: "pipe" }); |
| 26 | + } catch { |
| 27 | + execSync(`tar -cf "${archive}" --format=zip -C "${src}" supermodel.exe`, { stdio: "pipe" }); |
| 28 | + } |
| 29 | + } catch { |
| 30 | + fs.rmSync(src, { recursive: true, force: true }); |
| 31 | + return null; // zip tooling not available — caller should skip |
| 32 | + } |
| 33 | + fs.rmSync(src, { recursive: true, force: true }); |
| 34 | + return archive; |
| 35 | +} |
| 36 | + |
| 37 | +test("extractZip extracts via tar when tar succeeds", () => { |
| 38 | + const archive = createTestZip(); |
| 39 | + if (!archive) { |
| 40 | + // Skip gracefully if zip tooling unavailable (e.g. minimal CI image). |
| 41 | + console.log(" skipped: zip tooling not available"); |
| 42 | + return; |
| 43 | + } |
| 44 | + try { |
| 45 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "supermodel-test-out-")); |
| 46 | + try { |
| 47 | + let called = null; |
| 48 | + extractZip(archive, tmpDir, (cmd) => { |
| 49 | + called = cmd; |
| 50 | + // Only simulate tar; let the actual extraction happen via real execSync |
| 51 | + // if this is the tar command. |
| 52 | + if (cmd.startsWith("tar")) { |
| 53 | + execSync(cmd, { stdio: "pipe" }); |
| 54 | + } else { |
| 55 | + throw new Error("should not reach PowerShell"); |
| 56 | + } |
| 57 | + }); |
| 58 | + assert.ok(called.startsWith("tar"), "should have called tar first"); |
| 59 | + const extracted = fs.readdirSync(tmpDir); |
| 60 | + assert.ok(extracted.length > 0, "tmpDir should contain extracted files"); |
| 61 | + } finally { |
| 62 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 63 | + } |
| 64 | + } finally { |
| 65 | + fs.unlinkSync(archive); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +test("extractZip falls back to PowerShell when tar fails", () => { |
| 70 | + const commands = []; |
| 71 | + // Simulate tar failing; PowerShell "succeeds" (no-op). |
| 72 | + extractZip("/fake/archive.zip", "/fake/tmpdir", (cmd) => { |
| 73 | + commands.push(cmd); |
| 74 | + if (cmd.startsWith("tar")) throw new Error("tar not available"); |
| 75 | + // PowerShell call — just record it, don't execute. |
| 76 | + }); |
| 77 | + |
| 78 | + assert.equal(commands.length, 2, "should have attempted tar then PowerShell"); |
| 79 | + assert.ok(commands[0].startsWith("tar"), "first call should be tar"); |
| 80 | + assert.ok(commands[1].includes("powershell"), "second call should be PowerShell"); |
| 81 | + assert.ok(commands[1].includes("Expand-Archive"), "PowerShell command should use Expand-Archive"); |
| 82 | +}); |
| 83 | + |
| 84 | +test("extractZip PowerShell fallback includes retry loop", () => { |
| 85 | + const commands = []; |
| 86 | + extractZip("/fake/archive.zip", "/fake/tmpdir", (cmd) => { |
| 87 | + commands.push(cmd); |
| 88 | + if (cmd.startsWith("tar")) throw new Error("tar not available"); |
| 89 | + }); |
| 90 | + |
| 91 | + const psCmd = commands.find((c) => c.includes("powershell")); |
| 92 | + assert.ok(psCmd, "PowerShell command should be present"); |
| 93 | + assert.ok(psCmd.includes("$RetryCount"), "should include retry counter"); |
| 94 | + assert.ok(psCmd.includes("Start-Sleep"), "should include sleep between retries"); |
| 95 | + assert.ok(psCmd.includes("-lt 10"), "should retry up to 10 times"); |
| 96 | +}); |
| 97 | + |
| 98 | +test("extractZip uses tar when both succeed — tar wins", () => { |
| 99 | + const commands = []; |
| 100 | + extractZip("/fake/archive.zip", "/fake/tmpdir", (cmd) => { |
| 101 | + commands.push(cmd); |
| 102 | + // Both would succeed; tar is tried first and doesn't throw. |
| 103 | + }); |
| 104 | + |
| 105 | + assert.equal(commands.length, 1, "should only call tar when it succeeds"); |
| 106 | + assert.ok(commands[0].startsWith("tar"), "the single call should be tar"); |
| 107 | +}); |
| 108 | + |
| 109 | +test("extractZip passes archive and tmpDir paths into tar command", () => { |
| 110 | + const archive = "/tmp/test.zip"; |
| 111 | + const tmpDir = "/tmp/extract-dir"; |
| 112 | + let tarCmd = null; |
| 113 | + extractZip(archive, tmpDir, (cmd) => { |
| 114 | + tarCmd = cmd; |
| 115 | + }); |
| 116 | + |
| 117 | + assert.ok(tarCmd.includes(archive), "tar command should include archive path"); |
| 118 | + assert.ok(tarCmd.includes(tmpDir), "tar command should include tmpDir path"); |
| 119 | +}); |
| 120 | + |
| 121 | +test("extractZip passes archive and tmpDir paths into PowerShell fallback", () => { |
| 122 | + const archive = "/tmp/test.zip"; |
| 123 | + const tmpDir = "/tmp/extract-dir"; |
| 124 | + const commands = []; |
| 125 | + extractZip(archive, tmpDir, (cmd) => { |
| 126 | + commands.push(cmd); |
| 127 | + if (cmd.startsWith("tar")) throw new Error("tar failed"); |
| 128 | + }); |
| 129 | + |
| 130 | + const psCmd = commands[1]; |
| 131 | + assert.ok(psCmd.includes(archive), "PowerShell command should include archive path"); |
| 132 | + assert.ok(psCmd.includes(tmpDir), "PowerShell command should include tmpDir path"); |
| 133 | +}); |
0 commit comments