// @vitest-environment jsdom
import { act } from 'react';
import { createRoot } from 'react-dom/client';
import { afterEach, expect, it, vi } from 'vitest';
import { MitreMatrixLibrary } from '../../src/features/project-import/MitreMatrixLibrary';
afterEach(() => vi.unstubAllGlobals());
it('offers automatic MITRE update, original download, and manual JSON fallback', async () => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
const onRefresh = vi.fn();
const onDownload = vi.fn();
const onUpload = vi.fn();
const onReset = vi.fn();
const container = document.createElement('div');
document.body.append(container);
const root = createRoot(container);
try {
await act(async () => root.render());
expect(container.textContent).toContain('v19.2 Active');
await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === 'Update from MITRE')!.click());
await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === 'Download from MITRE')!.click());
expect(onRefresh).toHaveBeenCalledOnce();
expect(onDownload).toHaveBeenCalledOnce();
expect(container.textContent).toContain('Import JSON file');
const input = container.querySelector('input[type="file"]')!;
const uploadButton = container.querySelector('.datahub-mitre-upload-btn')!;
expect(uploadButton.getAttribute('tabindex')).toBe('0');
const fileClick = vi.spyOn(input, 'click');
await act(async () => uploadButton.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })));
expect(fileClick).toHaveBeenCalledOnce();
await act(async () => input.dispatchEvent(new Event('change', { bubbles: true })));
expect(onUpload).toHaveBeenCalledOnce();
expect(container.textContent).not.toContain('Use active catalog');
await act(async () => root.render());
expect(container.textContent).toContain('Custom v19.1');
await act(async () => [...container.querySelectorAll('button')].find(button => button.textContent === 'Use active catalog')!.click());
expect(onReset).toHaveBeenCalledOnce();
} finally {
await act(async () => root.unmount());
container.remove();
}
});