// This program has been developed by students from the bachelor Computer Science at Utrecht
// University within the Software Project course.
// © Copyright Utrecht University (Department of Information and Computing Sciences)
// Adds jest-dom matchers for React testing library
import '@testing-library/jest-dom';

// Minimal browser API mocks for the test environment.
// Fetch
if (!globalThis.fetch) {
  globalThis.fetch = jest.fn(async () => ({
    ok: true,
    status: 200,
    json: async () => [],
    text: async () => '',
  })) as unknown as typeof fetch;
}

// EventSource
if (!globalThis.EventSource) {
  class MockEventSource {
    url: string;
    readyState = 1;
    onmessage: ((event: MessageEvent) => void) | null = null;
    onerror: ((event: Event) => void) | null = null;
    onopen: ((event: Event) => void) | null = null;

    constructor(url: string) {
      this.url = url;
    }

    close() {
      this.readyState = 2;
    }

    addEventListener(type: string, listener: (event: MessageEvent) => void) {
      if (type === 'message') {
        this.onmessage = listener;
      }
    }

    removeEventListener(type: string, listener: (event: MessageEvent) => void) {
      if (type === 'message' && this.onmessage === listener) {
        this.onmessage = null;
      }
    }
  }

  globalThis.EventSource = MockEventSource as unknown as typeof EventSource;
}
