import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, expect, test, vi } from "vitest";

import Page from "@/app/page";

afterEach(() => {
  cleanup();
  vi.unstubAllGlobals();
});

test("展示来自 Host 投影的创作目标", async () => {
  vi.stubGlobal(
    "fetch",
    vi.fn(async (input: RequestInfo | URL) => {
      const url = String(input);
      const payload = url.endsWith("/api/runs")
        ? [
            {
              script_build_id: 42,
              status: "running",
              summary: "正在生成脚本",
              started_at: null,
              completed_at: null,
            },
          ]
        : {
            schema_version: "script-build-journey/v1",
            generated_at: "2026-07-21T00:00:00Z",
            script_build_id: 42,
            root_trace_id: "root-42",
            status: "running",
            objective: "为选题生成一份可发布的脚本",
            input_summary: {
              topic: "一次反转如何改变信念",
              account_name: "每天心理学",
              persona_point_count: 3,
              strategy_names: ["先冲突后解法"],
              snapshot_id: "11",
              digest: "sha256:input",
            },
            steps: [],
            edges: [],
            warnings: [],
          };
      return { ok: true, json: async () => payload } as Response;
    }),
  );

  render(<Page />);

  expect(
    await screen.findByRole("heading", { name: "为选题生成一份可发布的脚本" }),
  ).toBeTruthy();
});
