/**
 * Regression test: don't show an Ollama footer status unless Ollama is
 * actually usable (running with at least one discovered model).
 */

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

const __dirname = dirname(fileURLToPath(import.meta.url));
const src = readFileSync(join(__dirname, "index.ts"), "utf-8");

test("probeAndRegister returns false when no Ollama models are discovered", () => {
	assert.match(
		src,
		/if \(models\.length === 0\)[\s\S]*return false;/,
		"running-without-models should not be treated as available",
	);
});

test("interactive session clears ollama footer status when unavailable", () => {
	assert.match(
		src,
		/ctx\.ui\.setStatus\("ollama", found \? "Ollama" : undefined\)/,
		"status should be cleared when probeAndRegister reports unavailable",
	);
});

test("registration is gated on OLLAMA_HOST being set", () => {
	// Top-level guard in the default export — no probe, no commands, no tool
	// registration happens without an explicit OLLAMA_HOST. This makes the
	// extension opt-in for users who never run local Ollama.
	assert.match(
		src,
		/export default function ollama\([\s\S]*?if \(!isOllamaConfigured\(\)\) return;/,
		"default export should short-circuit when OLLAMA_HOST is unset",
	);
});

test("probeAndRegister bails out before hitting the network when unconfigured", () => {
	assert.match(
		src,
		/async function probeAndRegister\([\s\S]*?if \(!isOllamaConfigured\(\)\) return false;/,
		"probeAndRegister should skip client.isRunning() when OLLAMA_HOST is unset",
	);
});

test("isOllamaConfigured keys off OLLAMA_HOST env var", () => {
	assert.match(
		src,
		/function isOllamaConfigured\(\)[\s\S]*?process\.env\.OLLAMA_HOST/,
		"configuration check should read OLLAMA_HOST",
	);
});
