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

startup.test.ts 5.6 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
  1. import { getDefaultPort } from '../../src/constants';
  2. import logger from '../../src/logger';
  3. import { startServer } from '../../src/server/server';
  4. import { BrowserBehavior, checkServerRunning } from '../../src/util/server';
  5. jest.mock('../../src/logger');
  6. jest.mock('../../src/server/server');
  7. jest.mock('../../src/util/server');
  8. jest.mock('../../src/constants');
  9. describe('server startup behavior', () => {
  10. const mockPort = 15500;
  11. const mockCheckServerRunning = jest.mocked(checkServerRunning);
  12. const mockStartServer = jest.mocked(startServer);
  13. const mockGetDefaultPort = jest.mocked(getDefaultPort);
  14. const mockLoggerInfo = jest.mocked(logger.info);
  15. const mockLoggerError = jest.mocked(logger.error);
  16. beforeEach(() => {
  17. mockGetDefaultPort.mockReturnValue(mockPort);
  18. });
  19. afterEach(() => {
  20. jest.resetAllMocks();
  21. });
  22. // This test documents the expected behavior of the server startup logic
  23. // The actual implementation is in src/server/index.ts
  24. describe('expected server startup behavior', () => {
  25. it('should check if server is running and log message if it is', async () => {
  26. // Given a server is already running
  27. mockCheckServerRunning.mockResolvedValue(true);
  28. // When attempting to start the server
  29. // The following simulates what happens in src/server/index.ts
  30. const port = getDefaultPort();
  31. const isRunning = await checkServerRunning(port);
  32. if (isRunning) {
  33. logger.info(`Promptfoo server already running at http://localhost:${port}`);
  34. process.exitCode = 1;
  35. // Early return - don't start server
  36. } else {
  37. await startServer(port, BrowserBehavior.SKIP);
  38. }
  39. // Then it should have checked if server is running
  40. expect(mockCheckServerRunning).toHaveBeenCalledWith(mockPort);
  41. // And logged the appropriate message
  42. expect(mockLoggerInfo).toHaveBeenCalledWith(
  43. `Promptfoo server already running at http://localhost:${mockPort}`,
  44. );
  45. // And NOT attempted to start the server
  46. expect(mockStartServer).not.toHaveBeenCalled();
  47. });
  48. it('should set process.exitCode to 1 when server is already running', async () => {
  49. // Given a server is already running
  50. mockCheckServerRunning.mockResolvedValue(true);
  51. // Save original process.exitCode
  52. const originalExitCode = process.exitCode;
  53. process.exitCode = undefined;
  54. // When attempting to start the server
  55. const port = getDefaultPort();
  56. const isRunning = await checkServerRunning(port);
  57. if (isRunning) {
  58. logger.info(`Promptfoo server already running at http://localhost:${port}`);
  59. process.exitCode = 1;
  60. return;
  61. }
  62. // Then process.exitCode should be set to 1
  63. expect(process.exitCode).toBe(1);
  64. // Restore original process.exitCode
  65. process.exitCode = originalExitCode;
  66. });
  67. it('should start server when none is running', async () => {
  68. // Given no server is running
  69. mockCheckServerRunning.mockResolvedValue(false);
  70. // When attempting to start the server
  71. const port = getDefaultPort();
  72. const isRunning = await checkServerRunning(port);
  73. if (isRunning) {
  74. logger.info(`Promptfoo server already running at http://localhost:${port}`);
  75. process.exitCode = 1;
  76. } else {
  77. await startServer(port, BrowserBehavior.SKIP);
  78. }
  79. // Then it should have started the server
  80. expect(mockStartServer).toHaveBeenCalledWith(mockPort, BrowserBehavior.SKIP);
  81. // And NOT logged the "already running" message
  82. expect(mockLoggerInfo).not.toHaveBeenCalled();
  83. });
  84. it('should handle errors gracefully', async () => {
  85. // Given an error occurs during startup
  86. const testError = new Error('Connection failed');
  87. mockCheckServerRunning.mockRejectedValue(testError);
  88. // When attempting to start the server with error handling
  89. try {
  90. const port = getDefaultPort();
  91. const isRunning = await checkServerRunning(port);
  92. if (isRunning) {
  93. logger.info(`Promptfoo server already running at http://localhost:${port}`);
  94. process.exitCode = 1;
  95. } else {
  96. await startServer(port, BrowserBehavior.SKIP);
  97. }
  98. } catch (err) {
  99. logger.error(`Failed to start server: ${String(err)}`);
  100. }
  101. // Then it should log the error
  102. expect(mockLoggerError).toHaveBeenCalledWith(
  103. `Failed to start server: ${testError.toString()}`,
  104. );
  105. // And NOT attempt to start the server
  106. expect(mockStartServer).not.toHaveBeenCalled();
  107. });
  108. it('should set process.exitCode to 1 on error', async () => {
  109. // Given an error occurs during startup
  110. const testError = new Error('Connection failed');
  111. mockCheckServerRunning.mockRejectedValue(testError);
  112. // Save original process.exitCode
  113. const originalExitCode = process.exitCode;
  114. process.exitCode = undefined;
  115. // When the main function encounters an error
  116. try {
  117. const port = getDefaultPort();
  118. const isRunning = await checkServerRunning(port);
  119. if (isRunning) {
  120. logger.info(`Promptfoo server already running at http://localhost:${port}`);
  121. process.exitCode = 1;
  122. } else {
  123. await startServer(port, BrowserBehavior.SKIP);
  124. }
  125. } catch (err) {
  126. logger.error(`Failed to start server: ${String(err)}`);
  127. process.exitCode = 1;
  128. }
  129. // Then process.exitCode should be set to 1
  130. expect(process.exitCode).toBe(1);
  131. // Restore original process.exitCode
  132. process.exitCode = originalExitCode;
  133. });
  134. });
  135. });
Tip!

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

Comments

Loading...