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

import { reviewIndependentModelEvidence } from "../../scripts/lib/wp7-02-manual-review.mjs";

const modelIds = ["gemini-3.1-flash-image", "gpt-image-2"];
const dimensions = { "3:4": [1080, 1440], "1:1": [1080, 1080], "4:3": [1440, 1080], "9:16": [1080, 1920] };

function entry(modelId, complete) {
  const calls = Object.entries(dimensions).map(([ratio, [width, height]], index) => ({
    input: "pure_text", requested_ratio: ratio, response: { dimensions: { height, width } },
    scenario_id: `real-${index + 1}`, source: "real_gateway", status: "passed",
  }));
  calls.push({ ...calls[1], input: "reference_image", scenario_id: "real-5" });
  return {
    externalCalls: complete ? {
      approved_real_call_limit: 120,
      attempts: calls.map((call) => ({ attempt_no: 1, scenario_id: call.scenario_id, status: "passed" })),
      calls, maximum_real_calls: 6, planned_real_calls: 5, real_calls: 5, status: "passed",
    } : { attempts: [{ attempt_no: 1, scenario_id: "real-1", status: "failed" }], calls: [], maximum_real_calls: 6, planned_real_calls: 5, real_calls: 1, status: "externally_blocked" },
    matrix: complete ? {
      config_version: 2,
      contract_change: { full_matrix_reapplied: true },
      error_scenarios: Array.from({ length: 9 }, () => ({ status: "passed" })),
      execution_modes: ["covered_by_real_calls", "passed", "passed"].map((status) => ({ status })),
      model_id: modelId,
      pure_text: { status: "passed" },
      ratios: Object.keys(dimensions).map((ratio) => ({ ratio, status: "passed" })),
      reference_image: { status: "passed" },
      settlements: [{}, {}, {}],
      status: "passed",
    } : { config_version: 2, model_id: modelId, status: "externally_blocked" },
    modelId,
    readiness: { evidence_id: `sha256:${modelId}`, status: complete ? "passed" : "externally_blocked" },
    redaction: { secret_scan: "passed", status: "passed" },
  };
}

test("TDD-WP7-EXT-001 reviews each model independently when the set is mixed", () => {
  const result = reviewIndependentModelEvidence(modelIds.map((modelId, index) => entry(modelId, index === 1)), {
    reviewedAt: "2026-08-04T09:30:00.000Z",
    runId: "wp7-02-mixed-review",
  });
  assert.equal(result.status, "externally_blocked");
  assert.deepEqual(result.reviews.map((review) => [review.model_id, review.status]), [
    [modelIds[0], "blocked"], [modelIds[1], "passed"],
  ]);
});
