|
1 | 1 | // Browser tests cover pw tools core.snapshot plugin behavior. |
| 2 | +import { createDeferred } from "openclaw/plugin-sdk/extension-shared"; |
2 | 3 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 4 |
|
4 | 5 | const getPageForTargetId = vi.fn(); |
@@ -218,6 +219,123 @@ describe("pw-tools-core aria snapshot storage", () => { |
218 | 219 | expect(storeRoleRefsForTarget).not.toHaveBeenCalled(); |
219 | 220 | }); |
220 | 221 |
|
| 222 | + it("returns selector no-match snapshots without collecting URLs", async () => { |
| 223 | + const ariaSnapshot = vi.fn(async () => { |
| 224 | + throw new Error("ariaSnapshot should not run for a selector with no matches"); |
| 225 | + }); |
| 226 | + const locator = { |
| 227 | + count: vi.fn(async () => 0), |
| 228 | + ariaSnapshot, |
| 229 | + }; |
| 230 | + const page = { |
| 231 | + locator: vi.fn(() => locator), |
| 232 | + mainFrame: vi.fn(() => ({ id: "main-frame" })), |
| 233 | + on: vi.fn(), |
| 234 | + off: vi.fn(), |
| 235 | + evaluate: vi.fn(async () => [{ text: "link", url: "https://example.test" }]), |
| 236 | + }; |
| 237 | + getPageForTargetId.mockResolvedValue(page); |
| 238 | + |
| 239 | + const mod = await import("./pw-tools-core.snapshot.js"); |
| 240 | + const result = await mod.snapshotRoleViaPlaywright({ |
| 241 | + cdpUrl: "http://127.0.0.1:9222", |
| 242 | + targetId: "tab-1", |
| 243 | + selector: "#missing", |
| 244 | + urls: true, |
| 245 | + }); |
| 246 | + |
| 247 | + expect(result.snapshot).toBe("(empty)"); |
| 248 | + expect(result.snapshot).not.toContain("Links:"); |
| 249 | + expect(locator.count).toHaveBeenCalledOnce(); |
| 250 | + expect(ariaSnapshot).not.toHaveBeenCalled(); |
| 251 | + expect(page.evaluate).not.toHaveBeenCalled(); |
| 252 | + }); |
| 253 | + |
| 254 | + it("keeps URLs for valid snapshots whose depth filter removes every ref", async () => { |
| 255 | + const locator = { |
| 256 | + count: vi.fn(async () => 1), |
| 257 | + ariaSnapshot: vi.fn(async () => '- main:\n - link "Docs"'), |
| 258 | + }; |
| 259 | + const page = { |
| 260 | + locator: vi.fn(() => locator), |
| 261 | + mainFrame: vi.fn(() => ({ id: "main-frame" })), |
| 262 | + on: vi.fn(), |
| 263 | + off: vi.fn(), |
| 264 | + evaluate: vi.fn(async () => [{ text: "Docs", url: "https://example.test/docs" }]), |
| 265 | + }; |
| 266 | + getPageForTargetId.mockResolvedValue(page); |
| 267 | + |
| 268 | + const mod = await import("./pw-tools-core.snapshot.js"); |
| 269 | + const result = await mod.snapshotRoleViaPlaywright({ |
| 270 | + cdpUrl: "http://127.0.0.1:9222", |
| 271 | + targetId: "tab-1", |
| 272 | + selector: "main", |
| 273 | + options: { maxDepth: 0 }, |
| 274 | + urls: true, |
| 275 | + }); |
| 276 | + |
| 277 | + expect(result.refs).toEqual({}); |
| 278 | + expect(result.snapshot).toContain("- main:"); |
| 279 | + expect(result.snapshot).toContain("https://example.test/docs"); |
| 280 | + expect(locator.count).toHaveBeenCalledOnce(); |
| 281 | + expect(page.evaluate).toHaveBeenCalledOnce(); |
| 282 | + }); |
| 283 | + |
| 284 | + it("times out a stalled selector probe without publishing late refs", async () => { |
| 285 | + const mod = await import("./pw-tools-core.snapshot.js"); |
| 286 | + const pendingCount = createDeferred<number>(); |
| 287 | + const ariaSnapshot = vi.fn(async () => '- button "Late"'); |
| 288 | + const page = { |
| 289 | + ...makeAriaSnapshotPage(ariaSnapshot), |
| 290 | + locator: vi.fn(() => ({ count: () => pendingCount.promise, ariaSnapshot })), |
| 291 | + }; |
| 292 | + getPageForTargetId.mockResolvedValue(page); |
| 293 | + vi.useFakeTimers(); |
| 294 | + try { |
| 295 | + const promise = mod.snapshotRoleViaPlaywright({ |
| 296 | + cdpUrl: "http://127.0.0.1:9222", |
| 297 | + targetId: "tab-1", |
| 298 | + selector: "#present", |
| 299 | + timeoutMs: 750, |
| 300 | + }); |
| 301 | + const rejected = expect(promise).rejects.toThrow(/timed out/); |
| 302 | + await vi.advanceTimersByTimeAsync(750); |
| 303 | + await rejected; |
| 304 | + pendingCount.resolve(1); |
| 305 | + await vi.advanceTimersByTimeAsync(0); |
| 306 | + expect(ariaSnapshot).not.toHaveBeenCalled(); |
| 307 | + expect(storeRoleRefsForTarget).not.toHaveBeenCalled(); |
| 308 | + expect(page.off).toHaveBeenCalledTimes(2); |
| 309 | + } finally { |
| 310 | + vi.useRealTimers(); |
| 311 | + } |
| 312 | + }); |
| 313 | + |
| 314 | + it("shares the capture timeout between selector lookup and snapshot", async () => { |
| 315 | + const mod = await import("./pw-tools-core.snapshot.js"); |
| 316 | + const pendingCount = createDeferred<number>(); |
| 317 | + const ariaSnapshot = vi.fn(async () => '- button "Present"'); |
| 318 | + getPageForTargetId.mockResolvedValue({ |
| 319 | + ...makeAriaSnapshotPage(ariaSnapshot), |
| 320 | + locator: () => ({ count: () => pendingCount.promise, ariaSnapshot }), |
| 321 | + }); |
| 322 | + vi.useFakeTimers(); |
| 323 | + try { |
| 324 | + const promise = mod.snapshotRoleViaPlaywright({ |
| 325 | + cdpUrl: "http://127.0.0.1:9222", |
| 326 | + targetId: "tab-1", |
| 327 | + selector: "#present", |
| 328 | + timeoutMs: 750, |
| 329 | + }); |
| 330 | + await vi.advanceTimersByTimeAsync(500); |
| 331 | + pendingCount.resolve(1); |
| 332 | + await promise; |
| 333 | + expect(ariaSnapshot).toHaveBeenCalledWith({ timeout: 250 }); |
| 334 | + } finally { |
| 335 | + vi.useRealTimers(); |
| 336 | + } |
| 337 | + }); |
| 338 | + |
221 | 339 | it("stores frame-scoped refs with the exact captured frame", async () => { |
222 | 340 | const ariaSnapshot = vi.fn(async () => '- button "Save"'); |
223 | 341 | const frame = { id: "frame-1", locator: vi.fn(() => ({ ariaSnapshot })) }; |
|
0 commit comments