import assert from "node:assert/strict";
import { describe, test } from "vitest";
import {
	buildModeState,
	defaultModelModeForWorkMode,
	defaultPermissionProfileForRunControl,
	isModelMode,
	isPermissionProfile,
	isRunControlMode,
	isWorkMode,
	MODEL_MODES,
	modelModeToTier,
	PERMISSION_PROFILES,
	RUN_CONTROL_MODES,
	resolveModelMode,
	resolvePermissionProfile,
	resolveRunControlMode,
	resolveWorkMode,
	runControlModeForSession,
	tierToModelMode,
	WORK_MODES,
} from "../operating-model.js";

describe("operating model vocabulary", () => {
	test("workModes_are_chat_plan_build_review_repair_research", () => {
		assert.deepEqual(WORK_MODES, [
			"chat",
			"plan",
			"build",
			"review",
			"repair",
			"research",
		]);
		assert.equal(isWorkMode("chat"), true);
		assert.equal(isWorkMode("debug"), false);
	});

	test("runControlModes_are_manual_assisted_autonomous_only", () => {
		assert.deepEqual(RUN_CONTROL_MODES, ["manual", "assisted", "autonomous"]);
		assert.equal(isRunControlMode("auto"), false);
		assert.equal(isRunControlMode("full"), false);
	});

	test("permissionProfiles_are_trust_posture_not_run_control", () => {
		assert.deepEqual(PERMISSION_PROFILES, [
			"restricted",
			"normal",
			"trusted",
			"unrestricted",
		]);
		assert.equal(isPermissionProfile("autonomous"), false);
	});

	test("modelModes_are_fast_smart_deep", () => {
		assert.deepEqual(MODEL_MODES, ["fast", "smart", "deep"]);
		assert.equal(isModelMode("smart"), true);
		assert.equal(isModelMode("rush"), false);
	});

	test("resolvers_fail_closed", () => {
		assert.equal(resolveWorkMode("debug"), "chat");
		assert.equal(resolveRunControlMode("auto"), "manual");
		assert.equal(resolvePermissionProfile("full"), "restricted");
		assert.equal(resolveModelMode("rush"), "smart");
	});

	test("session_step_bit_derives_assisted_or_autonomous_run_control", () => {
		assert.equal(runControlModeForSession({ stepMode: true }), "assisted");
		assert.equal(runControlModeForSession({ stepMode: false }), "autonomous");
	});

	test("default_permission_profile_is_independent_from_autonomous_looping", () => {
		assert.equal(defaultPermissionProfileForRunControl("autonomous"), "normal");
		assert.equal(defaultPermissionProfileForRunControl("assisted"), "normal");
		assert.equal(defaultPermissionProfileForRunControl("manual"), "restricted");
	});

	test("default_model_mode_matches_work_mode_intent", () => {
		assert.equal(defaultModelModeForWorkMode("plan"), "deep");
		assert.equal(defaultModelModeForWorkMode("review"), "deep");
		assert.equal(defaultModelModeForWorkMode("research"), "deep");
		assert.equal(defaultModelModeForWorkMode("build"), "smart");
		assert.equal(defaultModelModeForWorkMode("repair"), "smart");
		assert.equal(defaultModelModeForWorkMode("chat"), "fast");
		assert.equal(defaultModelModeForWorkMode("unknown"), "fast");
	});

	test("buildModeState_normalizes_all_axes", () => {
		const state = buildModeState({
			workMode: "build",
			runControl: "autonomous",
			permissionProfile: "trusted",
			modelMode: "smart",
			surface: "tui",
		});
		assert.equal(state.workMode, "build");
		assert.equal(state.runControl, "autonomous");
		assert.equal(state.permissionProfile, "trusted");
		assert.equal(state.modelMode, "smart");
		assert.equal(state.surface, "tui");
		assert.ok(state.updatedAt);
	});

	test("buildModeState_fails_closed_on_invalid_values", () => {
		const state = buildModeState({
			workMode: "debug",
			runControl: "auto",
			permissionProfile: "full",
			modelMode: "rush",
		});
		assert.equal(state.workMode, "chat");
		assert.equal(state.runControl, "manual");
		assert.equal(state.permissionProfile, "restricted");
		assert.equal(state.modelMode, "smart");
	});

	test("buildModeState_has_safe_defaults", () => {
		const state = buildModeState();
		assert.equal(state.workMode, "chat");
		assert.equal(state.runControl, "manual");
		assert.equal(state.permissionProfile, "restricted");
		assert.equal(state.modelMode, "smart");
		assert.equal(state.surface, "tui");
	});

	test("modelModeToTier_maps_fast_smart_deep_to_light_standard_heavy", () => {
		assert.equal(modelModeToTier("fast"), "light");
		assert.equal(modelModeToTier("smart"), "standard");
		assert.equal(modelModeToTier("deep"), "heavy");
		assert.equal(modelModeToTier("unknown"), "standard");
	});

	test("tierToModelMode_maps_light_standard_heavy_to_fast_smart_deep", () => {
		assert.equal(tierToModelMode("light"), "fast");
		assert.equal(tierToModelMode("standard"), "smart");
		assert.equal(tierToModelMode("heavy"), "deep");
		assert.equal(tierToModelMode("unknown"), "smart");
	});
});
