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

import {
  AI_GATEWAY_CREDENTIAL_TARGET,
  WP7_02_MODEL_IDS,
  buildBlockedModelEvidence,
  buildModelContractPlan,
  inspectAiGatewayReadiness,
  productModelIdForControlledState,
  validateCandidateDependency,
  validateIndependentEvidenceSet,
} from "../../scripts/lib/wp7-02-external-contract.mjs";

const candidate = () => ({
  browsers: [
    { brand: "Google Chrome", full_version: "150.0.7871.187", major: 150, source: "installed_executable" },
    { brand: "Microsoft Edge", full_version: "151.0.4129.59", major: 151, source: "installed_executable" },
  ],
  build_commit: "623cad25b2a2a9a003502c9a92ebd318dad06248",
  candidate_package: { release_status: "candidate_unvalidated", sha256: "A".repeat(64) },
  final_release: false,
  fixed_port: 43121,
  recorded_at: "2026-08-04T05:28:11.257Z",
  schema_version: "1.0",
  status: "candidate_unvalidated",
});

test("TDD-WP7-EXT-001 fixes the user-approved replacement model set", () => {
  assert.deepEqual(WP7_02_MODEL_IDS, [
    "gemini-3.1-flash-image",
    "gpt-image-2",
  ]);
  assert.equal(productModelIdForControlledState("gemini-3.1-flash-image"), "gemini-3.1-flash-image-preview");
  assert.equal(productModelIdForControlledState("gpt-image-2"), "gpt-image-2");
  assert.throws(() => productModelIdForControlledState("gemini-3-pro-image-preview"), /WP7_02_MODEL_NOT_ALLOWED/);
  for (const modelId of WP7_02_MODEL_IDS) {
    const plan = buildModelContractPlan(modelId);
    assert.equal(plan.model_id, modelId);
    assert.deepEqual(plan.inputs, ["pure_text", "reference_image"]);
    assert.deepEqual(plan.ratios, ["3:4", "1:1", "4:3", "9:16"]);
    assert.deepEqual(plan.execution_modes, ["sync", "async", "poll"]);
    assert.deepEqual(plan.response_checks, ["single_image", "mime", "dimensions", "sanitized_usage"]);
    assert.deepEqual(plan.planned_request_breakdown, {
      contract_change_full_revalidation: 20,
      error_categories: 9,
      execution_modes_and_poll: 3,
      input_and_ratio_success: 6,
      settlement_boundaries: 2,
    });
    assert.equal(Object.values(plan.planned_request_breakdown).reduce((total, count) => total + count, 0), 40);
    assert.deepEqual(plan.error_categories, [
      "upstream_timeout", "upstream_failed", "safety_rejected", "model_disabled",
      "gateway_balance_insufficient", "gateway_contract_invalid", "reference_invalid",
      "unknown_retryable", "unknown_non_retryable",
    ]);
    assert.deepEqual(plan.error_expectations.safety_rejected, {
      credit_effect: "release_once",
      job_outcome: "rejected",
      user_action: "modify_prompt_or_reference",
    });
    assert.deepEqual(plan.error_expectations.reference_invalid, {
      credit_effect: "no_reserve_or_release_once",
      job_outcome: "not_created_or_failed",
      user_action: "replace_or_remove_reference",
    });
    assert.deepEqual(plan.state_checks, [
      "credit_commit_once", "credit_release_once_per_terminal_failure",
      "contract_change_invalidation", "full_revalidation",
    ]);
  }
});

test("TDD-WP7-EXT-001 rejects candidate drift and final-release substitution", () => {
  assert.equal(validateCandidateDependency(candidate()).build_commit, candidate().build_commit);
  assert.throws(() => validateCandidateDependency({ ...candidate(), final_release: true }), /WP7_02_CANDIDATE_FINAL_RELEASE_FORBIDDEN/);
  const drifted = candidate();
  drifted.browsers[0].full_version = "150.0.7871.188";
  assert.throws(() => validateCandidateDependency(drifted), /WP7_02_CANDIDATE_BROWSER_MISMATCH/);
});

test("TDD-WP7-EXT-001 remains externally blocked without confirmation, config and credential", () => {
  const readiness = inspectAiGatewayReadiness({
    candidateRecord: candidate(),
    confirmed: false,
    credentialTargets: [],
    modelConfig: undefined,
    modelId: WP7_02_MODEL_IDS[0],
  });
  assert.equal(AI_GATEWAY_CREDENTIAL_TARGET, "Dada/P0A/worker/ai-gateway");
  assert.equal(readiness.status, "externally_blocked");
  assert.equal(readiness.real_calls, 0);
  assert.deepEqual(readiness.blockers, [
    "explicit_confirmation_absent",
    "real_gateway_credentials_absent",
    "real_model_config_absent",
  ]);
  assert.equal("verified" in readiness, false);
});

test("TDD-WP7-EXT-001 fixes independent OneAPI routes without embedding credentials", () => {
  const manifest = JSON.parse(readFileSync("config/wp7-02-oneapi-test.json", "utf8"));
  assert.equal(manifest.config_set_version, 8);
  assert.equal(manifest.gateway_account_ref, "oneapi-intelligrow-test");
  assert.deepEqual(manifest.models.map((entry) => entry.model_id), WP7_02_MODEL_IDS);
  assert.deepEqual(manifest.models.map((entry) => entry.route_profile.protocol_version), [
    "gemini-openai-chat-v1",
    "openai-images-v1",
  ]);
  assert.deepEqual(manifest.models.map((entry) => entry.config_version), [7, 2]);
  assert.equal(manifest.models[0].route_profile.endpoint, "https://oneapi.intelligrow.cn/v1/chat/completions");
  assert.equal(manifest.models[0].route_profile.provider_model_id, "gemini-3.1-flash-image");
  assert.equal(manifest.models[1].route_profile.reference_endpoint, "https://oneapi.intelligrow.cn/v1/images/edits");
  assert.equal(manifest.models.every((entry) => entry.route_profile.endpoint.startsWith("https://oneapi.intelligrow.cn/")), true);
  assert.doesNotMatch(JSON.stringify(manifest), /api[_-]?key|authorization|bearer|sk-[A-Za-z0-9]/i);
});

test("TDD-WP7-EXT-001 writes blocked evidence without mock or sensitive payloads", () => {
  const evidence = WP7_02_MODEL_IDS.map((modelId) => buildBlockedModelEvidence({
    blockers: ["real_gateway_credentials_absent", "real_model_config_absent"],
    candidateRecord: candidate(),
    modelId,
    runId: "wp7-02-red-test",
  }));
  validateIndependentEvidenceSet(evidence);
  assert.equal(new Set(evidence.map((entry) => entry.evidence_id)).size, 2);
  for (const entry of evidence) {
    assert.equal(entry.status, "externally_blocked");
    assert.equal(entry.external_calls.real_calls, 0);
    assert.equal(entry.external_calls.mode, "controlled_real_not_executed");
    assert.equal(entry.matrix.scenarios.every((scenario) => scenario.status === "not_run"), true);
    assert.equal(entry.manual_review.status, "blocked");
    assert.equal(entry.redaction.secret_scan, "passed");
    assert.doesNotMatch(JSON.stringify(entry), /raw_prompt|raw_provider|credential_value|[A-Za-z]:\\\\Users\\\\/i);
  }

  const shared = structuredClone(evidence);
  shared[1].evidence_id = shared[0].evidence_id;
  assert.throws(() => validateIndependentEvidenceSet(shared), /WP7_02_SHARED_EVIDENCE_FORBIDDEN/);

  const mixed = structuredClone(evidence);
  mixed[1].status = "passed";
  mixed[1].matrix = { model_id: mixed[1].model_id, status: "passed" };
  mixed[1].external_calls = { real_calls: 5, status: "passed" };
  mixed[1].manual_review = { status: "pending" };
  mixed[1].redaction = { status: "passed" };
  assert.doesNotThrow(() => validateIndependentEvidenceSet(mixed));
  mixed[1].manual_review = { status: "passed" };
  assert.doesNotThrow(() => validateIndependentEvidenceSet(mixed));
});
