import assert from "node:assert/strict";
import test from "node:test";

import { createExaToolApiOverride, mapExaToolName, prefixExaTool } from "../extensions/exa-tool-prefix.ts";

test("Exa tools use a provider prefix followed by the original semantic name", () => {
	assert.equal(mapExaToolName("web_search_exa"), "exa_web_search");
	assert.equal(mapExaToolName("web_fetch_exa"), "exa_web_fetch");
	assert.equal(mapExaToolName("web_search_advanced_exa"), "exa_web_search_advanced");
	assert.equal(mapExaToolName("unrelated_tool"), "unrelated_tool");

	const tool = prefixExaTool({ name: "web_search_exa", label: "MCP: web_search_exa" });
	assert.equal(tool.name, "exa_web_search");
	assert.equal(tool.label, "Exa Web Search");
});

test("Exa API override keeps registered and active tool names aligned", () => {
	const registered: Array<{ name: string; label: string }> = [];
	let active: string[] = [];
	const unregistered: string[] = [];
	const api = createExaToolApiOverride({
		registerTool(tool: unknown) {
			registered.push(tool as { name: string; label: string });
		},
		setActiveTools(names: string[]) {
			active = names;
		},
		unregisterTool(name: string) {
			unregistered.push(name);
			return true;
		},
	});

	api.registerTool({ name: "web_fetch_exa", label: "MCP: web_fetch_exa" });
	api.registerTool({ name: "codegraph_explore", label: "MCP: codegraph_explore" });
	api.setActiveTools(["read", "web_fetch_exa", "web_fetch_exa", "codegraph_explore"]);
	api.unregisterTool("web_fetch_exa");
	api.unregisterTool("codegraph_explore");

	assert.equal(registered[0]?.name, "exa_web_fetch");
	assert.equal(registered[0]?.label, "Exa Web Fetch");
	assert.equal(registered[1]?.name, "codegraph_explore");
	assert.deepEqual(active, ["read", "exa_web_fetch", "codegraph_explore"]);
	assert.deepEqual(unregistered, ["exa_web_fetch", "codegraph_explore"]);
});
