|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { filterImageDataFromResult } from "../../src/tools/browser-eval.js" |
| 3 | + |
| 4 | +describe("browser-eval screenshot filter", () => { |
| 5 | + it("should filter out base64 image data from screenshot result", () => { |
| 6 | + const mockResponse = { |
| 7 | + content: [ |
| 8 | + { |
| 9 | + type: "text", |
| 10 | + text: "### Result\nTook the viewport screenshot and saved it as /tmp/screenshot.png", |
| 11 | + }, |
| 12 | + { |
| 13 | + type: "image", |
| 14 | + data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", |
| 15 | + }, |
| 16 | + ], |
| 17 | + } |
| 18 | + |
| 19 | + const formattedResult = filterImageDataFromResult(mockResponse) as { |
| 20 | + content: { type: string; text?: string }[] |
| 21 | + } |
| 22 | + |
| 23 | + // Should remove the image block |
| 24 | + expect(formattedResult.content.length).toBe(2) // Original text + new guidance text |
| 25 | + expect(formattedResult.content.every((block) => block.type !== "image")).toBe(true) |
| 26 | + |
| 27 | + // Should contain guidance about reading the file |
| 28 | + const guidanceBlock = formattedResult.content.find((block) => |
| 29 | + block.text?.includes('To view this screenshot, use the "read" tool with the file path') |
| 30 | + ) |
| 31 | + expect(guidanceBlock).toBeDefined() |
| 32 | + expect(guidanceBlock?.text).toContain("/tmp/screenshot.png") |
| 33 | + }) |
| 34 | + |
| 35 | + it("should extract file path from various text formats", () => { |
| 36 | + const formats = [ |
| 37 | + "saved it as /path/to/screenshot.png", |
| 38 | + "saved as /path/to/screenshot.png", |
| 39 | + "Took the viewport screenshot and saved it as /var/folders/temp/screenshot.png", |
| 40 | + ] |
| 41 | + |
| 42 | + formats.forEach((text) => { |
| 43 | + const mockResult = { content: [{ type: "text", text }] } |
| 44 | + |
| 45 | + const formattedResult = filterImageDataFromResult(mockResult) as { |
| 46 | + content: { type: string; text?: string }[] |
| 47 | + } |
| 48 | + |
| 49 | + const guidanceBlock = formattedResult.content.find((block) => |
| 50 | + block.text?.includes("To view this screenshot") |
| 51 | + ) |
| 52 | + expect(guidanceBlock).toBeDefined() |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it("should handle results without image data", () => { |
| 57 | + const mockResult = { content: [{ type: "text", text: "Some other result" }] } |
| 58 | + |
| 59 | + const formattedResult = filterImageDataFromResult(mockResult) as { |
| 60 | + content: { type: string; text?: string }[] |
| 61 | + } |
| 62 | + |
| 63 | + // Should not add guidance if no screenshot path found |
| 64 | + expect(formattedResult.content.length).toBe(1) |
| 65 | + }) |
| 66 | + |
| 67 | + it("should handle non-object results", () => { |
| 68 | + expect(filterImageDataFromResult(null)).toBe(null) |
| 69 | + expect(filterImageDataFromResult("string")).toBe("string") |
| 70 | + expect(filterImageDataFromResult(123)).toBe(123) |
| 71 | + }) |
| 72 | + |
| 73 | + it("should handle responses without content array", () => { |
| 74 | + const mockResult = { other: "data" } |
| 75 | + expect(filterImageDataFromResult(mockResult)).toEqual(mockResult) |
| 76 | + }) |
| 77 | +}) |
0 commit comments