@useavalon/caelence-agent 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -8
- package/package.json +7 -2
- package/src/browser/browser-use.test.ts +54 -2
- package/src/browser/browser-use.ts +75 -3
- package/src/browser/own-chrome.test.ts +593 -0
- package/src/browser/own-chrome.ts +542 -0
- package/src/core/approval.test.ts +126 -0
- package/src/core/approval.ts +17 -0
- package/src/core/events.ts +7 -1
- package/src/core/host-extensions.test.ts +605 -0
- package/src/core/mcp.ts +7 -0
- package/src/core/run-turn.test.ts +85 -0
- package/src/core/run-turn.ts +134 -28
- package/src/core/strands.test.ts +89 -0
- package/src/core/strands.ts +3 -1
- package/src/decisions/shortcuts.ts +5 -0
- package/src/desktop/bridge.ts +66 -12
- package/src/desktop/host.test.ts +244 -0
- package/src/desktop/host.ts +59 -0
- package/src/index.ts +57 -3
- package/src/runtime.ts +110 -20
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { type McpToolDefinition, textResult, UNKNOWN_TOOL_ANNOTATIONS } from "../core/mcp.ts";
|
|
3
|
+
import {
|
|
4
|
+
BROWSER_APPS,
|
|
5
|
+
CHROME_NOT_READY,
|
|
6
|
+
createOwnChromeGuard,
|
|
7
|
+
hostsInText,
|
|
8
|
+
liveTarget,
|
|
9
|
+
NOT_READY_ENDPOINT,
|
|
10
|
+
nodeSearchDirs,
|
|
11
|
+
notReadyMessage,
|
|
12
|
+
ownChromeServerOptions,
|
|
13
|
+
PLAYWRIGHT_MCP_VERSION,
|
|
14
|
+
portAnswers,
|
|
15
|
+
readDevToolsEndpoint,
|
|
16
|
+
resolveNpx,
|
|
17
|
+
resolveTarget,
|
|
18
|
+
startOwnChromeTools,
|
|
19
|
+
} from "./own-chrome.ts";
|
|
20
|
+
|
|
21
|
+
/** A snapshot in the exact shape Playwright MCP returns, captured from a real run. */
|
|
22
|
+
const PAGE = (url: string) => `### Open tabs
|
|
23
|
+
- 0: (current) [Test side](${url})
|
|
24
|
+
### Page
|
|
25
|
+
- Page URL: ${url}
|
|
26
|
+
- Page Title: Test side
|
|
27
|
+
### Snapshot
|
|
28
|
+
\`\`\`yaml
|
|
29
|
+
- generic [active] [ref=e1]:
|
|
30
|
+
- heading "Skriv et opslag" [level=1] [ref=e2]
|
|
31
|
+
- textbox "Hvad sker der?" [ref=e3]
|
|
32
|
+
- searchbox "Søg" [ref=e4]
|
|
33
|
+
- button "Gem kladde" [ref=e5]
|
|
34
|
+
- button "Slå op" [ref=e6]
|
|
35
|
+
- button "Annuller" [ref=e7]
|
|
36
|
+
- link "Andet link" [ref=e8] [cursor=pointer]:
|
|
37
|
+
- /url: /other
|
|
38
|
+
\`\`\``;
|
|
39
|
+
|
|
40
|
+
function fake(name: string, reply: (input: Record<string, unknown>) => string | Error) {
|
|
41
|
+
const calls: Array<Record<string, unknown>> = [];
|
|
42
|
+
const tool: McpToolDefinition = {
|
|
43
|
+
name: `browser__${name}`,
|
|
44
|
+
description: name,
|
|
45
|
+
inputSchema: { type: "object", properties: {} },
|
|
46
|
+
annotations: UNKNOWN_TOOL_ANNOTATIONS,
|
|
47
|
+
async handler(input) {
|
|
48
|
+
calls.push(input);
|
|
49
|
+
const out = reply(input);
|
|
50
|
+
return out instanceof Error ? textResult(out.message, true) : textResult(out);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
return { tool, calls };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function setup(trusted: string[] = ["x.com"]) {
|
|
57
|
+
const url = { current: "https://x.com/" };
|
|
58
|
+
const names = [
|
|
59
|
+
"browser_navigate",
|
|
60
|
+
"browser_tabs",
|
|
61
|
+
"browser_snapshot",
|
|
62
|
+
"browser_click",
|
|
63
|
+
"browser_type",
|
|
64
|
+
"browser_fill_form",
|
|
65
|
+
"browser_press_key",
|
|
66
|
+
"browser_handle_dialog",
|
|
67
|
+
"browser_evaluate",
|
|
68
|
+
"browser_run_code_unsafe",
|
|
69
|
+
"browser_file_upload",
|
|
70
|
+
"browser_network_requests",
|
|
71
|
+
"browser_close",
|
|
72
|
+
];
|
|
73
|
+
// Playwright MCP returns a full snapshot only for some tools. A click or a keystroke returns
|
|
74
|
+
// just the code it ran, with no refs, and the guard must keep the refs from the last snapshot.
|
|
75
|
+
const ACTS = new Set(["browser_click", "browser_type", "browser_fill_form", "browser_press_key"]);
|
|
76
|
+
const RAN = "### Ran Playwright code\n```js\nawait page.getByRole('textbox').fill('x');\n```";
|
|
77
|
+
const made = Object.fromEntries(
|
|
78
|
+
names.map((n) => [
|
|
79
|
+
n,
|
|
80
|
+
fake(n, (input) => {
|
|
81
|
+
if (typeof input.url === "string") url.current = input.url;
|
|
82
|
+
return ACTS.has(n) ? RAN : PAGE(url.current);
|
|
83
|
+
}),
|
|
84
|
+
]),
|
|
85
|
+
);
|
|
86
|
+
const guard = createOwnChromeGuard();
|
|
87
|
+
guard.trust(trusted);
|
|
88
|
+
const wrapped = guard.tools(Object.values(made).map((m) => m.tool));
|
|
89
|
+
const by = (n: string) => wrapped.find((t) => t.name === `browser__${n}`);
|
|
90
|
+
return { guard, wrapped, made, by };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const call = (t: McpToolDefinition | undefined, input: Record<string, unknown>) => {
|
|
94
|
+
if (!t) throw new Error("tool missing");
|
|
95
|
+
return t.handler(input);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
describe("tool allowlist", () => {
|
|
99
|
+
test("offers the page tools and none that run code, upload files, or read traffic", () => {
|
|
100
|
+
const { wrapped } = setup();
|
|
101
|
+
const names = wrapped.map((t) => t.name.replace("browser__", ""));
|
|
102
|
+
expect(names).toContain("browser_click");
|
|
103
|
+
expect(names).toContain("browser_snapshot");
|
|
104
|
+
for (const banned of [
|
|
105
|
+
"browser_evaluate",
|
|
106
|
+
"browser_run_code_unsafe",
|
|
107
|
+
"browser_file_upload",
|
|
108
|
+
"browser_network_requests",
|
|
109
|
+
"browser_close",
|
|
110
|
+
]) {
|
|
111
|
+
expect(names).not.toContain(banned);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("own tab", () => {
|
|
117
|
+
test("navigate is refused until the agent has opened a tab of its own", async () => {
|
|
118
|
+
const { by, made } = setup();
|
|
119
|
+
const res = await call(by("browser_navigate"), { url: "https://x.com/home" });
|
|
120
|
+
expect(res.isError).toBe(true);
|
|
121
|
+
expect(res.content[0]?.text).toContain("browser_tabs action=new");
|
|
122
|
+
expect(made.browser_navigate?.calls).toHaveLength(0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("after opening its own tab, navigation works", async () => {
|
|
126
|
+
const { by, made } = setup();
|
|
127
|
+
await call(by("browser_tabs"), { action: "new", url: "https://x.com/" });
|
|
128
|
+
const res = await call(by("browser_navigate"), { url: "https://x.com/home" });
|
|
129
|
+
expect(res.isError).toBeFalsy();
|
|
130
|
+
expect(made.browser_navigate?.calls).toHaveLength(1);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("closing and switching the user's tabs is refused", async () => {
|
|
134
|
+
const { by, made } = setup();
|
|
135
|
+
for (const action of ["close", "select"]) {
|
|
136
|
+
const res = await call(by("browser_tabs"), { action, index: 0 });
|
|
137
|
+
expect(res.isError).toBe(true);
|
|
138
|
+
}
|
|
139
|
+
expect(made.browser_tabs?.calls).toHaveLength(0);
|
|
140
|
+
expect((await call(by("browser_tabs"), { action: "list" })).isError).toBeFalsy();
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("opening sites", () => {
|
|
145
|
+
test("a site the user named does not ask, including its subdomains", () => {
|
|
146
|
+
const { by } = setup(["x.com"]);
|
|
147
|
+
expect(
|
|
148
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://x.com/compose/post" }),
|
|
149
|
+
).toBeUndefined();
|
|
150
|
+
expect(
|
|
151
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://www.x.com/" }),
|
|
152
|
+
).toBeUndefined();
|
|
153
|
+
expect(
|
|
154
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://api.x.com/" }),
|
|
155
|
+
).toBeUndefined();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("any other site asks first, and names it", async () => {
|
|
159
|
+
const { by } = setup(["x.com"]);
|
|
160
|
+
const reason = by("browser_tabs")?.confirm?.({
|
|
161
|
+
action: "new",
|
|
162
|
+
url: "https://mybank.example/login",
|
|
163
|
+
});
|
|
164
|
+
expect(reason).toContain("mybank.example");
|
|
165
|
+
// navigate asks too once there is an own tab
|
|
166
|
+
await call(by("browser_tabs"), { action: "new", url: "https://x.com/" });
|
|
167
|
+
expect(by("browser_navigate")?.confirm?.({ url: "https://evil.example/" })).toContain(
|
|
168
|
+
"evil.example",
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("a look-alike host is not covered by a trusted one", () => {
|
|
173
|
+
const { by } = setup(["x.com"]);
|
|
174
|
+
expect(
|
|
175
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://notx.com/" }),
|
|
176
|
+
).toBeDefined();
|
|
177
|
+
expect(
|
|
178
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://x.com.evil.example/" }),
|
|
179
|
+
).toBeDefined();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("once the user approves a site, later opens of it do not ask again", async () => {
|
|
183
|
+
const { by } = setup(["x.com"]);
|
|
184
|
+
expect(
|
|
185
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://github.com/" }),
|
|
186
|
+
).toBeDefined();
|
|
187
|
+
await call(by("browser_tabs"), { action: "new", url: "https://github.com/" });
|
|
188
|
+
expect(
|
|
189
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://github.com/pulls" }),
|
|
190
|
+
).toBeUndefined();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("a refused open is not remembered", async () => {
|
|
194
|
+
const { by } = setup(["x.com"]);
|
|
195
|
+
// The gate refused, so the handler never ran. The site must still ask next time.
|
|
196
|
+
expect(
|
|
197
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://github.com/" }),
|
|
198
|
+
).toBeDefined();
|
|
199
|
+
expect(
|
|
200
|
+
by("browser_tabs")?.confirm?.({ action: "new", url: "https://github.com/" }),
|
|
201
|
+
).toBeDefined();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("clicks that commit", () => {
|
|
206
|
+
async function onPage() {
|
|
207
|
+
const ctx = setup(["x.com"]);
|
|
208
|
+
await call(ctx.by("browser_tabs"), { action: "new", url: "https://x.com/" });
|
|
209
|
+
await call(ctx.by("browser_snapshot"), {});
|
|
210
|
+
return ctx;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
test("a button that names a commit asks, in English and Danish", async () => {
|
|
214
|
+
const { by } = await onPage();
|
|
215
|
+
// e6 is "Slå op" (Danish for post) on the real snapshot above.
|
|
216
|
+
expect(by("browser_click")?.confirm?.({ element: "Slå op button", target: "e6" })).toContain(
|
|
217
|
+
"Slå op",
|
|
218
|
+
);
|
|
219
|
+
// English wording in the model's description of an element the page does not label.
|
|
220
|
+
expect(by("browser_click")?.confirm?.({ element: "Post button", target: "eX" })).toContain(
|
|
221
|
+
"Post",
|
|
222
|
+
);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("the approval shows the page's own label, even when the model describes it differently", async () => {
|
|
226
|
+
const { by } = await onPage();
|
|
227
|
+
// The model says "Post button" but ref e5 is really "Gem kladde". The user must see the truth.
|
|
228
|
+
const reason = by("browser_click")?.confirm?.({ element: "Post button", target: "e5" });
|
|
229
|
+
expect(reason).toContain("Gem kladde");
|
|
230
|
+
expect(reason).not.toContain("Post button");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("a harmless click does not ask", async () => {
|
|
234
|
+
const { by } = await onPage();
|
|
235
|
+
expect(by("browser_click")?.confirm?.({ element: "Gem kladde", target: "e5" })).toBeUndefined();
|
|
236
|
+
expect(by("browser_click")?.confirm?.({ element: "Annuller", target: "e7" })).toBeUndefined();
|
|
237
|
+
expect(by("browser_click")?.confirm?.({ element: "link", target: "e8" })).toBeUndefined();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test("commit words match whole words only", async () => {
|
|
241
|
+
const { by } = await onPage();
|
|
242
|
+
expect(
|
|
243
|
+
by("browser_click")?.confirm?.({ element: "Postcode field", target: "eX" }),
|
|
244
|
+
).toBeUndefined();
|
|
245
|
+
expect(
|
|
246
|
+
by("browser_click")?.confirm?.({ element: "Compost guide", target: "eX" }),
|
|
247
|
+
).toBeUndefined();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("after typing, any button asks, whatever language it is in", async () => {
|
|
251
|
+
const { by } = await onPage();
|
|
252
|
+
await call(by("browser_type"), { target: "e3", text: "hello" });
|
|
253
|
+
// "Gem kladde" (save draft) asks too: the rule does not read words
|
|
254
|
+
expect(by("browser_click")?.confirm?.({ element: "x", target: "e5" })).toContain("Gem kladde");
|
|
255
|
+
expect(by("browser_click")?.confirm?.({ element: "x", target: "e7" })).toContain("Annuller");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("the page's labels survive a type call that returns no snapshot", async () => {
|
|
259
|
+
const { by } = await onPage();
|
|
260
|
+
await call(by("browser_type"), { target: "e3", text: "hello" });
|
|
261
|
+
await call(by("browser_click"), { target: "e4" });
|
|
262
|
+
// e6 was read from the snapshot before typing. A type and a click later it must still resolve.
|
|
263
|
+
expect(by("browser_click")?.confirm?.({ element: "knap", target: "e6" })).toContain("Slå op");
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("refs from before a navigation are forgotten", async () => {
|
|
267
|
+
const guard = createOwnChromeGuard();
|
|
268
|
+
guard.trust(["x.com"]);
|
|
269
|
+
// Navigation that returns no snapshot, as a back or a redirect might.
|
|
270
|
+
const nav = fake("browser_navigate", () => "### Result\nNavigated");
|
|
271
|
+
const tabs = fake("browser_tabs", () => PAGE("https://x.com/"));
|
|
272
|
+
const snap = fake("browser_snapshot", () => PAGE("https://x.com/"));
|
|
273
|
+
const click = fake("browser_click", () => "### Ran Playwright code");
|
|
274
|
+
const wrapped = guard.tools([nav.tool, tabs.tool, snap.tool, click.tool]);
|
|
275
|
+
const by = (n: string) => wrapped.find((t) => t.name === `browser__${n}`);
|
|
276
|
+
await call(by("browser_tabs"), { action: "new", url: "https://x.com/" });
|
|
277
|
+
expect(by("browser_click")?.confirm?.({ element: "x", target: "e6" })).toContain("Slå op");
|
|
278
|
+
await call(by("browser_navigate"), { url: "https://x.com/home" });
|
|
279
|
+
// The page changed and nothing re-read it, so the old e6 may be a different element now.
|
|
280
|
+
expect(by("browser_click")?.confirm?.({ element: "Post button", target: "e6" })).toContain(
|
|
281
|
+
"Post button",
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("after typing, clicking another text field does not ask", async () => {
|
|
286
|
+
const { by } = await onPage();
|
|
287
|
+
await call(by("browser_type"), { target: "e3", text: "hello" });
|
|
288
|
+
expect(by("browser_click")?.confirm?.({ element: "search", target: "e4" })).toBeUndefined();
|
|
289
|
+
expect(by("browser_click")?.confirm?.({ element: "text box", target: "e3" })).toBeUndefined();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("navigating away clears the after-typing rule", async () => {
|
|
293
|
+
const { by } = await onPage();
|
|
294
|
+
await call(by("browser_type"), { target: "e3", text: "hello" });
|
|
295
|
+
await call(by("browser_navigate"), { url: "https://x.com/home" });
|
|
296
|
+
await call(by("browser_snapshot"), {});
|
|
297
|
+
expect(by("browser_click")?.confirm?.({ element: "x", target: "e5" })).toBeUndefined();
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test("typing with submit, and Enter after typing, ask", async () => {
|
|
301
|
+
const { by } = await onPage();
|
|
302
|
+
expect(by("browser_type")?.confirm?.({ target: "e3", text: "hi", submit: true })).toBeDefined();
|
|
303
|
+
expect(by("browser_press_key")?.confirm?.({ key: "Enter" })).toBeUndefined();
|
|
304
|
+
await call(by("browser_type"), { target: "e3", text: "hi" });
|
|
305
|
+
expect(by("browser_press_key")?.confirm?.({ key: "Enter" })).toBeDefined();
|
|
306
|
+
expect(by("browser_press_key")?.confirm?.({ key: "Tab" })).toBeUndefined();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test("accepting a dialog asks, dismissing does not", async () => {
|
|
310
|
+
const { by } = await onPage();
|
|
311
|
+
expect(by("browser_handle_dialog")?.confirm?.({ accept: true })).toBeDefined();
|
|
312
|
+
expect(by("browser_handle_dialog")?.confirm?.({ accept: false })).toBeUndefined();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("reading tools never ask", async () => {
|
|
316
|
+
const { by } = await onPage();
|
|
317
|
+
expect(by("browser_snapshot")?.confirm?.({})).toBeUndefined();
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe("setup errors", () => {
|
|
322
|
+
test("a refused connection becomes steps the user can follow", async () => {
|
|
323
|
+
const guard = createOwnChromeGuard();
|
|
324
|
+
const failing = fake("browser_tabs", () => {
|
|
325
|
+
return new Error(
|
|
326
|
+
"Error: Could not connect to chrome: DevToolsActivePort file not found.\nNavigate to chrome://inspect/#remote-debugging",
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
const [tool] = guard.tools([failing.tool]);
|
|
330
|
+
const res = await call(tool, { action: "list" });
|
|
331
|
+
expect(res.isError).toBe(true);
|
|
332
|
+
expect(res.content[0]?.text).toBe(CHROME_NOT_READY);
|
|
333
|
+
expect(CHROME_NOT_READY).toContain("chrome://inspect/#remote-debugging");
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test("other errors pass through untouched", async () => {
|
|
337
|
+
const guard = createOwnChromeGuard();
|
|
338
|
+
const failing = fake("browser_click", () => new Error("Element not found"));
|
|
339
|
+
const [tool] = guard.tools([failing.tool]);
|
|
340
|
+
const res = await call(tool, { target: "e1" });
|
|
341
|
+
expect(res.content[0]?.text).toBe("Element not found");
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
describe("server command", () => {
|
|
346
|
+
test("pins Playwright MCP, attaches to Chrome, and keeps output out of the project", () => {
|
|
347
|
+
const opts = ownChromeServerOptions(
|
|
348
|
+
undefined,
|
|
349
|
+
"/proj",
|
|
350
|
+
{ command: "/usr/local/bin/npx", dir: "/usr/local/bin" },
|
|
351
|
+
"/usr/bin:/bin",
|
|
352
|
+
);
|
|
353
|
+
expect(opts.command).toBe("/usr/local/bin/npx");
|
|
354
|
+
expect(opts.args).toContain(`@playwright/mcp@${PLAYWRIGHT_MCP_VERSION}`);
|
|
355
|
+
expect(opts.args).toContain("--cdp-endpoint=chrome");
|
|
356
|
+
expect(opts.args?.some((a) => a.startsWith("--output-dir=") && !a.includes("/proj"))).toBe(
|
|
357
|
+
true,
|
|
358
|
+
);
|
|
359
|
+
expect(opts.namePrefix).toBe("browser");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("puts node's folder on PATH so npx can start node under a bare PATH", () => {
|
|
363
|
+
const opts = ownChromeServerOptions(
|
|
364
|
+
undefined,
|
|
365
|
+
"/p",
|
|
366
|
+
{ command: "/usr/local/bin/npx", dir: "/usr/local/bin" },
|
|
367
|
+
"/usr/bin:/bin",
|
|
368
|
+
);
|
|
369
|
+
expect(opts.env?.PATH).toBe("/usr/local/bin:/usr/bin:/bin");
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test("uses a custom endpoint when one is configured", () => {
|
|
373
|
+
const opts = ownChromeServerOptions({ chromeEndpoint: "http://127.0.0.1:9333" }, "/p", {
|
|
374
|
+
command: "npx",
|
|
375
|
+
});
|
|
376
|
+
expect(opts.args).toContain("--cdp-endpoint=http://127.0.0.1:9333");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("finds npx on PATH first, then in the usual install folders", () => {
|
|
380
|
+
const home = "/Users/someone";
|
|
381
|
+
expect(resolveNpx({ PATH: "/opt/tools" }, (p) => p === "/opt/tools/npx", home)).toEqual({
|
|
382
|
+
command: "/opt/tools/npx",
|
|
383
|
+
dir: "/opt/tools",
|
|
384
|
+
});
|
|
385
|
+
expect(resolveNpx({ PATH: "/usr/bin:/bin" }, (p) => p === "/usr/local/bin/npx", home)).toEqual({
|
|
386
|
+
command: "/usr/local/bin/npx",
|
|
387
|
+
dir: "/usr/local/bin",
|
|
388
|
+
});
|
|
389
|
+
expect(resolveNpx({}, () => false, home)).toEqual({ command: "npx" });
|
|
390
|
+
expect(nodeSearchDirs(home)).toContain("/opt/homebrew/bin");
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
describe("hostsInText", () => {
|
|
395
|
+
test("reads hosts the user wrote", () => {
|
|
396
|
+
expect(hostsInText("write about useavalon.dev on https://x.com/home please")).toEqual([
|
|
397
|
+
"useavalon.dev",
|
|
398
|
+
"x.com",
|
|
399
|
+
]);
|
|
400
|
+
expect(hostsInText("go to www.github.com/pulls.")).toEqual(["github.com"]);
|
|
401
|
+
expect(hostsInText("write an article")).toEqual([]);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
describe("which browser to attach to", () => {
|
|
406
|
+
const home = "/Users/someone";
|
|
407
|
+
const file = (map: Record<string, string>) => (path: string) => {
|
|
408
|
+
const hit = map[path];
|
|
409
|
+
if (hit === undefined) throw new Error("ENOENT");
|
|
410
|
+
return hit;
|
|
411
|
+
};
|
|
412
|
+
const ARC_FILE = `${home}/Library/Application Support/Arc/User Data/DevToolsActivePort`;
|
|
413
|
+
|
|
414
|
+
test("Chrome is the default and is found by Playwright's channel name", () => {
|
|
415
|
+
expect(resolveTarget(undefined, home)).toMatchObject({
|
|
416
|
+
endpoint: "chrome",
|
|
417
|
+
label: "Chrome",
|
|
418
|
+
app: "chrome",
|
|
419
|
+
});
|
|
420
|
+
expect(resolveTarget({ app: "edge" }, home)).toMatchObject({
|
|
421
|
+
endpoint: "msedge",
|
|
422
|
+
label: "Edge",
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
test("Arc is read from its own port file, as a ws:// address", () => {
|
|
427
|
+
const target = resolveTarget(
|
|
428
|
+
{ app: "arc" },
|
|
429
|
+
home,
|
|
430
|
+
file({ [ARC_FILE]: "55123\n/devtools/browser/abc-123\n" }),
|
|
431
|
+
);
|
|
432
|
+
expect(target).toEqual({
|
|
433
|
+
endpoint: "ws://127.0.0.1:55123/devtools/browser/abc-123",
|
|
434
|
+
label: "Arc",
|
|
435
|
+
app: "arc",
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
test("an Arc that is not set up fails closed and never falls through to Chrome", () => {
|
|
440
|
+
const target = resolveTarget({ app: "arc" }, home, file({}));
|
|
441
|
+
expect(target.endpoint).toBe(NOT_READY_ENDPOINT);
|
|
442
|
+
expect(target.endpoint).not.toBe("chrome");
|
|
443
|
+
expect(target.label).toBe("Arc");
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test("a custom data folder is read the same way", () => {
|
|
447
|
+
const target = resolveTarget(
|
|
448
|
+
{ app: "brave", userDataDir: "/data/brave" },
|
|
449
|
+
home,
|
|
450
|
+
file({ "/data/brave/DevToolsActivePort": "40001\n/devtools/browser/zz\n" }),
|
|
451
|
+
);
|
|
452
|
+
expect(target.endpoint).toBe("ws://127.0.0.1:40001/devtools/browser/zz");
|
|
453
|
+
expect(resolveTarget({ app: "brave", userDataDir: "/none" }, home, file({})).endpoint).toBe(
|
|
454
|
+
NOT_READY_ENDPOINT,
|
|
455
|
+
);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
test("an explicit endpoint wins over the app", () => {
|
|
459
|
+
const target = resolveTarget(
|
|
460
|
+
{ app: "arc", chromeEndpoint: "http://127.0.0.1:9333" },
|
|
461
|
+
home,
|
|
462
|
+
file({}),
|
|
463
|
+
);
|
|
464
|
+
expect(target.endpoint).toBe("http://127.0.0.1:9333");
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
test("every app has a label and a data folder", () => {
|
|
468
|
+
for (const profile of Object.values(BROWSER_APPS)) {
|
|
469
|
+
expect(profile.label.length).toBeGreaterThan(0);
|
|
470
|
+
expect(profile.dataDir.length).toBeGreaterThan(0);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
describe("reading DevToolsActivePort", () => {
|
|
476
|
+
const read = (text: string) => () => text;
|
|
477
|
+
test("accepts the real two-line format", () => {
|
|
478
|
+
expect(readDevToolsEndpoint("/d", read("62800\n/devtools/browser/8f6b-44a9\n"))).toBe(
|
|
479
|
+
"ws://127.0.0.1:62800/devtools/browser/8f6b-44a9",
|
|
480
|
+
);
|
|
481
|
+
});
|
|
482
|
+
test("rejects anything else", () => {
|
|
483
|
+
for (const text of [
|
|
484
|
+
"",
|
|
485
|
+
"62800",
|
|
486
|
+
"abc\n/devtools/browser/x",
|
|
487
|
+
"62800\n/other/path",
|
|
488
|
+
"0\n/devtools/browser/x",
|
|
489
|
+
"999999\n/devtools/browser/x",
|
|
490
|
+
]) {
|
|
491
|
+
expect(readDevToolsEndpoint("/d", read(text))).toBeUndefined();
|
|
492
|
+
}
|
|
493
|
+
expect(
|
|
494
|
+
readDevToolsEndpoint("/d", () => {
|
|
495
|
+
throw new Error("nope");
|
|
496
|
+
}),
|
|
497
|
+
).toBeUndefined();
|
|
498
|
+
});
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
describe("a stale port file", () => {
|
|
502
|
+
const ws = {
|
|
503
|
+
endpoint: "ws://127.0.0.1:55123/devtools/browser/abc",
|
|
504
|
+
label: "Arc",
|
|
505
|
+
app: "arc" as const,
|
|
506
|
+
};
|
|
507
|
+
test("is used only when something answers on the port", async () => {
|
|
508
|
+
expect((await liveTarget(ws, async () => true)).endpoint).toBe(ws.endpoint);
|
|
509
|
+
const dead = await liveTarget(ws, async () => false);
|
|
510
|
+
expect(dead.endpoint).toBe(NOT_READY_ENDPOINT);
|
|
511
|
+
expect(dead.label).toBe("Arc");
|
|
512
|
+
});
|
|
513
|
+
test("a channel name is not probed", async () => {
|
|
514
|
+
let probed = 0;
|
|
515
|
+
const chrome = { endpoint: "chrome", label: "Chrome", app: "chrome" as const };
|
|
516
|
+
await liveTarget(chrome, async () => {
|
|
517
|
+
probed += 1;
|
|
518
|
+
return false;
|
|
519
|
+
});
|
|
520
|
+
expect(probed).toBe(0);
|
|
521
|
+
});
|
|
522
|
+
test("a closed port does not answer", async () => {
|
|
523
|
+
expect(await portAnswers("ws://127.0.0.1:1/devtools/browser/x", 500)).toBe(false);
|
|
524
|
+
expect(await portAnswers("not a url", 500)).toBe(false);
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
describe("setup message names the browser", () => {
|
|
529
|
+
test("Chrome keeps the plain steps", () => {
|
|
530
|
+
expect(notReadyMessage("Chrome")).toBe(CHROME_NOT_READY);
|
|
531
|
+
expect(CHROME_NOT_READY).not.toContain("no such setting");
|
|
532
|
+
});
|
|
533
|
+
test("Arc is named, and the agent may say the setting is missing", () => {
|
|
534
|
+
const text = notReadyMessage("Arc");
|
|
535
|
+
expect(text).toContain("Make sure Arc is open");
|
|
536
|
+
expect(text).toContain("in Arc open chrome://inspect/#remote-debugging");
|
|
537
|
+
expect(text).toContain("If Arc has no such setting, say so plainly and suggest Chrome");
|
|
538
|
+
});
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
describe("the server is rebuilt after a failed attach", () => {
|
|
542
|
+
function loader() {
|
|
543
|
+
const started: string[] = [];
|
|
544
|
+
const closed: number[] = [];
|
|
545
|
+
const load: typeof import("../core/stdio-mcp.ts").loadStdioMcpTools = async (options) => {
|
|
546
|
+
const id = started.length;
|
|
547
|
+
started.push(options.args?.find((a) => a.startsWith("--cdp-endpoint=")) ?? "");
|
|
548
|
+
return {
|
|
549
|
+
tools: [
|
|
550
|
+
{
|
|
551
|
+
name: "browser__browser_tabs",
|
|
552
|
+
description: "tabs",
|
|
553
|
+
inputSchema: { type: "object", properties: {} },
|
|
554
|
+
annotations: UNKNOWN_TOOL_ANNOTATIONS,
|
|
555
|
+
async handler() {
|
|
556
|
+
return textResult(
|
|
557
|
+
"Error: Could not connect to chrome: DevToolsActivePort file not found",
|
|
558
|
+
true,
|
|
559
|
+
);
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
],
|
|
563
|
+
close: () => {
|
|
564
|
+
closed.push(id);
|
|
565
|
+
},
|
|
566
|
+
};
|
|
567
|
+
};
|
|
568
|
+
return { load, started, closed };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
test("a failed attach marks the session stale, and a good one does not", async () => {
|
|
572
|
+
const { load } = loader();
|
|
573
|
+
const session = await startOwnChromeTools(
|
|
574
|
+
{ app: "arc" },
|
|
575
|
+
"/tmp",
|
|
576
|
+
load,
|
|
577
|
+
async () => false,
|
|
578
|
+
"/nonexistent-home",
|
|
579
|
+
);
|
|
580
|
+
expect(session.stale?.()).toBe(false);
|
|
581
|
+
const tabs = session.tools[0];
|
|
582
|
+
const res = await tabs?.handler({ action: "list" });
|
|
583
|
+
expect(res?.isError).toBe(true);
|
|
584
|
+
expect(res?.content[0]?.text).toContain("Make sure Arc is open");
|
|
585
|
+
expect(session.stale?.()).toBe(true);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test("the attach address is read when the server starts, so a later start sees a newer port", async () => {
|
|
589
|
+
const { load, started } = loader();
|
|
590
|
+
await startOwnChromeTools({ app: "arc" }, "/tmp", load, async () => false, "/nonexistent-home");
|
|
591
|
+
expect(started[0]).toBe(`--cdp-endpoint=${NOT_READY_ENDPOINT}`);
|
|
592
|
+
});
|
|
593
|
+
});
|