File size: 6,647 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * HealthStatus Component Tests
 */

import { describe, it, expect, vi, beforeEach } from "vitest";

// Define types locally
interface HealthDetails {
  is_initializing: boolean;
  is_playwright_ready: boolean;
  is_browser_connected: boolean;
  is_page_ready: boolean;
  workerRunning: boolean;
  queueLength: number;
  launchMode: string;
  browserAndPageCritical: boolean;
}

interface HealthResponse {
  status: "OK" | "Error";
  message: string;
  details: HealthDetails;
}

describe("HealthStatus Logic", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe("Data Fetching", () => {
    it("returns loading state structure", () => {
      const result = {
        data: undefined,
        isLoading: true,
        error: null,
        dataUpdatedAt: 0,
      };
      expect(result.isLoading).toBe(true);
    });

    it("returns health data when loaded", () => {
      const mockHealth: HealthResponse = {
        status: "OK",
        message: "All systems operational",
        details: {
          is_initializing: false,
          is_playwright_ready: true,
          is_browser_connected: true,
          is_page_ready: true,
          workerRunning: true,
          queueLength: 0,
          launchMode: "normal",
          browserAndPageCritical: true,
        },
      };

      const result = {
        data: mockHealth,
        isLoading: false,
        error: null,
        dataUpdatedAt: Date.now(),
      };
      expect(result.data.status).toBe("OK");
      expect(result.data.details.is_playwright_ready).toBe(true);
    });

    it("returns error state", () => {
      const result = {
        data: undefined,
        isLoading: false,
        error: new Error("Network error"),
        dataUpdatedAt: 0,
      };
      expect(result.error).toBeInstanceOf(Error);
    });
  });

  describe("Health Status Determination", () => {
    it("identifies healthy status", () => {
      const data: HealthResponse = {
        status: "OK",
        message: "Healthy",
        details: {} as HealthDetails,
      };

      const isHealthy = data.status === "OK";
      expect(isHealthy).toBe(true);
    });

    it("identifies unhealthy status", () => {
      const data: HealthResponse = {
        status: "Error",
        message: "Service unavailable",
        details: {} as HealthDetails,
      };

      const isHealthy = data.status === "OK";
      expect(isHealthy).toBe(false);
    });

    it("handles null data as unhealthy", () => {
      const data: HealthResponse | null = null;
      const isHealthy = data?.status === "OK";
      expect(isHealthy).toBe(false);
    });
  });

  describe("Status Item Evaluation", () => {
    it("evaluates playwright_ready status", () => {
      const details: HealthDetails = {
        is_initializing: false,
        is_playwright_ready: true,
        is_browser_connected: true,
        is_page_ready: true,
        workerRunning: true,
        queueLength: 0,
        launchMode: "normal",
        browserAndPageCritical: true,
      };

      expect(details.is_playwright_ready).toBe(true);
    });

    it("evaluates browser_connected status", () => {
      const details: HealthDetails = {
        is_initializing: false,
        is_playwright_ready: true,
        is_browser_connected: false,
        is_page_ready: false,
        workerRunning: true,
        queueLength: 0,
        launchMode: "normal",
        browserAndPageCritical: false,
      };

      expect(details.is_browser_connected).toBe(false);
    });

    it("evaluates queue length status (healthy when < 10)", () => {
      const queueLength = 5;
      const isQueueHealthy = queueLength < 10;
      expect(isQueueHealthy).toBe(true);
    });

    it("evaluates queue length status (unhealthy when >= 10)", () => {
      const queueLength = 15;
      const isQueueHealthy = queueLength < 10;
      expect(isQueueHealthy).toBe(false);
    });

    it("evaluates worker running status", () => {
      const details: HealthDetails = {
        is_initializing: false,
        is_playwright_ready: true,
        is_browser_connected: true,
        is_page_ready: true,
        workerRunning: false,
        queueLength: 0,
        launchMode: "normal",
        browserAndPageCritical: true,
      };

      expect(details.workerRunning).toBe(false);
    });
  });

  describe("Display Values", () => {
    it("formats ready status as 就绪", () => {
      const isReady = true;
      const displayValue = isReady ? "就绪" : "未就绪";
      expect(displayValue).toBe("就绪");
    });

    it("formats not ready status as 未就绪", () => {
      const isReady = false;
      const displayValue = isReady ? "就绪" : "未就绪";
      expect(displayValue).toBe("未就绪");
    });

    it("formats connected status as 已连接", () => {
      const isConnected = true;
      const displayValue = isConnected ? "已连接" : "未连接";
      expect(displayValue).toBe("已连接");
    });

    it("formats queue length display", () => {
      const queueLength = 5;
      const displayValue = `${queueLength} 个请求`;
      expect(displayValue).toBe("5 个请求");
    });

    it("formats worker status as 运行中", () => {
      const isRunning = true;
      const displayValue = isRunning ? "运行中" : "已停止";
      expect(displayValue).toBe("运行中");
    });
  });

  describe("Last Updated Display", () => {
    it("shows last updated when dataUpdatedAt > 0", () => {
      const dataUpdatedAt = Date.now();
      const shouldShow = dataUpdatedAt > 0;
      expect(shouldShow).toBe(true);
    });

    it("hides last updated when dataUpdatedAt is 0", () => {
      const dataUpdatedAt = 0;
      const shouldShow = dataUpdatedAt > 0;
      expect(shouldShow).toBe(false);
    });

    it("formats timestamp as locale time string", () => {
      const timestamp = new Date(2024, 0, 1, 12, 30, 45).getTime();
      const formatted = new Date(timestamp).toLocaleTimeString();
      expect(formatted).toContain(":");
    });
  });

  describe("Visibility-based Fetching", () => {
    it("configures refetch interval when visible", () => {
      const isVisible = true;
      const refetchInterval = isVisible ? 3000 : false;
      expect(refetchInterval).toBe(3000);
    });

    it("disables refetch interval when not visible", () => {
      const isVisible = false;
      const refetchInterval = isVisible ? 3000 : false;
      expect(refetchInterval).toBe(false);
    });

    it("disables enabled when not visible initially", () => {
      const isVisible = false;
      const enabled = isVisible;
      expect(enabled).toBe(false);
    });
  });
});