-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-simulator.test.ts
More file actions
188 lines (165 loc) · 5.44 KB
/
runtime-simulator.test.ts
File metadata and controls
188 lines (165 loc) · 5.44 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
AcpRuntime,
SIMULATOR_AGENT_ACP_REGISTRY_ID,
type AcpRuntimeAuthorityHandlers,
type AcpRuntimeTurnEvent,
createStdioAcpConnectionFactory,
} from "../index.js";
import { resolveBuiltSimulatorWorkspaceCliPath } from "../internal/simulator-workspace.js";
const tempDirs: string[] = [];
afterEach(async () => {
await Promise.all(
tempDirs.splice(0).map(async (path) => {
await rm(path, { force: true, recursive: true });
}),
);
});
async function createTestDirs(): Promise<{
projectDir: string;
storageDir: string;
}> {
const root = await mkdtemp(join(tmpdir(), "acp-runtime-simulator-"));
const projectDir = join(root, "project");
const storageDir = join(root, "storage");
await mkdir(projectDir, { recursive: true });
await mkdir(storageDir, { recursive: true });
tempDirs.push(root);
return { projectDir, storageDir };
}
function createFilesystemHandlers(): AcpRuntimeAuthorityHandlers["filesystem"] {
return {
async readTextFile(path) {
const { readFile } = await import("node:fs/promises");
return readFile(path, "utf8");
},
async writeTextFile(input) {
await mkdir(dirname(input.path), { recursive: true });
const { writeFile } = await import("node:fs/promises");
await writeFile(input.path, input.content, "utf8");
},
};
}
function createAuthorityHandlers(): AcpRuntimeAuthorityHandlers {
return {
filesystem: createFilesystemHandlers(),
permission() {
return {
decision: "allow",
scope: "once",
};
},
};
}
function collectEvents(): {
events: AcpRuntimeTurnEvent[];
onEvent(event: AcpRuntimeTurnEvent): void;
} {
const events: AcpRuntimeTurnEvent[] = [];
return {
events,
onEvent(event) {
events.push(event);
},
};
}
describe("AcpRuntime x simulator-agent ACP", () => {
it("creates, configures, snapshots, and resumes simulator sessions through the runtime", async () => {
const cliPath = resolveBuiltSimulatorWorkspaceCliPath();
const { projectDir, storageDir } = await createTestDirs();
const runtime = new AcpRuntime(createStdioAcpConnectionFactory());
const handlers = createAuthorityHandlers();
const firstPath = join(projectDir, "notes-one.txt");
const secondPath = join(projectDir, "notes-two.txt");
await writeFile(firstPath, "seed one\n", "utf8");
await writeFile(secondPath, "seed two\n", "utf8");
const session = await runtime.create({
agent: {
args: [cliPath, "--storage-dir", storageDir],
command: process.execPath,
type: SIMULATOR_AGENT_ACP_REGISTRY_ID,
},
cwd: projectDir,
handlers,
});
expect(session.snapshot().agent.type).toBe(SIMULATOR_AGENT_ACP_REGISTRY_ID);
expect(session.listAgentModes().map((mode) => mode.id)).toEqual([
"deny",
"accept-edits",
"yolo",
]);
expect(session.listAgentConfigOptions().map((option) => option.id)).toEqual([
"approval-policy",
"model",
"reasoning",
]);
const renameTurn = collectEvents();
const renameResult = await session.send(
"/rename Integration Session",
renameTurn,
);
expect(renameResult.outputText).toContain(
'Renamed session to "Integration Session".',
);
expect(session.metadata.title).toBe("Integration Session");
const guardedWrite = collectEvents();
const guardedWriteResult = await session.send(
`/write ${firstPath} first`,
guardedWrite,
);
expect(guardedWriteResult.outputText).toContain("Wrote");
expect(
guardedWrite.events.some(
(event) => event.type === "permission_requested",
),
).toBe(true);
await session.setAgentMode("yolo");
expect(session.metadata.currentModeId).toBe("yolo");
const unguardedWrite = collectEvents();
const unguardedWriteResult = await session.send(
`/write ${secondPath} second`,
unguardedWrite,
);
expect(unguardedWriteResult.outputText).toContain("Wrote");
expect(
unguardedWrite.events.some(
(event) => event.type === "permission_requested",
),
).toBe(false);
const snapshot = session.snapshot();
expect(snapshot.config).toMatchObject({
"approval-policy": "accept-edits",
model: "claude",
reasoning: "medium",
});
expect(snapshot.currentModeId).toBe("yolo");
const resumed = await runtime.resume({
handlers,
snapshot,
});
expect(resumed.metadata.id).toBe(snapshot.session.id);
expect(resumed.metadata.config).toMatchObject({
"approval-policy": "accept-edits",
model: "claude",
reasoning: "medium",
});
await resumed.setAgentMode("deny");
expect(resumed.metadata.currentModeId).toBe("deny");
const deniedSnapshot = resumed.snapshot();
expect(deniedSnapshot.currentModeId).toBe("deny");
const describeResult = await resumed.send("/describe");
expect(describeResult.outputText).toContain("Current mode: deny");
expect(describeResult.outputText).toContain("Permission policy: deny");
const resumedDenied = await runtime.resume({
handlers,
snapshot: deniedSnapshot,
});
expect(resumedDenied.metadata.currentModeId).toBe("deny");
await resumedDenied.close();
await resumed.close();
await session.close();
});
});