import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { test } from "vitest";

import {
	DIRECT_SF_COMMAND_NAMES,
	getSfTopLevelCommandCompletions,
} from "../commands/catalog.js";
import { registerSFCommands } from "../commands/index.js";

test("direct SF command surface registers workflow verbs without legacy sf namespace", () => {
	const registered = [];
	const pi = {
		registerCommand(name, options) {
			registered.push({ name, options });
		},
	};

	registerSFCommands(pi);
	const names = registered.map((entry) => entry.name).sort();

	assert.ok(names.includes("autonomous"));
	assert.ok(names.includes("plan"));
	assert.ok(names.includes("schedule"));
	assert.ok(names.includes("doctor"));
	assert.ok(names.includes("status"));
	assert.ok(!names.includes("sf"));
	assert.ok(!names.includes("stop"));
	assert.deepEqual(names, [...DIRECT_SF_COMMAND_NAMES].sort());
});

test("direct command completions strip the already typed command name", () => {
	assert.deepEqual(getSfTopLevelCommandCompletions("autonomous", "--"), [
		{
			value: "--verbose",
			label: "--verbose",
			description: "Show detailed execution output",
		},
		{
			value: "--debug",
			label: "--debug",
			description: "Enable debug logging",
		},
	]);
});

test("extension_manifest_uses_permission_profile_command_name", () => {
	const manifest = JSON.parse(
		readFileSync(
			join(
				process.cwd(),
				"src/resources/extensions/sf/extension-manifest.json",
			),
			"utf8",
		),
	);
	const commands = manifest.provides?.commands ?? [];
	assert.equal(commands.includes("permission-profile"), true);
	assert.equal(commands.includes("trust"), false);
});

test("human_facing_cli_help_when_describing_sf_surfaces_uses_direct_commands", () => {
	const sourceFiles = ["src/help-text.ts", "src/cli.ts", "src/loader.ts"];
	for (const sourceFile of sourceFiles) {
		const source = readFileSync(join(process.cwd(), sourceFile), "utf8");
		assert.equal(
			source.includes("Machine surface for /sf"),
			false,
			`${sourceFile} must describe headless as the direct-command machine surface`,
		);
		assert.equal(
			source.includes("Run /sf autonomous"),
			false,
			`${sourceFile} must not advertise /sf autonomous`,
		);
		assert.equal(
			source.includes("Manage: /sf schedule list"),
			false,
			`${sourceFile} must not advertise the removed slash namespace`,
		);
	}
});
