Skip to content

Commit f230033

Browse files
authored
added tools unit test (#25)
* feat: add prompt for browser permission before opening login URL fix: format output messages to include new lines for better readability * refactor: wrap command execution in async function for better error handling * test refactor: update tests to use new logger methods and remove unused mocks * feat: add mock data for Auth0 actions, forms, and resource servers for testing * lint
1 parent 4e1e192 commit f230033

File tree

12 files changed

+1598
-315
lines changed

12 files changed

+1598
-315
lines changed

test/init.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import init from '../src/init';
3+
import { requestAuthorization } from '../src/auth/device-auth-flow';
4+
import { findAndUpdatedClaudeConfig } from '../src/clients/claude';
5+
import { log } from '../src/utils/logger';
36

47
// Mock dependencies
5-
vi.mock('../src/utils/auth/device-auth-flow', () => ({
8+
vi.mock('../src/auth/device-auth-flow', () => ({
69
requestAuthorization: vi.fn().mockResolvedValue(undefined),
710
}));
811

@@ -12,13 +15,10 @@ vi.mock('../src/clients/claude', () => ({
1215

1316
vi.mock('../src/utils/logger', () => ({
1417
log: vi.fn(),
18+
logInfo: vi.fn(),
19+
logError: vi.fn(),
1520
}));
1621

17-
// Import mocked modules
18-
import { requestAuthorization } from '../src/utils/auth/device-auth-flow';
19-
import { findAndUpdatedClaudeConfig } from '../src/clients/claude';
20-
import { log } from '../src/utils/logger';
21-
2222
describe('Init Module', () => {
2323
beforeEach(() => {
2424
vi.resetAllMocks();

test/mocks/auth0/actions.ts

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,84 @@
11
// Mock Auth0 actions data for testing
22
export const mockActions = [
33
{
4-
id: 'act_1',
4+
id: 'action1',
55
name: 'Test Action 1',
66
supported_triggers: [
77
{
88
id: 'post-login',
99
version: 'v2',
1010
},
1111
],
12-
code: `
13-
/**
14-
* Handler that will be called during the execution of a PostLogin flow.
15-
*
16-
* @param {Event} event - Details about the user and the context in which they are logging in.
17-
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
18-
*/
19-
exports.onExecutePostLogin = async (event, api) => {
20-
console.log('Test Action 1 executed');
21-
};
22-
`,
12+
code: 'exports.onExecutePostLogin = async (event, api) => { console.log("Hello from action 1"); };',
13+
runtime: 'node18',
2314
status: 'built',
24-
deployed: true,
25-
runtime: 'node16',
26-
created_at: '2023-01-01T00:00:00.000Z',
27-
updated_at: '2023-01-01T00:00:00.000Z',
15+
dependencies: [
16+
{
17+
name: 'lodash',
18+
version: '4.17.21',
19+
},
20+
],
21+
secrets: [
22+
{
23+
name: 'API_KEY',
24+
updated_at: '2023-01-01T00:00:00.000Z',
25+
},
26+
],
2827
},
2928
{
30-
id: 'act_2',
29+
id: 'action2',
3130
name: 'Test Action 2',
3231
supported_triggers: [
3332
{
3433
id: 'credentials-exchange',
3534
version: 'v2',
3635
},
3736
],
38-
code: `
39-
/**
40-
* Handler that will be called during the execution of a Client Credentials exchange.
41-
*
42-
* @param {Event} event - Details about the token request.
43-
* @param {CredentialsExchangeAPI} api - Interface whose methods can be used to change the behavior of the client credentials exchange.
44-
*/
45-
exports.onExecuteCredentialsExchange = async (event, api) => {
46-
console.log('Test Action 2 executed');
47-
};
48-
`,
37+
code: 'exports.onExecuteCredentialsExchange = async (event, api) => { console.log("Hello from action 2"); };',
38+
runtime: 'node18',
4939
status: 'built',
50-
deployed: true,
51-
runtime: 'node16',
52-
created_at: '2023-02-01T00:00:00.000Z',
53-
updated_at: '2023-02-01T00:00:00.000Z',
40+
dependencies: [],
41+
secrets: [],
5442
},
5543
];
5644

45+
// Mock action list response
46+
export const mockActionListResponse = {
47+
actions: mockActions,
48+
total: mockActions.length,
49+
page: 0,
50+
per_page: 10,
51+
};
52+
5753
// Mock single action response
5854
export const mockSingleAction = mockActions[0];
5955

6056
// Mock create action response
6157
export const mockCreateActionResponse = {
62-
id: 'new-act-id',
58+
id: 'new-action-id',
6359
name: 'New Test Action',
6460
supported_triggers: [
6561
{
6662
id: 'post-login',
6763
version: 'v2',
6864
},
6965
],
70-
code: `
71-
/**
72-
* Handler that will be called during the execution of a PostLogin flow.
73-
*
74-
* @param {Event} event - Details about the user and the context in which they are logging in.
75-
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
76-
*/
77-
exports.onExecutePostLogin = async (event, api) => {
78-
console.log('New Test Action executed');
79-
};
80-
`,
81-
status: 'built',
82-
deployed: false,
83-
runtime: 'node16',
84-
created_at: '2023-03-01T00:00:00.000Z',
85-
updated_at: '2023-03-01T00:00:00.000Z',
66+
code: 'exports.onExecutePostLogin = async (event, api) => { console.log("Hello from new action"); };',
67+
runtime: 'node18',
68+
status: 'pending',
69+
dependencies: [],
70+
secrets: [],
8671
};
8772

8873
// Mock update action response
8974
export const mockUpdateActionResponse = {
9075
...mockActions[0],
9176
name: 'Updated Test Action',
92-
code: `
93-
/**
94-
* Handler that will be called during the execution of a PostLogin flow.
95-
*
96-
* @param {Event} event - Details about the user and the context in which they are logging in.
97-
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
98-
*/
99-
exports.onExecutePostLogin = async (event, api) => {
100-
console.log('Updated Test Action executed');
101-
};
102-
`,
103-
updated_at: '2023-03-15T00:00:00.000Z',
77+
code: 'exports.onExecutePostLogin = async (event, api) => { console.log("Updated action"); };',
78+
};
79+
80+
// Mock deploy action response
81+
export const mockDeployActionResponse = {
82+
...mockActions[0],
83+
status: 'built',
10484
};

test/mocks/auth0/forms.ts

Lines changed: 117 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,137 @@
11
// Mock Auth0 forms data for testing
22
export const mockForms = [
33
{
4-
id: 'form_1',
5-
name: 'login',
6-
prompt: 'login',
7-
status: 'enabled',
8-
is_active: true,
9-
layout: {
10-
form_elements: [
11-
{
12-
label: 'Email',
13-
type: 'text',
14-
name: 'email',
15-
placeholder: 'Enter your email',
16-
required: true,
17-
},
18-
{
19-
label: 'Password',
20-
type: 'password',
21-
name: 'password',
22-
placeholder: 'Enter your password',
23-
required: true,
24-
},
25-
],
26-
submit_button: {
27-
text: 'Log In',
4+
id: 'form1',
5+
name: 'Test Form 1',
6+
messages: {
7+
success: 'Form submitted successfully',
8+
},
9+
languages: {
10+
default: 'en',
11+
supported: ['en', 'es'],
12+
},
13+
translations: {
14+
en: {
15+
title: 'Test Form',
16+
submit: 'Submit',
17+
},
18+
es: {
19+
title: 'Formulario de Prueba',
20+
submit: 'Enviar',
2821
},
2922
},
23+
nodes: [
24+
{
25+
id: 'node1',
26+
type: 'text',
27+
label: 'Name',
28+
required: true,
29+
},
30+
{
31+
id: 'node2',
32+
type: 'email',
33+
label: 'Email',
34+
required: true,
35+
},
36+
],
37+
start: {
38+
node: 'node1',
39+
},
40+
ending: {
41+
redirect: 'https://example.com/thank-you',
42+
},
43+
style: {
44+
theme: 'light',
45+
},
3046
},
3147
{
32-
id: 'form_2',
33-
name: 'signup',
34-
prompt: 'signup',
35-
status: 'enabled',
36-
is_active: true,
37-
layout: {
38-
form_elements: [
39-
{
40-
label: 'Email',
41-
type: 'text',
42-
name: 'email',
43-
placeholder: 'Enter your email',
44-
required: true,
45-
},
46-
{
47-
label: 'Password',
48-
type: 'password',
49-
name: 'password',
50-
placeholder: 'Create a password',
51-
required: true,
52-
},
53-
{
54-
label: 'Confirm Password',
55-
type: 'password',
56-
name: 'confirm_password',
57-
placeholder: 'Confirm your password',
58-
required: true,
59-
},
60-
],
61-
submit_button: {
62-
text: 'Sign Up',
48+
id: 'form2',
49+
name: 'Test Form 2',
50+
messages: {
51+
success: 'Thank you for your submission',
52+
},
53+
languages: {
54+
default: 'en',
55+
supported: ['en'],
56+
},
57+
translations: {
58+
en: {
59+
title: 'Another Test Form',
60+
submit: 'Submit',
6361
},
6462
},
63+
nodes: [
64+
{
65+
id: 'node1',
66+
type: 'text',
67+
label: 'Full Name',
68+
required: true,
69+
},
70+
],
71+
start: {
72+
node: 'node1',
73+
},
74+
ending: {
75+
message: 'Thank you for your submission',
76+
},
77+
style: {
78+
theme: 'dark',
79+
},
6580
},
6681
];
6782

83+
// Mock form list response
84+
export const mockFormListResponse = {
85+
forms: mockForms,
86+
total: mockForms.length,
87+
page: 0,
88+
per_page: 50,
89+
};
90+
6891
// Mock single form response
6992
export const mockSingleForm = mockForms[0];
7093

94+
// Mock create form response
95+
export const mockCreateFormResponse = {
96+
id: 'new-form-id',
97+
name: 'New Test Form',
98+
messages: {
99+
success: 'Form created successfully',
100+
},
101+
languages: {
102+
default: 'en',
103+
supported: ['en'],
104+
},
105+
translations: {
106+
en: {
107+
title: 'New Form',
108+
submit: 'Submit',
109+
},
110+
},
111+
nodes: [
112+
{
113+
id: 'node1',
114+
type: 'text',
115+
label: 'Name',
116+
required: true,
117+
},
118+
],
119+
start: {
120+
node: 'node1',
121+
},
122+
ending: {
123+
message: 'Thank you',
124+
},
125+
style: {
126+
theme: 'light',
127+
},
128+
};
129+
71130
// Mock update form response
72131
export const mockUpdateFormResponse = {
73132
...mockForms[0],
74-
layout: {
75-
...mockForms[0].layout,
76-
form_elements: [
77-
...mockForms[0].layout.form_elements,
78-
{
79-
label: 'Remember Me',
80-
type: 'checkbox',
81-
name: 'remember_me',
82-
required: false,
83-
},
84-
],
133+
name: 'Updated Test Form',
134+
messages: {
135+
success: 'Form updated successfully',
85136
},
86137
};

0 commit comments

Comments
 (0)