// 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)
import { describe, it } from '@jest/globals';
import '@testing-library/jest-dom';
import { screen } from '@testing-library/react';
import type { Node } from '@xyflow/react';
import { renderWithProviders } from '../../../../test-utils/test-utils.tsx';
import StartNode, {
  StartConnectionSource, StartConnectionTarget,
  StartReduce
} from '../../../../../src/pages/VisProgPage/visualProgrammingUI/nodes/StartNode';


describe('StartNode', () => {


  describe('Rendering', () => {
    it('renders the StartNode correctly', () => {
      const mockNode: Node = {
        id: 'start-1',
        type: 'start', // TypeScript now knows this is a string
        position: { x: 0, y: 0 },
        data: {
          label: 'Start Node',
          droppable: false,
          hasReduce: true,
        },
      };

      renderWithProviders(
        <StartNode
            id={mockNode.id}
            type={mockNode.type!} // <--- fix here
            data={mockNode.data as any}
            selected={false}
            isConnectable={true}
            zIndex={0}
            dragging={false}
            selectable={true}
            deletable={false}
            draggable={true}
            positionAbsoluteX={0}
            positionAbsoluteY={0}
        />
        );


        expect(screen.getByText('Start')).toBeInTheDocument();

        // The handle should exist in the DOM
        expect(document.querySelector('[data-handleid="source"]')).toBeInTheDocument();

    });
  });

  describe('StartReduce Function', () => {
    it('reduces the StartNode to its minimal structure', () => {
      const mockNode: Node = {
        id: 'start-1',
        type: 'start',
        position: { x: 0, y: 0 },
        data: {
          label: 'Start Node',
          droppable: false,
          hasReduce: true,
        },
      };

      const result = StartReduce(mockNode, [mockNode]);
      expect(result).toEqual({ id: 'start-1' });
    });
  });

  describe('StartConnects Function', () => {
    it('handles connections without throwing', () => {
      const startNode: Node = {
        id: 'start-1',
        type: 'start',
        position: { x: 0, y: 0 },
        data: {
          label: 'Start Node',
          droppable: false,
          hasReduce: true,
        },
      };

      const otherNode: Node = {
        id: 'norm-1',
        type: 'norm',
        position: { x: 100, y: 0 },
        data: {
          label: 'Norm Node',
          droppable: true,
          norm: 'test',
          hasReduce: true,
        },
      };

      expect(() => StartConnectionSource(startNode, otherNode.id)).not.toThrow();
      expect(() => StartConnectionTarget(startNode, otherNode.id)).not.toThrow();
    });
  });
});
