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

setup.test.ts 3.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
  1. import { Command } from 'commander';
  2. import { getDefaultPort } from '../../../src/constants';
  3. import { redteamSetupCommand } from '../../../src/redteam/commands/setup';
  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, checkServerRunning, openBrowser } from '../../../src/util/server';
  9. jest.mock('../../../src/server/server');
  10. jest.mock('../../../src/util/server');
  11. jest.mock('../../../src/util', () => ({
  12. setupEnv: jest.fn(),
  13. }));
  14. jest.mock('../../../src/util/config/manage', () => ({
  15. setConfigDirectoryPath: jest.fn(),
  16. }));
  17. jest.mock('../../../src/telemetry', () => ({
  18. record: jest.fn(),
  19. }));
  20. describe('redteamSetupCommand', () => {
  21. let program: Command;
  22. beforeEach(() => {
  23. program = new Command();
  24. jest.clearAllMocks();
  25. });
  26. it('should register the setup command with correct options', () => {
  27. redteamSetupCommand(program);
  28. const setupCmd = program.commands.find((cmd) => cmd.name() === 'setup');
  29. expect(setupCmd).toBeDefined();
  30. expect(setupCmd?.description()).toBe('Start browser UI and open to redteam setup');
  31. expect(setupCmd?.opts().port).toBe(getDefaultPort().toString());
  32. });
  33. it('should handle setup command without directory', async () => {
  34. jest.mocked(checkServerRunning).mockResolvedValue(false);
  35. redteamSetupCommand(program);
  36. await program.parseAsync(['node', 'test', 'setup', '--port', '3000']);
  37. expect(setupEnv).toHaveBeenCalledWith(undefined);
  38. expect(telemetry.record).toHaveBeenCalledWith('command_used', {
  39. name: 'redteam setup',
  40. });
  41. expect(startServer).toHaveBeenCalledWith('3000', BrowserBehavior.OPEN_TO_REDTEAM_CREATE);
  42. });
  43. it('should handle setup command with directory', async () => {
  44. jest.mocked(checkServerRunning).mockResolvedValue(false);
  45. redteamSetupCommand(program);
  46. await program.parseAsync(['node', 'test', 'setup', 'test-dir', '--port', '3000']);
  47. expect(setConfigDirectoryPath).toHaveBeenCalledWith('test-dir');
  48. expect(startServer).toHaveBeenCalledWith('3000', BrowserBehavior.OPEN_TO_REDTEAM_CREATE);
  49. });
  50. it('should open browser if server is already running', async () => {
  51. jest.mocked(checkServerRunning).mockResolvedValue(true);
  52. redteamSetupCommand(program);
  53. await program.parseAsync(['node', 'test', 'setup']);
  54. expect(openBrowser).toHaveBeenCalledWith(BrowserBehavior.OPEN_TO_REDTEAM_CREATE);
  55. expect(startServer).not.toHaveBeenCalled();
  56. });
  57. it('should ignore filter description option', async () => {
  58. jest.mocked(checkServerRunning).mockResolvedValue(false);
  59. redteamSetupCommand(program);
  60. await program.parseAsync(['node', 'test', 'setup', '--filter-description', 'test.*']);
  61. expect(startServer).toHaveBeenCalledWith(
  62. getDefaultPort().toString(),
  63. BrowserBehavior.OPEN_TO_REDTEAM_CREATE,
  64. );
  65. });
  66. it('should handle setup command with env file path', async () => {
  67. jest.mocked(checkServerRunning).mockResolvedValue(false);
  68. redteamSetupCommand(program);
  69. await program.parseAsync(['node', 'test', 'setup', '--env-file', '.env.test']);
  70. expect(setupEnv).toHaveBeenCalledWith('.env.test');
  71. });
  72. });
Tip!

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

Comments

Loading...