import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { PromptDrawer } from "@/components/decision/PromptDrawer";

describe("PromptDrawer truth labels", () => {
  const prompt = {
    promptRef: "prompt:event:50",
    actor: { role: "implementer" as const, label: "实现 Agent" },
    exactTask: { content: "补齐烹饪步骤", accuracy: "run-exact" as const },
    systemPrompt: {
      content: "按当前实现 Agent 规则完成任务。",
      source: "current-db" as const,
      version: 8,
      accuracy: "current-not-run-snapshot" as const,
      truncated: false,
    },
    notices: [{
      code: "PROMPT_NOT_SNAPSHOTTED",
      message: "系统没有保存这次运行所用提示词的历史快照；下方展示当前提示词。",
    }],
  };

  it("separates the exact run task from the current rule version", () => {
    render(<PromptDrawer prompt={prompt} loading={false} onClose={vi.fn()} />);
    expect(screen.getByRole("heading", { name: "本次真实任务" })).toBeInTheDocument();
    expect(screen.getByText("补齐烹饪步骤")).toBeInTheDocument();
    expect(screen.getByRole("heading", { name: "当前提示词" })).toBeInTheDocument();
    expect(screen.getByText("当前数据库版本 · v8")).toBeInTheDocument();
    expect(screen.getByText(/没有保存这次运行所用提示词的历史快照/)).toBeInTheDocument();
    expect(screen.queryByText("当次运行完整 Prompt")).not.toBeInTheDocument();
  });

  it("closes with Escape and starts focus on the close button", () => {
    const onClose = vi.fn();
    render(<PromptDrawer prompt={prompt} loading={false} onClose={onClose} />);
    expect(screen.getByRole("button", { name: "关闭提示词" })).toHaveFocus();
    fireEvent.keyDown(window, { key: "Escape" });
    expect(onClose).toHaveBeenCalledTimes(1);
  });
});
