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

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

async function main() {
  const suffix = Date.now().toString(36);
  const title = `TEST-HERMES-AI-baseline-${suffix}`;
  const browser = await chromium.launch({ headless: true });
  const context = await browser.newContext({ viewport: { width: 1440, height: 960 } });
  const page = await context.newPage();
  const captured = [];
  const createdIds = [];

  page.on("request", (request) => {
    const url = request.url();
    if (url.includes("/api/ai-agent/run") || url.includes("/api/hermes/client/")) {
      captured.push({
        phase: "request",
        method: request.method(),
        url,
        postData: request.postData() || "",
      });
    }
  });
  page.on("response", async (response) => {
    const url = response.url();
    if (!url.includes("/api/ai-agent/run") && !url.includes("/api/hermes/client/")) {
      return;
    }
    let body = "";
    try {
      body = (await response.text()).slice(0, 1600);
    } catch {
      body = "<unreadable>";
    }
    captured.push({
      phase: "response",
      status: response.status(),
      url,
      body,
      headers: response.headers(),
    });
  });

  try {
    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("概括当前页面标题和第一段", { timeout: UI_TIMEOUT_MS });
    await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
    await page.waitForFunction(
      () => document.querySelectorAll('[data-testid="wolai-page-ai-drawer"] .wolai-page-ai-message--assistant').length > 0,
      null,
      { timeout: UI_TIMEOUT_MS },
    );

    const aiRequests = captured.filter((entry) => entry.phase === "request");
    assert(aiRequests.length > 0, "未捕获页面 AI 主请求");
    const firstRequest = aiRequests[0];
    const legacyRunHit = aiRequests.some((entry) => entry.url.includes("/api/ai-agent/run"));
    const hermesClientHit = aiRequests.some((entry) => entry.url.includes("/api/hermes/client/"));
    const providerHermes502 = captured.some(
      (entry) =>
        entry.phase === "response" &&
        entry.status === 502 &&
        /provider|ai_provider_bridge_unavailable|hermes/i.test(entry.body || ""),
    );

    console.log(
      JSON.stringify(
        {
          ok: true,
          baseUrl: BASE_URL,
          title,
          documentId: target.documentId,
          workspaceId: target.workspaceId,
          firstRequestUrl: firstRequest.url,
          legacyRunHit,
          hermesClientHit,
          providerHermes502,
          captured,
        },
        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);
});
