import { readFileSync } from "node:fs";

import { describe, expect, test } from "vitest";

const source = (relativePath: string) =>
  readFileSync(new URL(relativePath, import.meta.url), "utf-8");

describe("Workspace frontend regression contracts", () => {
  test("keeps keyboard resizing on the visual viewport instead of page scroll", () => {
    const page = source(
      "../../components/workspace/project-workspace-page.tsx"
    );
    const viewport = source("../../hooks/workspace/use-visual-viewport.ts");
    const root = source("../../root.tsx");
    const styles = source("../../index.css");

    expect(root).toContain("interactive-widget=resizes-content");
    expect(page).toContain("style={viewportStyle}");
    expect(page).toContain("fixed inset-x-0 top-0");
    expect(viewport).toContain("workspace-viewport-lock");
    expect(styles).toContain("html.workspace-viewport-lock body");
    expect(styles).toContain("overflow: hidden");
  });

  test("keeps the responsive shell shrinkable with a pinned composer", () => {
    const page = source(
      "../../components/workspace/project-workspace-page.tsx"
    );
    const feed = source("../../components/workspace/conversation-feed.tsx");
    const composer = source(
      "../../components/workspace/conversation-composer.tsx"
    );

    expect(page).toContain('className="flex min-w-0 flex-1 flex-col"');
    expect(feed).toContain('<Conversation className="min-h-0 flex-1">');
    expect(composer).toContain('className="shrink-0 border-t');
    expect(composer).toContain(
      'className="mx-auto flex max-w-2xl items-end gap-2"'
    );
  });

  test("keeps attachment strips contained and markdown capabilities enabled", () => {
    const attachment = source("../../components/chat/chat-attachments.tsx");
    const attachmentPrimitive = source(
      "../../../../../packages/ui/src/components/attachment.tsx"
    );
    const chatMessage = source("../../components/chat/chat-message.tsx");
    const messageRenderer = source(
      "../../../../../packages/ui/src/components/ai-elements/message.tsx"
    );

    expect(attachment).toContain('className="max-w-full');
    expect(attachment).toContain("useAuthenticatedImageSource");
    expect(attachment).toContain(`authorization: \`Bearer \${accessToken}\``);
    expect(attachmentPrimitive).toContain("overflow-x-auto");
    expect(chatMessage).toContain("<MessageAttachments parts={fileParts} />");
    expect(chatMessage).toContain("<MessageResponse");
    expect(messageRenderer).toContain("const streamdownPlugins = {");
    expect(messageRenderer).toContain("mermaid");
    expect(messageRenderer).toContain("plugins={streamdownPlugins}");
  });
});
