import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, test } from "vitest";

import {
	blockModel,
	isModelBlocked,
	loadBlockedModels,
} from "../blocked-models.js";

let tmp;

afterEach(() => {
	if (tmp) rmSync(tmp, { recursive: true, force: true });
	tmp = undefined;
});

function tempProject() {
	tmp = mkdtempSync(join(tmpdir(), "sf-blocked-models-"));
	return tmp;
}

describe("blocked models", () => {
	test("isModelBlocked_when_temporary_block_expired_returns_false", () => {
		const basePath = tempProject();

		blockModel(basePath, "google-gemini-cli", "gemini-2.5-pro", "capacity", {
			expiresAt: Date.now() - 1_000,
		});

		assert.equal(
			isModelBlocked(basePath, "google-gemini-cli", "gemini-2.5-pro"),
			false,
		);
		assert.deepEqual(loadBlockedModels(basePath), []);
	});

	test("isModelBlocked_when_temporary_block_active_returns_true", () => {
		const basePath = tempProject();

		blockModel(basePath, "google-gemini-cli", "gemini-2.5-pro", "capacity", {
			expiresAt: Date.now() + 60_000,
		});

		assert.equal(
			isModelBlocked(basePath, "google-gemini-cli", "gemini-2.5-pro"),
			true,
		);
	});
});
