import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render } from 'vitest-browser-svelte';
import StammbaumAffordance from './StammbaumAffordance.svelte';

const STORAGE_KEY = 'stammbaumAffordanceDismissedAt';

describe('StammbaumAffordance (#692)', () => {
	beforeEach(() => localStorage.clear());

	it('shows the hint on a touch device that has not dismissed it', async () => {
		render(StammbaumAffordance, { touch: true });
		await vi.waitFor(() => expect(document.querySelector('[role="status"]')).not.toBeNull());
		expect(document.body.textContent).toContain('Ziehen');
	});

	it('does not show on non-touch devices (OQ-008)', async () => {
		render(StammbaumAffordance, { touch: false });
		expect(document.querySelector('[role="status"]')).toBeNull();
	});

	it('hides and records dismissal when the close button is clicked', async () => {
		render(StammbaumAffordance, { touch: true });
		const dismiss = [...document.querySelectorAll<HTMLButtonElement>('button')][0];
		dismiss.click();
		await vi.waitFor(() => expect(document.querySelector('[role="status"]')).toBeNull());
		expect(localStorage.getItem(STORAGE_KEY)).toBeTruthy();
	});

	it('does not reappear within the 30-day window (NFR-USE-001)', async () => {
		localStorage.setItem(STORAGE_KEY, String(Date.now()));
		render(StammbaumAffordance, { touch: true });
		expect(document.querySelector('[role="status"]')).toBeNull();
	});
});
