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

modelScan.test.ts 5.9 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
218
219
220
221
222
223
224
225
  1. import { type ChildProcess, exec, spawn } from 'child_process';
  2. import { Command } from 'commander';
  3. import { checkModelAuditInstalled, modelScanCommand } from '../../src/commands/modelScan';
  4. import logger from '../../src/logger';
  5. jest.mock('child_process');
  6. jest.mock('../../src/logger');
  7. describe('modelScanCommand', () => {
  8. let program: Command;
  9. let mockSpawn: jest.SpyInstance;
  10. let mockExec: jest.SpyInstance;
  11. let mockExit: jest.SpyInstance;
  12. beforeEach(() => {
  13. program = new Command();
  14. mockSpawn = jest.spyOn({ spawn }, 'spawn');
  15. mockExec = jest.spyOn({ exec }, 'exec');
  16. mockExit = jest.spyOn(process, 'exit').mockImplementation(() => undefined as never);
  17. jest.clearAllMocks();
  18. });
  19. afterEach(() => {
  20. mockExit.mockRestore();
  21. });
  22. it('should exit if no paths are provided', async () => {
  23. const mockChildProcess = {
  24. on: jest.fn(),
  25. } as unknown as ChildProcess;
  26. mockSpawn.mockReturnValue(mockChildProcess);
  27. mockExec.mockImplementation((cmd, callback) => {
  28. if (callback) {
  29. callback(null, '', '');
  30. }
  31. return undefined;
  32. });
  33. modelScanCommand(program);
  34. const command = program.commands.find((cmd) => cmd.name() === 'scan-model');
  35. await command?.parseAsync(['scan-model']);
  36. expect(logger.error).toHaveBeenCalledWith(
  37. 'No paths specified. Please provide at least one model file or directory to scan.',
  38. );
  39. expect(mockExit).toHaveBeenCalledWith(1);
  40. });
  41. it('should exit if modelaudit is not installed', async () => {
  42. const mockChildProcess = {
  43. on: jest.fn(),
  44. } as unknown as ChildProcess;
  45. mockSpawn.mockReturnValue(mockChildProcess);
  46. mockExec.mockImplementation((cmd, callback) => {
  47. if (callback) {
  48. callback(new Error('command not found'), '', '');
  49. }
  50. return undefined;
  51. });
  52. modelScanCommand(program);
  53. const command = program.commands.find((cmd) => cmd.name() === 'scan-model');
  54. await command?.parseAsync(['scan-model', 'path/to/model']);
  55. expect(logger.error).toHaveBeenCalledWith('ModelAudit is not installed.');
  56. expect(mockExit).toHaveBeenCalledWith(1);
  57. });
  58. it('should spawn modelaudit process with correct arguments', async () => {
  59. mockExec.mockImplementation((cmd, callback) => {
  60. if (callback) {
  61. callback(null, '', '');
  62. }
  63. return undefined;
  64. });
  65. const mockChildProcess = {
  66. on: jest.fn().mockImplementation((event: string, callback: any) => {
  67. if (event === 'close') {
  68. callback(0);
  69. }
  70. return mockChildProcess;
  71. }),
  72. } as unknown as ChildProcess;
  73. mockSpawn.mockReturnValue(mockChildProcess);
  74. modelScanCommand(program);
  75. const command = program.commands.find((cmd) => cmd.name() === 'scan-model')!;
  76. await command.parseAsync([
  77. 'node',
  78. 'scan-model',
  79. 'path1',
  80. 'path2',
  81. '--blacklist',
  82. 'pattern1',
  83. '--format',
  84. 'json',
  85. '--output',
  86. 'output.json',
  87. '--timeout',
  88. '600',
  89. '--verbose',
  90. '--max-file-size',
  91. '1000000',
  92. '--max-total-size',
  93. '5000000',
  94. ]);
  95. expect(mockSpawn).toHaveBeenCalledWith(
  96. 'modelaudit',
  97. [
  98. 'scan',
  99. 'path1',
  100. 'path2',
  101. '--blacklist',
  102. 'pattern1',
  103. '--format',
  104. 'json',
  105. '--output',
  106. 'output.json',
  107. '--timeout',
  108. '600',
  109. '--verbose',
  110. '--max-file-size',
  111. '1000000',
  112. '--max-total-size',
  113. '5000000',
  114. ],
  115. { stdio: 'inherit' },
  116. );
  117. });
  118. it('should handle modelaudit process error', async () => {
  119. mockExec.mockImplementation((cmd, callback) => {
  120. if (callback) {
  121. callback(null, '', '');
  122. }
  123. return undefined;
  124. });
  125. const mockChildProcess = {
  126. on: jest.fn().mockImplementation((event: string, callback: any) => {
  127. if (event === 'error') {
  128. callback(new Error('spawn error'));
  129. }
  130. return mockChildProcess;
  131. }),
  132. } as unknown as ChildProcess;
  133. mockSpawn.mockReturnValue(mockChildProcess);
  134. modelScanCommand(program);
  135. const command = program.commands.find((cmd) => cmd.name() === 'scan-model');
  136. await command?.parseAsync(['scan-model', 'path/to/model']);
  137. expect(logger.error).toHaveBeenCalledWith('Failed to start modelaudit: spawn error');
  138. expect(mockExit).toHaveBeenCalledWith(1);
  139. });
  140. it('should handle non-zero exit code', async () => {
  141. mockExec.mockImplementation((cmd, callback) => {
  142. if (callback) {
  143. callback(null, '', '');
  144. }
  145. return undefined;
  146. });
  147. const mockChildProcess = {
  148. on: jest.fn().mockImplementation((event: string, callback: any) => {
  149. if (event === 'close') {
  150. callback(1);
  151. }
  152. return mockChildProcess;
  153. }),
  154. } as unknown as ChildProcess;
  155. mockSpawn.mockReturnValue(mockChildProcess);
  156. modelScanCommand(program);
  157. const command = program.commands.find((cmd) => cmd.name() === 'scan-model');
  158. await command?.parseAsync(['scan-model', 'path/to/model']);
  159. expect(logger.error).toHaveBeenCalledWith('Model scan completed with issues. Exit code: 1');
  160. expect(mockExit).toHaveBeenCalledWith(1);
  161. });
  162. });
  163. describe('checkModelAuditInstalled', () => {
  164. let mockExec: jest.SpyInstance;
  165. beforeEach(() => {
  166. mockExec = jest.spyOn({ exec }, 'exec');
  167. });
  168. it('should return true if modelaudit is installed', async () => {
  169. mockExec.mockImplementation((cmd, callback) => {
  170. if (callback) {
  171. callback(null, '', '');
  172. }
  173. return undefined;
  174. });
  175. const result = await checkModelAuditInstalled();
  176. expect(result).toBe(true);
  177. });
  178. it('should return false if modelaudit is not installed', async () => {
  179. mockExec.mockImplementation((cmd, callback) => {
  180. if (callback) {
  181. callback(new Error('command not found'), '', '');
  182. }
  183. return undefined;
  184. });
  185. const result = await checkModelAuditInstalled();
  186. expect(result).toBe(false);
  187. });
  188. });
Tip!

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

Comments

Loading...