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

const assert = require("node:assert");

const BASE_URL = (process.env.MNOTE_UI_BASE_URL || "http://127.0.0.1:3000").replace(/\/$/, "");

async function main() {
  const response = await fetch(`${BASE_URL}/api/hermes/bridge`, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({
      stream: true,
      scope: "document",
      messages: [{ role: "user", content: "生成摘要" }],
      context: { documentId: "doc_1", workspaceId: "ws_demo" },
    }),
  });
  const payload = await response.json();
  assert.equal(response.status, 200, `/api/hermes/bridge 请求失败: ${response.status}`);
  assert.equal(response.headers.get("x-mnote-ai-bridge-owner"), "rust-web-hermes");
  assert.equal(payload.canonicalRoute, "/api/hermes/bridge");
  assert.match(payload.eventStreamEndpoint || "", /\/api\/hermes\/events\//);
  assert.equal(payload.contract.structuredWriteOwner, "rust-web-hermes");
  assert.deepEqual(payload.structuredWrite.allowedCommands, [
    "page.body.save",
    "tree.node.create",
    "kernel.edge.attach",
  ]);

  console.log(JSON.stringify({ ok: true, bridgeOwner: "rust-web-hermes" }, null, 2));
}

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