1
+import { describe, it, expect } from 'vitest';
2
+import { transformToolkitRetrieveResponse } from '../../../src/utils/transformers/toolkits';
3
+
4
+type RawRetrieveResponse = Parameters<typeof transformToolkitRetrieveResponse>[0];
5
+
6
+/**
7
+ * The API describes every auth form field with an `is_secret` flag ("Clients use
8
+ * it to decide whether to mask the input"), a `legacy_template_name`, and every
9
+ * auth method with an `auth_hint_url`. All three are snake_case on the wire, so a
10
+ * pass-through leaves them under keys the zod schema does not know and validation
11
+ * strips them.
12
+ *
13
+ * Every one of the four field groups is populated here, because the transformer
14
+ * maps each of them separately.
15
+ */
16
+const rawToolkit = {
17
+ name: 'Shopify',
18
+ slug: 'shopify',
19
+ meta: {
20
+ created_at: '2024-01-01T00:00:00Z',
21
+ updated_at: '2024-01-02T00:00:00Z',
22
+ tools_count: 12,
23
+ triggers_count: 3,
24
+ },
25
+ is_local_toolkit: false,
26
+ composio_managed_auth_schemes: ['OAUTH2'],
27
+ auth_config_details: [
28
+ {
29
+ name: 'API Key',
30
+ mode: 'API_KEY',
31
+ auth_hint_url: 'https://shopify.dev/api-keys',
32
+ fields: {
33
+ auth_config_creation: {
34
+ required: [
35
+ {
36
+ name: 'client_secret',
37
+ displayName: 'Client Secret',
38
+ description: 'The app client secret',
39
+ type: 'string',
40
+ required: true,
41
+ is_secret: true,
42
+ legacy_template_name: 'clientSecret',
43
+ },
44
+ ],
45
+ optional: [
46
+ {
47
+ name: 'scopes',
48
+ displayName: 'Scopes',
49
+ description: 'Comma separated scopes',
50
+ type: 'string',
51
+ required: false,
52
+ is_secret: false,
53
+ default: null,
54
+ legacy_template_name: 'oauthScopes',
55
+ },
56
+ ],
57
+ },
58
+ connected_account_initiation: {
59
+ required: [
60
+ {
61
+ name: 'api_key',
62
+ displayName: 'API Key',
63
+ description: 'Your Shopify API key',
64
+ type: 'string',
65
+ required: true,
66
+ is_secret: true,
67
+ legacy_template_name: 'apiKey',
68
+ },
69
+ ],
70
+ optional: [
71
+ {
72
+ name: 'shop_domain',
73
+ displayName: 'Shop Domain',
74
+ description: 'your-store.myshopify.com',
75
+ type: 'string',
76
+ required: false,
77
+ is_secret: false,
78
+ default: 'example.myshopify.com',
79
+ },
80
+ ],
81
+ },
82
+ },
83
+ },
84
+ ],
85
+} as unknown as RawRetrieveResponse;
86
+
87
+/** Same shape, but the API omits every optional key. */
88
+const rawToolkitWithoutOptionalKeys = {
89
+ ...rawToolkit,
90
+ auth_config_details: [
91
+ {
92
+ name: 'Bearer',
93
+ mode: 'BEARER_TOKEN',
94
+ fields: {
95
+ auth_config_creation: { required: [], optional: [] },
96
+ connected_account_initiation: {
97
+ required: [
98
+ {
99
+ name: 'token',
100
+ displayName: 'Token',
101
+ description: 'A bearer token',
102
+ type: 'string',
103
+ required: true,
104
+ },
105
+ ],
106
+ optional: [],
107
+ },
108
+ },
109
+ },
110
+ ],
111
+} as unknown as RawRetrieveResponse;
112
+
113
+const initiationRequired = (raw: RawRetrieveResponse) =>
114
+ transformToolkitRetrieveResponse(raw).authConfigDetails?.[0].fields.connectedAccountInitiation
115
+ .required[0];
116
+
117
+describe('transformToolkitRetrieveResponse', () => {
118
+ it('preserves is_secret as isSecret in all four auth field groups', () => {
119
+ const fields = transformToolkitRetrieveResponse(rawToolkit).authConfigDetails?.[0].fields;
120
+
121
+ expect(fields?.authConfigCreation.required[0].isSecret).toBe(true);
122
+ expect(fields?.authConfigCreation.optional[0].isSecret).toBe(false);
123
+ expect(fields?.connectedAccountInitiation.required[0].isSecret).toBe(true);
124
+ expect(fields?.connectedAccountInitiation.optional[0].isSecret).toBe(false);
125
+ });
126
+
127
+ it('preserves legacy_template_name as legacyTemplateName', () => {
128
+ const fields = transformToolkitRetrieveResponse(rawToolkit).authConfigDetails?.[0].fields;
129
+
130
+ expect(fields?.authConfigCreation.required[0].legacyTemplateName).toBe('clientSecret');
131
+ expect(fields?.authConfigCreation.optional[0].legacyTemplateName).toBe('oauthScopes');
132
+ expect(fields?.connectedAccountInitiation.required[0].legacyTemplateName).toBe('apiKey');
133
+ });
134
+
135
+ it('maps a whole field without losing or inventing keys', () => {
136
+ expect(initiationRequired(rawToolkit)).toEqual({
137
+ name: 'api_key',
138
+ displayName: 'API Key',
139
+ description: 'Your Shopify API key',
140
+ type: 'string',
141
+ required: true,
142
+ isSecret: true,
143
+ legacyTemplateName: 'apiKey',
144
+ });
145
+ });
146
+
147
+ it('keeps a null default and a false flag rather than dropping them', () => {
148
+ const field =
149
+ transformToolkitRetrieveResponse(rawToolkit).authConfigDetails?.[0].fields.authConfigCreation
150
+ .optional[0];
151
+
152
+ expect(field?.default).toBeNull();
153
+ expect(field?.isSecret).toBe(false);
154
+ });
155
+
156
+ it('maps auth_hint_url to authHintUrl', () => {
157
+ const details = transformToolkitRetrieveResponse(rawToolkit).authConfigDetails?.[0];
158
+
159
+ expect(details?.authHintUrl).toBe('https://shopify.dev/api-keys');
160
+ });
161
+
162
+ /**
163
+ * `toEqual` treats a missing property and one holding `undefined` as equal, so
164
+ * these assertions use `toHaveProperty`. Creating the key unconditionally would
165
+ * override a caller's fallback in `{ default: 'fallback', ...field }`.
166
+ */
167
+ it('omits optional keys entirely when the API does not send them', () => {
168
+ const field = initiationRequired(rawToolkitWithoutOptionalKeys);
169
+
170
+ expect(field).not.toHaveProperty('default');
171
+ expect(field).not.toHaveProperty('isSecret');
172
+ expect(field).not.toHaveProperty('legacyTemplateName');
173
+ expect(
174
+ transformToolkitRetrieveResponse(rawToolkitWithoutOptionalKeys).authConfigDetails?.[0]
175
+ ).not.toHaveProperty('authHintUrl');
176
+ });
177
+
178
+ it('lets a caller fallback survive spreading a field with no default', () => {
179
+ const field = initiationRequired(rawToolkitWithoutOptionalKeys);
180
+
181
+ expect({ default: 'caller fallback', ...field }.default).toBe('caller fallback');
182
+ });
183
+});