import "@testing-library/jest-dom";
import { beforeEach, vi } from "vitest";

// Provide a default fetch mock so components that call API endpoints on mount
// don't throw URL-parse errors in the jsdom test environment.  Tests that need
// specific responses should mock the relevant `api.*` method as usual.
beforeEach(() => {
	vi.stubGlobal(
		"fetch",
		vi.fn((input: string | URL | Request) => {
			const url = typeof input === "string" ? input : input.toString();
			// Endpoints that return arrays need [] not {} to avoid "not iterable" errors.
			const arrayEndpoints = ["/agents", "/agents/config"];
			const body = arrayEndpoints.some((ep) => url.endsWith(ep))
				? JSON.stringify([])
				: JSON.stringify({});
			return Promise.resolve(new Response(body, { status: 200 }));
		}),
	);
});
