File size: 5,634 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { render } from '../../test-utils/render.js';
import {
  useBackgroundTaskManager,
  type BackgroundTaskManagerProps,
} from './useBackgroundTaskManager.js';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { act } from 'react';
import { type BackgroundTask } from './shellReducer.js';

describe('useBackgroundTaskManager', () => {
  const setEmbeddedShellFocused = vi.fn();
  const terminalHeight = 30;

  beforeEach(() => {
    vi.clearAllMocks();
  });

  const renderHook = async (props: BackgroundTaskManagerProps) => {
    let hookResult: ReturnType<typeof useBackgroundTaskManager>;
    function TestComponent({ p }: { p: BackgroundTaskManagerProps }) {
      hookResult = useBackgroundTaskManager(p);
      return null;
    }
    const { rerender } = await render(<TestComponent p={props} />);
    return {
      result: {
        get current() {
          return hookResult;
        },
      },
      rerender: (newProps: BackgroundTaskManagerProps) =>
        rerender(<TestComponent p={newProps} />),
    };
  };

  it('should initialize with correct default values', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>();
    const { result } = await renderHook({
      backgroundTasks,
      backgroundTaskCount: 0,
      isBackgroundTaskVisible: false,
      activePtyId: null,
      embeddedShellFocused: false,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    expect(result.current.isBackgroundTaskListOpen).toBe(false);
    expect(result.current.activeBackgroundTaskPid).toBe(null);
    expect(result.current.backgroundTaskHeight).toBe(0);
  });

  it('should auto-select the first background shell when added', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>();
    const { result, rerender } = await renderHook({
      backgroundTasks,
      backgroundTaskCount: 0,
      isBackgroundTaskVisible: false,
      activePtyId: null,
      embeddedShellFocused: false,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    const newShells = new Map<number, BackgroundTask>([
      [123, {} as BackgroundTask],
    ]);
    rerender({
      backgroundTasks: newShells,
      backgroundTaskCount: 1,
      isBackgroundTaskVisible: false,
      activePtyId: null,
      embeddedShellFocused: false,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    expect(result.current.activeBackgroundTaskPid).toBe(123);
  });

  it('should reset state when all shells are removed', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>([
      [123, {} as BackgroundTask],
    ]);
    const { result, rerender } = await renderHook({
      backgroundTasks,
      backgroundTaskCount: 1,
      isBackgroundTaskVisible: true,
      activePtyId: null,
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    act(() => {
      result.current.setIsBackgroundTaskListOpen(true);
    });
    expect(result.current.isBackgroundTaskListOpen).toBe(true);

    rerender({
      backgroundTasks: new Map(),
      backgroundTaskCount: 0,
      isBackgroundTaskVisible: true,
      activePtyId: null,
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    expect(result.current.activeBackgroundTaskPid).toBe(null);
    expect(result.current.isBackgroundTaskListOpen).toBe(false);
  });

  it('should unfocus embedded shell when no shells are active', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>([
      [123, {} as BackgroundTask],
    ]);
    await renderHook({
      backgroundTasks,
      backgroundTaskCount: 1,
      isBackgroundTaskVisible: false, // Background shell not visible
      activePtyId: null, // No foreground shell
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    expect(setEmbeddedShellFocused).toHaveBeenCalledWith(false);
  });

  it('should calculate backgroundTaskHeight correctly when visible', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>([
      [123, {} as BackgroundTask],
    ]);
    const { result } = await renderHook({
      backgroundTasks,
      backgroundTaskCount: 1,
      isBackgroundTaskVisible: true,
      activePtyId: null,
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight: 100,
    });

    // 100 * 0.3 = 30
    expect(result.current.backgroundTaskHeight).toBe(30);
  });

  it('should maintain current active shell if it still exists', async () => {
    const backgroundTasks = new Map<number, BackgroundTask>([
      [123, {} as BackgroundTask],
      [456, {} as BackgroundTask],
    ]);
    const { result, rerender } = await renderHook({
      backgroundTasks,
      backgroundTaskCount: 2,
      isBackgroundTaskVisible: true,
      activePtyId: null,
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    act(() => {
      result.current.setActiveBackgroundTaskPid(456);
    });
    expect(result.current.activeBackgroundTaskPid).toBe(456);

    // Remove the OTHER shell
    const updatedShells = new Map<number, BackgroundTask>([
      [456, {} as BackgroundTask],
    ]);
    rerender({
      backgroundTasks: updatedShells,
      backgroundTaskCount: 1,
      isBackgroundTaskVisible: true,
      activePtyId: null,
      embeddedShellFocused: true,
      setEmbeddedShellFocused,
      terminalHeight,
    });

    expect(result.current.activeBackgroundTaskPid).toBe(456);
  });
});