Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

server.test.ts 5.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. import opener from 'opener';
  2. import { VERSION, DEFAULT_PORT } from '../../src/constants';
  3. import logger from '../../src/logger';
  4. // Import the module under test after mocks are set up
  5. import { BrowserBehavior, checkServerRunning, openBrowser } from '../../src/util/server';
  6. // Mock opener
  7. jest.mock('opener', () => jest.fn());
  8. // Mock logger
  9. jest.mock('../../src/logger', () => ({
  10. info: jest.fn(),
  11. debug: jest.fn(),
  12. error: jest.fn(),
  13. }));
  14. // Mock readline
  15. const mockQuestion = jest.fn();
  16. const mockClose = jest.fn();
  17. jest.mock('readline', () => ({
  18. createInterface: jest.fn(() => ({
  19. question: mockQuestion,
  20. close: mockClose,
  21. on: jest.fn(),
  22. })),
  23. }));
  24. // Properly mock fetch
  25. const originalFetch = global.fetch;
  26. const mockFetch = jest.fn();
  27. describe('Server Utilities', () => {
  28. beforeEach(() => {
  29. jest.clearAllMocks();
  30. // Setup global.fetch as a Jest mock
  31. global.fetch = mockFetch;
  32. });
  33. afterEach(() => {
  34. global.fetch = originalFetch;
  35. });
  36. describe('checkServerRunning', () => {
  37. it('should return true when server is running with matching version', async () => {
  38. mockFetch.mockResolvedValueOnce({
  39. json: async () => ({ status: 'OK', version: VERSION }),
  40. });
  41. const result = await checkServerRunning();
  42. expect(mockFetch).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/health`);
  43. expect(result).toBe(true);
  44. });
  45. it('should return false when server status is not OK', async () => {
  46. mockFetch.mockResolvedValueOnce({
  47. json: async () => ({ status: 'ERROR', version: VERSION }),
  48. });
  49. const result = await checkServerRunning();
  50. expect(result).toBe(false);
  51. });
  52. it('should return false when server version does not match', async () => {
  53. mockFetch.mockResolvedValueOnce({
  54. json: async () => ({ status: 'OK', version: 'wrong-version' }),
  55. });
  56. const result = await checkServerRunning();
  57. expect(result).toBe(false);
  58. });
  59. it('should return false when fetch throws an error', async () => {
  60. mockFetch.mockRejectedValueOnce(new Error('Connection refused'));
  61. const result = await checkServerRunning();
  62. expect(result).toBe(false);
  63. expect(logger.debug).toHaveBeenCalledWith(
  64. expect.stringContaining('Failed to check server health'),
  65. );
  66. });
  67. it('should use custom port when provided', async () => {
  68. const customPort = 4000;
  69. mockFetch.mockResolvedValueOnce({
  70. json: async () => ({ status: 'OK', version: VERSION }),
  71. });
  72. await checkServerRunning(customPort);
  73. expect(mockFetch).toHaveBeenCalledWith(`http://localhost:${customPort}/health`);
  74. });
  75. });
  76. describe('openBrowser', () => {
  77. it('should open browser with default URL when BrowserBehavior.OPEN', async () => {
  78. await openBrowser(BrowserBehavior.OPEN);
  79. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}`);
  80. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Press Ctrl+C'));
  81. });
  82. it('should open browser with report URL when BrowserBehavior.OPEN_TO_REPORT', async () => {
  83. await openBrowser(BrowserBehavior.OPEN_TO_REPORT);
  84. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/report`);
  85. });
  86. it('should open browser with redteam setup URL when BrowserBehavior.OPEN_TO_REDTEAM_CREATE', async () => {
  87. await openBrowser(BrowserBehavior.OPEN_TO_REDTEAM_CREATE);
  88. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/redteam/setup`);
  89. });
  90. it('should not open browser when BrowserBehavior.SKIP', async () => {
  91. await openBrowser(BrowserBehavior.SKIP);
  92. expect(opener).not.toHaveBeenCalled();
  93. expect(logger.info).not.toHaveBeenCalled();
  94. });
  95. it('should handle opener errors gracefully', async () => {
  96. jest.mocked(opener).mockImplementationOnce(() => {
  97. throw new Error('Failed to open browser');
  98. });
  99. await openBrowser(BrowserBehavior.OPEN);
  100. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Failed to open browser'));
  101. });
  102. it('should ask user before opening browser when BrowserBehavior.ASK', async () => {
  103. // Setup readline to return 'y'
  104. mockQuestion.mockImplementationOnce((_, callback) => callback('y'));
  105. await openBrowser(BrowserBehavior.ASK);
  106. expect(mockQuestion).toHaveBeenCalledWith(
  107. 'Open URL in browser? (y/N): ',
  108. expect.any(Function),
  109. );
  110. expect(mockClose).toHaveBeenCalledWith();
  111. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}`);
  112. });
  113. it('should not open browser when user answers no to ASK prompt', async () => {
  114. // Setup readline to return 'n'
  115. mockQuestion.mockImplementationOnce((_, callback) => callback('n'));
  116. await openBrowser(BrowserBehavior.ASK);
  117. expect(mockQuestion).toHaveBeenCalledWith(
  118. 'Open URL in browser? (y/N): ',
  119. expect.any(Function),
  120. );
  121. expect(mockClose).toHaveBeenCalledWith();
  122. expect(opener).not.toHaveBeenCalled();
  123. });
  124. it('should use custom port when provided', async () => {
  125. const customPort = 5000;
  126. await openBrowser(BrowserBehavior.OPEN, customPort);
  127. expect(opener).toHaveBeenCalledWith(`http://localhost:${customPort}`);
  128. });
  129. });
  130. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...