/**
 * Tests for the cross-tier escalation policy block injected into the SF
 * system prompt (buildEscalationPolicyBlock in bootstrap/system-context.ts).
 *
 * Verifies:
 * - canAskUser=true  → "Ask the user via" language, no "DO NOT" language
 * - canAskUser=false → "DO NOT call" + "exit with a structured blocker"
 * - Both variants include all three tiers and key tool names
 */

import assert from "node:assert/strict";
import { describe, it } from 'vitest';

import { buildEscalationPolicyBlock } from "../bootstrap/system-context.js";

describe("buildEscalationPolicyBlock", () => {
	describe("canAskUser=true (auto/step mode)", () => {
		const block = buildEscalationPolicyBlock(true);

		it('includes "Ask the user via" language', () => {
			assert.ok(
				block.includes("Ask the user via"),
				`expected "Ask the user via" in block:\n${block}`,
			);
		});

		it('does not include the "DO NOT" autonomous language', () => {
			assert.ok(
				!block.includes("DO NOT call"),
				`expected no "DO NOT call" in auto-mode block:\n${block}`,
			);
		});

		it("does not include the structured-blocker exit instruction", () => {
			assert.ok(
				!block.includes("exit with a structured blocker message"),
				`expected no blocker-exit language in auto-mode block:\n${block}`,
			);
		});

		it("includes Tier 1 header", () => {
			assert.ok(block.includes("Tier 1"), `missing Tier 1 in block`);
		});

		it("includes Tier 2 header", () => {
			assert.ok(block.includes("Tier 2"), `missing Tier 2 in block`);
		});

		it("includes Tier 3 header", () => {
			assert.ok(block.includes("Tier 3"), `missing Tier 3 in block`);
		});

		it("mentions sift in Tier 1", () => {
			assert.ok(block.includes("sift"), `missing "sift" in block`);
		});

		it("mentions WebSearch in Tier 2", () => {
			assert.ok(block.includes("WebSearch"), `missing "WebSearch" in block`);
		});

		it("mentions Context7 in Tier 2", () => {
			assert.ok(block.includes("Context7"), `missing "Context7" in block`);
		});
	});

	describe("canAskUser=false (autonomous mode)", () => {
		const block = buildEscalationPolicyBlock(false);

		it('includes "DO NOT call `ask_user_questions`"', () => {
			assert.ok(
				block.includes("DO NOT call"),
				`expected "DO NOT call" in autonomous block:\n${block}`,
			);
		});

		it('includes "exit with a structured blocker message"', () => {
			assert.ok(
				block.includes("exit with a structured blocker message"),
				`expected blocker-exit language in autonomous block:\n${block}`,
			);
		});

		it('does not include "Ask the user via" language', () => {
			assert.ok(
				!block.includes("Ask the user via"),
				`expected no "Ask the user via" in autonomous block:\n${block}`,
			);
		});

		it("includes Tier 1 header", () => {
			assert.ok(block.includes("Tier 1"), `missing Tier 1 in block`);
		});

		it("includes Tier 2 header", () => {
			assert.ok(block.includes("Tier 2"), `missing Tier 2 in block`);
		});

		it("includes Tier 3 header", () => {
			assert.ok(block.includes("Tier 3"), `missing Tier 3 in block`);
		});

		it("mentions sift in Tier 1", () => {
			assert.ok(block.includes("sift"), `missing "sift" in block`);
		});

		it("mentions WebSearch in Tier 2", () => {
			assert.ok(block.includes("WebSearch"), `missing "WebSearch" in block`);
		});

		it("mentions Context7 in Tier 2", () => {
			assert.ok(block.includes("Context7"), `missing "Context7" in block`);
		});
	});
});
