/**
 * Tests for remote steering surface.
 */
import assert from "node:assert";
import { test } from "vitest";
import {
	applyRemoteSteeringDirectives,
	formatRemoteSteeringResults,
	parseRemoteSteeringDirectives,
} from "../remote-steering.js";

// --- Tests ---

const testParseNoDirectives = () => {
	const result = parseRemoteSteeringDirectives({ answers: ["yes", "no"] });
	assert.strictEqual(result.steering, false);
	assert.strictEqual(result.directives.length, 0);
};

const testParseModeDirective = () => {
	const result = parseRemoteSteeringDirectives({
		answers: ["/mode build", "proceed"],
	});
	assert.strictEqual(result.steering, true);
	assert.strictEqual(result.directives.length, 1);
	assert.strictEqual(result.directives[0].cmd, "mode");
	assert.strictEqual(result.directives[0].value, "build");
};

const testParseMultipleDirectives = () => {
	const result = parseRemoteSteeringDirectives({
		text: "/mode review /permission-profile trusted /model-mode deep",
	});
	assert.strictEqual(result.steering, true);
	assert.strictEqual(result.directives.length, 3);
	assert.deepStrictEqual(result.directives[0], {
		cmd: "mode",
		value: "review",
	});
	assert.deepStrictEqual(result.directives[1], {
		cmd: "permission-profile",
		value: "trusted",
	});
	assert.deepStrictEqual(result.directives[2], {
		cmd: "model-mode",
		value: "deep",
	});
};

const testParseStringAndFullAxisAliases = () => {
	const result = parseRemoteSteeringDirectives(
		"/work-mode repair /run-control autonomous /permission-profile trusted",
	);
	assert.strictEqual(result.steering, true);
	assert.deepStrictEqual(result.directives, [
		{ cmd: "mode", value: "repair" },
		{ cmd: "control", value: "autonomous" },
		{ cmd: "permission-profile", value: "trusted" },
	]);
};

const testParseRoundResultNotes = () => {
	const result = parseRemoteSteeringDirectives({
		answers: {
			mode_note: {
				selected: "Other",
				notes: "/mode build /permission-profile normal",
			},
		},
	});
	assert.strictEqual(result.steering, true);
	assert.deepStrictEqual(result.directives, [
		{ cmd: "mode", value: "build" },
		{ cmd: "permission-profile", value: "normal" },
	]);
};

const testParseInvalidDirectiveIgnored = () => {
	const result = parseRemoteSteeringDirectives({
		text: "/mode invalidmode /control autonomous",
	});
	assert.strictEqual(result.steering, true);
	assert.strictEqual(result.directives.length, 1);
	assert.strictEqual(result.directives[0].cmd, "control");
};

const testApplyDirectives = () => {
	// This will fail if getAutoSession returns null (no session)
	// In test environment without a full session, we just verify it doesn't throw
	try {
		const results = applyRemoteSteeringDirectives([
			{ cmd: "mode", value: "build" },
		]);
		// If session exists, should apply
		assert.ok(Array.isArray(results));
	} catch (err) {
		// Expected in test environment without session
		assert.ok(
			err.message.includes("getAutoSession") || err.message.includes("null"),
		);
	}
};

const testFormatResults = () => {
	const results = [
		{ cmd: "mode", value: "build", applied: true },
		{
			cmd: "permission-profile",
			value: "trusted",
			applied: false,
			error: "test error",
		},
	];
	const formatted = formatRemoteSteeringResults(results);
	assert.ok(formatted.includes("SF Mode Steering"));
	assert.ok(formatted.includes("[ok] /mode build"));
	assert.ok(formatted.includes("[blocked] /permission-profile trusted"));
};

test(
	"parseRemoteSteeringDirectives_without_directives_returns_false",
	testParseNoDirectives,
);
test(
	"parseRemoteSteeringDirectives_accepts_mode_directive",
	testParseModeDirective,
);
test(
	"parseRemoteSteeringDirectives_accepts_multiple_axes",
	testParseMultipleDirectives,
);
test(
	"parseRemoteSteeringDirectives_accepts_string_and_full_axis_aliases",
	testParseStringAndFullAxisAliases,
);
test(
	"parseRemoteSteeringDirectives_accepts_round_result_notes",
	testParseRoundResultNotes,
);
test(
	"parseRemoteSteeringDirectives_ignores_invalid_values",
	testParseInvalidDirectiveIgnored,
);
test(
	"applyRemoteSteeringDirectives_returns_results_or_session_error",
	testApplyDirectives,
);
test(
	"formatRemoteSteeringResults_renders_current_mode_summary",
	testFormatResults,
);
