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

import {
  WP7_02_CONTROLLED_REAL_LIMIT,
  buildControlledExecutionPlan,
  buildProviderRequest,
  buildSanitizedResponseEvidence,
  describeProviderResponseShape,
  executeProviderRequest,
  normalizeProviderResponse,
  validateSanitizedEvidence,
} from "../../scripts/lib/wp7-02-controlled-executor.mjs";
import {
  assembleControlledModelEvidence,
  buildDeterministicExecutionEvidence,
  createControlledReferencePng,
  runControlledRealScenarios,
} from "../../scripts/lib/wp7-02-controlled-matrix.mjs";

const models = [
  {
    config_version: 7,
    model_id: "gemini-3.1-flash-image",
    route_profile: {
      endpoint: "https://oneapi.intelligrow.cn/v1beta/models/gemini-3.1-flash-image:generateContent",
      mode: "sync",
      protocol_version: "gemini-native-v1beta",
    },
  },
  {
    config_version: 2,
    model_id: "gpt-image-2",
    route_profile: {
      endpoint: "https://oneapi.intelligrow.cn/v1/images/generations",
      mode: "sync",
      protocol_version: "openai-images-v1",
      reference_endpoint: "https://oneapi.intelligrow.cn/v1/images/edits",
    },
  },
];

const stableFlashInteractionModel = {
  config_version: 4,
  model_id: "gemini-3.1-flash-image",
  route_profile: {
    endpoint: "https://oneapi.intelligrow.cn/v1beta/interactions",
    mode: "sync",
    protocol_version: "gemini-interactions-v1beta",
    provider_model_id: "gemini-3.1-flash-image",
  },
};

const stableFlashOpenAiImageModel = {
  config_version: 6,
  model_id: "gemini-3.1-flash-image",
  route_profile: {
    endpoint: "https://oneapi.intelligrow.cn/v1/images/generations",
    mode: "sync",
    protocol_version: "openai-images-v1",
    provider_model_id: "gemini-3.1-flash-image",
    reference_endpoint: "https://oneapi.intelligrow.cn/v1/images/edits",
  },
};

const stableFlashOpenAiChatModel = {
  config_version: 7,
  model_id: "gemini-3.1-flash-image",
  route_profile: {
    endpoint: "https://oneapi.intelligrow.cn/v1/chat/completions",
    mode: "sync",
    protocol_version: "gemini-openai-chat-v1",
    provider_model_id: "gemini-3.1-flash-image",
  },
};

const onePixelPng = Buffer.from(
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
  "base64",
);

test("TDD-WP7-EXT-001 caps real calls and separates real from deterministic scenarios", () => {
  const plans = models.map((model) => buildControlledExecutionPlan(model));
  assert.equal(WP7_02_CONTROLLED_REAL_LIMIT, 120);
  assert.equal(plans.reduce((total, plan) => total + plan.planned_real_calls, 0) <= WP7_02_CONTROLLED_REAL_LIMIT, true);
  for (const plan of plans) {
    assert.deepEqual(plan.real_scenarios.map((entry) => entry.ratio), ["3:4", "1:1", "4:3", "9:16", "1:1"]);
    assert.deepEqual(plan.real_scenarios.map((entry) => entry.input), ["pure_text", "pure_text", "pure_text", "pure_text", "reference_image"]);
    assert.deepEqual(plan.execution_modes, [
      { mode: "sync", source: "real_gateway" },
      { mode: "async", source: "deterministic_local" },
      { mode: "poll", source: "deterministic_local" },
    ]);
    assert.equal(plan.error_scenarios.length, 9);
    assert.equal(plan.error_scenarios.every((entry) => entry.source === "deterministic_local"), true);
    assert.deepEqual(plan.state_scenarios.map((entry) => entry.name), [
      "credit_commit_once",
      "credit_release_once_per_terminal_failure",
      "contract_change_invalidation",
      "full_revalidation",
    ]);
  }
});

test("TDD-WP7-EXT-001 builds protocol-specific requests without auth in arguments", () => {
  const reference = { bytes: onePixelPng, mime_type: "image/png" };
  const gemini = buildProviderRequest({ modelConfig: models[0], prompt: "controlled fixture prompt", ratio: "3:4", reference });
  assert.equal(gemini.method, "POST");
  assert.equal(gemini.body.contents[0].parts.some((part) => part.inlineData?.data), true);
  assert.deepEqual(gemini.body.generationConfig.responseModalities, ["IMAGE"]);
  assert.deepEqual(gemini.body.generationConfig.imageConfig, {
    aspectRatio: "3:4",
    imageSize: "1K",
  });
  assert.equal("responseFormat" in gemini.body.generationConfig, false);
  assert.equal("authorization" in gemini.headers, false);

  const interaction = buildProviderRequest({
    modelConfig: stableFlashInteractionModel,
    prompt: "controlled fixture prompt",
    ratio: "3:4",
    reference,
  });
  assert.deepEqual(interaction.body, {
    input: [
      { text: "controlled fixture prompt", type: "text" },
      { data: onePixelPng.toString("base64"), mime_type: "image/png", type: "image" },
    ],
    model: "gemini-3.1-flash-image",
    response_format: { aspect_ratio: "3:4", image_size: "1K", type: "image" },
  });
  assert.equal(interaction.url, "https://oneapi.intelligrow.cn/v1beta/interactions");
  assert.equal("authorization" in interaction.headers, false);

  const stableOpenAiImage = buildProviderRequest({
    modelConfig: stableFlashOpenAiImageModel,
    prompt: "controlled fixture prompt",
    ratio: "4:3",
  });
  assert.deepEqual(stableOpenAiImage.body, {
    model: "gemini-3.1-flash-image",
    prompt: "controlled fixture prompt",
    response_format: "b64_json",
    size: "1408x1056",
  });

  const stableOpenAiChat = buildProviderRequest({
    modelConfig: stableFlashOpenAiChatModel,
    prompt: "controlled fixture prompt",
    ratio: "4:3",
    reference,
  });
  assert.deepEqual(stableOpenAiChat.body, {
    extra_body: { google: { image_config: { aspect_ratio: "4:3", image_size: "1K" } } },
    messages: [{
      content: [
        { text: "controlled fixture prompt", type: "text" },
        { image_url: { url: `data:image/png;base64,${onePixelPng.toString("base64")}` }, type: "image_url" },
      ],
      role: "user",
    }],
    model: "gemini-3.1-flash-image",
    stream: false,
  });
  assert.equal(stableOpenAiChat.url, "https://oneapi.intelligrow.cn/v1/chat/completions");
  assert.equal("authorization" in stableOpenAiChat.headers, false);

  const openai = buildProviderRequest({ modelConfig: models[1], prompt: "controlled fixture prompt", ratio: "9:16" });
  assert.deepEqual(openai.body, {
    model: "gpt-image-2",
    prompt: "controlled fixture prompt",
    response_format: "b64_json",
    size: "1008x1792",
  });
  assert.equal("authorization" in openai.headers, false);

  const openaiEdit = buildProviderRequest({ modelConfig: models[1], prompt: "controlled fixture prompt", ratio: "1:1", reference });
  assert.equal(openaiEdit.url, "https://oneapi.intelligrow.cn/v1/images/edits");
  assert.equal(openaiEdit.body instanceof FormData, true);
  assert.equal(openaiEdit.body.get("model"), "gpt-image-2");
  assert.equal(openaiEdit.body.get("size"), "1088x1088");
  assert.equal(openaiEdit.body.get("image[]") instanceof Blob, true);
  assert.equal("content-type" in openaiEdit.headers, false);
});

test("TDD-WP7-EXT-001 retains only response metadata and hashes", () => {
  const geminiResponse = {
    candidates: [{ content: { parts: [{ inlineData: { data: onePixelPng.toString("base64"), mimeType: "image/png" } }] } }],
    usageMetadata: { candidatesTokenCount: 7, promptTokenCount: 5, totalTokenCount: 12 },
  };
  const normalized = normalizeProviderResponse(models[0], geminiResponse);
  const evidence = buildSanitizedResponseEvidence(normalized);
  assert.deepEqual(evidence.dimensions, { height: 1, width: 1 });
  assert.equal(evidence.mime, "image/png");
  assert.match(evidence.evidence_hash, /^sha256:[A-F0-9]{64}$/);
  assert.deepEqual(evidence.usage_summary, { input_units: 5, output_units: 7, total_units: 12 });
  assert.doesNotMatch(JSON.stringify(evidence), /iVBOR|bytes|data|prompt|authorization|token/i);
  assert.equal(validateSanitizedEvidence(evidence), evidence);

  const interactionNormalized = normalizeProviderResponse(stableFlashInteractionModel, {
    status: "completed",
    steps: [{ content: [{ data: onePixelPng.toString("base64"), mime_type: "image/png", type: "image" }], type: "model_output" }],
    usage: { total_input_tokens: 11, total_output_tokens: 13, total_tokens: 24 },
  });
  assert.deepEqual(interactionNormalized.dimensions, { height: 1, width: 1 });
  assert.equal(interactionNormalized.mime, "image/png");
  assert.deepEqual(interactionNormalized.usage_summary, { input_units: 11, output_units: 13, total_units: 24 });

  const chatNormalized = normalizeProviderResponse(stableFlashOpenAiChatModel, {
    choices: [{ message: { content: `![image](data:image/png;base64,${onePixelPng.toString("base64")})` } }],
    usage: { completion_tokens: 17, prompt_tokens: 15, total_tokens: 32 },
  });
  assert.deepEqual(chatNormalized.dimensions, { height: 1, width: 1 });
  assert.equal(chatNormalized.mime, "image/png");
  assert.deepEqual(chatNormalized.usage_summary, { input_units: 15, output_units: 17, total_units: 32 });
});

test("TDD-WP7-EXT-001 rejects sensitive or shared evidence fields", () => {
  for (const key of ["raw_prompt", "raw_provider_payload", "credential_value", "authorization", "absolute_path"]) {
    assert.throws(() => validateSanitizedEvidence({ [key]: "forbidden", status: "passed" }), /WP7_02_SENSITIVE_EVIDENCE_FORBIDDEN/);
  }
  assert.throws(() => validateSanitizedEvidence({ status: "passed", verified: true }), /WP7_02_SHARED_VERIFIED_FORBIDDEN/);
});

test("TDD-WP7-EXT-001 confines the credential to the request header and discards provider error bodies", async () => {
  const credentialMarker = "controlled-secret-value-for-test-only";
  const success = await executeProviderRequest({
    fetchImpl: async (_url, init) => {
      assert.equal(init.headers.authorization, `Bearer ${credentialMarker}`);
      return new Response(JSON.stringify({
        candidates: [{ content: { parts: [{ inlineData: { data: onePixelPng.toString("base64"), mimeType: "image/png" } }] } }],
      }), { headers: { "content-type": "application/json" }, status: 200 });
    },
    modelConfig: models[0],
    prompt: "controlled fixture prompt",
    ratio: "1:1",
    token: credentialMarker,
  });
  assert.equal(success.http_status, 200);
  assert.deepEqual(success.response_evidence.dimensions, { height: 1080, width: 1080 });
  assert.deepEqual(success.response_evidence.normalization, {
    applied: true,
    upstream_dimensions: { height: 1, width: 1 },
  });
  assert.doesNotMatch(JSON.stringify({ ...success, normalized: undefined }), new RegExp(credentialMarker));

  await assert.rejects(() => executeProviderRequest({
    fetchImpl: async () => new Response(JSON.stringify({ provider_body: credentialMarker }), { status: 502 }),
    modelConfig: models[0],
    prompt: "controlled fixture prompt",
    ratio: "1:1",
    token: credentialMarker,
  }), (error) => {
    assert.equal(error.message, "WP7_02_UPSTREAM_HTTP_502");
    assert.doesNotMatch(error.message, new RegExp(credentialMarker));
    return true;
  });
});

test("TDD-WP7-EXT-001 executes only five real success probes per model and keeps failed ratios blocking", async () => {
  let fetchCalls = 0;
  const execution = await runControlledRealScenarios({
    fetchImpl: async () => {
      fetchCalls += 1;
      return new Response(JSON.stringify({
        candidates: [{ content: { parts: [{ inlineData: { data: onePixelPng.toString("base64"), mimeType: "image/png" } }] } }],
      }), { status: 200 });
    },
    maxRealCalls: 120,
    modelConfig: models[0],
    token: "controlled-secret-value-for-test-only",
  });
  assert.equal(fetchCalls, 5);
  assert.equal(execution.real_calls, 5);
  assert.equal(execution.attempts.length, 5);
  assert.equal(execution.status, "externally_blocked");
  assert.equal(execution.calls.filter((call) => call.status === "passed").length, 2);
  assert.doesNotMatch(JSON.stringify(execution), /controlled-secret|fixture prompt|iVBOR/i);
  await assert.rejects(() => runControlledRealScenarios({ maxRealCalls: 121, modelConfig: models[0], token: "not-used" }), /WP7_02_REAL_CALL_LIMIT_INVALID/);
});

test("TDD-WP7-EXT-001 retries one timeout once and records every real attempt", async () => {
  let fetchCalls = 0;
  const execution = await runControlledRealScenarios({
    fetchImpl: async () => {
      fetchCalls += 1;
      if (fetchCalls === 1) {
        const timeout = new Error("sanitized timeout fixture");
        timeout.name = "AbortError";
        throw timeout;
      }
      return new Response(JSON.stringify({
        candidates: [{ content: { parts: [{ inlineData: { data: onePixelPng.toString("base64"), mimeType: "image/png" } }] } }],
      }), { status: 200 });
    },
    maxRealCalls: 120,
    modelConfig: models[0],
    token: "controlled-secret-value-for-test-only",
  });
  assert.equal(fetchCalls, 6);
  assert.equal(execution.real_calls, 6);
  assert.equal(execution.calls.length, 5);
  assert.equal(execution.attempts.length, 6);
  assert.deepEqual(execution.attempts.slice(0, 2).map((attempt) => [attempt.scenario_id, attempt.attempt_no, attempt.error_code ?? attempt.status]), [
    ["real-1", 1, "WP7_02_UPSTREAM_TIMEOUT"],
    ["real-1", 2, "WP7_02_RESPONSE_DIMENSIONS_INVALID"],
  ]);
});

test("TDD-WP7-EXT-001 describes only protocol structure and stops repeated contract-shape calls", async () => {
  const uriShape = describeProviderResponseShape({ candidates: [{ content: { parts: [{ text: "https://first.invalid/generated" }] } }] });
  const equivalentUriShape = describeProviderResponseShape({ candidates: [{ content: { parts: [{ text: "https://other.invalid/result" }] } }] });
  assert.deepEqual(uriShape, equivalentUriShape);
  assert.match(JSON.stringify(uriShape), /"representation":"uri"/);
  assert.doesNotMatch(JSON.stringify(uriShape), /first\.invalid|other\.invalid/);
  assert.match(JSON.stringify(describeProviderResponseShape({ candidates: [{ content: { parts: [{ text: "![result](https:\/\/example.invalid\/generated)" }] } }] })), /"representation":"markdown_uri"/);
  assert.match(JSON.stringify(describeProviderResponseShape({ candidates: [{ content: { parts: [{ text: "data:image\/png;base64,AAAA" }] } }] })), /"representation":"inline_media"/);
  assert.match(JSON.stringify(describeProviderResponseShape({ candidates: [{ content: { parts: [{ text: "ordinary explanation" }] } }] })), /"representation":"plain_text"/);

  let fetchCalls = 0;
  const execution = await runControlledRealScenarios({
    fetchImpl: async () => {
      fetchCalls += 1;
      return new Response(JSON.stringify({ envelope: { outputs: [{ binary: "private-response-value" }] } }), { status: 200 });
    },
    maxRealCalls: 120,
    modelConfig: models[0],
    token: "controlled-secret-value-for-test-only",
  });
  assert.equal(fetchCalls, 1);
  assert.equal(execution.real_calls, 1);
  assert.equal(execution.calls[0].error_code, "WP7_02_RESPONSE_SINGLE_IMAGE_REQUIRED");
  assert.deepEqual(execution.calls[0].response_shape, describeProviderResponseShape({ envelope: { outputs: [{ binary: "different-private-value" }] } }));
  assert.doesNotMatch(JSON.stringify(execution.calls[0].response_shape), /private-response-value|different-private-value/);
});

test("TDD-WP7-EXT-001 assembles independent complete evidence without retaining reference bytes", () => {
  const reference = createControlledReferencePng();
  assert.equal(reference.subarray(0, 8).toString("hex"), "89504e470d0a1a0a");
  const calls = ["3:4", "1:1", "4:3", "9:16"].map((ratio, index) => ({
    duration_ms: 1,
    http_status: 200,
    input: "pure_text",
    requested_ratio: ratio,
    response: { dimensions: { height: 1, width: 1 }, evidence_hash: `sha256:${"A".repeat(64)}`, mime: "image/png", usage_summary: { input_units: 0, output_units: 0, total_units: 0 } },
    scenario_id: `real-${index + 1}`,
    source: "real_gateway",
    status: "passed",
  }));
  calls.push({ ...calls[1], input: "reference_image", scenario_id: "real-5" });
  const deterministicState = {
    contract_change: { full_matrix_reapplied: true },
    error_scenarios: Array.from({ length: 9 }, (_, index) => ({ category: `category-${index}`, status: "passed" })),
    model_id: models[0].model_id,
    settlements: ["succeeded", "failed", "rejected"].map((outcome) => ({ outcome })),
    status: "passed",
  };
  const evidence = assembleControlledModelEvidence({
    deterministicState,
    modelConfig: models[0],
    realExecution: {
      attempts: calls.map((call, index) => ({ attempt_no: 1, http_status: 200, scenario_id: call.scenario_id, status: "passed" })),
      calls, maximum_real_calls: 6, planned_real_calls: 5, real_calls: 5, status: "passed",
    },
    runId: "wp7-02-assembly-test",
  });
  assert.equal(evidence.status, "passed");
  assert.equal(evidence.manual_review.status, "pending");
  assert.equal(evidence.external_calls.attempts.length, 5);
  assert.equal(evidence.external_calls.maximum_real_calls, 6);
  assert.equal(evidence.matrix.error_scenarios.length, 9);
  assert.doesNotMatch(JSON.stringify(evidence), /iVBOR|image_bytes|raw_prompt|authorization/i);
  const execution = buildDeterministicExecutionEvidence(models[0].model_id, "wp7-02-assembly-test");
  assert.deepEqual(execution.modes.map((entry) => entry.mode), ["sync", "async", "poll"]);
  assert.deepEqual(execution.trace.map((entry) => `${entry.action}:${entry.before}->${entry.after}`), [
    "start:created->pending",
    "poll:pending->completed",
    "poll:completed->completed",
  ]);
  assert.equal(execution.trace[2].replay, true);
});
