import assert from "node:assert/strict";
import { test } from "vitest";

const { filterInitialSfHeader } = await import(
	"../../web/lib/initial-sf-header-filter.ts"
);

const SF_LOGO_LINES = [
	"   ██████╗ ███████╗██████╗ ",
	"  ██╔════╝ ██╔════╝██╔══██╗",
	"  ██║  ███╗███████╗██║  ██║",
	"  ██║   ██║╚════██║██║  ██║",
	"  ╚██████╔╝███████║██████╔╝",
	"   ╚═════╝ ╚══════╝╚═════╝ ",
] as const;

test("filterInitialSfHeader strips a plain startup banner and keeps real terminal content", () => {
	const warning = "Warning: Google Search is not configured.";
	const raw = [
		...SF_LOGO_LINES,
		"  Singularity Forge v2.33.1",
		"",
		warning,
	].join("\n");

	const result = filterInitialSfHeader(raw);

	assert.equal(result.status, "matched");
	assert.equal(result.text, warning);
});

test("filterInitialSfHeader strips ANSI-colored startup banner output", () => {
	const cyan = "\u001b[36m";
	const reset = "\u001b[39m";
	const bold = "\u001b[1m";
	const boldReset = "\u001b[22m";
	const dim = "\u001b[2m";
	const dimReset = "\u001b[22m";
	const warning = "Warning: terminal content starts here.\r\n";

	const raw =
		SF_LOGO_LINES.map((line) => `${cyan}${line}${reset}\r\n`).join("") +
		`  ${bold}Singularity Forge${boldReset} ${dim}v2.33.1${dimReset}\r\n\r\n` +
		warning;

	const result = filterInitialSfHeader(raw);

	assert.equal(result.status, "matched");
	assert.equal(result.text, warning);
});

test("filterInitialSfHeader waits for more data when the startup banner is incomplete", () => {
	const partial = `${SF_LOGO_LINES[0]}\n${SF_LOGO_LINES[1]}\n${SF_LOGO_LINES[2]}`;

	const result = filterInitialSfHeader(partial);

	assert.deepEqual(result, { status: "needs-more", text: "" });
});

test("filterInitialSfHeader passes normal terminal output through untouched", () => {
	const raw = "Warning: already in the shell\r\n$ ";

	const result = filterInitialSfHeader(raw);

	assert.equal(result.status, "passthrough");
	assert.equal(result.text, raw);
});
