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

const here = dirname(fileURLToPath(import.meta.url));
const sfRoot = join(here, "..");

function readSfFile(relativePath) {
	return readFileSync(join(sfRoot, relativePath), "utf8");
}

function assertLocalFirstGithubCodeSearchPolicy(relativePath) {
	const content = readSfFile(relativePath);

	assert.match(content, /GitHub code search/i);
	assert.match(content, /remote-only fallback/i);
	assert.match(content, /\/search\/code/);
	assert.match(content, /git grep/);
	assert.match(content, /\brg\b/);
	assert.match(content, /sift_search/);
	assert.match(content, /codebase_search/);
	assert.match(content, /code_search/);
	assert.match(content, /403/);
}

test("research_prompts_when_repo_is_local_prefer_local_search_over_github_code_search", () => {
	for (const relativePath of [
		"prompts/research-slice.md",
		"prompts/guided-research-slice.md",
		"skills/researcher/SKILL.md",
	]) {
		assertLocalFirstGithubCodeSearchPolicy(relativePath);
	}
});

test("top_level_prompts_when_scouting_code_warn_about_github_code_search_quota", () => {
	for (const relativePath of [
		"prompts/system.md",
		"prompts/discuss.md",
		"prompts/discuss-headless.md",
	]) {
		assertLocalFirstGithubCodeSearchPolicy(relativePath);
	}
});
