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

view.test.ts 7.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
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
  1. import { Command } from 'commander';
  2. import { viewCommand } from '../../src/commands/view';
  3. import { getDefaultPort } from '../../src/constants';
  4. import { startServer } from '../../src/server/server';
  5. import telemetry from '../../src/telemetry';
  6. import { setupEnv } from '../../src/util';
  7. import { setConfigDirectoryPath } from '../../src/util/config/manage';
  8. import { BrowserBehavior } from '../../src/util/server';
  9. jest.mock('../../src/server/server');
  10. jest.mock('../../src/telemetry');
  11. jest.mock('../../src/util');
  12. jest.mock('../../src/util/config/manage');
  13. describe('viewCommand', () => {
  14. let program: Command;
  15. beforeEach(() => {
  16. program = new Command();
  17. jest.clearAllMocks();
  18. });
  19. it('should register view command with correct options', () => {
  20. viewCommand(program);
  21. const viewCmd = program.commands[0];
  22. expect(viewCmd.name()).toBe('view');
  23. expect(viewCmd.description()).toBe('Start browser UI');
  24. const options = viewCmd.opts();
  25. expect(options).toEqual({
  26. port: getDefaultPort().toString(),
  27. });
  28. });
  29. it('should call startServer with correct parameters when executed', async () => {
  30. viewCommand(program);
  31. const viewCmd = program.commands[0];
  32. await viewCmd.parseAsync(['node', 'test', '--port', '3001']);
  33. expect(startServer).toHaveBeenCalledWith('3001', BrowserBehavior.ASK);
  34. });
  35. it('should handle directory parameter and set config directory', async () => {
  36. viewCommand(program);
  37. const viewCmd = program.commands[0];
  38. await viewCmd.parseAsync(['node', 'test', 'testdir', '--port', '3001']);
  39. expect(setConfigDirectoryPath).toHaveBeenCalledWith('testdir');
  40. });
  41. it('should handle browser behavior options correctly', async () => {
  42. viewCommand(program);
  43. const viewCmd = program.commands[0];
  44. await viewCmd.parseAsync(['node', 'test', '--yes']);
  45. expect(startServer).toHaveBeenCalledWith(getDefaultPort().toString(), BrowserBehavior.OPEN);
  46. jest.clearAllMocks();
  47. // Use a unique port to avoid confusion with default port
  48. await viewCmd.parseAsync(['node', 'test', '--no', '--port', '15500']);
  49. // --no sets BrowserBehavior.OPEN when both --yes and --no are supplied due to commander behavior
  50. expect(startServer).toHaveBeenCalledWith('15500', BrowserBehavior.OPEN);
  51. });
  52. it('should ignore filter description option', async () => {
  53. viewCommand(program);
  54. const viewCmd = program.commands[0];
  55. await viewCmd.parseAsync(['node', 'test', '--filter-description', 'test-pattern']);
  56. expect(startServer).toHaveBeenCalledWith(getDefaultPort().toString(), BrowserBehavior.ASK);
  57. });
  58. it('should setup environment from env file path', async () => {
  59. viewCommand(program);
  60. const viewCmd = program.commands[0];
  61. await viewCmd.parseAsync(['node', 'test', '--env-path', '.env.test']);
  62. expect(setupEnv).toHaveBeenCalledWith('.env.test');
  63. });
  64. it('should record telemetry when command is used', async () => {
  65. viewCommand(program);
  66. const viewCmd = program.commands[0];
  67. await viewCmd.parseAsync(['node', 'test']);
  68. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  69. name: 'view',
  70. });
  71. });
  72. it('should handle both --yes and --no options with --yes taking precedence', async () => {
  73. viewCommand(program);
  74. const viewCmd = program.commands[0];
  75. await viewCmd.parseAsync(['node', 'test', '--yes', '--no']);
  76. expect(startServer).toHaveBeenCalledWith(getDefaultPort().toString(), BrowserBehavior.OPEN);
  77. });
  78. it('should call startServer with default port if no port is specified', async () => {
  79. viewCommand(program);
  80. const viewCmd = program.commands[0];
  81. await viewCmd.parseAsync(['node', 'test']);
  82. expect(startServer).toHaveBeenCalledWith(getDefaultPort().toString(), BrowserBehavior.ASK);
  83. });
  84. it('should support all options together', async () => {
  85. viewCommand(program);
  86. const viewCmd = program.commands[0];
  87. await viewCmd.parseAsync([
  88. 'node',
  89. 'test',
  90. 'mydir',
  91. '--port',
  92. '9876',
  93. '--yes',
  94. '--filter-description',
  95. 'desc',
  96. '--env-path',
  97. '.env.foo',
  98. ]);
  99. expect(setConfigDirectoryPath).toHaveBeenCalledWith('mydir');
  100. expect(setupEnv).toHaveBeenCalledWith('.env.foo');
  101. expect(startServer).toHaveBeenCalledWith('9876', BrowserBehavior.OPEN);
  102. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  103. name: 'view',
  104. });
  105. });
  106. it('should pass undefined directory if not provided', async () => {
  107. viewCommand(program);
  108. const viewCmd = program.commands[0];
  109. await viewCmd.parseAsync(['node', 'test', '--port', '3002']);
  110. expect(setConfigDirectoryPath).not.toHaveBeenCalled();
  111. });
  112. it('should prefer --yes over --no if both are supplied', async () => {
  113. viewCommand(program);
  114. const viewCmd = program.commands[0];
  115. await viewCmd.parseAsync(['node', 'test', '--yes', '--no', '--port', '4444']);
  116. expect(startServer).toHaveBeenCalledWith('4444', BrowserBehavior.OPEN);
  117. });
  118. it('should parse port as string if passed as number', async () => {
  119. viewCommand(program);
  120. const viewCmd = program.commands[0];
  121. // Simulate user passing a numeric port
  122. await viewCmd.parseAsync(['node', 'test', '--port', '7777']);
  123. expect(startServer).toHaveBeenCalledWith('7777', BrowserBehavior.ASK);
  124. });
  125. it('should call startServer with undefined filterDescription if not provided', async () => {
  126. viewCommand(program);
  127. const viewCmd = program.commands[0];
  128. await viewCmd.parseAsync(['node', 'test', '--yes', '--port', '2222']);
  129. expect(startServer).toHaveBeenCalledWith('2222', BrowserBehavior.OPEN);
  130. });
  131. it('should handle --filter-description with empty string', async () => {
  132. viewCommand(program);
  133. const viewCmd = program.commands[0];
  134. await viewCmd.parseAsync(['node', 'test', '--filter-description', '']);
  135. expect(startServer).toHaveBeenCalledWith(getDefaultPort().toString(), BrowserBehavior.ASK);
  136. });
  137. it('should call setupEnv with undefined if --env-path not provided', async () => {
  138. viewCommand(program);
  139. const viewCmd = program.commands[0];
  140. await viewCmd.parseAsync(['node', 'test']);
  141. expect(setupEnv).toHaveBeenCalledWith(undefined);
  142. });
  143. it('should call startServer with correct port and browserBehavior when only --no is supplied and --yes is not present', async () => {
  144. viewCommand(program);
  145. const viewCmd = program.commands[0];
  146. jest.clearAllMocks();
  147. // Only --no, no --yes
  148. await viewCmd.parseAsync(['node', 'test', '--no', '--port', '15501']);
  149. // Commander sets --no to true, --yes to undefined, so browserBehavior should be SKIP
  150. expect(startServer).toHaveBeenCalledWith('15501', BrowserBehavior.SKIP);
  151. });
  152. it('should call startServer with correct port and browserBehavior when only --yes is supplied', async () => {
  153. viewCommand(program);
  154. const viewCmd = program.commands[0];
  155. jest.clearAllMocks();
  156. await viewCmd.parseAsync(['node', 'test', '--yes', '--port', '16600']);
  157. expect(startServer).toHaveBeenCalledWith('16600', BrowserBehavior.OPEN);
  158. });
  159. it('should call startServer with ASK when neither --yes nor --no is supplied', async () => {
  160. viewCommand(program);
  161. const viewCmd = program.commands[0];
  162. jest.clearAllMocks();
  163. await viewCmd.parseAsync(['node', 'test', '--port', '17700']);
  164. expect(startServer).toHaveBeenCalledWith('17700', BrowserBehavior.ASK);
  165. });
  166. });
Tip!

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

Comments

Loading...