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

import {
	buildSection,
	END,
	parseBundledExtensions,
	parseKnownProviders,
	parseSearchProviders,
	parseSfNativeTools,
	START,
	updateFeaturesContent,
} from "../../scripts/generate-features-inventory.mjs";

test("features inventory generator surfaces expected SF native tool, extension, search, and provider inventories", () => {
	const extensions = parseBundledExtensions();
	const sfNativeTools = parseSfNativeTools();
	const searchProviders = parseSearchProviders();
	const knownProviders = parseKnownProviders();

	assert.ok(sfNativeTools.includes("plan_milestone"));
	assert.ok(sfNativeTools.includes("complete_task"));
	assert.ok(sfNativeTools.includes("validate_milestone"));
	assert.ok(!sfNativeTools.includes("capture_thought"));

	assert.ok(extensions.includes("sf"));
	assert.ok(extensions.includes("search-the-web"));
	assert.ok(extensions.includes("guardrails"));

	assert.deepEqual(searchProviders, [
		"brave",
		"exa",
		"ollama",
		"serper",
		"tavily",
	]);

	assert.ok(knownProviders.includes("mistral"));
	assert.ok(knownProviders.includes("zai"));
	assert.ok(knownProviders.includes("kimi-coding"));
	assert.ok(knownProviders.includes("xiaomi"));
});

test("features inventory generator injects a rendered appendix between markers", () => {
	const original = ["# FEATURES", "", "Intro text.", "", START, END, ""].join(
		"\n",
	);

	const updated = updateFeaturesContent(original);
	const generated = buildSection();

	assert.match(
		updated,
		new RegExp(
			`${START.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\n\\n### SF Native Tools`,
		),
	);
	assert.match(updated, /### SF Native Tools/);
	assert.match(updated, /### Bundled Extensions/);
	assert.match(updated, /### Search Providers/);
	assert.match(updated, /### Known Model Providers/);
	assert.match(updated, /- `search-the-web` — \[extension-manifest\.json]/);
	assert.match(updated, /- `plan_milestone`/);
	assert.match(updated, /- `brave`/);
	assert.match(updated, /- `xiaomi`/);
	assert.ok(updated.includes(generated));
	assert.ok(updated.includes(END));
});

test("features inventory generator fails closed when markers are missing", () => {
	assert.throws(
		() => updateFeaturesContent("# FEATURES\n\nNo markers here.\n"),
		/FEATURES\.md is missing generated inventory markers/,
	);
});
