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 4.8 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
  1. import opener from 'opener';
  2. import { VERSION, DEFAULT_PORT } from '../../src/constants';
  3. import logger from '../../src/logger';
  4. import { BrowserBehavior } from '../../src/util/server';
  5. import { checkServerRunning, openBrowser } from '../../src/util/server';
  6. jest.mock('opener');
  7. jest.mock('../../src/logger');
  8. jest.mock('readline');
  9. const mockFetch = jest.fn();
  10. global.fetch = mockFetch;
  11. describe('server utilities', () => {
  12. let mockReadline: { question: jest.Mock; close: jest.Mock };
  13. beforeEach(() => {
  14. jest.clearAllMocks();
  15. mockReadline = {
  16. question: jest.fn(),
  17. close: jest.fn(),
  18. };
  19. const readline = jest.requireMock('readline');
  20. readline.createInterface.mockReturnValue(mockReadline);
  21. });
  22. describe('checkServerRunning', () => {
  23. it('returns true when server is running with matching version', async () => {
  24. mockFetch.mockResolvedValueOnce({
  25. json: () => Promise.resolve({ status: 'OK', version: VERSION }),
  26. });
  27. const result = await checkServerRunning(DEFAULT_PORT);
  28. expect(result).toBe(true);
  29. expect(mockFetch).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/health`);
  30. });
  31. it('returns false when server returns non-OK status', async () => {
  32. mockFetch.mockResolvedValueOnce({
  33. json: () => Promise.resolve({ status: 'ERROR', version: VERSION }),
  34. });
  35. const result = await checkServerRunning();
  36. expect(result).toBe(false);
  37. });
  38. it('returns false when server version does not match', async () => {
  39. mockFetch.mockResolvedValueOnce({
  40. json: () => Promise.resolve({ status: 'OK', version: '0.0.0' }),
  41. });
  42. const result = await checkServerRunning();
  43. expect(result).toBe(false);
  44. });
  45. it('returns false and logs debug message when fetch fails', async () => {
  46. mockFetch.mockRejectedValueOnce(new Error('Connection refused'));
  47. const result = await checkServerRunning();
  48. expect(result).toBe(false);
  49. expect(logger.debug).toHaveBeenCalledWith(
  50. expect.stringContaining('Failed to check server health'),
  51. );
  52. });
  53. it('uses default port when not specified', async () => {
  54. mockFetch.mockResolvedValueOnce({
  55. json: () => Promise.resolve({ status: 'OK', version: VERSION }),
  56. });
  57. await checkServerRunning();
  58. expect(mockFetch).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/health`);
  59. });
  60. });
  61. describe('openBrowser', () => {
  62. it('opens browser directly with OPEN behavior', async () => {
  63. await openBrowser(BrowserBehavior.OPEN);
  64. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}`);
  65. expect(logger.info).toHaveBeenCalledWith('Press Ctrl+C to stop the server');
  66. });
  67. it('opens report page with OPEN_TO_REPORT behavior', async () => {
  68. await openBrowser(BrowserBehavior.OPEN_TO_REPORT);
  69. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/report`);
  70. });
  71. it('opens redteam setup with OPEN_TO_REDTEAM_CREATE behavior', async () => {
  72. await openBrowser(BrowserBehavior.OPEN_TO_REDTEAM_CREATE);
  73. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}/redteam/setup`);
  74. });
  75. it('does not open browser with SKIP behavior', async () => {
  76. await openBrowser(BrowserBehavior.SKIP);
  77. expect(opener).not.toHaveBeenCalled();
  78. });
  79. it('prompts user with ASK behavior and opens on yes', async () => {
  80. mockReadline.question.mockImplementationOnce((_, callback) => {
  81. callback('y');
  82. });
  83. await openBrowser(BrowserBehavior.ASK);
  84. expect(mockReadline.question).toHaveBeenCalledWith(
  85. 'Open URL in browser? (y/N): ',
  86. expect.any(Function),
  87. );
  88. expect(opener).toHaveBeenCalledWith(`http://localhost:${DEFAULT_PORT}`);
  89. expect(mockReadline.close).toHaveBeenCalledWith();
  90. });
  91. it('prompts user with ASK behavior and skips on no', async () => {
  92. mockReadline.question.mockImplementationOnce((_, callback) => {
  93. callback('n');
  94. });
  95. await openBrowser(BrowserBehavior.ASK);
  96. expect(mockReadline.question).toHaveBeenCalledWith(
  97. 'Open URL in browser? (y/N): ',
  98. expect.any(Function),
  99. );
  100. expect(opener).not.toHaveBeenCalled();
  101. expect(mockReadline.close).toHaveBeenCalledWith();
  102. });
  103. it('handles opener errors gracefully', async () => {
  104. jest.mocked(opener).mockImplementation(() => {
  105. throw new Error('Failed to open browser');
  106. });
  107. await openBrowser(BrowserBehavior.OPEN);
  108. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Failed to open browser'));
  109. });
  110. it('uses custom port when specified', async () => {
  111. const customPort = 3000;
  112. await openBrowser(BrowserBehavior.OPEN, customPort);
  113. expect(opener).toHaveBeenCalledWith(`http://localhost:${customPort}`);
  114. });
  115. });
  116. });
Tip!

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

Comments

Loading...