Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

28.3 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// @vitest-environment jsdom

import type { IWorkbookData, Workbook } from '@univerjs/core';
import {
    DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
    FOCUSING_FX_BAR_EDITOR,
    FOCUSING_SHEET,
    ICommandService,
    IContextService,
    Injector,
    IUniverInstanceService,
    Univer,
    UniverInstanceType,
} from '@univerjs/core';
import { DocSelectionRenderService } from '@univerjs/docs-ui';
import { DeviceInputEventType, IRenderManagerService, SHEET_VIEWPORT_KEY } from '@univerjs/engine-render';
import { ClearSelectionFormatCommand, SetWorksheetActiveOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { DISABLE_AUTO_FOCUS_KEY, IPlatformService, PlatformService } from '@univerjs/ui';
import { Subject } from 'rxjs';
import { describe, expect, it, onTestFinished, vi } from 'vitest';
import type { EmbedRuntimeFocusCoordinator } from '../../../services/sheet-embed-integration.service';
import { SetZoomRatioCommand } from '../../../commands/commands/set-zoom-ratio.command';
import { SetActivateCellEditOperation } from '../../../commands/operations/activate-cell-edit.operation';
import { SetCellEditVisibleOperation } from '../../../commands/operations/cell-edit.operation';
import { SHEET_VIEW_KEY } from '../../../common/keys';
import { IEditorBridgeService } from '../../../services/editor-bridge.service';
import {
    EMBED_INTERACTION_BOUNDARY_OWNER_ATTRIBUTE,
    ISheetEmbedRuntimeFocusCoordinator,
} from '../../../services/sheet-embed-integration.service';
import { SheetSkeletonManagerService } from '../../../services/sheet-skeleton-manager.service';
import { DesktopCellEditRenderController } from '../desktop-cell-edit.render-controller';
import { EditorBridgeRenderController } from '../editor-bridge.render-controller';

function createEventSubject() {
    const handlers = new Set<(evt: any) => void>();

    return {
        subscribeEvent: vi.fn((arg: any) => {
            const handler = typeof arg === 'function' ? arg : arg.next;
            handlers.add(handler);
            return { dispose: vi.fn(() => handlers.delete(handler)) };
        }),
        emit: (evt: any) => handlers.forEach((handler) => handler(evt)),
    };
}

function createCommandService() {
    const afterHandlers = new Set<(command: any, options?: any) => void>();
    const beforeHandlers = new Set<(command: any, options?: any) => void>();

    return {
        executeCommand: vi.fn(),
        syncExecuteCommand: vi.fn(),
        onCommandExecuted: vi.fn((handler: (command: any, options?: any) => void) => {
            afterHandlers.add(handler);
            return { dispose: vi.fn(() => afterHandlers.delete(handler)) };
        }),
        beforeCommandExecuted: vi.fn((handler: (command: any, options?: any) => void) => {
            beforeHandlers.add(handler);
            return { dispose: vi.fn(() => beforeHandlers.delete(handler)) };
        }),
        emitAfter: (command: any, options?: any) => afterHandlers.forEach((handler) => handler(command, options)),
        emitBefore: (command: any, options?: any) => beforeHandlers.forEach((handler) => handler(command, options)),
    };
}

function createController(options?: {
    currentEditLocation?: { unitId: string; sheetId: string; row: number; column: number };
    currentEditCellState?: { documentLayoutObject?: { documentModel?: { getSnapshot?: () => { body?: { dataStream?: string } } } } };
    editorVisible?: boolean;
    forceKeepVisible?: boolean;
    focusedUnitId?: string;
    isEmbedRuntimeEvent?: boolean;
    isEmbedActiveSession?: boolean;
    focusingSheet?: boolean;
    disableAutoFocus?: boolean;
    isEmbedRuntimeEventImpl?: (unitId: string | undefined, target?: EventTarget | null, event?: Event) => boolean;
}) {
    const workbook$ = new Subject<any>();
    const selectionMoveEnd$ = new Subject<any>();
    const selectionMoveStart$ = new Subject<any>();
    const selectionSet$ = new Subject<any>();
    const inputBefore$ = new Subject<any>();
    const renderCreated$ = new Subject<unknown>();
    const spreadsheet = {
        onDblclick$: createEventSubject(),
        onPointerDown$: createEventSubject(),
    };
    const spreadsheetRowHeader = { onPointerDown$: createEventSubject() };
    const spreadsheetColumnHeader = { onPointerDown$: createEventSubject() };
    const spreadsheetLeftTopPlaceholder = { onPointerDown$: createEventSubject() };
    const viewportMain = { onScrollEnd$: createEventSubject() };
    const scene = {
        getViewport: vi.fn((key: string) => key === SHEET_VIEWPORT_KEY.VIEW_MAIN ? viewportMain : null),
    };
    const engine = { width: 800, height: 600 };
    const univer = new Univer();
    onTestFinished(() => univer.dispose());
    const workbook = univer.createUnit<IWorkbookData, Workbook>(UniverInstanceType.UNIVER_SHEET, {
        id: 'unit-1',
        sheetOrder: ['sheet-1'],
        sheets: { 'sheet-1': { id: 'sheet-1', rowCount: 20, columnCount: 20 } },
    });
    const commandService = createCommandService();
    const contextValues = new Map<string, unknown>([
        [FOCUSING_SHEET, options?.focusingSheet ?? true],
        [FOCUSING_FX_BAR_EDITOR, false],
        [DISABLE_AUTO_FOCUS_KEY, options?.disableAutoFocus ?? false],
    ]);
    const focusingSheet$ = new Subject<boolean>();
    const context = {
        unitId: 'unit-1',
        unit: workbook,
        isMainScene: true,
        scene,
        engine,
        mainComponent: spreadsheet,
        components: new Map([
            [SHEET_VIEW_KEY.ROW, spreadsheetRowHeader],
            [SHEET_VIEW_KEY.COLUMN, spreadsheetColumnHeader],
            [SHEET_VIEW_KEY.LEFT_TOP, spreadsheetLeftTopPlaceholder],
        ]),
    };
    const docSelectionRenderService = {
        focus: vi.fn(),
        isFocusing: false,
        onInputBefore$: inputBefore$,
        setInputPosition: vi.fn(),
    };
    const renderManagerService = {
        created$: renderCreated$,
        getRenderUnitById: vi.fn((unitId: string) => unitId === DOCS_NORMAL_EDITOR_UNIT_ID_KEY
            ? {
                unitId,
                with: vi.fn((token) => token === DocSelectionRenderService ? docSelectionRenderService : null),
            }
            : null),
    };
    const editorVisible = options?.editorVisible ?? false;
    const editorBridgeService = {
        getEditCellState: vi.fn(() => options?.currentEditCellState ?? null),
        getEditCellLayout: vi.fn(() => ({
            position: { startX: 120, startY: 80 },
            canvasOffset: { left: 30, top: 20 },
        })),
        getEditLocation: vi.fn(() => options?.currentEditLocation ?? null),
        isVisible: vi.fn(() => ({ visible: editorVisible })),
        isForceKeepVisible: vi.fn(() => options?.forceKeepVisible ?? false),
        refreshEditCellState: vi.fn(),
        refreshEditCellPosition: vi.fn(),
    };
    commandService.syncExecuteCommand.mockReturnValue(true);
    const instanceService = {
        getCurrentTypeOfUnit$: vi.fn(() => workbook$),
        getFocusedUnit: vi.fn(() => ({
            getUnitId: () => options?.focusedUnitId ?? 'unit-1',
        })),
        getUnit: vi.fn(() => ({
            getBody: () => ({ dataStream: '=\r\n' }),
        })),
    };
    const selectionManagerService = {
        selectionMoveEnd$,
        selectionMoveStart$,
        selectionSet$,
        getWorkbookSelections: vi.fn(() => ({
            selectionMoveEnd$,
            selectionMoveStart$,
            selectionSet$,
            getCurrentSelections: vi.fn(() => [{
                primary: {
                    actualRow: 3,
                    actualColumn: 4,
                    startRow: 3,
                    startColumn: 4,
                    endRow: 3,
                    endColumn: 4,
                },
            }]),
        })),
    };
    const contextService = {
        getContextValue: vi.fn((key: string) => contextValues.get(key)),
        setContextValue: vi.fn((key: string, value: unknown) => contextValues.set(key, value)),
        subscribeContextValue$: vi.fn((key: string) => key === FOCUSING_SHEET ? focusingSheet$ : new Subject<unknown>()),
    };
    const embedRuntimeFocusCoordinator = {
        isChildUnitRuntimeEvent: vi.fn((unitId, target, event) =>
            options?.isEmbedRuntimeEventImpl?.(unitId, target, event) ?? options?.isEmbedRuntimeEvent ?? false),
        isChildUnitInActiveSession: vi.fn(() => options?.isEmbedActiveSession ?? false),
    } as unknown as EmbedRuntimeFocusCoordinator;
    const sheetSkeletonManagerService = {
        getSkeletonParam: vi.fn(() => ({
            skeleton: {
                getCellWithCoordByIndex: vi.fn(() => ({
                    actualRow: 1,
                    actualColumn: 2,
                    isMerged: true,
                    isMergedMainCell: true,
                    mergeInfo: {
                        startRow: 1,
                        startColumn: 2,
                        endRow: 4,
                        endColumn: 5,
                    },
                })),
            },
        })),
    };
    const injector = new Injector([
        [IPlatformService, { useClass: PlatformService }],
        [IUniverInstanceService, { useValue: instanceService }],
        [ICommandService, { useValue: commandService }],
        [IEditorBridgeService, { useValue: editorBridgeService }],
        [SheetsSelectionsService, { useValue: selectionManagerService }],
        [SheetSkeletonManagerService, { useValue: sheetSkeletonManagerService }],
        [ISheetEmbedRuntimeFocusCoordinator, { useValue: embedRuntimeFocusCoordinator }],
        [IContextService, { useValue: contextService }],
        [IRenderManagerService, { useValue: renderManagerService }],
    ]);
    const editorBridgeRenderController = injector.createInstance(EditorBridgeRenderController, context as never);
    const desktopCellEditRenderController = injector.createInstance(DesktopCellEditRenderController, context as never);
    const controller = {
        dispose: () => {
            desktopCellEditRenderController.dispose();
            editorBridgeRenderController.dispose();
            injector.dispose();
        },
        refreshEditorPosition: () => editorBridgeRenderController.refreshEditorPosition(),
    };
    onTestFinished(() => controller.dispose());
    workbook$.next(workbook);

    return {
        commandService,
        controller,
        docSelectionRenderService,
        editorBridgeService,
        inputBefore$,
        renderCreated$,
        selectionMoveEnd$,
        selectionSet$,
        spreadsheet,
        spreadsheetColumnHeader,
        spreadsheetLeftTopPlaceholder,
        spreadsheetRowHeader,
        viewportMain,
        workbook,
        workbook$,
    };
}

describe('DesktopCellEditRenderController integration with the editor bridge', () => {
    it('moves the sheet editor to the latest selected cell and respects merged-cell coordinates', () => {
        const { commandService, controller, selectionMoveEnd$ } = createController();

        selectionMoveEnd$.next([{
            primary: {
                actualRow: 3,
                actualColumn: 4,
                startRow: 3,
                startColumn: 4,
                endRow: 3,
                endColumn: 4,
            },
        }]);

        expect(commandService.executeCommand).toHaveBeenCalledWith(SetActivateCellEditOperation.id, {
            scene: expect.any(Object),
            engine: expect.any(Object),
            primary: {
                actualRow: 1,
                actualColumn: 2,
                isMerged: true,
                startRow: 1,
                startColumn: 2,
                endRow: 4,
                endColumn: 5,
                isMergedMainCell: true,
            },
            unitId: 'unit-1',
            sheetId: 'sheet-1',
        });

        controller.refreshEditorPosition();
        expect(commandService.executeCommand).toHaveBeenCalledTimes(2);

        controller.dispose();
    });

    it('does not reactivate the same edit cell for repeated selection sync events', () => {
        const { commandService, controller, selectionMoveEnd$, selectionSet$ } = createController({
            currentEditLocation: {
                unitId: 'unit-1',
                sheetId: 'sheet-1',
                row: 1,
                column: 2,
            },
        });

        const selection = [{
            primary: {
                actualRow: 3,
                actualColumn: 4,
                startRow: 3,
                startColumn: 4,
                endRow: 3,
                endColumn: 4,
            },
        }];

        selectionSet$.next(selection);
        selectionMoveEnd$.next(selection);

        expect(commandService.executeCommand).not.toHaveBeenCalledWith(SetActivateCellEditOperation.id, expect.anything());

        controller.dispose();
    });

    it('opens sheet editing from double click and keyboard input, then refreshes a hidden editor after formatting commands', () => {
        const { commandService, controller, editorBridgeService, inputBefore$, spreadsheet } = createController();

        spreadsheet.onDblclick$.emit({ button: 2 });
        expect(commandService.syncExecuteCommand).not.toHaveBeenCalledWith(SetCellEditVisibleOperation.id, expect.anything());

        const macPlatform = vi.spyOn(PlatformService.prototype, 'isMac', 'get').mockReturnValue(true);
        spreadsheet.onDblclick$.emit({ button: 0, ctrlKey: true, clientX: 150, clientY: 180 });
        expect(commandService.syncExecuteCommand).not.toHaveBeenCalledWith(SetCellEditVisibleOperation.id, expect.anything());
        macPlatform.mockRestore();

        spreadsheet.onDblclick$.emit({ button: 0, clientX: 150, clientY: 180 });
        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Dblclick,
            unitId: 'unit-1',
            pointerPosition: { x: 150, y: 180 },
        });

        inputBefore$.next({ event: { data: 'A', which: 65 } });
        inputBefore$.next({ event: { inputType: 'deleteContentBackward', which: 8 } });
        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(2);
        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Keyboard,
            keycode: 65,
            initialValue: 'A',
            unitId: 'unit-1',
        });

        commandService.emitAfter({ id: ClearSelectionFormatCommand.id });
        commandService.emitAfter({ id: SetZoomRatioCommand.id });
        expect(editorBridgeService.refreshEditCellState).toHaveBeenCalledTimes(2);

        controller.dispose();
    });

    it('requires an explicit edit action when mobile auto focus is disabled', () => {
        const { commandService, controller, docSelectionRenderService, inputBefore$, spreadsheet } = createController({
            disableAutoFocus: true,
        });

        inputBefore$.next({ event: { data: 'A', which: 65 } });
        expect(commandService.syncExecuteCommand).not.toHaveBeenCalledWith(SetCellEditVisibleOperation.id, expect.anything());

        spreadsheet.onPointerDown$.emit({});
        expect(docSelectionRenderService.focus).not.toHaveBeenCalled();

        spreadsheet.onDblclick$.emit({ button: 0, clientX: 150, clientY: 180 });
        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Dblclick,
            unitId: 'unit-1',
            pointerPosition: { x: 150, y: 180 },
        });

        controller.dispose();
    });

    it('moves the hidden input when the user finishes selecting a cell', () => {
        const { controller, docSelectionRenderService, selectionMoveEnd$ } = createController();

        selectionMoveEnd$.next([{
            primary: {
                actualRow: 3,
                actualColumn: 4,
                startRow: 3,
                startColumn: 4,
                endRow: 3,
                endColumn: 4,
            },
        }]);

        expect(docSelectionRenderService.setInputPosition).toHaveBeenCalledWith(150, 100);

        controller.dispose();
    });

    it('keeps the hidden input on the selected cell after the sheet scrolls', () => {
        const { controller, docSelectionRenderService, editorBridgeService, viewportMain } = createController();

        editorBridgeService.getEditCellLayout.mockReturnValue({
            position: { startX: 60, startY: 40 },
            canvasOffset: { left: 30, top: 20 },
        });
        viewportMain.onScrollEnd$.emit({});

        expect(editorBridgeService.refreshEditCellPosition).toHaveBeenCalledWith(false);
        expect(docSelectionRenderService.setInputPosition).toHaveBeenCalledWith(90, 60);

        controller.dispose();
    });

    it('does not move the hidden input on scroll while the cell editor is visible', () => {
        const { controller, docSelectionRenderService, editorBridgeService, viewportMain } = createController({ editorVisible: true });

        viewportMain.onScrollEnd$.emit({});

        expect(editorBridgeService.refreshEditCellPosition).not.toHaveBeenCalled();
        expect(docSelectionRenderService.setInputPosition).not.toHaveBeenCalled();

        controller.dispose();
    });

    it('does not reopen mobile editing implicitly when a selection changes', () => {
        const { commandService, controller, selectionMoveEnd$, spreadsheet } = createController({
            editorVisible: true,
            disableAutoFocus: true,
        });

        spreadsheet.onPointerDown$.emit({});
        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: false,
            eventType: DeviceInputEventType.PointerDown,
            unitId: 'unit-1',
        });

        selectionMoveEnd$.next([{
            primary: {
                actualRow: 3,
                actualColumn: 4,
                startRow: 3,
                startColumn: 4,
                endRow: 3,
                endColumn: 4,
            },
        }]);

        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(1);
        expect(commandService.executeCommand).not.toHaveBeenCalled();

        controller.dispose();
    });

    it('focuses the hidden input when an active embedded sheet clicks the unchanged cell', () => {
        const { controller, docSelectionRenderService, spreadsheet } = createController({
            focusedUnitId: 'host-unit',
            focusingSheet: false,
            isEmbedActiveSession: true,
        });

        spreadsheet.onPointerDown$.emit({});

        expect(docSelectionRenderService.focus).toHaveBeenCalledTimes(1);
        controller.dispose();
    });

    it('opens sheet editing from keyboard input while the sheet is an active embed child session', () => {
        const { commandService, controller, inputBefore$ } = createController({
            focusingSheet: false,
            focusedUnitId: 'host-unit',
            isEmbedActiveSession: true,
        });

        inputBefore$.next({ event: { data: '=', which: 187 } });

        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Keyboard,
            keycode: 187,
            initialValue: '=',
            unitId: 'unit-1',
        });

        controller.dispose();
    });

    it('does not stack keyboard input listeners when the current workbook emits repeatedly', () => {
        const { commandService, controller, inputBefore$, workbook, workbook$ } = createController();

        workbook$.next(workbook);
        workbook$.next(workbook);
        inputBefore$.next({ event: { data: '=', which: 187 } });

        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(1);
        expect(commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Keyboard,
            keycode: 187,
            initialValue: '=',
            unitId: 'unit-1',
        });

        controller.dispose();
    });

    it('continues opening sheet editing after the internal editor render is recreated', () => {
        const { commandService, controller, inputBefore$, renderCreated$, workbook$ } = createController();
        const recreatedInputBefore$ = new Subject<unknown>();
        const createRender = (input$: Subject<unknown>) => ({
            unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
            with: vi.fn((token: unknown) => token === DocSelectionRenderService
                ? { onInputBefore$: input$ }
                : null),
        });

        inputBefore$.next({ event: { data: 'A', which: 65 } });
        renderCreated$.next(createRender(recreatedInputBefore$));
        inputBefore$.next({ event: { data: 'B', which: 66 } });
        recreatedInputBefore$.next({ event: { data: 'C', which: 67 } });

        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(2);
        expect(commandService.syncExecuteCommand).toHaveBeenLastCalledWith(SetCellEditVisibleOperation.id, {
            visible: true,
            eventType: DeviceInputEventType.Keyboard,
            keycode: 67,
            initialValue: 'C',
            unitId: 'unit-1',
        });

        renderCreated$.next(createRender(recreatedInputBefore$));
        recreatedInputBefore$.next({ event: { data: 'D', which: 68 } });
        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(3);

        workbook$.next(null);
        recreatedInputBefore$.next({ event: { data: 'E', which: 69 } });
        expect(commandService.syncExecuteCommand).toHaveBeenCalledTimes(3);

        controller.dispose();
    });

    it('hides a visible editor from sheet pointer actions but keeps it while formula range input is active', () => {
        const keepVisible = createController({ editorVisible: true, forceKeepVisible: true });
        keepVisible.spreadsheet.onPointerDown$.emit({});
        expect(keepVisible.commandService.syncExecuteCommand).not.toHaveBeenCalled();
        keepVisible.controller.dispose();

        const normal = createController({ editorVisible: true });
        normal.spreadsheetColumnHeader.onPointerDown$.emit({});
        expect(normal.commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: false,
            eventType: DeviceInputEventType.PointerDown,
            unitId: 'unit-1',
        });

        normal.commandService.syncExecuteCommand.mockClear();
        normal.commandService.emitBefore({ id: SetWorksheetActiveOperation.id, params: { unitId: 'unit-1', subUnitId: 'sheet-1' } });
        expect(normal.commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: false,
            eventType: DeviceInputEventType.PointerDown,
            unitId: 'unit-1',
        });

        normal.commandService.syncExecuteCommand.mockClear();
        normal.commandService.emitBefore({ id: SetWorksheetActiveOperation.id, params: { unitId: 'unit-1', subUnitId: 'sheet-1' } }, { fromCollab: true });
        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        normal.controller.dispose();
    });

    it('keeps a visible embedded cell editor when pointer down originates inside the same embed owner', () => {
        const normal = createController({ editorVisible: true });
        const embedRoot = document.createElement('div');
        const editorTarget = document.createElement('div');
        embedRoot.setAttribute(EMBED_INTERACTION_BOUNDARY_OWNER_ATTRIBUTE, 'embed-1');
        editorTarget.setAttribute(EMBED_INTERACTION_BOUNDARY_OWNER_ATTRIBUTE, 'embed-1');
        editorTarget.setAttribute('data-u-comp', 'editor');
        document.body.append(embedRoot, editorTarget);

        normal.spreadsheet.onPointerDown$.emit({ target: editorTarget });
        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        editorTarget.getBoundingClientRect = () => ({
            x: 100,
            y: 200,
            left: 100,
            top: 200,
            right: 180,
            bottom: 224,
            width: 80,
            height: 24,
            toJSON: () => {},
        });
        normal.spreadsheet.onPointerDown$.emit({ clientX: 120, clientY: 210 });
        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        normal.spreadsheet.onPointerDown$.emit({ target: document.createElement('div') });
        expect(normal.commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: false,
            eventType: DeviceInputEventType.PointerDown,
            unitId: 'unit-1',
        });

        normal.controller.dispose();
        embedRoot.remove();
        editorTarget.remove();
    });

    it('hides a visible embedded cell editor when a regular edit points at the child runtime canvas', () => {
        const normal = createController({ editorVisible: true, isEmbedRuntimeEvent: true });
        const runtimeCanvas = document.createElement('canvas');

        normal.spreadsheet.onPointerDown$.emit({ target: runtimeCanvas });

        expect(normal.commandService.syncExecuteCommand).toHaveBeenCalledWith(SetCellEditVisibleOperation.id, {
            visible: false,
            eventType: DeviceInputEventType.PointerDown,
            unitId: 'unit-1',
        });

        normal.controller.dispose();
    });

    it('keeps a visible embedded cell editor when formula range selection points at the child runtime canvas', () => {
        const normal = createController({
            editorVisible: true,
            isEmbedRuntimeEvent: true,
            isEmbedActiveSession: true,
            currentEditCellState: {
                documentLayoutObject: {
                    documentModel: {
                        getSnapshot: () => ({ body: { dataStream: '=SUM(' } }),
                    },
                },
            },
        });
        const runtimeCanvas = document.createElement('canvas');

        normal.spreadsheet.onPointerDown$.emit({ target: runtimeCanvas });

        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        normal.controller.dispose();
    });

    it('passes engine pointer coordinates to embed runtime detection for formula range selection', () => {
        const runtimeCanvas = document.createElement('canvas');
        const normal = createController({
            editorVisible: true,
            isEmbedActiveSession: true,
            currentEditCellState: {
                documentLayoutObject: {
                    documentModel: {
                        getSnapshot: () => ({ body: { dataStream: '=SUM(' } }),
                    },
                },
            },
            isEmbedRuntimeEventImpl: (_unitId, _target, event) => (
                (event as unknown as { clientX?: number; clientY?: number })?.clientX === 120 &&
                (event as unknown as { clientX?: number; clientY?: number })?.clientY === 210
            ),
        });

        normal.spreadsheet.onPointerDown$.emit({ target: runtimeCanvas, clientX: 120, clientY: 210 });

        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        normal.controller.dispose();
    });

    it('keeps an embedded formula editor visible during worksheet activation without a pointer event', () => {
        const normal = createController({
            editorVisible: true,
            isEmbedActiveSession: true,
            currentEditCellState: {
                documentLayoutObject: {
                    documentModel: {
                        getSnapshot: () => ({ body: { dataStream: '=A1\r\n' } }),
                    },
                },
            },
        });

        normal.commandService.emitBefore({ id: SetWorksheetActiveOperation.id, params: { unitId: 'unit-1', subUnitId: 'sheet-1' } });

        expect(normal.commandService.syncExecuteCommand).not.toHaveBeenCalled();

        normal.controller.dispose();
    });
});