// #region TestRunDetailPageHelpers [C:3] [TYPE Module] [SEMANTICS test,frontend,validation,run-detail,helpers]
// @BRIEF Verify +page.ts helper functions (extractFromRaw, isPathA) handle all edge cases
//   including null/undefined raw_response, malformed JSON, and path detection.
// @RELATION BINDS_TO -> [ValidationRunDetailPageLoad]
// @TEST_EDGE: null_raw_response -> extractFromRaw returns []
// @TEST_EDGE: malformed_json_raw_response -> extractFromRaw returns []
// @TEST_EDGE: empty_screenshot_paths -> isPathA returns false
// @TEST_EDGE: execution_path_screenshot -> isPathA returns true
// @TEST_EDGE: execution_path_null_with_screenshots -> isPathA returns true (backward compat)
// @TEST_INVARIANT: path_a_requires_screenshots -> VERIFIED_BY: test_is_path_a_with_screenshot_paths, test_is_path_a_with_execution_path_screenshot
// @TEST_INVARIANT: path_b_is_default -> VERIFIED_BY: test_is_path_b_default, test_is_path_b_text_only

import { describe, it, expect } from 'vitest';

// ── Pure helper functions (replicated from +page.ts for unit testing) ──

function extractFromRaw(rec: Record<string, unknown>, field: string): unknown[] {
	const val = rec[field];
	if (val && (Array.isArray(val) ? (val as unknown[]).length > 0 : true)) {
		return val as unknown[];
	}
	try {
		const raw = rec.raw_response;
		const parsed = typeof raw === 'string' ? JSON.parse(raw) : (raw || {});
		return (parsed as Record<string, unknown>)[field] as unknown[] || [];
	} catch {
		return [];
	}
}

function isPathA(rec: Record<string, unknown>): boolean {
	if (rec.path === 'A') return true;
	if (rec.execution_path === 'screenshot' || rec.execution_path === 'multimodal') return true;
	const shots = extractFromRaw(rec, 'screenshot_paths');
	return Array.isArray(shots) && shots.length > 0;
}


// #region TestExtractFromRaw [C:2] [TYPE Class]
// @BRIEF Verify extractFromRaw handles all edge cases for field extraction from records.
describe('extractFromRaw', () => {
	// #region test_returns_top_level_field_when_present [C:2] [TYPE Function]
	it('returns top-level field when present', () => {
		const rec = { screenshot_paths: ['/path/a.webp', '/path/b.webp'] };
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual(['/path/a.webp', '/path/b.webp']);
	});
	// #endregion

	it('returns non-array top-level field as-is', () => {
		const rec = { execution_path: 'screenshot' };
		expect(extractFromRaw(rec, 'execution_path')).toBe('screenshot');
	});

	it('falls back to raw_response JSON string', () => {
		const rec = {
			screenshot_paths: [],
			raw_response: JSON.stringify({ screenshot_paths: ['/from/raw.webp'] }),
		};
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual(['/from/raw.webp']);
	});

	it('falls back to raw_response object', () => {
		const rec = {
			screenshot_paths: [],
			raw_response: { screenshot_paths: ['/from/obj.webp'] },
		};
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual(['/from/obj.webp']);
	});

	it('returns empty array for null raw_response', () => {
		const rec = { raw_response: null };
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual([]);
	});

	it('returns empty array for undefined raw_response', () => {
		const rec = {};
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual([]);
	});

	it('returns empty array for malformed JSON', () => {
		const rec = { raw_response: '{invalid json!!!' };
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual([]);
	});

	it('falls back when top-level is empty array', () => {
		const rec = {
			screenshot_paths: [],
			raw_response: JSON.stringify({ screenshot_paths: ['/fallback.webp'] }),
		};
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual(['/fallback.webp']);
	});

	it('returns empty when field missing from both locations', () => {
		const rec = { raw_response: JSON.stringify({ other_field: 'value' }) };
		expect(extractFromRaw(rec, 'screenshot_paths')).toEqual([]);
	});
});
// #endregion TestExtractFromRaw


// #region TestIsPathA [C:2] [TYPE Class]
describe('isPathA', () => {
	it('returns true for execution_path=screenshot', () => {
		expect(isPathA({ execution_path: 'screenshot' })).toBe(true);
	});

	it('returns true for execution_path=multimodal', () => {
		expect(isPathA({ execution_path: 'multimodal' })).toBe(true);
	});

	it('returns true for path=A', () => {
		expect(isPathA({ path: 'A' })).toBe(true);
	});

	it('returns true when screenshot_paths has entries', () => {
		expect(isPathA({ screenshot_paths: ['/a.webp'] })).toBe(true);
	});

	it('returns true when screenshot_paths in raw_response', () => {
		expect(isPathA({
			raw_response: JSON.stringify({ screenshot_paths: ['/from/raw.webp'] }),
		})).toBe(true);
	});

	it('returns false for record with no path indicators', () => {
		expect(isPathA({})).toBe(false);
	});

	it('returns false for execution_path=text_only', () => {
		expect(isPathA({ execution_path: 'text_only' })).toBe(false);
	});

	it('returns false for empty screenshot_paths', () => {
		expect(isPathA({ screenshot_paths: [] })).toBe(false);
	});

	it('returns true when execution_path is null but screenshots exist', () => {
		expect(isPathA({
			execution_path: null,
			screenshot_paths: ['/old/screenshot.webp'],
		})).toBe(true);
	});
});
// #endregion TestIsPathA

// #endregion TestRunDetailPageHelpers
