import { test, expect, request as playwrightRequest } from "@playwright/test";
import { getAuthToken } from "@open-mercato/core/modules/core/__integration__/helpers/api";
test.describe("TC-AI-002: AI agent dispatcher policy gate", () => {
  const chatPath = "/api/ai_assistant/ai/chat";
  test("unknown agent returns 404 agent_unknown", async ({ request }) => {
    const token = await getAuthToken(request, "superadmin");
    const response = await request.fetch(`${chatPath}?agent=does.not_exist`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        messages: [{ role: "user", content: "hello" }]
      })
    });
    expect(response.status()).toBe(404);
    const body = await response.json();
    expect(body.code).toBe("agent_unknown");
  });
  test("malformed agent query param returns 400 validation_error", async ({ request }) => {
    const token = await getAuthToken(request, "superadmin");
    const response = await request.fetch(`${chatPath}?agent=BadAgent`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        messages: [{ role: "user", content: "hello" }]
      })
    });
    expect(response.status()).toBe(400);
    const body = await response.json();
    expect(body.code).toBe("validation_error");
  });
  test("missing agent query param returns 400 validation_error", async ({ request }) => {
    const token = await getAuthToken(request, "superadmin");
    const response = await request.fetch(chatPath, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": "application/json"
      },
      data: JSON.stringify({
        messages: [{ role: "user", content: "hello" }]
      })
    });
    expect(response.status()).toBe(400);
    const body = await response.json();
    expect(body.code).toBe("validation_error");
  });
  test("unauthenticated caller returns 401", async ({ baseURL }) => {
    const context = await playwrightRequest.newContext({ baseURL });
    try {
      const response = await context.fetch(`${chatPath}?agent=does.not_exist`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        data: JSON.stringify({
          messages: [{ role: "user", content: "hello" }]
        })
      });
      expect(response.status()).toBe(401);
      const body = await response.json();
      expect(body.code === "unauthenticated" || body.error === "Unauthorized").toBe(true);
    } finally {
      await context.dispose();
    }
  });
});
//# sourceMappingURL=TC-AI-002-agent-policy.spec.js.map
