// This file was moved from the `server` package to here and `server` has `strictNullChecks` disabled.
// This code was originally written with the assumption that `widget.children` is never undefined, despite the type saying otherwise.
// So we are using `!` to assert that `widget.children` is never undefined.
// Note that `!` does not change the runtime behavior of the code so the code will work the same way as before.

/* eslint-disable @typescript-eslint/no-explicit-any */

import { cloneDeep } from 'lodash';
import { describe, test, expect } from 'vitest';

import with2ModalsAppV6DSL from '../../../utils/pageFixtures/v6/with_2_modals.js';
import { adjustModalCanvases } from './adjustModalCanvases.js';
import { migrate6to7 } from './v6to7.js';

describe('adjustModalCanvases', () => {
  test('should update a modals child canvas to have the correct height unless its children are taller', () => {
    const v8 = adjustModalCanvases(cloneDeep(with2ModalsAppV6DSL as any));

    const mainCanvas = v8;
    expect(mainCanvas.widgetName).toEqual('MainContainer');
    expect(mainCanvas.children![0].widgetName).toEqual('Modal1');

    const modalCanvas = mainCanvas.children![0].children![0];
    expect(modalCanvas.widgetName).toEqual('Canvas1');
    expect(modalCanvas.bottomRow).toEqual(31);

    // This second one has a widget that is taller than the canvas
    // so that should be the height of the canvas
    const modalCanvas2 = mainCanvas.children![2].children![0];
    expect(modalCanvas2.widgetName).toEqual('Canvas2');
    expect(modalCanvas2.bottomRow).toEqual(32);
  });

  test('FULL - should update a modals child canvas to have the correct height unless its children are taller', () => {
    const migrated = migrate6to7(with2ModalsAppV6DSL as any);

    expect(migrated.children![0].widgetName).toEqual('Section1');

    const mainCanvas = migrated.children![0].children![0];
    expect(mainCanvas.widgetName).toEqual('Canvas3');
    expect(mainCanvas.children![0].widgetName).toEqual('Modal1');
    expect(mainCanvas.children![0].children![0].widgetName).toEqual('Body1');
    const modalCanvas = mainCanvas.children![0].children![0].children![0];
    expect(modalCanvas.widgetName).toEqual('Canvas1');
    expect(modalCanvas.height).toEqual({ mode: 'gridUnit', value: 31 });

    // This second one has a widget that is taller than the canvas
    // so that should be the height of the canvas
    const modalCanvas2 = mainCanvas.children![2].children![0].children![0];
    expect(modalCanvas2.widgetName).toEqual('Canvas2');
    expect(modalCanvas2.height).toEqual({ mode: 'gridUnit', value: 32 });
  });
});
