import { describe, expect, it } from "vitest";
import { containsControlPayloadFinal } from "./control-payload-classifier.js";

describe("control payload classifier", () => {
  it("detects large multi-line runtime-control JSON with late tool fields", () => {
    const text = `Assistant output:\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "kind": "tool",\n  "toolName": "shell",\n  "input": {}\n}`;

    expect(containsControlPayloadFinal(text, ["shell"])).toBe(true);
  });

  it("detects large multi-line provider-native JSON with late tool_calls fields", () => {
    const text = `Assistant output:\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "tool_calls": [\n    {"name":"shell","arguments":{}}\n  ]\n}`;

    expect(containsControlPayloadFinal(text, ["shell"])).toBe(true);
  });

  it("detects large multi-line plan and phase result JSON with late fields", () => {
    const plan = `Assistant output:\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "kind": "plan_update",\n  "todos": []\n}`;
    const phase = `Assistant output:\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "kind": "phase_result",\n  "phaseName": "verify"\n}`;

    expect(containsControlPayloadFinal(plan)).toBe(true);
    expect(containsControlPayloadFinal(phase)).toBe(true);
  });

  it("allows large multi-line business JSON without control evidence", () => {
    const text = `Business object:\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "label": "Hammer",\n  "price": 10\n}`;

    expect(containsControlPayloadFinal(text, ["shell"])).toBe(false);
  });

  it("detects malformed standalone runtime-control JSON with control evidence", () => {
    expect(
      containsControlPayloadFinal('{"kind":"tool","toolName":"shell","input":}', ["shell"]),
    ).toBe(true);
    expect(
      containsControlPayloadFinal('[{"kind":"tool","toolName":"shell","input":}]', ["shell"]),
    ).toBe(true);
  });

  it("detects malformed multi-line runtime-control JSON after prose", () => {
    const text = `I will call:\n{\n  "kind": "tool",\n  "toolName": "shell",\n  "input":\n}`;

    expect(containsControlPayloadFinal(text, ["shell"])).toBe(true);
  });

  it("detects malformed provider-native tool call JSON with control evidence", () => {
    expect(
      containsControlPayloadFinal('{"tool_calls":[{"name":"shell","arguments":}]}', ["shell"]),
    ).toBe(true);
  });

  it("does not treat malformed business JSON as runtime-control JSON without evidence", () => {
    expect(containsControlPayloadFinal('{"kind":"tool","label":}', ["shell"])).toBe(false);
  });

  it("allows fenced large runtime-like JSON in assistant terminal output", () => {
    const text = `\`\`\`json\n{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "kind": "tool",\n  "toolName": "shell",\n  "input": {}\n}\n\`\`\``;

    expect(containsControlPayloadFinal(text, ["shell"])).toBe(false);
  });

  it("detects control evidence before the scan cap and documents capped late evidence", () => {
    const withinCap = `{\n  "metadata": "${"safe ".repeat(2_000)}",\n  "kind": "tool",\n  "toolName": "shell",\n  "input": {}\n}`;
    const afterCap = `{\n  "metadata": "${"safe ".repeat(30_000)}",\n  "kind": "tool",\n  "toolName": "shell",\n  "input": {}\n}`;

    expect(containsControlPayloadFinal(withinCap, ["shell"])).toBe(true);
    expect(containsControlPayloadFinal(afterCap, ["shell"])).toBe(false);
  });
});
