139 lines
6.8 KiB
TypeScript
139 lines
6.8 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { createHash } from "node:crypto";
|
|
import { existsSync, mkdtempSync, mkdirSync, readFileSync, readlinkSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
|
|
// Shell fixtures start several processes; allow headroom on a busy host.
|
|
const fixtures: string[] = [];
|
|
afterEach(() => { for (const root of fixtures.splice(0)) rmSync(root, { recursive: true, force: true }); });
|
|
const hash = (data: Buffer | string) => createHash("sha256").update(data).digest("hex");
|
|
|
|
function fixture(options: { badHash?: boolean; missingEntry?: boolean; duplicate?: boolean; rawOnly?: boolean } = {}) {
|
|
const root = mkdtempSync(join(tmpdir(), "hose-installer-test-"));
|
|
fixtures.push(root);
|
|
const tools = join(root, "tools"), source = join(root, "release"), target = join(root, "bin");
|
|
for (const dir of [tools, source, target]) mkdirSync(dir);
|
|
const binary = `#!/bin/sh
|
|
if [ "$1" = "skill" ]; then
|
|
printf '%s\\n' "$*" > "$HOSE_FIXTURE_LOG"
|
|
[ "$2" = "install" ] && [ "$3" = "--yes" ] || exit 10
|
|
[ "\${HOSE_FIXTURE_SKILL_FAIL:-0}" != "1" ] || exit 3
|
|
else
|
|
printf 'hose-use v9.9.9\\n'
|
|
fi
|
|
`;
|
|
const asset = "hose-use-linux-x64";
|
|
writeFileSync(join(source, asset), binary, { mode: 0o755 });
|
|
symlinkSync(asset, join(source, "hose-aipro"));
|
|
writeFileSync(join(source, "unrelated.txt"), "must not reach the install directory");
|
|
const archive = `${asset}.tar.gz`;
|
|
expect(spawnSync("tar", ["-czf", join(source, archive), "-C", source, asset, "hose-aipro", "unrelated.txt"]).status).toBe(0);
|
|
const entry = `${options.badHash ? "0".repeat(64) : hash(readFileSync(join(source, archive)))} ${archive}\n`;
|
|
writeFileSync(join(source, "checksums.txt"), (options.missingEntry ? "" : entry + (options.duplicate ? entry : "")) + `${options.badHash ? "0".repeat(64) : hash(binary)} ${asset}\n`);
|
|
if (options.rawOnly) rmSync(join(source, archive));
|
|
const curl = `#!/bin/bash
|
|
set -eu
|
|
url=""; dest=""; head_only=false
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
-o) dest="$2"; shift 2 ;;
|
|
--connect-timeout|--max-time) shift 2 ;;
|
|
-fIs) head_only=true; shift ;;
|
|
https://*) url="$1"; shift ;;
|
|
*) shift ;;
|
|
esac
|
|
done
|
|
printf '%s\\n' "$url" >> "$HOSE_FIXTURE_REQUESTS"
|
|
if [ "\${HOSE_FIXTURE_GITEA_FAIL:-0}" = "1" ] && [[ "$url" == *git.ying-qing.cn* ]]; then exit 22; fi
|
|
if [[ "$url" == */releases/latest ]]; then
|
|
[ "\${HOSE_FIXTURE_NO_TAG:-0}" != "1" ] || exit 22
|
|
printf '{"tag_name": "v9.9.9"}'
|
|
exit 0
|
|
fi
|
|
name="\${url##*/}"
|
|
if [ "$name" = "checksums.txt" ] && [ "\${HOSE_FIXTURE_NO_MANIFEST:-0}" = "1" ]; then exit 22; fi
|
|
file="$HOSE_FIXTURE_SOURCE/$name"
|
|
[ -f "$file" ] || exit 22
|
|
if [ "$head_only" = "true" ]; then exit 0; fi
|
|
if [ -n "$dest" ]; then cp "$file" "$dest"; else cat "$file"; fi
|
|
`;
|
|
writeFileSync(join(tools, "curl"), curl, { mode: 0o755 });
|
|
writeFileSync(join(tools, "uname"), '#!/bin/sh\nif [ "$1" = "-s" ]; then echo Linux; else echo x86_64; fi\n', { mode: 0o755 });
|
|
const run = (env: Record<string, string> = {}) => spawnSync("bash", [resolve(import.meta.dir, "install.sh")], {
|
|
cwd: root,
|
|
env: { ...process.env, PATH: `${tools}:${process.env.PATH}`, HOSE_USE_INSTALL_DIR: target, HOSE_USE_VERSION: "9.9.9", HOSE_USE_MIRROR: "auto", HOSE_FIXTURE_SOURCE: source, HOSE_FIXTURE_LOG: join(root, "skill.log"), HOSE_FIXTURE_REQUESTS: join(root, "requests.log"), ...env },
|
|
encoding: "utf8", timeout: 30_000,
|
|
});
|
|
return { root, source, target, run, binary };
|
|
}
|
|
|
|
describe("verified standalone installer", () => {
|
|
test("installs a platform-named archive and alias, then explicitly confirms Skill setup", () => {
|
|
const f = fixture();
|
|
const result = f.run();
|
|
expect(result.status).toBe(0);
|
|
expect(readFileSync(join(f.target, "hose-use"), "utf8")).toBe(f.binary);
|
|
expect(readlinkSync(join(f.target, "hose-aipro"))).toBe("hose-use");
|
|
expect(readFileSync(join(f.root, "skill.log"), "utf8").trim()).toBe("skill install --yes");
|
|
expect(readdirSync(f.target).sort()).toEqual(["hose-aipro", "hose-use"]);
|
|
}, 60_000);
|
|
|
|
for (const options of [{ badHash: true }, { missingEntry: true }, { duplicate: true }]) {
|
|
test(`rejects an invalid manifest/archive and preserves the old binary: ${JSON.stringify(options)}`, () => {
|
|
const f = fixture(options);
|
|
// Remove raw fallback, so a missing archive entry cannot be hidden by another valid asset.
|
|
rmSync(join(f.source, "hose-use-linux-x64"));
|
|
writeFileSync(join(f.target, "hose-use"), "old binary");
|
|
const result = f.run();
|
|
expect(result.status).toBe(1);
|
|
expect(readFileSync(join(f.target, "hose-use"), "utf8")).toBe("old binary");
|
|
expect(existsSync(join(f.root, "skill.log"))).toBe(false);
|
|
}, 60_000);
|
|
}
|
|
|
|
test("does not silently bypass missing checksums or guess an old tag", () => {
|
|
const f = fixture();
|
|
expect(f.run({ HOSE_FIXTURE_NO_MANIFEST: "1" }).status).toBe(1);
|
|
const unknown = f.run({ HOSE_USE_VERSION: "", HOSE_FIXTURE_NO_TAG: "1" });
|
|
expect(unknown.status).toBe(1);
|
|
expect(unknown.stderr).toContain("could not resolve");
|
|
expect(existsSync(join(f.target, "hose-use"))).toBe(false);
|
|
expect(readFileSync(join(f.root, "requests.log"), "utf8")).not.toContain("v0.8.9");
|
|
}, 60_000);
|
|
|
|
test("uses a verified raw fallback and a fixed tag when the first mirror fails", () => {
|
|
const f = fixture({ rawOnly: true });
|
|
const result = f.run({ HOSE_FIXTURE_GITEA_FAIL: "1", HOSE_USE_VERSION: "" });
|
|
expect(result.status).toBe(0);
|
|
expect(readFileSync(join(f.target, "hose-use"), "utf8")).toBe(f.binary);
|
|
const requests = readFileSync(join(f.root, "requests.log"), "utf8");
|
|
expect(requests).toContain("/download/v9.9.9/");
|
|
expect(requests).not.toContain("/latest/download/");
|
|
}, 60_000);
|
|
|
|
test("reports Skill failure without claiming it was installed", () => {
|
|
const f = fixture();
|
|
const result = f.run({ HOSE_FIXTURE_SKILL_FAIL: "1" });
|
|
expect(result.status).toBe(1);
|
|
expect(result.stderr).toContain("binary installed");
|
|
expect(result.stdout).not.toContain("Agent skill installed");
|
|
expect(result.stdout).not.toContain("successfully installed");
|
|
}, 60_000);
|
|
|
|
test("does not replace directories or follow an existing alias into a directory", () => {
|
|
const f = fixture();
|
|
mkdirSync(join(f.root, "personal"));
|
|
symlinkSync(join(f.root, "personal"), join(f.target, "hose-aipro"));
|
|
expect(f.run().status).toBe(0);
|
|
expect(readdirSync(join(f.root, "personal"))).toEqual([]);
|
|
expect(readlinkSync(join(f.target, "hose-aipro"))).toBe("hose-use");
|
|
rmSync(join(f.target, "hose-aipro"));
|
|
mkdirSync(join(f.target, "hose-aipro"));
|
|
writeFileSync(join(f.target, "hose-use"), "old binary");
|
|
expect(f.run().status).toBe(1);
|
|
expect(readFileSync(join(f.target, "hose-use"), "utf8")).toBe("old binary");
|
|
}, 60_000);
|
|
});
|