Skip to content

Commit bd78443

Browse files
CCM-13135: Adding test
1 parent efac356 commit bd78443

File tree

2 files changed

+305
-1
lines changed

2 files changed

+305
-1
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
import { mockDeep } from 'jest-mock-extended';
2+
import { Client, IParameterStore } from 'utils';
3+
import type { App } from 'app';
4+
import { ClientManagement } from '../../..';
5+
import { main } from '../../../entrypoint/cli';
6+
7+
// Store original argv
8+
const originalArgv = process.argv;
9+
const originalExitCode = process.exitCode;
10+
11+
const mockParameterStore: IParameterStore = {
12+
getParameter: jest.fn(),
13+
getAllParameters: jest.fn(),
14+
addParameter: jest.fn(),
15+
deleteParameter: jest.fn(),
16+
clearCachedParameter: jest.fn(),
17+
};
18+
19+
const mockClientManagement: App = mockDeep<App>({
20+
deleteClient: jest.fn(),
21+
getClient: jest.fn(),
22+
listClients: jest.fn(),
23+
putClient: jest.fn(),
24+
});
25+
26+
jest.mock('utils', () => ({
27+
ParameterStore: jest.fn(() => mockParameterStore),
28+
}));
29+
30+
jest.mock('../../..', () => ({
31+
ClientManagement: jest.fn(() => mockClientManagement),
32+
}));
33+
34+
// Mock console methods
35+
const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
36+
const mockConsoleTable = jest.spyOn(console, 'table').mockImplementation();
37+
const mockConsoleError = jest.spyOn(console, 'error').mockImplementation();
38+
39+
describe('CLI entrypoint', () => {
40+
const mockClient: Client = {
41+
clientId: 'test-client-id',
42+
clientName: 'Test Client',
43+
meshMailboxSenderId: 'test-sender',
44+
meshMailboxReportsId: 'test-reports',
45+
fallbackWaitTimeSeconds: 300,
46+
routingConfigId: 'test-routing',
47+
};
48+
49+
beforeEach(() => {
50+
jest.clearAllMocks();
51+
});
52+
53+
afterAll(() => {
54+
mockConsoleLog.mockRestore();
55+
mockConsoleTable.mockRestore();
56+
mockConsoleError.mockRestore();
57+
process.argv = originalArgv;
58+
process.exitCode = originalExitCode;
59+
});
60+
61+
describe('delete-client command', () => {
62+
it('should delete a client and print result as table by default', async () => {
63+
process.argv = [
64+
'node',
65+
'cli',
66+
'delete-client',
67+
'--environment',
68+
'test',
69+
'--client-id',
70+
'test-client-id',
71+
];
72+
73+
// Import and execute main
74+
await main();
75+
76+
expect(ClientManagement).toHaveBeenCalledWith({
77+
parameterStore: mockParameterStore,
78+
configOverrides: { environment: 'test' },
79+
});
80+
expect(mockClientManagement.deleteClient).toHaveBeenCalledWith({
81+
clientId: 'test-client-id',
82+
});
83+
expect(mockConsoleTable).toHaveBeenCalledWith([
84+
{ clientId: 'test-client-id' },
85+
]);
86+
});
87+
88+
it('should delete a client and print result as JSON when format is json', async () => {
89+
process.argv = [
90+
'node',
91+
'cli',
92+
'delete-client',
93+
'--environment',
94+
'test',
95+
'--client-id',
96+
'test-client-id',
97+
'--format',
98+
'json',
99+
];
100+
101+
await main();
102+
103+
expect(mockClientManagement.deleteClient).toHaveBeenCalledWith({
104+
clientId: 'test-client-id',
105+
});
106+
expect(mockConsoleLog).toHaveBeenCalledWith(
107+
JSON.stringify({ clientId: 'test-client-id' }, null, 2),
108+
);
109+
});
110+
});
111+
112+
describe('get-client command', () => {
113+
it('should get a client and print result as table by default', async () => {
114+
(mockClientManagement.getClient as jest.Mock).mockResolvedValue(
115+
mockClient,
116+
);
117+
118+
process.argv = [
119+
'node',
120+
'cli',
121+
'get-client',
122+
'--environment',
123+
'test',
124+
'--client-id',
125+
'test-client-id',
126+
];
127+
128+
await main();
129+
130+
expect(mockClientManagement.getClient).toHaveBeenCalledWith({
131+
clientId: 'test-client-id',
132+
});
133+
expect(mockConsoleTable).toHaveBeenCalledWith([mockClient]);
134+
});
135+
136+
it('should get a client and print result as JSON when format is json', async () => {
137+
(mockClientManagement.getClient as jest.Mock).mockResolvedValue(
138+
mockClient,
139+
);
140+
141+
process.argv = [
142+
'node',
143+
'cli',
144+
'get-client',
145+
'--environment',
146+
'test',
147+
'--client-id',
148+
'test-client-id',
149+
'--format',
150+
'json',
151+
];
152+
153+
await main();
154+
155+
expect(mockClientManagement.getClient).toHaveBeenCalledWith({
156+
clientId: 'test-client-id',
157+
});
158+
expect(mockConsoleLog).toHaveBeenCalledWith(
159+
JSON.stringify(mockClient, null, 2),
160+
);
161+
});
162+
});
163+
164+
describe('list-clients command', () => {
165+
it('should list clients and print result as table by default', async () => {
166+
const clients = [mockClient];
167+
(mockClientManagement.listClients as jest.Mock).mockResolvedValue(
168+
clients,
169+
);
170+
171+
process.argv = ['node', 'cli', 'list-clients', '--environment', 'test'];
172+
173+
await main();
174+
175+
expect(mockClientManagement.listClients).toHaveBeenCalled();
176+
expect(mockConsoleTable).toHaveBeenCalledWith(clients);
177+
});
178+
179+
it('should list clients and print result as JSON when format is json', async () => {
180+
const clients = [mockClient];
181+
(mockClientManagement.listClients as jest.Mock).mockResolvedValue(
182+
clients,
183+
);
184+
185+
process.argv = [
186+
'node',
187+
'cli',
188+
'list-clients',
189+
'--environment',
190+
'test',
191+
'--format',
192+
'json',
193+
];
194+
195+
await main();
196+
197+
expect(mockClientManagement.listClients).toHaveBeenCalled();
198+
expect(mockConsoleLog).toHaveBeenCalledWith(
199+
JSON.stringify(clients, null, 2),
200+
);
201+
});
202+
});
203+
204+
describe('put-client command', () => {
205+
it('should put a client and print result as table by default', async () => {
206+
(mockClientManagement.putClient as jest.Mock).mockResolvedValue(
207+
mockClient,
208+
);
209+
210+
process.argv = [
211+
'node',
212+
'cli',
213+
'put-client',
214+
'--environment',
215+
'test',
216+
'--client-id',
217+
'test-client-id',
218+
'--client-name',
219+
'Test Client',
220+
'--mesh-mailbox-sender-id',
221+
'test-sender',
222+
'--mesh-mailbox-reports-id',
223+
'test-reports',
224+
'--fallback-wait-time-seconds',
225+
'300',
226+
'--routing-config-id',
227+
'test-routing',
228+
];
229+
230+
await main();
231+
232+
expect(mockClientManagement.putClient).toHaveBeenCalledWith({
233+
clientId: 'test-client-id',
234+
clientName: 'Test Client',
235+
meshMailboxSenderId: 'test-sender',
236+
meshMailboxReportsId: 'test-reports',
237+
fallbackWaitTimeSeconds: 300,
238+
routingConfigId: 'test-routing',
239+
});
240+
expect(mockConsoleTable).toHaveBeenCalledWith([mockClient]);
241+
});
242+
243+
it('should put a client without client-id and print result as JSON when format is json', async () => {
244+
(mockClientManagement.putClient as jest.Mock).mockResolvedValue(
245+
mockClient,
246+
);
247+
248+
process.argv = [
249+
'node',
250+
'cli',
251+
'put-client',
252+
'--environment',
253+
'test',
254+
'--client-name',
255+
'Test Client',
256+
'--mesh-mailbox-sender-id',
257+
'test-sender',
258+
'--mesh-mailbox-reports-id',
259+
'test-reports',
260+
'--fallback-wait-time-seconds',
261+
'300',
262+
'--routing-config-id',
263+
'test-routing',
264+
'--format',
265+
'json',
266+
];
267+
268+
await main();
269+
270+
expect(mockClientManagement.putClient).toHaveBeenCalledWith({
271+
clientId: undefined,
272+
clientName: 'Test Client',
273+
meshMailboxSenderId: 'test-sender',
274+
meshMailboxReportsId: 'test-reports',
275+
fallbackWaitTimeSeconds: 300,
276+
routingConfigId: 'test-routing',
277+
});
278+
expect(mockConsoleLog).toHaveBeenCalledWith(
279+
JSON.stringify(mockClient, null, 2),
280+
);
281+
});
282+
});
283+
284+
describe('error handling', () => {
285+
it('should handle errors and set exit code to 1', async () => {
286+
const error = new Error('Test error');
287+
(mockClientManagement.getClient as jest.Mock).mockRejectedValue(error);
288+
289+
process.argv = [
290+
'node',
291+
'cli',
292+
'get-client',
293+
'--environment',
294+
'test',
295+
'--client-id',
296+
'test-client-id',
297+
];
298+
299+
// Expect main to throw the error
300+
await expect(main()).rejects.toThrow('Test error');
301+
});
302+
});
303+
});

utils/client-management/src/entrypoint/cli/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getPrinter(format: PrintFormat): PrintFunction {
1616
/* eslint-enable no-console */
1717
}
1818

19-
async function main() {
19+
export async function main() {
2020
let clientManagement: ReturnType<typeof ClientManagement>;
2121
let print: PrintFunction;
2222

@@ -30,6 +30,7 @@ async function main() {
3030
}
3131

3232
await yargs(hideBin(process.argv))
33+
.exitProcess(false)
3334
.option('environment', {
3435
type: 'string',
3536
global: true,

0 commit comments

Comments
 (0)