-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractor.test.ts
More file actions
336 lines (282 loc) · 11.2 KB
/
Copy pathextractor.test.ts
File metadata and controls
336 lines (282 loc) · 11.2 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import puppeteer, { type Browser, type Page } from "puppeteer";
import {
createObserverScript,
createExtractorScript,
flattenSemanticTree,
formatSemanticTreeText,
summarizeSemanticTree,
type AgentHandoff,
type SemanticNode,
} from "../src/index";
let browser: Browser;
let page: Page;
beforeAll(async () => {
browser = await puppeteer.launch({ headless: true });
page = await browser.newPage();
});
afterAll(async () => {
await browser?.close();
});
describe("extractSemanticTree", () => {
it("exports the agent handoff type from the public entry", () => {
const handoff: Pick<AgentHandoff, "decision" | "mode" | "operation" | "instruction"> = {
decision: "return",
mode: "read",
operation: "return",
instruction: "Answer now.",
};
expect(handoff.decision).toBe("return");
});
it("extracts headings, form controls, names, and state", async () => {
await page.setContent(`
<main>
<h1>Checkout</h1>
<label for="email">Email address</label>
<input id="email" type="email" required placeholder="you@example.com" />
<button aria-pressed="true">Continue</button>
<a href="/terms">Terms</a>
</main>
`);
const tree = await extract(page);
const flat = flattenSemanticTree(tree);
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("heading:Checkout");
expect(namedRoles).toContain("textbox:Email address");
expect(namedRoles).toContain("button:Continue");
expect(namedRoles).toContain("link:Terms");
expect(flat.find((node) => node.role === "textbox")?.state?.required).toBe(true);
expect(flat.find((node) => node.role === "button")?.state?.pressed).toBe(true);
});
it("extracts ARIA state values that need option-aware normalization", async () => {
await page.setContent(`
<main>
<button aria-controls="results" aria-live="polite" aria-valuetext="Ready">Run report</button>
</main>
`);
const tree = await extract(page);
const button = flattenSemanticTree(tree).find((node) => node.role === "button");
expect(button?.state).toMatchObject({
controls: "results",
live: "polite",
valueText: "Ready",
});
});
it("prunes hidden content and keeps aria-labelled controls", async () => {
await page.setContent(`
<section>
<h2 id="search-title">Search docs</h2>
<input aria-labelledby="search-title" />
<button style="display:none">Hidden button</button>
<button aria-hidden="true">Also hidden</button>
<button style="opacity:0">Transparent button</button>
</section>
`);
const tree = await extract(page);
const text = formatSemanticTreeText(tree);
expect(text).toContain("textbox 'Search docs'");
expect(text).not.toContain("Hidden button");
expect(text).not.toContain("Also hidden");
expect(text).not.toContain("Transparent button");
});
it("unrolls select options", async () => {
await page.setContent(`
<label for="car">Choose a car</label>
<select id="car">
<option value="volvo">Volvo</option>
<option value="audi" selected>Audi</option>
</select>
`);
const tree = await extract(page);
const flat = flattenSemanticTree(tree);
const options = flat.filter((node) => node.role === "option");
expect(flat.find((node) => node.role === "combobox")?.name).toBe("Choose a car");
expect(options.map((node) => node.name)).toEqual(expect.arrayContaining(["Volvo", "Audi"]));
expect(options.find((node) => node.name === "Audi")?.state?.selected).toBe(true);
});
it("can keep select controls compact without option unrolling", async () => {
await page.setContent(`
<label for="language">Language</label>
<select id="language">
<option value="en">English</option>
<option value="ko">Korean</option>
</select>
`);
const tree = (await page.evaluate(
createExtractorScript({ mode: "compact", includeBounds: false, includeSelectOptions: false }),
)) as SemanticNode;
const flat = flattenSemanticTree(tree);
expect(flat.find((node) => node.role === "combobox")?.name).toBe("Language");
expect(flat.filter((node) => node.role === "option")).toHaveLength(0);
});
it("prunes custom element wrappers that only contain semantic descendants", async () => {
await page.setContent(`
<main>
<mdn-button>
<button>Save</button>
</mdn-button>
</main>
`);
const tree = await extract(page);
const flat = flattenSemanticTree(tree);
expect(flat.find((node) => node.tag === "mdn-button")).toBeUndefined();
expect(flat.find((node) => node.role === "button")?.name).toBe("Save");
});
it("does not name structural landmarks from all descendant text", async () => {
await page.setContent(`
<main>
<h1>Article title</h1>
<p>Long article body that should not become the main landmark name.</p>
</main>
<ul>
<li><a href="/a">First link</a></li>
</ul>
`);
const tree = await extract(page);
const flat = flattenSemanticTree(tree);
expect(flat.find((node) => node.role === "main")?.name).toBe("");
expect(flat.find((node) => node.role === "list")?.name).toBe("");
expect(flat.find((node) => node.role === "listitem")?.name).toBe("");
expect(flat.find((node) => node.role === "heading")?.name).toBe("Article title");
expect(flat.find((node) => node.role === "link")?.name).toBe("First link");
});
it("uses link contents before title fallback for accessible names", async () => {
await page.setContent(`
<a href="/en" title="English — Wikipedia — The Free Encyclopedia">
English <small>7,189,000+ articles</small>
</a>
<a href="/empty" title="Fallback title"></a>
`);
const tree = await extract(page);
const links = flattenSemanticTree(tree).filter((node) => node.role === "link");
expect(links[0]?.name).toBe("English 7,189,000+ articles");
expect(links[1]?.name).toBe("Fallback title");
});
it("only exposes section and form landmarks when they have explicit names", async () => {
await page.setContent(`
<section>
<h2>Unnamed section</h2>
</section>
<section aria-label="Named section">
<p>Body</p>
</section>
<form>
<button>Unnamed form button</button>
</form>
<form aria-label="Search">
<button>Search</button>
</form>
`);
const tree = await extract(page);
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("region:Named section");
expect(namedRoles).toContain("form:Search");
expect(namedRoles).not.toContain("region:Unnamed section");
expect(namedRoles).not.toContain("form:Unnamed form button");
});
it("can exclude likely ad placements when requested", async () => {
await page.setContent(`
<main>
<a class="ad" href="/ad">Ad</a>
<a href="/real">Real link</a>
</main>
`);
const tree = (await page.evaluate(
createExtractorScript({ mode: "compact", includeBounds: false, excludeLikelyAds: true }),
)) as SemanticNode;
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("link:Real link");
expect(namedRoles).not.toContain("link:Ad");
});
it("extracts table cell names for layout-table comparison", async () => {
await page.setContent(`
<table>
<tbody>
<tr>
<td><a href="/news">Story title</a></td>
<td>12 comments</td>
</tr>
</tbody>
</table>
`);
const tree = await extract(page);
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("cell:Story title");
expect(namedRoles).toContain("cell:12 comments");
expect(namedRoles).toContain("link:Story title");
});
it("extracts semantic children from open shadow roots", async () => {
await page.setContent(`<main><x-card></x-card></main>`);
await page.evaluate(() => {
const host = document.querySelector("x-card");
const shadow = host?.attachShadow({ mode: "open" });
if (shadow) shadow.innerHTML = `<button>Shadow action</button>`;
});
const tree = await extract(page);
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("button:Shadow action");
});
it("extracts semantic children from same-origin iframes", async () => {
await page.setContent(`
<main>
<iframe srcdoc="<button>Frame action</button>"></iframe>
</main>
`);
await page.waitForFunction(() => {
const iframe = document.querySelector("iframe");
return iframe?.contentDocument?.body?.querySelector("button");
});
const tree = await extract(page);
const namedRoles = summarizeSemanticTree(tree).namedRoles;
expect(namedRoles).toContain("button:Frame action");
});
it("marks cross-origin iframes as unavailable placeholders", async () => {
await page.setContent(`
<main>
<iframe title="Remote frame" src="https://example.com"></iframe>
</main>
`);
const tree = await extract(page);
const iframePlaceholder = flattenSemanticTree(tree).find((node) =>
node.tag === "iframe" && Boolean(node.unavailableReason)
);
expect(iframePlaceholder?.tag).toBe("iframe");
});
it("streams semantic tree changes from DOM mutations", async () => {
await page.setContent(`<main id="root"></main>`);
await page.evaluate(() => {
(window as unknown as { __changes: unknown[] }).__changes = [];
window.addEventListener("__AX_LITE_OBSERVER__:change", (event) => {
(window as unknown as { __changes: unknown[] }).__changes.push((event as CustomEvent).detail);
});
});
await page.evaluate(createObserverScript({ debounceMs: 10, includeBounds: false }));
await page.evaluate(() => {
const button = document.createElement("button");
button.textContent = "Later action";
document.getElementById("root")?.append(button);
});
await page.waitForFunction(() => (window as unknown as { __changes: unknown[] }).__changes.length > 0);
const change = await page.evaluate(() => {
return (window as unknown as { __changes: Array<{ tree: SemanticNode; mutationCount: number }> }).__changes[0];
});
if (!change) throw new Error("Expected a semantic tree mutation change");
const namedRoles = summarizeSemanticTree(change.tree).namedRoles;
expect(change.mutationCount).toBeGreaterThan(0);
expect(namedRoles).toContain("button:Later action");
});
it("supports text output mode for prompt-sized inspection", async () => {
await page.setContent(`
<main>
<h1>Example</h1>
<button>Run</button>
</main>
`);
const text = await page.evaluate(createExtractorScript({ format: "text", mode: "compact" }));
expect(text).toContain("heading 'Example'");
expect(text).toContain("[i] button 'Run'");
});
});
async function extract(page: Page): Promise<SemanticNode> {
return (await page.evaluate(createExtractorScript({ mode: "compact", includeBounds: false }))) as SemanticNode;
}