-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathautonomous.test.ts
More file actions
56 lines (50 loc) · 1.97 KB
/
autonomous.test.ts
File metadata and controls
56 lines (50 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { expect, test } from "bun:test";
import { decisionPrompt, isCommandAllowed, parseDecision } from "./autonomous";
test("parseDecision accepts valid JSON decisions and clamps sleep", () => {
expect(
parseDecision('{"action":"RUN","command":"ls","note":"inspect"}'),
).toEqual({
action: "RUN",
command: "ls",
note: "inspect",
});
expect(parseDecision('{"action":"SLEEP","sleepMs":1,"note":"wait"}')).toEqual(
{
action: "SLEEP",
sleepMs: 100,
note: "wait",
},
);
expect(parseDecision('{"action":"STOP","note":"done"}')).toEqual({
action: "STOP",
note: "done",
});
});
test("parseDecision rejects incomplete or malformed decisions", () => {
expect(parseDecision("not json")).toBeNull();
expect(parseDecision('{"action":"RUN","note":"missing command"}')).toBeNull();
expect(parseDecision('{"action":"SLEEP","sleepMs":"nope"}')).toBeNull();
expect(parseDecision('{"action":"DELETE","note":"bad action"}')).toBeNull();
});
test("isCommandAllowed blocks shell metacharacters and unknown commands", () => {
const allowed = ["ls", "pwd", "cat", "echo"];
expect(isCommandAllowed("ls -la", allowed)).toBe(true);
expect(isCommandAllowed(" echo hello ", allowed)).toBe(true);
expect(isCommandAllowed("rm file", allowed)).toBe(false);
expect(isCommandAllowed("ls && rm file", allowed)).toBe(false);
expect(isCommandAllowed("cat file | grep x", allowed)).toBe(false);
expect(isCommandAllowed("echo hello\npwd", allowed)).toBe(false);
});
test("decisionPrompt includes sandbox, commands, goal, and history", () => {
const prompt = decisionPrompt({
goal: "Write STATUS.txt",
allowedDirectory: "/tmp/sandbox",
allowedCommands: ["ls", "echo"],
recentSteps: "[step 1] SLEEP",
});
expect(prompt).toContain("Write STATUS.txt");
expect(prompt).toContain("/tmp/sandbox");
expect(prompt).toContain("ls, echo");
expect(prompt).toContain("[step 1] SLEEP");
expect(prompt).toContain('"action": "RUN|SLEEP|STOP"');
});