#!/usr/bin/env node
"use strict";

const assert = require("node:assert");
const fs = require("node:fs");
const { chromium } = require("playwright");
const {
  BASE_URL,
  UI_TIMEOUT_MS,
  cleanupDocuments,
  createTempDocument,
  ensureAuthenticated,
  renameDocument,
} = require("./tree-shell-smoke-helpers");

function resolveChromiumExecutablePath() {
  const explicit = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH || "";
  if (explicit && fs.existsSync(explicit)) return explicit;
  return [
    "/usr/bin/google-chrome-stable",
    "/usr/bin/google-chrome",
    "/snap/bin/chromium",
    "/usr/bin/chromium",
    "/usr/bin/chromium-browser",
  ].find((candidate) => fs.existsSync(candidate)) || "";
}

async function main() {
  const suffix = Date.now().toString(36);
  const title = `TEST-HERMES-AI-smoke-${suffix}`;
  const sessionId = `mnote_smoke_${suffix}`;
  const runId = `run_smoke_${suffix}`;
  let runRequestCount = 0;
  let sessionDetailHits = 0;
    const captured = [];
  const createdIds = [];
  const executablePath = resolveChromiumExecutablePath();
  const browser = await chromium.launch({
    headless: true,
    ...(executablePath ? { executablePath } : {}),
  });
  const context = await browser.newContext({ viewport: { width: 1440, height: 960 } });
  const page = await context.newPage();

  try {
    await page.route("**/api/ai-agent/run", async (route) => {
      throw new Error(`页面 AI 不应请求旧 /api/ai-agent/run: ${route.request().url()}`);
    });
    await page.route("**/api/hermes/client/gateway/health**", async (route) => {
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          ok: true,
          gateway: { ok: true, status: "mocked", upstream: "http://127.0.0.1:8644" },
          profile: { name: "default", modelConfigured: true, apiKeyConfigured: true },
          suggestions: [],
        }),
      });
    });
    await page.route("**/api/hermes/client/tools**", async (route) => {
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ ok: true, tools: [] }),
      });
    });
    await page.route("**/api/hermes/client/profiles", async (route) => {
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          ok: true,
          active: "default",
          profiles: [{ name: "default", label: "Default", modelConfigured: true, apiKeyConfigured: true }],
        }),
      });
    });
    await page.route("**/api/hermes/client/sessions", async (route) => {
      captured.push({ kind: "session", method: route.request().method(), body: route.request().postData() || "" });
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          ok: true,
          sessionId,
          title: "当前页问答",
          traceId: "trace_smoke",
          persistence: "hermes_on_first_run",
        }),
      });
    });
    await page.route(`**/api/hermes/client/sessions/${sessionId}/resume`, async (route) => {
      sessionDetailHits += 1;
      captured.push({ kind: "session-detail", method: route.request().method(), body: "" });
      assert.equal(route.request().method(), "POST", "刷新恢复必须调用 runtime resume 接口");
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          ok: true,
          sessionId,
          traceId: "trace_restore",
          session: {
            sessionId,
            messages: [],
          },
          runtime: {
            sessionId,
            runId,
            status: "completed",
            profile: "default",
            documentId: "doc_smoke",
            traceId: "trace_restore",
            lastToolName: "mnote.page.get",
          },
        }),
      });
    });
    await page.route("**/api/hermes/client/runs", async (route) => {
      runRequestCount += 1;
      const currentRunId = `${runId}_${runRequestCount}`;
      captured.push({ kind: "run", method: route.request().method(), body: route.request().postData() || "" });
      await route.fulfill({
        status: 200,
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          ok: true,
          sessionId,
          runId: currentRunId,
          events: [],
          traceId: "trace_smoke",
        }),
      });
    });
    await page.route("**/api/hermes/client/events/**", async (route) => {
      const currentRunId = route.request().url().split("/").pop() || runId;
      captured.push({ kind: "events", method: route.request().method(), body: "" });
      const evidenceEvents = Array.from({ length: 24 }, (_, index) => {
        const callId = `call_smoke_knowledge_rag_${index}`;
        return (
          `data: ${JSON.stringify({ event: "tool.started", run_id: currentRunId, session_id: sessionId, toolCallId: callId, name: "mnote.knowledge_rag.query", args: { query: `evidence ${index}` } })}\n\n` +
          `data: ${JSON.stringify({ event: "tool.completed", run_id: currentRunId, session_id: sessionId, toolCallId: callId, name: "mnote.knowledge_rag.query", summary: `资料库 ${index}`, auditId: `audit_smoke_knowledge_rag_${index}` })}\n\n`
        );
      }).join("");
      if (runRequestCount > 1) {
        await route.fulfill({
          status: 200,
          headers: { "content-type": "text/event-stream; charset=utf-8" },
          body:
            `data: ${JSON.stringify({ event: "message.delta", run_id: currentRunId, session_id: sessionId, delta: "Second " })}\n\n` +
            `data: ${JSON.stringify({ event: "message.delta", run_id: currentRunId, session_id: sessionId, delta: "response" })}\n\n` +
            `data: ${JSON.stringify({ event: "run.completed", run_id: currentRunId, session_id: sessionId, output: "Second response" })}\n\n`,
        });
        return;
      }
      await route.fulfill({
        status: 200,
        headers: { "content-type": "text/event-stream; charset=utf-8" },
        body:
          evidenceEvents +
          `data: ${JSON.stringify({ event: "tool.started", run_id: currentRunId, session_id: sessionId, toolCallId: "call_smoke_page_get", name: "mnote.page.get", args: { includeBody: false } })}\n\n` +
          `data: ${JSON.stringify({ event: "tool.completed", run_id: currentRunId, session_id: sessionId, toolCallId: "call_smoke_page_get", name: "mnote.page.get", summary: "读取当前页面", auditId: "audit_smoke_page_get" })}\n\n` +
          `data: ${JSON.stringify({ event: "tool.started", run_id: currentRunId, session_id: sessionId, toolCallId: "call_smoke_page_save", name: "mnote.page.save", args: { dryRun: true } })}\n\n` +
          `data: ${JSON.stringify({ event: "tool.failed", run_id: currentRunId, session_id: sessionId, toolCallId: "call_smoke_page_save", name: "mnote.page.save", code: "permission_denied", error: "写入被拒绝", auditId: "audit_smoke_page_save" })}\n\n` +
          `data: ${JSON.stringify({ event: "message.delta", run_id: currentRunId, session_id: sessionId, delta: "Smoke " })}\n\n` +
          `data: ${JSON.stringify({ event: "message.delta", run_id: currentRunId, session_id: sessionId, delta: "response" })}\n\n` +
          `data: ${JSON.stringify({
            event: "run.completed",
            run_id: currentRunId,
            session_id: sessionId,
            output: "Smoke response",
            agentAudit: {
              eventId: "audit_smoke_changed_files",
              rootUri: "file:///tmp/mnote-smoke",
              diffSummary: "1 changed file(s)",
              changedFiles: [
                {
                  path: "README.md",
                  changeType: "modified",
                  summary: "修改文件 size:10→20 lines:1→2",
                },
              ],
            },
          })}\n\n`,
      });
    });

    await ensureAuthenticated(page, context.request);
    const target = await createTempDocument(context.request);
    createdIds.push(target.documentId);
    await renameDocument(context.request, target.workspaceId, target.documentId, title);
    await page.goto(
      `${BASE_URL}/documents/${encodeURIComponent(target.documentId)}?workspaceId=${encodeURIComponent(target.workspaceId)}`,
      { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS },
    );

    await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
    await page.locator("[data-page-ai-input]").fill(`请总结 ${title}`, { timeout: UI_TIMEOUT_MS });
    await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
    await page.waitForFunction(
      () => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("Smoke response"),
      null,
      { timeout: UI_TIMEOUT_MS },
    );
    await page.waitForFunction(
      () => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("mnote.page.get"),
      null,
      { timeout: UI_TIMEOUT_MS },
    );
    await page.waitForFunction(
      () => (document.querySelector("[data-page-ai-last-tool]")?.textContent || "").includes("mnote.page.save"),
      null,
      { timeout: UI_TIMEOUT_MS },
    );
    const toolCards = await page.$$eval("[data-page-ai-tool-card]", (cards) =>
      cards.map((card) => ({
        id: card.getAttribute("data-page-ai-tool-call-id"),
        group: card.getAttribute("data-page-ai-tool-group"),
        status: card.getAttribute("data-page-ai-tool-status"),
        text: card.textContent || "",
      })),
    );
    assert.equal(toolCards.length, 1, `批量工具调用应折叠成 1 张工具组卡：${JSON.stringify(toolCards)}`);
    assert(toolCards[0].text.includes("调用工具") && toolCards[0].text.includes("27 个"), `工具组摘要应显示调用数量：${JSON.stringify(toolCards)}`);
    const toolItems = await page.$$eval("[data-page-ai-tool-item]", (items) =>
      items.map((item) => ({
        id: item.getAttribute("data-page-ai-tool-call-id"),
        status: item.getAttribute("data-page-ai-tool-status"),
        text: item.textContent || "",
      })),
    );
    assert(toolItems.length >= 27, `工具组展开内容应包含批量工具调用：${JSON.stringify(toolItems)}`);
    assert(
      toolItems.some((item) => item.status === "completed" && item.text.includes("call_smoke_page_get")),
      `缺少 completed 工具项：${JSON.stringify(toolItems)}`,
    );
    assert(
      toolItems.some((item) => item.status === "failed" && item.text.includes("permission_denied")),
      `缺少 failed 工具项：${JSON.stringify(toolItems)}`,
    );
    assert(
      toolItems.some((item) => item.status === "completed" && item.text.includes("agent.changed_files") && item.text.includes("README.md")),
      `缺少 changed files 工具项：${JSON.stringify(toolItems)}`,
    );
    const toolDetailsOpenByDefault = await page.$$eval("[data-page-ai-tool-card] > .wolai-page-ai-message-text > details", (details) => details.map((node) => node.open));
    assert.equal(toolDetailsOpenByDefault.length, 1, "工具调用应共用一个 details 折叠容器");
    assert(toolDetailsOpenByDefault.every((open) => open === false), "工具调用组默认应折叠");
    const orderState = await page.locator('[data-page-ai-panel="chat"] [data-page-ai-conversation]').evaluate((node) => {
      const toolGroup = node.querySelector("[data-page-ai-tool-card]");
      const assistant = Array.from(node.querySelectorAll(".wolai-page-ai-message--assistant")).find((item) =>
        (item.textContent || "").includes("Smoke response")
      );
      return {
        toolBeforeAssistant: Boolean(toolGroup && assistant && (toolGroup.compareDocumentPosition(assistant) & Node.DOCUMENT_POSITION_FOLLOWING)),
      };
    });
    assert(orderState.toolBeforeAssistant, "工具调用组应显示在 AI 输出结果之前");
    await page.locator("[data-page-ai-tool-card] summary").first().click({ timeout: UI_TIMEOUT_MS });
    assert(
      await page.locator("[data-page-ai-tool-card] > .wolai-page-ai-message-text > details").first().evaluate((node) => node.open),
      "点击工具组 summary 后应展开详情",
    );
    const scrollStateBeforeFollowup = await page.locator('[data-page-ai-panel="chat"] [data-page-ai-conversation]').evaluate((node) => {
      node.scrollTop = 0;
      return {
        scrollTop: node.scrollTop,
        scrollHeight: node.scrollHeight,
        clientHeight: node.clientHeight,
      };
    });
    assert(scrollStateBeforeFollowup.scrollHeight > scrollStateBeforeFollowup.clientHeight, `批量工具调用应撑出滚动区：${JSON.stringify(scrollStateBeforeFollowup)}`);
    await page.locator("[data-page-ai-input]").fill("继续补充一句", { timeout: UI_TIMEOUT_MS });
    await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
    await page.waitForFunction(
      () => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("Second response"),
      null,
      { timeout: UI_TIMEOUT_MS },
    );
    const scrollStateAfterFollowup = await page.locator('[data-page-ai-panel="chat"] [data-page-ai-conversation]').evaluate((node) => ({
      scrollTop: node.scrollTop,
      scrollHeight: node.scrollHeight,
      clientHeight: node.clientHeight,
      firstToolOpen: Boolean(node.querySelector("[data-page-ai-tool-card] > .wolai-page-ai-message-text > details")?.open),
    }));
    assert(scrollStateAfterFollowup.scrollTop <= 4, `用户已上滚时新输出不应强制贴底：${JSON.stringify(scrollStateAfterFollowup)}`);
    assert(scrollStateAfterFollowup.firstToolOpen, "重渲染后应保留用户展开的工具调用详情");

    const sessionRequest = captured.find((entry) => entry.kind === "session");
    const runRequest = captured.find((entry) => entry.kind === "run");
    const eventRequest = captured.find((entry) => entry.kind === "events");
    assert(sessionRequest, "未捕获 /api/hermes/client/sessions 请求");
    assert(runRequest, "未捕获 /api/hermes/client/runs 请求");
    assert(eventRequest, "未捕获 /api/hermes/client/events 请求");
    const runBody = JSON.parse(runRequest.body);
    assert.equal(runBody.sessionId, sessionId, "run 请求必须携带 Hermes sessionId");
    assert.equal(runBody.documentId, target.documentId, "run 请求必须携带 documentId");
    assert(runBody.pageContext, "run 请求必须携带最小 pageContext");
    assert.equal(runBody.pageContext.documentBlocks, null, "run 请求不应直接携带页面正文 blocks");
    assert.equal(runBody.pageContext.subtree, null, "run 请求不应直接携带页面 subtree");
    assert.equal(runBody.pageContext.outline, null, "run 请求不应直接携带页面 outline");
    assert(
      runBody.pageContext.contentAccess,
      `run 请求必须声明正文访问方式：${JSON.stringify(runBody.pageContext)}`,
    );

    console.log(
      JSON.stringify(
        {
          ok: true,
          baseUrl: BASE_URL,
          documentId: target.documentId,
          workspaceId: target.workspaceId,
          sessionId,
          runId,
          capturedKinds: captured.map((entry) => entry.kind),
          sessionDetailHits,
        },
        null,
        2,
      ),
    );
  } finally {
    await cleanupDocuments(context.request, createdIds).catch(() => undefined);
    await context.close().catch(() => undefined);
    await browser.close().catch(() => undefined);
  }
}

main().catch((error) => {
  console.error(error instanceof Error ? error.stack || error.message : String(error));
  process.exit(1);
});
