File size: 8,028 Bytes
1c730d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2025-2026, RTE (https://www.rte-france.com)
// This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0.
// If a copy of the Mozilla Public License, version 2.0 was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { renderHook, act } from '@testing-library/react';

const registerLeverHandler = vi.fn();
const isGameMode = vi.fn(() => true);
vi.mock('../game/gameBridge', () => ({
    gameBridge: {
        isGameMode: () => isGameMode(),
        registerLeverHandler: (fn: unknown) => registerLeverHandler(fn),
    },
}));

const getElementVoltageLevels = vi.fn();
vi.mock('../api', () => ({
    api: { getElementVoltageLevels: (id: string) => getElementVoltageLevels(id) },
}));

const notifyError = vi.fn();
const notifyInfo = vi.fn();
vi.mock('../utils/notifications', () => ({
    notifyError: (m: string) => notifyError(m),
    notifyInfo: (m: string) => notifyInfo(m),
}));

import { useLeverInteraction, type LeverInteractionParams } from './useLeverInteraction';
import type { DiagramsState } from './useDiagrams';
import type { LeverInteraction } from '../types';

type Handler = (i: LeverInteraction, mode: 'inspect' | 'simulate') => Promise<void>;

function makeParams(over: Partial<LeverInteractionParams> = {}): LeverInteractionParams {
    return {
        diagrams: {
            activeTab: 'contingency',
            setInspectQuery: vi.fn(),
            zoomToElement: vi.fn(),
        } as unknown as DiagramsState,
        handleSimulateUnsimulatedAction: vi.fn().mockResolvedValue(undefined),
        handleSimulateLever: vi.fn().mockResolvedValue(undefined),
        handleVlOpen: vi.fn(),
        ...over,
    };
}

/** Render the hook and return the handler it registered on the bridge. */
function renderAndGetHandler(params: LeverInteractionParams): Handler {
    renderHook(() => useLeverInteraction(params));
    return registerLeverHandler.mock.calls.at(-1)?.[0] as Handler;
}

describe('useLeverInteraction', () => {
    beforeEach(() => {
        vi.clearAllMocks();
        isGameMode.mockReturnValue(true);
    });

    it('registers a lever handler only in game mode', () => {
        isGameMode.mockReturnValue(false);
        renderHook(() => useLeverInteraction(makeParams()));
        expect(registerLeverHandler).not.toHaveBeenCalled();

        isGameMode.mockReturnValue(true);
        renderHook(() => useLeverInteraction(makeParams()));
        expect(registerLeverHandler).toHaveBeenCalledTimes(1);
    });

    it('inspect on a branch lever centers on the branch itself (no VL lookup, no SLD)', async () => {
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => { await handler({ inspectQuery: 'LINE_A', category: 'branch' }, 'inspect'); });

        expect(params.diagrams.setInspectQuery).toHaveBeenCalledWith('LINE_A');
        expect(getElementVoltageLevels).not.toHaveBeenCalled();
        expect(params.diagrams.zoomToElement).toHaveBeenCalledWith('LINE_A', 'contingency');
        expect(params.handleVlOpen).not.toHaveBeenCalled();
    });

    it('inspect on an injection lever resolves its VL, centers there and opens the SLD', async () => {
        getElementVoltageLevels.mockResolvedValue({ voltage_level_ids: ['VL_G1'] });
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => { await handler({ inspectQuery: 'G1', category: 'generation' }, 'inspect'); });

        expect(getElementVoltageLevels).toHaveBeenCalledWith('G1');
        expect(params.diagrams.zoomToElement).toHaveBeenCalledWith('VL_G1', 'contingency');
        expect(params.handleVlOpen).toHaveBeenCalledWith('VL_G1');
    });

    it('simulate on a catalogue lever runs the action id directly', async () => {
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => {
            await handler({ inspectQuery: 'LINE_A', category: 'branch', simulate: { actionId: 'disco_LINE_A' } }, 'simulate');
        });

        expect(params.handleSimulateUnsimulatedAction).toHaveBeenCalledWith('disco_LINE_A');
        // Direct simulate short-circuits — no VL lookup / centering.
        expect(getElementVoltageLevels).not.toHaveBeenCalled();
        expect(params.diagrams.zoomToElement).not.toHaveBeenCalled();
    });

    it('simulate on a coupling lever resolves the VL and runs the maneuver', async () => {
        getElementVoltageLevels.mockResolvedValue({ voltage_level_ids: ['VL_SW'] });
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => {
            await handler({ inspectQuery: 'SW1', category: 'voltage_level', simulate: { switches: { SW1: true } } }, 'simulate');
        });

        expect(params.handleSimulateLever).toHaveBeenCalledWith({ voltageLevelId: 'VL_SW', switches: { SW1: true } });
        expect(params.handleVlOpen).not.toHaveBeenCalled();
    });

    it('warns when a coupling maneuver cannot be located to a single VL', async () => {
        getElementVoltageLevels.mockResolvedValue({ voltage_level_ids: [] });
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => {
            await handler({ inspectQuery: 'SW1', category: 'voltage_level', simulate: { switches: { SW1: true } } }, 'simulate');
        });

        expect(params.handleSimulateLever).not.toHaveBeenCalled();
        expect(notifyError).toHaveBeenCalledWith('Could not locate the substation for this maneuver.');
    });

    it('double-clicking a magnitude-free lever (PST / raw setpoint) degrades to inspect with a hint', async () => {
        // redispatch / ls / rc levers now carry a simulate spec; a lever that
        // reaches the handler with NO simulate (PST, raw gen_p/load_p) degrades.
        getElementVoltageLevels.mockResolvedValue({ voltage_level_ids: ['VL_G1'] });
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => { await handler({ inspectQuery: 'G1', category: 'generation' }, 'simulate'); });

        expect(notifyInfo).toHaveBeenCalledWith('Set the amount in the substation diagram, then Simulate.');
        expect(params.handleSimulateLever).not.toHaveBeenCalled();
        expect(params.handleSimulateUnsimulatedAction).not.toHaveBeenCalled();
        // Falls through to inspect: centers on the VL and opens its SLD.
        expect(params.diagrams.zoomToElement).toHaveBeenCalledWith('VL_G1', 'contingency');
        expect(params.handleVlOpen).toHaveBeenCalledWith('VL_G1');
    });

    it('falls back to centering on the id when VL resolution fails', async () => {
        getElementVoltageLevels.mockRejectedValue(new Error('not loaded'));
        const params = makeParams();
        const handler = renderAndGetHandler(params);

        await act(async () => { await handler({ inspectQuery: 'G1', category: 'generation' }, 'inspect'); });

        expect(params.diagrams.zoomToElement).toHaveBeenCalledWith('G1', 'contingency');
        expect(params.handleVlOpen).not.toHaveBeenCalled();
    });

    it('remaps the overflow tab to the contingency tab for centering', async () => {
        const params = makeParams({
            diagrams: {
                activeTab: 'overflow',
                setInspectQuery: vi.fn(),
                zoomToElement: vi.fn(),
            } as unknown as DiagramsState,
        });
        const handler = renderAndGetHandler(params);

        await act(async () => { await handler({ inspectQuery: 'LINE_A', category: 'branch' }, 'inspect'); });

        expect(params.diagrams.setInspectQuery).toHaveBeenCalledWith('LINE_A');
        expect(params.diagrams.zoomToElement).toHaveBeenCalledWith('LINE_A', 'contingency');
    });
});