webElementContext.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildPromptWithWebElementContexts,
  4. parsePromptWebElementContexts,
  5. type WebElementContextComposerAttachment,
  6. } from "@/lib/webElementContext.js";
  7. describe("webElementContext", () => {
  8. const baseContext: WebElementContextComposerAttachment = {
  9. id: "web-element-1",
  10. workspacePath: "/workspace",
  11. pageUrl: "https://example.com/checkout",
  12. pageTitle: "Checkout",
  13. tagName: "button",
  14. role: "button",
  15. accessibleName: "Submit order",
  16. selector: 'main form button[type="submit"]',
  17. xpath: "/html/body/main/form/button[1]",
  18. text: "Submit order",
  19. nearbyText: "Checkout Total: $42.00 Submit order",
  20. htmlExcerpt: '<button type="submit">Submit order</button>',
  21. attributes: {
  22. type: "submit",
  23. "aria-label": "Submit order",
  24. },
  25. rect: {
  26. x: 10,
  27. y: 20,
  28. width: 120,
  29. height: 32,
  30. },
  31. style: {
  32. backgroundColor: "#FFFFFF",
  33. color: "#18202F",
  34. display: "inline-flex",
  35. fontFamily: "Inter, ui-sans-serif",
  36. fontSize: "16px",
  37. fontWeight: "600",
  38. },
  39. capturedAt: 1_716_000_000_000,
  40. };
  41. it("appends selected web element context as markdown", () => {
  42. const prompt = buildPromptWithWebElementContexts("What is this button?", [baseContext]);
  43. expect(prompt).toContain("What is this button?");
  44. expect(prompt).toContain("# Web page elements:");
  45. expect(prompt).toContain("URL: https://example.com/checkout");
  46. expect(prompt).toContain("Accessible name: Submit order");
  47. expect(prompt).toContain("Color: #18202F");
  48. expect(prompt).toContain("Font: 16px Inter, ui-sans-serif");
  49. expect(prompt).toContain("HTML excerpt:");
  50. expect(prompt).toContain("<button");
  51. });
  52. it("parses web element blocks back into visible content and chips", () => {
  53. const prompt = buildPromptWithWebElementContexts("Explain this", [
  54. baseContext,
  55. {
  56. ...baseContext,
  57. id: "web-element-2",
  58. tagName: "input",
  59. role: "textbox",
  60. accessibleName: "Email",
  61. text: "Email",
  62. },
  63. ]);
  64. const parsed = parsePromptWebElementContexts(prompt, {
  65. workspacePath: "/workspace",
  66. });
  67. expect(parsed.visibleContent).toBe("Explain this");
  68. expect(parsed.webElementContexts).toHaveLength(2);
  69. expect(parsed.webElementContexts[0]).toMatchObject({
  70. pageUrl: "https://example.com/checkout",
  71. pageTitle: "Checkout",
  72. tagName: "button",
  73. accessibleName: "Submit order",
  74. style: {
  75. backgroundColor: "#FFFFFF",
  76. color: "#18202F",
  77. display: "inline-flex",
  78. fontFamily: "Inter, ui-sans-serif",
  79. fontSize: "16px",
  80. fontWeight: "600",
  81. },
  82. text: "Submit order",
  83. });
  84. expect(parsed.webElementContexts[1]).toMatchObject({
  85. tagName: "input",
  86. role: "textbox",
  87. accessibleName: "Email",
  88. });
  89. });
  90. });