|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import * as fs from "node:fs/promises"; |
| 3 | +import * as os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import test from "node:test"; |
| 6 | +import { |
| 7 | + assessPatchloomCompatibility, |
| 8 | + comparePatchloomVersions, |
| 9 | + findOnPath, |
| 10 | + parsePatchloomVersion, |
| 11 | + resolvePatchloomStatusWithInputs |
| 12 | +} from "../../src/binary/patchloom"; |
| 13 | + |
| 14 | +async function withTempDir(fn: (dir: string) => Promise<void>): Promise<void> { |
| 15 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "patchloom-discovery-test-")); |
| 16 | + try { |
| 17 | + await fn(dir); |
| 18 | + } finally { |
| 19 | + await fs.rm(dir, { recursive: true, force: true }); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +test("findOnPath discovers a real executable in a temp directory", async () => { |
| 24 | + await withTempDir(async (dir) => { |
| 25 | + const fakeBinary = path.join(dir, "patchloom"); |
| 26 | + await fs.writeFile(fakeBinary, "#!/bin/sh\necho patchloom 0.1.0\n", { mode: 0o755 }); |
| 27 | + |
| 28 | + const found = await findOnPath(dir, "linux"); |
| 29 | + assert.equal(found, fakeBinary); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +test("findOnPath skips non-executable files", async () => { |
| 34 | + await withTempDir(async (dir) => { |
| 35 | + const fakeBinary = path.join(dir, "patchloom"); |
| 36 | + await fs.writeFile(fakeBinary, "#!/bin/sh\necho patchloom 0.1.0\n", { mode: 0o644 }); |
| 37 | + |
| 38 | + const found = await findOnPath(dir, "linux"); |
| 39 | + assert.equal(found, undefined); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +test("findOnPath searches multiple PATH directories in order", async () => { |
| 44 | + await withTempDir(async (parent) => { |
| 45 | + const firstDir = path.join(parent, "first"); |
| 46 | + const secondDir = path.join(parent, "second"); |
| 47 | + await fs.mkdir(firstDir); |
| 48 | + await fs.mkdir(secondDir); |
| 49 | + |
| 50 | + await fs.writeFile(path.join(firstDir, "patchloom"), "#!/bin/sh\necho first\n", { mode: 0o755 }); |
| 51 | + await fs.writeFile(path.join(secondDir, "patchloom"), "#!/bin/sh\necho second\n", { mode: 0o755 }); |
| 52 | + |
| 53 | + const found = await findOnPath(`${firstDir}:${secondDir}`, "linux"); |
| 54 | + assert.equal(found, path.join(firstDir, "patchloom"), "should find the first match in PATH order"); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +test("findOnPath returns undefined for empty PATH", async () => { |
| 59 | + const found = await findOnPath("", "linux"); |
| 60 | + assert.equal(found, undefined); |
| 61 | +}); |
| 62 | + |
| 63 | +test("findOnPath deduplicates PATH entries", async () => { |
| 64 | + await withTempDir(async (dir) => { |
| 65 | + const fakeBinary = path.join(dir, "patchloom"); |
| 66 | + await fs.writeFile(fakeBinary, "#!/bin/sh\necho v1\n", { mode: 0o755 }); |
| 67 | + let checkCount = 0; |
| 68 | + |
| 69 | + const found = await findOnPath(`${dir}:${dir}:${dir}`, "linux", async (candidate) => { |
| 70 | + checkCount++; |
| 71 | + try { |
| 72 | + await fs.access(candidate, fs.constants.X_OK); |
| 73 | + return true; |
| 74 | + } catch { |
| 75 | + return false; |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + assert.equal(found, fakeBinary); |
| 80 | + assert.equal(checkCount, 1, "should only check each directory once"); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +test("resolvePatchloomStatusWithInputs discovers a real executable via PATH", async () => { |
| 85 | + await withTempDir(async (dir) => { |
| 86 | + const fakeBinary = path.join(dir, "patchloom"); |
| 87 | + await fs.writeFile(fakeBinary, "#!/bin/sh\necho patchloom 0.1.0\n", { mode: 0o755 }); |
| 88 | + |
| 89 | + const status = await resolvePatchloomStatusWithInputs({ |
| 90 | + configuredPath: "", |
| 91 | + pathValue: dir, |
| 92 | + canExecute: async (candidate) => { |
| 93 | + try { |
| 94 | + await fs.access(candidate, fs.constants.X_OK); |
| 95 | + return true; |
| 96 | + } catch { |
| 97 | + return false; |
| 98 | + } |
| 99 | + }, |
| 100 | + getVersion: async () => "patchloom 0.1.0" |
| 101 | + }); |
| 102 | + |
| 103 | + assert.equal(status.ready, true); |
| 104 | + assert.equal(status.source, "path"); |
| 105 | + assert.equal(status.binaryPath, fakeBinary); |
| 106 | + assert.equal(status.version, "patchloom 0.1.0"); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +test("resolvePatchloomStatusWithInputs reports configured path that does not exist", async () => { |
| 111 | + await withTempDir(async (dir) => { |
| 112 | + const nonexistent = path.join(dir, "no-such-binary"); |
| 113 | + |
| 114 | + const status = await resolvePatchloomStatusWithInputs({ |
| 115 | + configuredPath: nonexistent, |
| 116 | + pathValue: "", |
| 117 | + canExecute: async (candidate) => { |
| 118 | + try { |
| 119 | + await fs.access(candidate, fs.constants.X_OK); |
| 120 | + return true; |
| 121 | + } catch { |
| 122 | + return false; |
| 123 | + } |
| 124 | + }, |
| 125 | + getVersion: async () => undefined |
| 126 | + }); |
| 127 | + |
| 128 | + assert.equal(status.ready, false); |
| 129 | + assert.equal(status.source, "setting"); |
| 130 | + assert.match(status.message, /not executable/); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +test("parsePatchloomVersion handles edge cases", () => { |
| 135 | + assert.equal(parsePatchloomVersion(undefined), undefined); |
| 136 | + assert.equal(parsePatchloomVersion(""), undefined); |
| 137 | + assert.equal(parsePatchloomVersion("0.1.0"), "0.1.0"); |
| 138 | + assert.equal(parsePatchloomVersion("patchloom v1.2.3+build.42"), "1.2.3+build.42"); |
| 139 | + assert.equal(parsePatchloomVersion("no version here"), undefined); |
| 140 | +}); |
| 141 | + |
| 142 | +test("comparePatchloomVersions handles major, minor, and patch differences", () => { |
| 143 | + assert.ok(comparePatchloomVersions("1.0.0", "0.9.9") > 0); |
| 144 | + assert.ok(comparePatchloomVersions("0.1.0", "0.0.99") > 0); |
| 145 | + assert.ok(comparePatchloomVersions("0.0.1", "0.0.0") > 0); |
| 146 | + assert.ok(comparePatchloomVersions("0.0.0", "0.0.1") < 0); |
| 147 | + assert.equal(comparePatchloomVersions("1.2.3", "1.2.3"), 0); |
| 148 | +}); |
| 149 | + |
| 150 | +test("assessPatchloomCompatibility correctly identifies supported versions", () => { |
| 151 | + const supported = assessPatchloomCompatibility("patchloom 0.1.0"); |
| 152 | + assert.equal(supported.compatibility, "supported"); |
| 153 | + assert.equal(supported.detectedVersion, "0.1.0"); |
| 154 | + |
| 155 | + const newer = assessPatchloomCompatibility("patchloom 1.0.0"); |
| 156 | + assert.equal(newer.compatibility, "supported"); |
| 157 | + |
| 158 | + const noVersion = assessPatchloomCompatibility("some unknown output"); |
| 159 | + assert.equal(noVersion.compatibility, "unknown"); |
| 160 | +}); |
0 commit comments