/**
 * @jest-environment jsdom
 */

const fs = require("fs");
const path = require("path");

describe("calculate() row223 Ellbow-Rotation Tests", () => {

    let calculate;
    let optimizeRobot;

    beforeEach(() => {
        // DOM erzeugen
        document.body.innerHTML = `<textarea id="analysis-log"></textarea>`;

        // Fetch mocken - wird pro Test konfiguriert
        global.fetch = jest.fn();

        // Modul erst JETZT laden (DOM existiert)
        ({ calculate, optimizeRobot} = require("../public/calculateAngles.js"));
    });

    test('berechnet y-Durchschnitt für Base / Arm1 / Joint1', async () => {
        const markersPath = path.resolve('./test/snapshots/snapshot_video0_1778407171886_two_cam.json');
        const robotPath   = path.resolve('./public/robot.json');

        const foundMarkers = JSON.parse(fs.readFileSync(markersPath, 'utf8'));
        const jsonRobot    = JSON.parse(fs.readFileSync(robotPath, 'utf8'));

        const result = await calculate(foundMarkers, jsonRobot);

        expect(jsonRobot.recognized.y).toBeDefined();
        expect(jsonRobot.Joints["jointD"].origin).toBeDefined();
        expect(jsonRobot.Joints["jointD"].origin[0]).toBeDefined();
        expect(result.status).toBe('ok');
        expect(result.result).toBeDefined();
    });


    test('berechnet X-Durchschnitt für Base / Arm1 / Joint1', async () => {
        const markersPath = path.resolve('./test/snapshots/snapshot_video0_1775406055428_two_cam.json');
        const robotPath   = path.resolve('./public/robot.json');

        const foundMarkers = JSON.parse(fs.readFileSync(markersPath, 'utf8'));
        const jsonRobot    = JSON.parse(fs.readFileSync(robotPath, 'utf8'));

        const result = await calculate(foundMarkers, jsonRobot);

        expect(result.status).toBe('ok');
        expect(result.result).toBeDefined();
    });
});
