Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

4.0 KB
Raw Blame
import type { PreviewAnnotationPayload } from "@akeru/contracts";
import { describe, expect, it } from "vite-plus/test";

import {
  appendPreviewAnnotationPrompt,
  buildPreviewAnnotationPrompt,
  extractTrailingPreviewAnnotation,
  previewAnnotationScreenshotFile,
} from "./previewAnnotation";

const annotation: PreviewAnnotationPayload = {
  id: "annotation_1",
  pageUrl: "http://localhost:3000",
  pageTitle: "Example",
  comment: "Make these cards feel related.",
  elements: [],
  regions: [{ id: "region_1", rect: { x: 10, y: 20, width: 100, height: 80 } }],
  strokes: [
    {
      id: "stroke_1",
      color: "#7c3aed",
      width: 4,
      points: [
        { x: 10, y: 10 },
        { x: 20, y: 20 },
      ],
      bounds: { x: 6, y: 6, width: 18, height: 18 },
    },
  ],
  styleChanges: [
    {
      targetId: "element_1",
      selector: ".card",
      property: "border-radius",
      previousValue: "4px",
      value: "16px",
    },
  ],
  screenshot: {
    dataUrl: "data:image/png;base64,AA==",
    width: 100,
    height: 80,
    cropRect: { x: 10, y: 20, width: 100, height: 80 },
  },
  createdAt: "2026-06-11T00:00:00.000Z",
};

describe("preview annotations", () => {
  it("describes regions, drawings, styles, and an omitted screenshot", () => {
    const result = buildPreviewAnnotationPrompt(annotation);
    expect(result).toContain("Make these cards feel related.");
    expect(result).toContain("1 marked region");
    expect(result).toContain("1 drawing");
    expect(result).toContain("border-radius: 4px → 16px");
    expect(result).toContain("Screenshot omitted because local pixel redaction is unavailable.");
  });

  it("appends to an existing composer prompt", () => {
    expect(
      appendPreviewAnnotationPrompt("Fix this", annotation).startsWith(
        "Fix this\n\n<preview_annotation>",
      ),
    ).toBe(true);
  });

  it("extracts annotation presentation from a sent prompt", () => {
    const result = extractTrailingPreviewAnnotation(
      appendPreviewAnnotationPrompt("Fix this", annotation),
    );

    expect(result.promptText).toBe("Fix this");
    expect(result.annotation).toMatchObject({
      title: "Example",
      targetSummary: "1 marked region, 1 drawing.",
      hasScreenshot: false,
    });
  });

  it("does not turn raw preview pixels into a provider attachment", async () => {
    expect(await previewAnnotationScreenshotFile(annotation)).toBeNull();
  });

  it("redacts annotation text and element context before provider egress", () => {
    const result = buildPreviewAnnotationPrompt({
      ...annotation,
      pageTitle: "[email protected]",
      comment: "token=annotation-secret",
      elements: [
        {
          id: "element_1",
          rect: { x: 0, y: 0, width: 10, height: 10 },
          element: {
            pageUrl: "file:///Users/leo/private-preview.html",
            pageTitle: "Private",
            tagName: "input",
            selector: "#secret",
            htmlPreview: '<input value="token=element-secret">',
            componentName: "SecretInput",
            source: null,
            stack: [],
            styles: "color: red",
            pickedAt: "2026-06-11T00:00:00.000Z",
          },
        },
      ],
    });

    expect(result).not.toContain("[email protected]");
    expect(result).not.toContain("annotation-secret");
    expect(result).not.toContain("Users/leo");
    expect(result).not.toContain("element-secret");
  });

  it("extracts multiple trailing annotations one at a time", () => {
    const first = appendPreviewAnnotationPrompt("Fix this", annotation);
    const secondAnnotation = { ...annotation, id: "annotation_2", pageTitle: "Details" };
    const second = appendPreviewAnnotationPrompt(first, secondAnnotation);
    const extractedSecond = extractTrailingPreviewAnnotation(second);
    const extractedFirst = extractTrailingPreviewAnnotation(extractedSecond.promptText);
    expect(extractedSecond.annotation?.id).toBe("annotation_2");
    expect(extractedFirst.annotation?.id).toBe("annotation_1");
    expect(extractedFirst.promptText).toBe("Fix this");
  });
});