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

init.test.ts 5.7 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
  1. import { Command } from 'commander';
  2. import fs from 'fs/promises';
  3. import * as init from '../../src/commands/init';
  4. import logger from '../../src/logger';
  5. // Add mock for redteam init
  6. jest.mock('../../src/redteam/commands/init', () => ({
  7. redteamInit: jest.fn(),
  8. }));
  9. jest.mock('../../src/server/server', () => ({
  10. startServer: jest.fn(),
  11. BrowserBehavior: {
  12. ASK: 0,
  13. OPEN: 1,
  14. SKIP: 2,
  15. OPEN_TO_REPORT: 3,
  16. OPEN_TO_REDTEAM_CREATE: 4,
  17. },
  18. }));
  19. jest.mock('../../src/commands/init', () => {
  20. const actual = jest.requireActual('../../src/commands/init');
  21. return {
  22. ...actual,
  23. downloadDirectory: jest.fn(actual.downloadDirectory),
  24. downloadExample: jest.fn(actual.downloadExample),
  25. getExamplesList: jest.fn(actual.getExamplesList),
  26. };
  27. });
  28. jest.mock('fs/promises');
  29. jest.mock('path');
  30. jest.mock('../../src/constants');
  31. jest.mock('../../src/logger');
  32. jest.mock('../../src/onboarding');
  33. jest.mock('../../src/telemetry');
  34. jest.mock('@inquirer/confirm');
  35. jest.mock('@inquirer/input');
  36. jest.mock('@inquirer/select');
  37. const mockFetch = jest.mocked(jest.fn());
  38. global.fetch = mockFetch;
  39. describe('init command', () => {
  40. beforeEach(() => {
  41. jest.clearAllMocks();
  42. });
  43. afterEach(() => {
  44. jest.restoreAllMocks();
  45. });
  46. describe('downloadFile', () => {
  47. it('should download a file successfully', async () => {
  48. const mockResponse = {
  49. ok: true,
  50. status: 200,
  51. text: jest.fn().mockResolvedValue('file content'),
  52. };
  53. mockFetch.mockResolvedValue(mockResponse);
  54. await init.downloadFile('https://example.com/file.txt', '/path/to/file.txt');
  55. expect(mockFetch).toHaveBeenCalledWith('https://example.com/file.txt');
  56. expect(fs.writeFile).toHaveBeenCalledWith('/path/to/file.txt', 'file content');
  57. });
  58. it('should throw an error if download fails', async () => {
  59. const mockResponse = {
  60. ok: false,
  61. status: 404,
  62. statusText: 'Not Found',
  63. };
  64. mockFetch.mockResolvedValue(mockResponse);
  65. await expect(
  66. init.downloadFile('https://example.com/file.txt', '/path/to/file.txt'),
  67. ).rejects.toThrow('Failed to download file: Not Found');
  68. });
  69. it('should handle network errors', async () => {
  70. mockFetch.mockRejectedValue(new Error('Network error'));
  71. await expect(
  72. init.downloadFile('https://example.com/file.txt', '/path/to/file.txt'),
  73. ).rejects.toThrow('Network error');
  74. });
  75. });
  76. describe('downloadDirectory', () => {
  77. it('should throw an error if fetching directory contents fails', async () => {
  78. const mockResponse = {
  79. ok: false,
  80. statusText: 'Not Found',
  81. };
  82. mockFetch.mockResolvedValue(mockResponse);
  83. await expect(init.downloadDirectory('example', '/path/to/target')).rejects.toThrow(
  84. 'Failed to fetch directory contents: Not Found',
  85. );
  86. });
  87. it('should handle network errors', async () => {
  88. mockFetch.mockRejectedValue(new Error('Network error'));
  89. await expect(init.downloadDirectory('example', '/path/to/target')).rejects.toThrow(
  90. 'Network error',
  91. );
  92. });
  93. });
  94. describe('downloadExample', () => {
  95. it('should throw an error if directory creation fails', async () => {
  96. jest.spyOn(fs, 'mkdir').mockRejectedValue(new Error('Permission denied'));
  97. await expect(init.downloadExample('example', '/path/to/target')).rejects.toThrow(
  98. 'Failed to download example: Permission denied',
  99. );
  100. });
  101. it('should throw an error if downloadDirectory fails', async () => {
  102. jest.spyOn(fs, 'mkdir').mockResolvedValue(undefined);
  103. jest.mocked(init.downloadDirectory).mockRejectedValue(new Error('Download failed'));
  104. await expect(init.downloadExample('example', '/path/to/target')).rejects.toThrow(
  105. 'Failed to download example: Network error',
  106. );
  107. });
  108. });
  109. describe('getExamplesList', () => {
  110. it('should return a list of examples', async () => {
  111. const mockResponse = {
  112. ok: true,
  113. status: 200,
  114. json: jest.fn().mockResolvedValue([
  115. { name: 'example1', type: 'dir' },
  116. { name: 'example2', type: 'dir' },
  117. { name: 'not-an-example', type: 'file' },
  118. ]),
  119. };
  120. mockFetch.mockResolvedValue(mockResponse);
  121. const examples = await init.getExamplesList();
  122. expect(examples).toEqual(['example1', 'example2']);
  123. });
  124. it('should return an empty array if fetching fails', async () => {
  125. const mockResponse = {
  126. ok: false,
  127. status: 404,
  128. statusText: 'Not Found',
  129. };
  130. mockFetch.mockResolvedValue(mockResponse);
  131. const examples = await init.getExamplesList();
  132. expect(examples).toEqual([]);
  133. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Not Found'));
  134. });
  135. it('should handle network errors', async () => {
  136. mockFetch.mockRejectedValue(new Error('Network error'));
  137. const examples = await init.getExamplesList();
  138. expect(examples).toEqual([]);
  139. expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('Network error'));
  140. });
  141. });
  142. describe('initCommand', () => {
  143. let program: Command;
  144. beforeEach(() => {
  145. program = new Command();
  146. init.initCommand(program);
  147. const initCmd = program.commands.find((cmd) => cmd.name() === 'init');
  148. if (!initCmd) {
  149. throw new Error('initCmd not found');
  150. }
  151. });
  152. it('should set up the init command correctly', () => {
  153. const initCmd = program.commands.find((cmd) => cmd.name() === 'init');
  154. expect(initCmd).toBeDefined();
  155. expect(initCmd?.description()).toBe(
  156. 'Initialize project with dummy files or download an example',
  157. );
  158. expect(initCmd?.options).toHaveLength(3);
  159. });
  160. });
  161. });
Tip!

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

Comments

Loading...