Skip to content

Commit 72f87ad

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

2 files changed

Lines changed: 314 additions & 1 deletion

File tree

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

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)