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

/**
 * Source-level regression test: the claude-cli onboarding path must persist
 * defaultProvider = 'claude-code' in settings.json so the user is not left
 * on the 'anthropic' direct-API provider after selecting Claude Code CLI.
 *
 * Without this, the auto-migration in cli.ts does not fire when the user
 * also has a stored Anthropic API key, leaving them on the wrong provider.
 */
test("onboarding claude-cli path persists defaultProvider to settings.json", () => {
	const source = readFileSync(
		join(import.meta.dirname, "..", "onboarding.ts"),
		"utf-8",
	);

	// The claude-cli branch must write defaultProvider = 'claude-code' to settings.json
	const blockStart = source.search(/method\s*===\s*["']claude-cli["']/);
	const blockEnd = source.indexOf("// ── Step 2", blockStart);
	const cliBlock = source.slice(blockStart, blockEnd);
	assert.ok(cliBlock.length > 0, "claude-cli block not found in onboarding.ts");
	assert.match(
		cliBlock,
		/raw\.defaultProvider\s*=\s*['"]claude-code['"]/,
		"claude-cli onboarding path must set defaultProvider = 'claude-code' in settings.json",
	);
});
