Files
AgentHub/server/__tests__/server-s3-tools.test.js
T
nntrivi2001 c0832182f2 feat(ai-tools): drop OpenCode and Grok Build
- opencode.json / grok.json leave the built-in catalog; opencode-usage.js,
  its poller, transcript branch and table creation go with it (existing
  tables and session rows stay as history).
- A Grok payload reaching the Claude hook route (Grok runs the Claude hooks
  itself) is dropped instead of recorded as tool = 'grok'.
- quota.js no longer reads Grok or OpenCode Go logins.
- Redone on v2.8.22 from c6b2892 (feat/grok-usage, never merged).
2026-10-26 09:31:30 +07:00

149 lines
6.0 KiB
JavaScript

/**
* @file server-s3-tools.test.js
* @description S3/S4 plan, P5: what a publish copies under the shared tools
* root — a native executable alone, an npm package's whole folder, a script's
* own version folder — and never a shared `bin` folder; the `bin/<command>`
* link; an unchanged tool is not copied twice; everything readable by every
* account; the app scripts and the `agenthub` launcher for members' panes.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { describe, it, before, after } = require("node:test");
const assert = require("node:assert/strict");
const path = require("node:path");
const fs = require("node:fs");
const os = require("node:os");
const TMP = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "server-s3-tools-")));
process.env.AGENTHUB_TOOLS_ROOT = path.join(TMP, "opt", "tools");
process.env.AGENTHUB_APP_ROOT = path.join(TMP, "opt", "app");
const shared = require("../lib/shared-tools");
const write = (p, body, mode = 0o755) => {
fs.mkdirSync(path.dirname(p), { recursive: true });
fs.writeFileSync(p, body, { mode });
return p;
};
const ELF = Buffer.concat([Buffer.from([0x7f, 0x45, 0x4c, 0x46]), Buffer.alloc(64)]);
after(() => fs.rmSync(TMP, { recursive: true, force: true }));
describe("what a publish copies", () => {
it("a native executable: that file alone", () => {
const f = write(path.join(TMP, "home", ".local", "share", "x", "versions", "1.0"), ELF);
assert.deepEqual(shared.planFor(f), { src: f, entry: "" });
});
it("a file in an npm package: the package folder (scoped too)", () => {
const plain = write(
path.join(TMP, "npm", "lib", "node_modules", "some-cli", "bin", "oc.js"),
"#!/usr/bin/env node\n"
);
assert.deepEqual(shared.planFor(plain), {
src: path.join(TMP, "npm", "lib", "node_modules", "some-cli"),
entry: path.join("bin", "oc.js"),
});
const scoped = write(
path.join(TMP, "npm", "lib", "node_modules", "@github", "copilot", "index.js"),
"x"
);
assert.equal(
shared.planFor(scoped).src,
path.join(TMP, "npm", "lib", "node_modules", "@github", "copilot")
);
});
it("a script in a version folder: that folder; in a shared bin: refused", () => {
const s = write(
path.join(TMP, "home", ".local", "share", "cursor-agent", "versions", "v1", "cursor-agent"),
"#!/bin/sh\n"
);
assert.deepEqual(shared.planFor(s), { src: path.dirname(s), entry: "cursor-agent" });
const b = write(path.join(TMP, "home", ".local", "bin", "tool.sh"), "#!/bin/sh\n");
assert.throws(() => shared.planFor(b), { code: "ESHAREDLAYOUT" });
// ~/.tool/tool: its login usually sits beside it — never the whole folder.
const realHome = process.env.HOME;
process.env.HOME = path.join(TMP, "home");
try {
const dot = write(path.join(TMP, "home", ".tool", "tool"), "#!/bin/sh\n");
assert.throws(() => shared.planFor(dot), { code: "ESHAREDLAYOUT" });
} finally {
process.env.HOME = realHome;
}
});
});
describe("publishing an installed tool", () => {
let real;
before(() => {
real = write(
path.join(TMP, "home", ".local", "share", "claude", "versions", "2.1.0"),
ELF,
0o700
);
write(path.join(TMP, "home", ".local", "bin", "claude"), ""); // replaced by a link below
fs.rmSync(path.join(TMP, "home", ".local", "bin", "claude"));
fs.symlinkSync(real, path.join(TMP, "home", ".local", "bin", "claude"));
const aiTools = require("../lib/ai-tools");
aiTools.detect = () => ({
installed: true,
path: path.join(TMP, "home", ".local", "bin", "claude"),
});
aiTools.get = (id) => (id === "claude" ? { id: "claude", detect: ["claude"] } : null);
});
it("copies it once, links bin/<command>, readable by everyone", async () => {
const r = await shared.publish("claude");
assert.equal(r.copied, true);
const link = path.join(process.env.AGENTHUB_TOOLS_ROOT, "bin", "claude");
assert.equal(fs.realpathSync(link), r.path);
assert.equal(shared.sharedPath("claude"), link);
assert.equal(fs.statSync(r.path).mode & 0o777, 0o755);
const again = await shared.publish("claude");
assert.equal(again.copied, false);
assert.equal(again.path, r.path);
});
it("never copies a file whose name says it holds a login", async () => {
const dir = path.join(TMP, "home", ".local", "share", "cur", "versions", "v1");
const exe = write(path.join(dir, "cur"), "#!/bin/sh\n");
write(path.join(dir, "lib.js"), "x", 0o644);
write(path.join(dir, "auth.json"), "{}", 0o600);
const aiTools = require("../lib/ai-tools");
const [get, detect] = [aiTools.get, aiTools.detect];
aiTools.get = (id) => (id === "cur" ? { id: "cur", detect: ["cur"] } : get(id));
aiTools.detect = (id, o) => (id === "cur" ? { installed: true, path: exe } : detect(id, o));
try {
const r = await shared.publish("cur");
const copy = path.dirname(r.path);
assert.ok(fs.existsSync(path.join(copy, "lib.js")));
assert.ok(!fs.existsSync(path.join(copy, "auth.json")));
} finally {
[aiTools.get, aiTools.detect] = [get, detect];
}
});
it("never names a command outside a plain file name", () => {
assert.equal(shared.sharedPath("../etc/passwd"), null);
assert.equal(shared.sharedPath("nope"), null);
});
});
describe("the app scripts members run", () => {
it("are copied with an agenthub launcher", async () => {
const root = path.join(TMP, "install");
write(path.join(root, "scripts", "hook-handler.js"), "// hook\n", 0o600);
write(path.join(root, "bin", "agenthub.js"), "// cli\n", 0o700);
await shared.publishApp(root);
const hook = path.join(process.env.AGENTHUB_APP_ROOT, "scripts", "hook-handler.js");
assert.equal(fs.readFileSync(hook, "utf8"), "// hook\n");
assert.equal(fs.statSync(hook).mode & 0o777, 0o644);
const launcher = fs.readFileSync(
path.join(process.env.AGENTHUB_TOOLS_ROOT, "bin", "agenthub"),
"utf8"
);
assert.match(launcher, /bin\/node .*\/bin\/agenthub\.js "\$@"/);
});
});