import assert from "node:assert/strict";
import { test } from "vitest";
import {
	buildModelRouteSnapshot,
	redactModelConfigSecrets,
	sanitizeModelRouteSnapshot,
} from "../uok/model-route-snapshot.js";

test("sanitizeModelRouteSnapshot_when_route_has_secret_fields_omits_credentials", () => {
	assert.deepEqual(
		sanitizeModelRouteSnapshot({
			provider: "zai",
			id: "glm-5.1",
			displayName: "Z.AI GLM",
			api: "openai-chat",
			authMode: "apiKey",
			baseUrl: "https://api.example.test/v1",
			apiKey: "secret-key",
			headers: { Authorization: "Bearer secret" },
			noImageSupport: true,
		}),
		{
			provider: "zai",
			model: "glm-5.1",
			displayName: "Z.AI GLM",
			api: "openai-chat",
			authMode: "apiKey",
			baseUrl: "https://api.example.test/v1",
			isCustom: false,
			byok: true,
			noImageSupport: true,
		},
	);
});

test("redactModelConfigSecrets_when_config_contains_credentials_replaces_secret_values", () => {
	assert.deepEqual(
		redactModelConfigSecrets({
			provider: "kimi-coding",
			apiKey: "key-123",
			authHeader: "x-api-key",
			headers: { Authorization: "Bearer token" },
			baseUrl: "https://api.example.test/v1",
		}),
		{
			provider: "kimi-coding",
			apiKey: "[REDACTED]",
			authHeader: "[REDACTED]",
			headers: "[REDACTED]",
			baseUrl: "https://api.example.test/v1",
		},
	);
});

test("buildModelRouteSnapshot_when_route_and_fallbacks_present_keeps_policy_auto_and_routes_safe", () => {
	const snapshot = buildModelRouteSnapshot({
		role: "worker",
		unitType: "execute-task",
		unitId: "M001/S01/T01",
		route: {
			provider: "kimi-coding",
			model: "kimi-k2.6",
			authMode: "apiKey",
			apiKey: "secret",
		},
		routingReason: "auto selector",
		fallbacksTried: [
			{
				provider: "ollama-cloud",
				model: "deepseek-v4-pro",
				authMode: "apiKey",
				apiKey: "secret",
			},
		],
		configEvidence: {
			apiKey: "secret",
			baseUrl: "https://api.example.test/v1",
		},
	});

	assert.equal(snapshot.schemaVersion, 1);
	assert.deepEqual(snapshot.requestedPolicy, { mode: "auto", constraints: [] });
	assert.equal(snapshot.route.provider, "kimi-coding");
	assert.equal(snapshot.route.model, "kimi-k2.6");
	assert.equal(snapshot.route.byok, true);
	assert.equal(snapshot.route.apiKey, undefined);
	assert.equal(snapshot.fallbacksTried[0].provider, "ollama-cloud");
	assert.equal(snapshot.fallbacksTried[0].apiKey, undefined);
	assert.equal(snapshot.configEvidence.apiKey, "[REDACTED]");
	assert.equal(snapshot.configEvidence.baseUrl, "https://api.example.test/v1");
});
