import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
	checkModelScope,
	matchesScopePattern,
	parseModelScopeConfig,
	resolveModelScopesForAgent,
	type ModelScopeConfig,
} from "../../src/runs/shared/model-scope.ts";

describe("matchesScopePattern", () => {
	it("matches an exact provider/id", () => {
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4", "anthropic/claude-sonnet-4"), true);
	});

	it("matches a provider wildcard", () => {
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4", "anthropic/*"), true);
		assert.equal(matchesScopePattern("anthropic/claude-haiku-4-5", "anthropic/*"), true);
	});

	it("matches a model-prefix wildcard", () => {
		assert.equal(matchesScopePattern("openai/gpt-5-mini", "openai/gpt-5-*"), true);
		// The dash in the pattern is literal, so the bare id without a trailing part does not match.
		assert.equal(matchesScopePattern("openai/gpt-5", "openai/gpt-5-*"), false);
		assert.equal(matchesScopePattern("openai/gpt-5", "openai/gpt-5*"), true);
	});

	it("does not match across providers", () => {
		assert.equal(matchesScopePattern("deepseek/deepseek-v4", "anthropic/*"), false);
		assert.equal(matchesScopePattern("openai/gpt-5-mini", "anthropic/*"), false);
	});

	it("ignores case on both sides", () => {
		assert.equal(matchesScopePattern("Anthropic/Claude-Sonnet-4", "anthropic/*"), true);
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4", "ANTHROPIC/CLAUDE-*"), true);
	});

	it("strips a thinking suffix before matching", () => {
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4:high", "anthropic/claude-sonnet-4"), true);
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4:high", "anthropic/*"), true);
	});

	it("does not treat arbitrary colon text as a thinking suffix", () => {
		assert.equal(matchesScopePattern("anthropic:claude-opus", "anthropic"), false);
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4:experimental", "anthropic/claude-sonnet-4"), false);
	});

	it("requires a full match, not a substring", () => {
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4", "anthropic/claude"), false);
		assert.equal(matchesScopePattern("anthropic/claude-sonnet-4", "*claude*"), true);
	});
});

describe("checkModelScope", () => {
	const scope: ModelScopeConfig = { enforce: true, allow: ["anthropic/*", "openai/gpt-5-*"] };

	it("returns undefined when enforcement is off", () => {
		assert.equal(checkModelScope("deepseek/deepseek-v4", { enforce: false, allow: ["anthropic/*"] }, "explicit"), undefined);
		assert.equal(checkModelScope("deepseek/deepseek-v4", undefined, "explicit"), undefined);
	});

	it("returns undefined when no allow list is configured", () => {
		assert.equal(checkModelScope("deepseek/deepseek-v4", { enforce: true }, "explicit"), undefined);
		assert.equal(checkModelScope("deepseek/deepseek-v4", { enforce: true, allow: [] }, "explicit"), undefined);
	});

	it("returns undefined when the model is in scope", () => {
		assert.equal(checkModelScope("anthropic/claude-sonnet-4", scope, "explicit"), undefined);
		assert.equal(checkModelScope("openai/gpt-5-mini:high", scope, "inherited"), undefined);
	});

	it("returns an error violation for an explicit out-of-scope model", () => {
		const violation = checkModelScope("deepseek/deepseek-v4", scope, "explicit");
		assert.equal(violation?.severity, "error");
		assert.equal(violation?.model, "deepseek/deepseek-v4");
		assert.deepEqual(violation?.allowedPatterns, ["anthropic/*", "openai/gpt-5-*"]);
		assert.equal(violation?.origin, "modelScope");
		assert.match(violation?.message ?? "", /outside the configured subagent model scope/);
	});

	it("returns a warn violation for an inherited out-of-scope model", () => {
		const violation = checkModelScope("deepseek/deepseek-v4", scope, "inherited");
		assert.equal(violation?.severity, "warn");
	});

	it("returns an error violation for an inherited model in strict mode", () => {
		const violation = checkModelScope("deepseek/deepseek-v4", { ...scope, strict: true }, "inherited");
		assert.equal(violation?.severity, "error");
	});

	it("defaults to inherited (warn) severity when source is omitted-ish via inherited", () => {
		// Caller passes "inherited" for frontmatter/parent-inherited models.
		assert.equal(checkModelScope("meta/llama-4", scope, "inherited")?.severity, "warn");
	});

	it("strips the thinking suffix from the reported model", () => {
		const violation = checkModelScope("deepseek/deepseek-v4:high", scope, "explicit");
		assert.equal(violation?.model, "deepseek/deepseek-v4");
	});

	it("returns undefined for an undefined model", () => {
		assert.equal(checkModelScope(undefined, scope, "explicit"), undefined);
	});
});

describe("resolveModelScopesForAgent", () => {
	it("returns independent global and agent checks with inherited flags", () => {
		assert.deepEqual(
			resolveModelScopesForAgent({ enforce: true, strict: true, allow: ["openai/*"], agents: { worker: { allow: ["openai/gpt-5-mini"] } } }, "worker", { provider: "openai", id: "gpt-5" }),
			[
				{ enforce: true, strict: true, allow: ["openai/*"], origin: "modelScope" },
				{ enforce: true, strict: true, allow: ["openai/gpt-5-mini"], origin: "modelScope.agents.worker" },
			],
		);
	});

	it("expands inherit against the current parent model", () => {
		const scopes = resolveModelScopesForAgent({ enforce: true, allow: ["inherit"], agents: { reviewer: { allow: ["inherit"] } } }, "reviewer", { provider: "anthropic", id: "claude-sonnet-4" });
		assert.deepEqual(scopes.map((scope) => scope.allow), [["anthropic/claude-sonnet-4"], ["anthropic/claude-sonnet-4"]]);
	});

	it("keeps inherit literal when no parent model is available so enforcement fails closed", () => {
		const [scope] = resolveModelScopesForAgent({ enforce: true, allow: ["inherit"] }, "worker", undefined);
		const violation = checkModelScope("openai/gpt-5-mini", scope, "explicit");
		assert.equal(violation?.severity, "error");
		assert.deepEqual(violation?.allowedPatterns, ["inherit"]);
	});

	it("does not apply another agent's scope", () => {
		assert.deepEqual(resolveModelScopesForAgent({ enforce: true, agents: { reviewer: { allow: ["inherit"] } } }, "worker", { provider: "openai", id: "gpt-5" }), []);
	});
});

describe("expandReservedPatterns via resolveModelScopesForAgent", () => {
	const parent = { provider: "anthropic", id: "claude-sonnet-4" };
	const scopedIds = ["anthropic/claude-sonnet-4", "openai/gpt-5-mini"];

	it("expands scoped to the full scoped-model snapshot", () => {
		const [scope] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped"] }, "worker", parent, scopedIds);
		assert.deepEqual(scope.allow, scopedIds);
	});

	it("does not expand other patterns alongside scoped", () => {
		const [scope] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped", "anthropic/*"] }, "worker", parent, scopedIds);
		assert.deepEqual(scope.allow, [...scopedIds, "anthropic/*"]);
	});

	it("expands scoped per agent scope", () => {
		const [agent] = resolveModelScopesForAgent({ enforce: true, agents: { reviewer: { allow: ["scoped"] } } }, "reviewer", parent, scopedIds);
		assert.deepEqual(agent.allow, scopedIds);
	});

	it("degrades scoped to the parent model when the snapshot is empty", () => {
		const [empty] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped"] }, "worker", parent, []);
		const [missing] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped"] }, "worker", parent, undefined);
		assert.deepEqual(empty.allow, ["anthropic/claude-sonnet-4"]);
		assert.deepEqual(missing.allow, ["anthropic/claude-sonnet-4"]);
	});

	it("keeps scoped literal when neither snapshot nor parent exists so enforcement fails closed", () => {
		const [scope] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped"] }, "worker", undefined, undefined);
		const violation = checkModelScope("openai/gpt-5-mini", scope, "explicit");
		assert.equal(violation?.severity, "error");
		assert.deepEqual(violation?.allowedPatterns, ["scoped"]);
	});

	it("summarizes the rendered pattern list for large scoped sets", () => {
		const manyIds = Array.from({ length: 12 }, (_, i) => `prov/model-${i}`);
		const [scope] = resolveModelScopesForAgent({ enforce: true, allow: ["scoped"] }, "worker", undefined, manyIds);
		const violation = checkModelScope("other/model", scope, "explicit");
		assert.match(violation?.message ?? "", /prov\/model-7, … \(12 patterns total\)/);
		assert.deepEqual(violation?.allowedPatterns, manyIds);
	});
});

describe("parseModelScopeConfig", () => {
	const meta = { filePath: "~/.pi/agent/settings.json" };

	it("returns undefined when the field is absent", () => {
		assert.equal(parseModelScopeConfig(undefined, meta), undefined);
	});

	it("parses a well-formed config", () => {
		assert.deepEqual(
			parseModelScopeConfig({ enforce: true, strict: true, allow: ["anthropic/*", "openai/gpt-5-*"] }, meta),
			{ enforce: true, strict: true, allow: ["anthropic/*", "openai/gpt-5-*"] },
		);
	});

	it("parses per-agent scopes and permits agent-only enforcement", () => {
		assert.deepEqual(
			parseModelScopeConfig({ enforce: true, strict: true, agents: { worker: { allow: [" openai/gpt-5-mini "] }, unknown: { enforce: false } } }, meta),
			{ enforce: true, strict: true, agents: { worker: { allow: ["openai/gpt-5-mini"] }, unknown: { enforce: false } } },
		);
	});

	it("parses enforce alone", () => {
		assert.deepEqual(parseModelScopeConfig({ enforce: false }, meta), { enforce: false });
	});

	it("trims allow patterns and drops empties", () => {
		assert.deepEqual(
			parseModelScopeConfig({ enforce: true, allow: ["  anthropic/*  ", "", "  "] }, meta),
			{ enforce: true, allow: ["anthropic/*"] },
		);
	});

	it("rejects a non-object value", () => {
		assert.throws(() => parseModelScopeConfig("anthropic/*", meta), /invalid 'modelScope'/);
		assert.throws(() => parseModelScopeConfig([], meta), /invalid 'modelScope'/);
	});

	it("rejects a non-boolean enforce", () => {
		assert.throws(() => parseModelScopeConfig({ enforce: "yes" }, meta), /invalid 'modelScope.enforce'/);
	});

	it("rejects a non-boolean strict value", () => {
		assert.throws(() => parseModelScopeConfig({ strict: "yes" }, meta), /invalid 'modelScope.strict'/);
	});

	it("rejects a non-array allow", () => {
		assert.throws(() => parseModelScopeConfig({ allow: "anthropic/*" }, meta), /invalid 'modelScope.allow'/);
	});

	it("rejects an allow array with non-string entries", () => {
		assert.throws(() => parseModelScopeConfig({ enforce: true, allow: ["anthropic/*", 42] }, meta), /invalid 'modelScope.allow'/);
	});

	it("rejects enforce without a non-empty allow list", () => {
		assert.throws(() => parseModelScopeConfig({ enforce: true }, meta), /without a non-empty 'allow'/);
		assert.throws(() => parseModelScopeConfig({ enforce: true, allow: [] }, meta), /non-empty array of patterns/);
	});

	it("rejects invalid agent scope shapes with the full field path", () => {
		assert.throws(() => parseModelScopeConfig({ enforce: true, agents: { worker: [] } }, meta), /modelScope\.agents\.worker/);
		assert.throws(() => parseModelScopeConfig({ agents: { worker: { allow: [] } } }, meta), /modelScope\.agents\.worker\.allow/);
		assert.throws(() => parseModelScopeConfig({ agents: { worker: { agents: {} } } }, meta), /modelScope\.agents\.worker\.agents/);
		assert.throws(() => parseModelScopeConfig({ agents: { " ": { allow: ["inherit"] } } }, meta), /non-empty agent name/);
	});
});
