import { describe, expect, it } from "vitest";
import { ClientResponseError } from "./client";
import { classifyRepoAvailability } from "./repoIndex";

const unsupported = () =>
	new ClientResponseError({ status: 404, data: { error: "XRPCNotSupported" } });

describe("classifyRepoAvailability", () => {
	it("recognizes a legacy knot that does not expose repository endpoints", () => {
		const result = classifyRepoAvailability(
			[unsupported(), unsupported(), unsupported()].map((cause) => ({ value: null, error: cause }))
		);

		expect(result).toEqual({ needsUpgrade: true, knotUnreachable: false });
	});

	it("keeps transport failures as unreachable", () => {
		const result = classifyRepoAvailability(
			["tree", "log", "branches"].map(() => ({ value: null, error: new TypeError("offline") }))
		);

		expect(result).toEqual({ needsUpgrade: false, knotUnreachable: true });
	});

	it("does not mistake a missing ref for an unavailable knot", () => {
		const result = classifyRepoAvailability([
			{ value: null, error: unsupported() },
			{ value: null, error: unsupported() },
			{ value: { branches: [] }, error: null }
		]);

		expect(result).toEqual({ needsUpgrade: false, knotUnreachable: false });
	});
});
