import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";

test("exposes authenticated KOC task, recovery, collection, and resource MCP tools", async () => {
  const [route, tools, operations, tokenService, migration, taskService, actionRoute, readme, envExample, packageJson] =
    await Promise.all([
      readFile(new URL("../app/api/mcp/route.ts", import.meta.url), "utf8"),
      readFile(new URL("../lib/mcp-tools.ts", import.meta.url), "utf8"),
      readFile(new URL("../lib/mcp-operations.ts", import.meta.url), "utf8"),
      readFile(new URL("../lib/mcp-export-token.ts", import.meta.url), "utf8"),
      readFile(new URL("../mysql/0001_init.sql", import.meta.url), "utf8"),
      readFile(new URL("../lib/task-service.ts", import.meta.url), "utf8"),
      readFile(new URL("../app/api/action/route.ts", import.meta.url), "utf8"),
      readFile(new URL("../README.md", import.meta.url), "utf8"),
      readFile(new URL("../.env.self-hosted.example", import.meta.url), "utf8"),
      readFile(new URL("../package.json", import.meta.url), "utf8"),
    ]);

  assert.match(route, /createMcpHandler/);
  assert.match(route, /create_distribution_task/);
  for (const name of [
    "task_list",
    "task_get",
    "recovery_list",
    "recovery_export",
    "collection_plan_set",
    "collection_run_due",
    "collection_collect_now",
    "collection_retry_failed",
    "resource_search",
    "resource_get",
    "resource_backfill_profile",
    "resource_export",
  ]) {
    assert.match(tools, new RegExp(`"${name}"`));
  }
  assert.match(route, /KOC_MCP_API_KEY/);
  assert.match(route, /Authorization/);
  assert.match(route, /Bearer/);
  assert.match(route, /idempotentHint: true/);
  assert.match(route, /brand_project\?\.trim\(\) \|\| "未设置项目"/);
  assert.match(route, /buildClaimUrl/);
  assert.match(route, /structuredContent: output/);
  assert.match(route, /legacy: "stateless"/);
  assert.match(route, /responseMode: "json"/);
  assert.doesNotMatch(route, /ADMIN_INTERNAL_TOKEN/);
  assert.match(route, /registerMcpOperationTools/);
  assert.match(operations, /runDueScheduledCollections/);
  assert.match(operations, /retryFailedCollections/);
  assert.match(operations, /enrichDistributionAccount/);
  assert.match(operations, /issueMcpExportToken/);
  assert.match(tokenService, /SHA-256/);
  assert.match(tokenService, /expires_at > CURRENT_TIMESTAMP/);
  assert.doesNotMatch(tokenService, /KOC_MCP_API_KEY/);
  assert.match(migration, /CREATE TABLE IF NOT EXISTS mcp_export_tokens/);
  assert.match(migration, /mcp_export_tokens_expires_at_idx/);

  assert.match(taskService, /readFeishuSource/);
  assert.match(taskService, /options\.deduplicate/);
  assert.ok(
    taskService.indexOf("findExistingTask(input.feishuUrl, input)") <
      taskService.indexOf("readFeishuSource(input.feishuUrl, bindings)"),
  );
  assert.match(taskService, /searchParams\.delete\("from"\)/);
  assert.match(taskService, /status IN \('active', 'importing'\)/);
  assert.match(taskService, /shareToken/);
  assert.match(actionRoute, /createDistributionTask/);
  assert.doesNotMatch(actionRoute, /function createTaskFromSource/);

  assert.match(readme, /create_distribution_task/);
  assert.match(readme, /collection_plan_set/);
  assert.match(readme, /resource_backfill_profile/);
  assert.match(readme, /\/api\/mcp/);
  assert.match(envExample, /KOC_MCP_API_KEY/);
  assert.match(packageJson, /@modelcontextprotocol\/server/);
  assert.match(packageJson, /"zod"/);
});
