import assert from "node:assert/strict";
import { describe, it } from 'vitest';
import { getEnvApiKey } from "./env-api-keys.js";

describe("getEnvApiKey", () => {
	it("uses GEMINI_API_KEY for google when present", () => {
		const savedGemini = process.env.GEMINI_API_KEY;
		const savedGoogleGenerative = process.env.GOOGLE_GENERATIVE_AI_API_KEY;

		process.env.GEMINI_API_KEY = "gemini-key";
		process.env.GOOGLE_GENERATIVE_AI_API_KEY = "google-generative-key";

		try {
			assert.equal(getEnvApiKey("google"), "gemini-key");
		} finally {
			if (savedGemini === undefined) delete process.env.GEMINI_API_KEY;
			else process.env.GEMINI_API_KEY = savedGemini;
			if (savedGoogleGenerative === undefined) delete process.env.GOOGLE_GENERATIVE_AI_API_KEY;
			else process.env.GOOGLE_GENERATIVE_AI_API_KEY = savedGoogleGenerative;
		}
	});

	it("accepts models.dev GOOGLE_GENERATIVE_AI_API_KEY for google", () => {
		const savedGemini = process.env.GEMINI_API_KEY;
		const savedGoogleGenerative = process.env.GOOGLE_GENERATIVE_AI_API_KEY;

		delete process.env.GEMINI_API_KEY;
		process.env.GOOGLE_GENERATIVE_AI_API_KEY = "google-generative-key";

		try {
			assert.equal(getEnvApiKey("google"), "google-generative-key");
		} finally {
			if (savedGemini === undefined) delete process.env.GEMINI_API_KEY;
			else process.env.GEMINI_API_KEY = savedGemini;
			if (savedGoogleGenerative === undefined) delete process.env.GOOGLE_GENERATIVE_AI_API_KEY;
			else process.env.GOOGLE_GENERATIVE_AI_API_KEY = savedGoogleGenerative;
		}
	});
});
