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

main.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
  1. import { Command } from 'commander';
  2. import { setLogLevel } from '../src/logger';
  3. import { addCommonOptionsRecursively } from '../src/main';
  4. import { setupEnv } from '../src/util';
  5. // Mock the dependencies
  6. jest.mock('../src/util', () => ({
  7. setupEnv: jest.fn(),
  8. }));
  9. jest.mock('../src/logger', () => ({
  10. __esModule: true,
  11. default: { debug: jest.fn() },
  12. setLogLevel: jest.fn(),
  13. }));
  14. describe('addCommonOptionsRecursively', () => {
  15. const originalExit = process.exit;
  16. let program: Command;
  17. let subCommand: Command;
  18. beforeAll(() => {
  19. process.exit = jest.fn() as any;
  20. });
  21. beforeEach(() => {
  22. program = new Command();
  23. program.action(() => {});
  24. subCommand = program.command('subcommand');
  25. subCommand.action(() => {});
  26. jest.clearAllMocks();
  27. });
  28. afterAll(() => {
  29. process.exit = originalExit;
  30. });
  31. it('should add both verbose and env-file options to a command', () => {
  32. addCommonOptionsRecursively(program);
  33. const hasVerboseOption = program.options.some(
  34. (option) => option.short === '-v' || option.long === '--verbose',
  35. );
  36. const hasEnvFileOption = program.options.some(
  37. (option) => option.long === '--env-file' || option.long === '--env-path',
  38. );
  39. expect(hasVerboseOption).toBe(true);
  40. expect(hasEnvFileOption).toBe(true);
  41. });
  42. it('should not add duplicate options if they already exist', () => {
  43. program.option('--env-file, --env-path <path>', 'Path to .env file');
  44. program.option('-v, --verbose', 'Show debug logs', false);
  45. // Count options before
  46. const envFileOptionsBefore = program.options.filter(
  47. (option) => option.long === '--env-file' || option.long === '--env-path',
  48. ).length;
  49. const verboseOptionsBefore = program.options.filter(
  50. (option) => option.short === '-v' || option.long === '--verbose',
  51. ).length;
  52. addCommonOptionsRecursively(program);
  53. // Count options after
  54. const envFileOptionsAfter = program.options.filter(
  55. (option) => option.long === '--env-file' || option.long === '--env-path',
  56. ).length;
  57. const verboseOptionsAfter = program.options.filter(
  58. (option) => option.short === '-v' || option.long === '--verbose',
  59. ).length;
  60. // Should still have the same number of options
  61. expect(envFileOptionsAfter).toBe(envFileOptionsBefore);
  62. expect(verboseOptionsAfter).toBe(verboseOptionsBefore);
  63. });
  64. it('should add options to subcommands', () => {
  65. addCommonOptionsRecursively(program);
  66. const hasSubcommandVerboseOption = subCommand.options.some(
  67. (option) => option.short === '-v' || option.long === '--verbose',
  68. );
  69. const hasSubcommandEnvFileOption = subCommand.options.some(
  70. (option) => option.long === '--env-file' || option.long === '--env-path',
  71. );
  72. expect(hasSubcommandVerboseOption).toBe(true);
  73. expect(hasSubcommandEnvFileOption).toBe(true);
  74. });
  75. it('should add options to nested subcommands', () => {
  76. // Create a deeper command structure
  77. const subSubCommand = subCommand.command('subsubcommand');
  78. subSubCommand.action(() => {});
  79. const subSubSubCommand = subSubCommand.command('subsubsubcommand');
  80. subSubSubCommand.action(() => {});
  81. addCommonOptionsRecursively(program);
  82. // Check all levels of commands have the options
  83. const hasMainVerboseOption = program.options.some(
  84. (option) => option.short === '-v' || option.long === '--verbose',
  85. );
  86. const hasMainEnvFileOption = program.options.some(
  87. (option) => option.long === '--env-file' || option.long === '--env-path',
  88. );
  89. const hasSubCommandVerboseOption = subCommand.options.some(
  90. (option) => option.short === '-v' || option.long === '--verbose',
  91. );
  92. const hasSubCommandEnvFileOption = subCommand.options.some(
  93. (option) => option.long === '--env-file' || option.long === '--env-path',
  94. );
  95. const hasSubSubCommandVerboseOption = subSubCommand.options.some(
  96. (option) => option.short === '-v' || option.long === '--verbose',
  97. );
  98. const hasSubSubCommandEnvFileOption = subSubCommand.options.some(
  99. (option) => option.long === '--env-file' || option.long === '--env-path',
  100. );
  101. const hasSubSubSubCommandVerboseOption = subSubSubCommand.options.some(
  102. (option) => option.short === '-v' || option.long === '--verbose',
  103. );
  104. const hasSubSubSubCommandEnvFileOption = subSubSubCommand.options.some(
  105. (option) => option.long === '--env-file' || option.long === '--env-path',
  106. );
  107. expect(hasMainVerboseOption).toBe(true);
  108. expect(hasMainEnvFileOption).toBe(true);
  109. expect(hasSubCommandVerboseOption).toBe(true);
  110. expect(hasSubCommandEnvFileOption).toBe(true);
  111. expect(hasSubSubCommandVerboseOption).toBe(true);
  112. expect(hasSubSubCommandEnvFileOption).toBe(true);
  113. expect(hasSubSubSubCommandVerboseOption).toBe(true);
  114. expect(hasSubSubSubCommandEnvFileOption).toBe(true);
  115. });
  116. it('should register a single hook that handles both options', () => {
  117. // Create a fake action that manually mocks the Commander hook system
  118. const mockHookRegister = jest.fn();
  119. (program as any).hook = mockHookRegister;
  120. // Apply common options
  121. addCommonOptionsRecursively(program);
  122. // Verify the hook was registered only once
  123. expect(mockHookRegister).toHaveBeenCalledTimes(1);
  124. expect(mockHookRegister).toHaveBeenCalledWith('preAction', expect.any(Function));
  125. // Get the hook function
  126. const preActionFn = mockHookRegister.mock.calls[0][1];
  127. // Test verbose option
  128. preActionFn({ opts: () => ({ verbose: true }) });
  129. expect(setLogLevel).toHaveBeenCalledWith('debug');
  130. // Test env-file option
  131. preActionFn({ opts: () => ({ envFile: '.env.test' }) });
  132. expect(setupEnv).toHaveBeenCalledWith('.env.test');
  133. // Test both options together
  134. preActionFn({ opts: () => ({ verbose: true, envFile: '.env.combined' }) });
  135. expect(setLogLevel).toHaveBeenCalledWith('debug');
  136. expect(setupEnv).toHaveBeenCalledWith('.env.combined');
  137. });
  138. });
Tip!

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

Comments

Loading...