Files
wehub-resource-sync cb15c5e0d8
Release / Check for new version (push) Has been cancelled
Release / Build macOS ARM64 (push) Has been cancelled
Release / Build macOS x64 (push) Has been cancelled
Release / Build Linux ARM64 (push) Has been cancelled
Release / Build Linux musl ARM64 (push) Has been cancelled
Release / Build Linux musl x64 (push) Has been cancelled
Release / Build Linux x64 (push) Has been cancelled
Release / Build Windows x64 (push) Has been cancelled
Release / Publish to npm (push) Has been cancelled
Release / Publish sandbox package to npm (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
CI / Rust (windows-latest - x86_64-pc-windows-msvc) (push) Has been cancelled
CI / Native E2E Tests (push) Has been cancelled
CI / Windows Integration Test (push) Has been cancelled
CI / Global Install (macos-latest) (push) Has been cancelled
CI / Global Install (ubuntu-latest) (push) Has been cancelled
CI / Version Sync Check (push) Has been cancelled
CI / Rust (push) Has been cancelled
CI / Dashboard (push) Has been cancelled
CI / Sandbox Package (push) Has been cancelled
CI / Rust (macos-latest - aarch64-apple-darwin) (push) Has been cancelled
CI / Rust (macos-latest - x86_64-apple-darwin) (push) Has been cancelled
CI / Global Install (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:58 +08:00

282 lines
6.5 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import {
createAgentBrowserSandbox,
createAgentBrowserSnapshot,
getSandboxCredentials,
installAgentBrowserInVercelSandbox,
runAgentBrowserCommand,
withAgentBrowserSandbox,
} from "../dist/vercel.js";
function commandResult(stdout = "{}", stderr = "", exitCode = 0) {
return {
exitCode,
async stderr() {
return stderr;
},
async stdout() {
return stdout;
},
};
}
test("imports vercel entry without loading a real @vercel/sandbox module", async () => {
assert.equal(typeof getSandboxCredentials, "function");
});
test("reads explicit Vercel credentials", () => {
assert.deepEqual(
getSandboxCredentials({
VERCEL_PROJECT_ID: "project",
VERCEL_TEAM_ID: "team",
VERCEL_TOKEN: "token",
}),
{ projectId: "project", teamId: "team", token: "token" },
);
assert.deepEqual(getSandboxCredentials({}), {});
});
test("runs agent-browser command in a Vercel sandbox", async () => {
const calls = [];
const sandbox = {
async runCommand(command, args) {
calls.push([command, args]);
return commandResult('{"ok":true}');
},
};
const result = await runAgentBrowserCommand(sandbox, ["open", "https://example.com"], {
session: "s1",
});
assert.deepEqual(result.json, { ok: true });
assert.deepEqual(calls, [
["agent-browser", ["--session", "s1", "open", "https://example.com", "--json"]],
]);
});
test("creates and bootstraps a fresh Vercel sandbox", async () => {
const calls = [];
const sandbox = {
async runCommand(command, args) {
calls.push([command, args]);
return commandResult();
},
async snapshot() {
return { snapshotId: "snap" };
},
async stop() {},
};
const Sandbox = {
async create(options) {
calls.push(["create", options]);
return sandbox;
},
};
await createAgentBrowserSandbox({
Sandbox,
env: {},
install: { installSpec: "agent-browser@1.2.3", systemDependencies: ["nss"] },
});
assert.equal(calls[0][0], "create");
assert.deepEqual(calls[1], [
"sh",
[
"-c",
"sudo dnf clean all 2>&1 && sudo dnf install -y --skip-broken -- nss 2>&1 && sudo ldconfig 2>&1",
],
]);
assert.deepEqual(calls[2], ["npm", ["install", "-g", "agent-browser@1.2.3"]]);
assert.deepEqual(calls[3], ["agent-browser", ["install"]]);
});
test("skips Vercel system dependencies when explicitly disabled", async () => {
const calls = [];
const sandbox = {
async runCommand(command, args) {
calls.push([command, args]);
return commandResult();
},
};
await installAgentBrowserInVercelSandbox(sandbox, {
installSpec: "agent-browser@1.2.3",
installSystemDependencies: false,
});
assert.deepEqual(calls, [
["npm", ["install", "-g", "agent-browser@1.2.3"]],
["agent-browser", ["install"]],
]);
});
test("rejects invalid Vercel system dependency names", async () => {
const calls = [];
const sandbox = {
async runCommand(command, args) {
calls.push([command, args]);
return commandResult();
},
};
await assert.rejects(
() =>
installAgentBrowserInVercelSandbox(sandbox, {
systemDependencies: ["nss; touch /tmp/pwned"],
}),
/Invalid system dependency name/,
);
assert.deepEqual(calls, []);
});
test("stops a fresh Vercel sandbox when bootstrap fails", async () => {
let stopped = false;
const sandbox = {
async runCommand() {
return commandResult("", "install failed", 1);
},
async snapshot() {
return { snapshotId: "snap" };
},
async stop() {
stopped = true;
},
};
await assert.rejects(
() =>
createAgentBrowserSandbox({
Sandbox: { async create() { return sandbox; } },
env: {},
install: { systemDependencies: [] },
}),
/install failed/,
);
assert.equal(stopped, true);
});
test("withAgentBrowserSandbox stops the sandbox", async () => {
let stopped = false;
const sandbox = {
async runCommand() {
return commandResult();
},
async snapshot() {
return { snapshotId: "snap" };
},
async stop() {
stopped = true;
},
};
const value = await withAgentBrowserSandbox(async () => 42, {
Sandbox: { async create() { return sandbox; } },
bootstrap: false,
env: {},
});
assert.equal(value, 42);
assert.equal(stopped, true);
});
test("withAgentBrowserSandbox preserves callback failure when stop fails", async () => {
const sandbox = {
async runCommand() {
return commandResult();
},
async snapshot() {
return { snapshotId: "snap" };
},
async stop() {
throw new Error("stop failed");
},
};
await assert.rejects(
() =>
withAgentBrowserSandbox(
async () => {
throw new Error("work failed");
},
{
Sandbox: { async create() { return sandbox; } },
bootstrap: false,
env: {},
},
),
/work failed/,
);
});
test("withAgentBrowserSandbox surfaces stop failure after success", async () => {
const sandbox = {
async runCommand() {
return commandResult();
},
async snapshot() {
return { snapshotId: "snap" };
},
async stop() {
throw new Error("stop failed");
},
};
await assert.rejects(
() =>
withAgentBrowserSandbox(async () => 42, {
Sandbox: { async create() { return sandbox; } },
bootstrap: false,
env: {},
}),
/stop failed/,
);
});
test("creates a Vercel sandbox snapshot", async () => {
const sandbox = {
async runCommand() {
return commandResult();
},
async snapshot() {
return { snapshotId: "snap_123" };
},
async stop() {},
};
const snapshotId = await createAgentBrowserSnapshot({
Sandbox: { async create() { return sandbox; } },
env: {},
install: { systemDependencies: [] },
});
assert.equal(snapshotId, "snap_123");
});
test("createAgentBrowserSnapshot preserves snapshot failure when stop fails", async () => {
const sandbox = {
async runCommand() {
return commandResult();
},
async snapshot() {
throw new Error("snapshot failed");
},
async stop() {
throw new Error("stop failed");
},
};
await assert.rejects(
() =>
createAgentBrowserSnapshot({
Sandbox: { async create() { return sandbox; } },
env: {},
install: { systemDependencies: [] },
}),
/snapshot failed/,
);
});