Skip to content
Source
Sign inSign up

AgentScope.test.ts

47 lines1.9 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6​
7import { describe, expect, it } from "@effect/vitest";
8​
9import { AGENT_OOM_SCORE_ADJ, agentScopeCommand, agentScopeLauncherScript } from "./AgentScope.ts";
10import * as HostProcess from "./HostProcess.ts";
11​
12describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("agentScopeCommand", () => {
13 it("raises the OOM score and execs the command with its arguments in the same process", () => {
14 const launch = agentScopeCommand({
15 command: "/bin/sh",
16 args: [
17 "-c",
18 'printf "%s\\n" "$$" "$(cat /proc/self/oom_score_adj)" "$@"',
19 "agent",
20 "a b",
21 "$HOME",
22 ],
23 });
24 const child = NodeChildProcess.spawnSync(launch.command, [...launch.args], {
25 encoding: "utf8",
26 });
27 const [pid, score, ...args] = child.stdout.trimEnd().split("\n");
28 expect(Number(pid)).toBe(child.pid);
29 expect(Number(score)).toBe(AGENT_OOM_SCORE_ADJ);
30 expect(args).toEqual(["a b", "$HOME"]);
31​
32 const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-agent-launcher-"));
33 try {
34 const launcher = NodePath.join(directory, "launch's");
35 NodeFS.writeFileSync(launcher, agentScopeLauncherScript(launch), { mode: 0o700 });
36 const launched = NodeChildProcess.spawnSync(launcher, ["--flag", "it's"], {
37 encoding: "utf8",
38 });
39 const [launchedPid, launchedScore, ...launchedArgs] = launched.stdout.trimEnd().split("\n");
40 expect(Number(launchedPid)).toBe(launched.pid);
41 expect(Number(launchedScore)).toBe(AGENT_OOM_SCORE_ADJ);
42 expect(launchedArgs).toEqual(["a b", "$HOME", "--flag", "it's"]);
43 } finally {
44 NodeFS.rmSync(directory, { recursive: true, force: true });
45 }
46 });
47});