import { beforeEach, describe, expect, it, vi } from 'vitest';
import { load } from './+page.server';

const mockApi = { GET: vi.fn() };

vi.mock('$lib/shared/api.server', () => ({
	createApiClient: () => mockApi,
	extractErrorCode: (e: unknown) => (e as { code?: string } | undefined)?.code
}));

beforeEach(() => vi.clearAllMocks());

describe('admin/ocr — load', () => {
	it('returns trainingInfo from API', async () => {
		mockApi.GET.mockResolvedValue({
			response: { ok: true },
			data: { availableBlocks: 10, ocrServiceAvailable: true, senderModels: [] }
		});

		const result = (await load({
			fetch,
			request: new Request('http://localhost/admin/ocr'),
			url: new URL('http://localhost/admin/ocr')
		} as never))!;

		expect(result.trainingInfo.availableBlocks).toBe(10);
		expect(result.trainingInfo.ocrServiceAvailable).toBe(true);
	});

	it('throws 503 when OCR API call fails', async () => {
		mockApi.GET.mockResolvedValue({ response: { ok: false, status: 503 }, error: {} });

		await expect(
			load({
				fetch,
				request: new Request('http://localhost/admin/ocr'),
				url: new URL('http://localhost/admin/ocr')
			} as never)
		).rejects.toMatchObject({ status: 503 });
	});
});
