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

test("cli.ts routes top-level schedule before interactive TUI", () => {
	const cliSource = readFileSync(join(__dirname, "..", "cli.ts"), "utf-8");
	const loaderSource = readFileSync(
		join(__dirname, "..", "loader.ts"),
		"utf-8",
	);
	const scheduleBranch = cliSource.indexOf(
		'if (cliFlags.messages[0] === "schedule")',
	);
	const interactiveMode = cliSource.indexOf("new InteractiveMode");

	assert.notEqual(scheduleBranch, -1, "top-level schedule branch must exist");
	assert.notEqual(
		interactiveMode,
		-1,
		"interactive mode construction must exist",
	);
	assert.ok(
		scheduleBranch < interactiveMode,
		"sf schedule must route before the interactive TUI path",
	);
	assert.ok(
		cliSource.includes("handleSchedule"),
		"top-level schedule branch must reuse the schedule handler",
	);
	assert.ok(
		cliSource.includes("process.argv.slice(3)"),
		"schedule branch must pass argv tokens so command payloads survive top-level parsing",
	);
	assert.ok(
		loaderSource.includes('firstArg !== "schedule"'),
		"loader schedule banner must stay quiet for top-level schedule commands",
	);
	assert.ok(
		loaderSource.includes("scheduled autonomous dispatch item"),
		"loader banner must distinguish autonomous schedule entries from passive reminders",
	);
});
