// #region FeatureGateTest.Fallback [C:2] [TYPE Module] [SEMANTICS test, ui, feature-gate, fallback]
// @BRIEF Unit tests for FeatureGate with empty i18n common — exercises default literal fallback strings.
// @RELATION BINDS_TO -> [FeatureGate]
// @TEST_EDGE: missing_i18n -> uses default literal when $t.common.feature_disabled is falsy
// @TEST_EDGE: missing_go_back -> uses default "Go back" fallback when $t.common.go_back is falsy

import { describe, it, expect } from 'vitest';
import { render } from '@testing-library/svelte';
import FeatureGate from '../FeatureGate.svelte';

// Mock i18n with empty common — forces fallback strings
vi.mock('$lib/i18n/index.svelte.js', () => {
	const tStore = {
		subscribe: (run: (v: unknown) => void) => {
			run({});  // empty common — no feature_disabled or go_back
			return () => {};
		},
	};
	return { t: tStore };
});

describe('FeatureGate — fallback strings', () => {
	it('uses default literal when $t.common.feature_disabled is missing', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'my_feature',
				features: { my_feature: false },
			},
		});
		expect(container.textContent).toContain('Feature "my_feature" is disabled.');
	});

	it('uses default "Go back" when $t.common.go_back is missing', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'my_feature',
				features: { my_feature: false },
				redirect: '/home',
			},
		});
		expect(container.textContent).toContain('Go back');
		const link = container.querySelector('a');
		expect(link!.getAttribute('href')).toBe('/home');
	});

	it('renders children when feature is enabled with empty i18n', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'my_feature',
				features: { my_feature: true },
			},
		});
		expect(container.textContent).not.toContain('disabled');
	});

	it('shows disabled message when feature is false with empty i18n', () => {
		const { container } = render(FeatureGate, {
			props: {
				feature: 'other_feature',
				features: { other_feature: false },
			},
		});
		expect(container.textContent).toContain('disabled.');
	});
});
// #endregion FeatureGateTest.Fallback
