Skip to content

Your lightweight open-source personal agent.

5.4 KB
Raw Blame
import { fireEvent, render, screen, within } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";

import { AttachmentTile } from "@/components/AttachmentTile";
import { FileReferenceChip, FileReferenceIcon, fileKindForPath } from "@/components/FileReferenceChip";
import { PreviewPane } from "@/components/PreviewPane";

describe("shared file icons", () => {
  it.each([
    ["chart.png", "file-image"],
    ["chart.svg", "file-image"],
    ["chart.avif", "file-image"],
    ["index.html", "file-code2"],
    ["styles.css", "file-code2"],
    ["app.js", "file-code2"],
    ["app.ts", "file-code2"],
    ["app#draft.ts", "file-code2"],
    ["app.tsx", "file-code2"],
    ["script.py", "file-code2"],
    ["analysis.ipynb", "file-code2"],
    ["DATA.JSON", "file-json2"],
    ["README.md", "file-text"],
    ["notes.txt", "file-text"],
    ["build.log", "file-text"],
    ["demo.mp4", "file-video2"],
    ["https://files.example.com/demo.mp4?signature=abc#download.zip", "file-video2"],
    ["archive.zip", "file-archive"],
    ["backup.tar.gz", "file-archive"],
    ["voice.mp3", "file-audio"],
    ["recording.flac", "file-audio"],
    ["REPORT#2.PDF", "file-text"],
    ["proposal.docx", "file-text"],
    ["budget.xlsx", "file-spreadsheet"],
    ["results.csv", "file-spreadsheet"],
    ["slides.pptx", "file-chart-column"],
    ["slides.odp", "file-chart-column"],
    ["payload.bin", "file"],
  ])("uses a consistent monochrome file-family icon for %s", (path, icon) => {
    const { container } = render(<FileReferenceIcon kind={fileKindForPath(path)} />);
    const svg = container.querySelector("svg")!;
    expect(svg).toHaveClass(`lucide-${icon}`);
    expect(svg).toHaveAttribute("viewBox", "0 0 24 24");
    expect(svg).toHaveAttribute("stroke", "currentColor");
    expect(svg).toHaveAttribute("stroke-width", "1.75");
    expect(svg).toHaveAttribute("fill", "none");
    expect(svg).toHaveAttribute("aria-hidden", "true");
    expect(svg).toHaveClass("h-[1em]", "w-[1em]", "shrink-0");
    expect(svg.querySelector("text, [fill], [stroke]")).toBeNull();
  });

  it.each([
    ["chart.png", "PNG"],
    ["index.html", "HTML"],
    ["data.json", "JSON"],
    ["script.py", "PY"],
    ["notes.md", "MD"],
    ["archive.zip", "ZIP"],
    ["report.pdf", "PDF"],
    ["budget.xlsx", "XLSX"],
    ["slides.pptx", "PPTX"],
    ["song.mp3", "MP3"],
  ])(
    "shares reply and tab glyphs for %s while keeping attachment type cards and actions distinct", (path, type) => {
      const open = vi.fn();
      const references = render(<>
        <FileReferenceChip path={path} onOpen={open} />
        <PreviewPane tabs={[{ id: path, value: path }]} activeId={path}
          width={544} isClosing={false} onSelect={() => {}} onCloseTab={() => {}}
          onClose={() => {}} onResizeStart={() => {}}><p>Preview</p></PreviewPane>
      </>);
      const attachments = render(<AttachmentTile attachment={{ kind: "file", name: path, url: `/download/${path}` }} />);
      const reference = within(references.container).getByRole("button", { name: path });
      const tab = within(references.container).getByRole("tab", { name: path });
      const information = within(attachments.container).getByRole("button", { name: path });
      const download = within(attachments.container).getByRole("link", { name: `Download ${path}` });
      const glyph = reference.querySelector("svg")!.innerHTML;
      expect(tab.querySelector("svg")!.innerHTML).toBe(glyph);
      expect(tab.querySelector("svg")).toHaveClass("size-3.5");
      expect(tab.querySelector("svg")).not.toHaveClass("translate-y-[0.12em]");
      expect(within(information).getByText(type)).toBeVisible();
      expect(information.querySelector("svg")).toHaveClass("lucide-file-text");
      expect(download.querySelector("svg")).toHaveClass("lucide-download", "size-4");
      expect(download).toHaveAttribute("href", `/download/${path}`);
      expect(download).toHaveAttribute("download", path);
      fireEvent.click(reference);
      expect(open).toHaveBeenCalledWith(path);
      fireEvent.click(information);
      expect(screen.getByRole("dialog", { name: path })).toHaveTextContent(path);
      expect(open).toHaveBeenCalledTimes(1);
    },
  );

  it("keeps iconless activity references named and keyboard-operable", () => {
    const open = vi.fn();
    render(<FileReferenceChip path="src/app.tsx" previewPath="/repo/src/app.tsx"
      display="path" showIcon={false} onOpen={open} />);

    const reference = screen.getByRole("button", { name: "src/app.tsx" });
    expect(reference).toHaveClass("text-content-link", "hover:text-content-link-hover");
    expect(reference).toHaveTextContent("src/app.tsx");
    expect(reference.querySelector("svg")).toBeNull();
    expect(reference).toHaveAttribute("tabindex", "0");
    fireEvent.keyDown(reference, { key: "Enter" });
    expect(open).toHaveBeenCalledWith("/repo/src/app.tsx");
  });

  it.each([
    ["image", "Image attachment"],
    ["video", "Video attachment"],
  ] as const)("retains %s identity when its attachment is unavailable", (kind, label) => {
    render(<AttachmentTile attachment={{ kind, name: "Unavailable" }} />);
    const information = screen.getByRole("button", { name: "Unavailable" });
    expect(within(information).getByText(label)).toBeVisible();
    expect(information).toHaveAccessibleDescription("Attachment unavailable");
    expect(screen.queryByRole("link")).not.toBeInTheDocument();
    expect(screen.getByText("Attachment unavailable")).toBeVisible();
  });
});