@pagelines/n8n-mcp 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,467 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest';
2
- import { N8nClient } from './n8n-client.js';
3
- import { N8N_WORKFLOW_WRITABLE_FIELDS, pickFields } from './types.js';
4
-
5
- // Mock fetch globally
6
- const mockFetch = vi.fn();
7
- global.fetch = mockFetch;
8
-
9
- describe('N8nClient', () => {
10
- let client: N8nClient;
11
-
12
- beforeEach(() => {
13
- client = new N8nClient({
14
- apiUrl: 'https://n8n.example.com',
15
- apiKey: 'test-api-key',
16
- });
17
- mockFetch.mockReset();
18
- });
19
-
20
- describe('constructor', () => {
21
- it('normalizes URL by removing trailing slash', () => {
22
- const clientWithSlash = new N8nClient({
23
- apiUrl: 'https://n8n.example.com/',
24
- apiKey: 'key',
25
- });
26
- // Access private property for testing
27
- expect((clientWithSlash as any).baseUrl).toBe('https://n8n.example.com');
28
- });
29
- });
30
-
31
- describe('listWorkflows', () => {
32
- it('calls correct endpoint', async () => {
33
- mockFetch.mockResolvedValueOnce({
34
- ok: true,
35
- text: async () => JSON.stringify({ data: [] }),
36
- });
37
-
38
- await client.listWorkflows();
39
-
40
- expect(mockFetch).toHaveBeenCalledWith(
41
- 'https://n8n.example.com/api/v1/workflows',
42
- expect.objectContaining({
43
- method: 'GET',
44
- headers: expect.objectContaining({
45
- 'X-N8N-API-KEY': 'test-api-key',
46
- }),
47
- })
48
- );
49
- });
50
-
51
- it('includes query params when provided', async () => {
52
- mockFetch.mockResolvedValueOnce({
53
- ok: true,
54
- text: async () => JSON.stringify({ data: [] }),
55
- });
56
-
57
- await client.listWorkflows({ active: true, limit: 10 });
58
-
59
- expect(mockFetch).toHaveBeenCalledWith(
60
- expect.stringContaining('active=true'),
61
- expect.any(Object)
62
- );
63
- expect(mockFetch).toHaveBeenCalledWith(
64
- expect.stringContaining('limit=10'),
65
- expect.any(Object)
66
- );
67
- });
68
- });
69
-
70
- describe('patchWorkflow', () => {
71
- const mockWorkflow = {
72
- id: '1',
73
- name: 'test_workflow',
74
- active: false,
75
- nodes: [
76
- {
77
- id: 'node1',
78
- name: 'existing_node',
79
- type: 'n8n-nodes-base.set',
80
- typeVersion: 1,
81
- position: [0, 0] as [number, number],
82
- parameters: { param1: 'value1', param2: 'value2' },
83
- },
84
- ],
85
- connections: {},
86
- createdAt: '2024-01-01',
87
- updatedAt: '2024-01-01',
88
- };
89
-
90
- beforeEach(() => {
91
- // Mock GET workflow
92
- mockFetch.mockResolvedValueOnce({
93
- ok: true,
94
- text: async () => JSON.stringify(mockWorkflow),
95
- });
96
- // Mock PUT workflow
97
- mockFetch.mockResolvedValueOnce({
98
- ok: true,
99
- text: async () => JSON.stringify(mockWorkflow),
100
- });
101
- });
102
-
103
- it('warns when updateNode would remove parameters', async () => {
104
- const { warnings } = await client.patchWorkflow('1', [
105
- {
106
- type: 'updateNode',
107
- nodeName: 'existing_node',
108
- properties: {
109
- parameters: { newParam: 'newValue' }, // Missing param1, param2
110
- },
111
- },
112
- ]);
113
-
114
- expect(warnings).toContainEqual(
115
- expect.stringContaining('remove parameters')
116
- );
117
- expect(warnings).toContainEqual(
118
- expect.stringContaining('param1')
119
- );
120
- });
121
-
122
- it('warns when removing non-existent node', async () => {
123
- const { warnings } = await client.patchWorkflow('1', [
124
- {
125
- type: 'removeNode',
126
- nodeName: 'nonexistent_node',
127
- },
128
- ]);
129
-
130
- expect(warnings).toContainEqual(
131
- expect.stringContaining('not found')
132
- );
133
- });
134
-
135
- it('adds node correctly', async () => {
136
- mockFetch.mockReset();
137
- mockFetch.mockResolvedValueOnce({
138
- ok: true,
139
- text: async () => JSON.stringify(mockWorkflow),
140
- });
141
-
142
- const updatedWorkflow = {
143
- ...mockWorkflow,
144
- nodes: [
145
- ...mockWorkflow.nodes,
146
- {
147
- id: 'new-id',
148
- name: 'new_node',
149
- type: 'n8n-nodes-base.code',
150
- typeVersion: 1,
151
- position: [100, 100],
152
- parameters: {},
153
- },
154
- ],
155
- };
156
-
157
- mockFetch.mockResolvedValueOnce({
158
- ok: true,
159
- text: async () => JSON.stringify(updatedWorkflow),
160
- });
161
-
162
- const { workflow } = await client.patchWorkflow('1', [
163
- {
164
- type: 'addNode',
165
- node: {
166
- name: 'new_node',
167
- type: 'n8n-nodes-base.code',
168
- typeVersion: 1,
169
- position: [100, 100],
170
- parameters: {},
171
- },
172
- },
173
- ]);
174
-
175
- // Verify PUT was called with the new node
176
- const putCall = mockFetch.mock.calls[1];
177
- const putBody = JSON.parse(putCall[1].body);
178
- expect(putBody.nodes).toHaveLength(2);
179
- expect(putBody.nodes[1].name).toBe('new_node');
180
- });
181
-
182
- it('adds connection correctly', async () => {
183
- mockFetch.mockReset();
184
- mockFetch.mockResolvedValueOnce({
185
- ok: true,
186
- text: async () => JSON.stringify(mockWorkflow),
187
- });
188
-
189
- const updatedWorkflow = {
190
- ...mockWorkflow,
191
- connections: {
192
- existing_node: {
193
- main: [[{ node: 'target_node', type: 'main', index: 0 }]],
194
- },
195
- },
196
- };
197
-
198
- mockFetch.mockResolvedValueOnce({
199
- ok: true,
200
- text: async () => JSON.stringify(updatedWorkflow),
201
- });
202
-
203
- await client.patchWorkflow('1', [
204
- {
205
- type: 'addConnection',
206
- from: 'existing_node',
207
- to: 'target_node',
208
- },
209
- ]);
210
-
211
- const putCall = mockFetch.mock.calls[1];
212
- const putBody = JSON.parse(putCall[1].body);
213
- expect(putBody.connections.existing_node.main[0][0].node).toBe('target_node');
214
- });
215
- });
216
-
217
- describe('error handling', () => {
218
- it('throws on non-ok response', async () => {
219
- mockFetch.mockResolvedValueOnce({
220
- ok: false,
221
- status: 404,
222
- text: async () => 'Not found',
223
- });
224
-
225
- await expect(client.getWorkflow('999')).rejects.toThrow('n8n API error (404)');
226
- });
227
- });
228
-
229
- describe('listNodeTypes', () => {
230
- it('calls correct endpoint', async () => {
231
- const mockNodeTypes = [
232
- {
233
- name: 'n8n-nodes-base.webhook',
234
- displayName: 'Webhook',
235
- description: 'Starts workflow on webhook call',
236
- group: ['trigger'],
237
- version: 2,
238
- },
239
- {
240
- name: 'n8n-nodes-base.set',
241
- displayName: 'Set',
242
- description: 'Set values',
243
- group: ['transform'],
244
- version: 3,
245
- },
246
- ];
247
-
248
- mockFetch.mockResolvedValueOnce({
249
- ok: true,
250
- text: async () => JSON.stringify(mockNodeTypes),
251
- });
252
-
253
- const result = await client.listNodeTypes();
254
-
255
- expect(mockFetch).toHaveBeenCalledWith(
256
- 'https://n8n.example.com/api/v1/nodes',
257
- expect.objectContaining({
258
- method: 'GET',
259
- headers: expect.objectContaining({
260
- 'X-N8N-API-KEY': 'test-api-key',
261
- }),
262
- })
263
- );
264
-
265
- expect(result).toHaveLength(2);
266
- expect(result[0].name).toBe('n8n-nodes-base.webhook');
267
- expect(result[1].name).toBe('n8n-nodes-base.set');
268
- });
269
- });
270
-
271
- describe('updateWorkflow', () => {
272
- it('strips disallowed properties before sending to API', async () => {
273
- const fullWorkflow = {
274
- id: '123',
275
- name: 'test_workflow',
276
- active: true,
277
- nodes: [{ id: 'n1', name: 'node1', type: 'test', typeVersion: 1, position: [0, 0] as [number, number], parameters: {} }],
278
- connections: {},
279
- settings: { timezone: 'UTC' },
280
- createdAt: '2024-01-01T00:00:00.000Z',
281
- updatedAt: '2024-01-02T00:00:00.000Z',
282
- versionId: 'v1',
283
- staticData: undefined,
284
- tags: [{ id: 't1', name: 'tag1' }],
285
- };
286
-
287
- mockFetch.mockResolvedValueOnce({
288
- ok: true,
289
- text: async () => JSON.stringify(fullWorkflow),
290
- });
291
-
292
- await client.updateWorkflow('123', fullWorkflow);
293
-
294
- // Verify the request body does NOT contain disallowed properties
295
- const putCall = mockFetch.mock.calls[0];
296
- const putBody = JSON.parse(putCall[1].body);
297
-
298
- // These should be stripped
299
- expect(putBody.id).toBeUndefined();
300
- expect(putBody.createdAt).toBeUndefined();
301
- expect(putBody.updatedAt).toBeUndefined();
302
- expect(putBody.active).toBeUndefined();
303
- expect(putBody.versionId).toBeUndefined();
304
-
305
- // These should be preserved
306
- expect(putBody.name).toBe('test_workflow');
307
- expect(putBody.nodes).toHaveLength(1);
308
- expect(putBody.connections).toEqual({});
309
- expect(putBody.settings).toEqual({ timezone: 'UTC' });
310
- expect(putBody.staticData).toBeUndefined();
311
- expect(putBody.tags).toEqual([{ id: 't1', name: 'tag1' }]);
312
- });
313
-
314
- it('works with partial workflow (only some fields)', async () => {
315
- mockFetch.mockResolvedValueOnce({
316
- ok: true,
317
- text: async () => JSON.stringify({ id: '123', name: 'updated' }),
318
- });
319
-
320
- await client.updateWorkflow('123', { name: 'updated', nodes: [] });
321
-
322
- const putCall = mockFetch.mock.calls[0];
323
- const putBody = JSON.parse(putCall[1].body);
324
-
325
- expect(putBody.name).toBe('updated');
326
- expect(putBody.nodes).toEqual([]);
327
- });
328
-
329
- it('handles workflow from formatWorkflow (simulating workflow_format apply)', async () => {
330
- // This simulates the exact scenario that caused the bug:
331
- // workflow_format returns a full N8nWorkflow object with id, createdAt, etc.
332
- const formattedWorkflow = {
333
- id: 'zbB1fCxWgZXgpjB1',
334
- name: 'my_workflow',
335
- active: false,
336
- nodes: [],
337
- connections: {},
338
- createdAt: '2024-01-01T00:00:00.000Z',
339
- updatedAt: '2024-01-02T00:00:00.000Z',
340
- };
341
-
342
- mockFetch.mockResolvedValueOnce({
343
- ok: true,
344
- text: async () => JSON.stringify(formattedWorkflow),
345
- });
346
-
347
- // This should NOT throw "must NOT have additional properties"
348
- await client.updateWorkflow('zbB1fCxWgZXgpjB1', formattedWorkflow);
349
-
350
- const putCall = mockFetch.mock.calls[0];
351
- const putBody = JSON.parse(putCall[1].body);
352
-
353
- // Only writable fields should be sent (schema-driven from N8N_WORKFLOW_WRITABLE_FIELDS)
354
- const sentKeys = Object.keys(putBody).sort();
355
- const expectedKeys = ['connections', 'name', 'nodes']; // Only non-undefined writable fields
356
- expect(sentKeys).toEqual(expectedKeys);
357
-
358
- // Read-only fields must NOT be in request
359
- expect(putBody.id).toBeUndefined();
360
- expect(putBody.createdAt).toBeUndefined();
361
- expect(putBody.updatedAt).toBeUndefined();
362
- expect(putBody.active).toBeUndefined();
363
- });
364
-
365
- it('filters out any unknown properties using schema-driven approach', async () => {
366
- // Real n8n API returns many properties not in our type definition
367
- // Schema-driven filtering ensures only N8N_WORKFLOW_WRITABLE_FIELDS are sent
368
- const realN8nWorkflow = {
369
- id: '123',
370
- name: 'test_workflow',
371
- active: true,
372
- nodes: [{ id: 'n1', name: 'node1', type: 'test', typeVersion: 1, position: [0, 0] as [number, number], parameters: {} }],
373
- connections: {},
374
- settings: { timezone: 'UTC' },
375
- staticData: { lastId: 5 },
376
- tags: [{ id: 't1', name: 'production' }],
377
- createdAt: '2024-01-01T00:00:00.000Z',
378
- updatedAt: '2024-01-02T00:00:00.000Z',
379
- versionId: 'v1',
380
- // Properties that real n8n returns but aren't in writable fields:
381
- homeProject: { id: 'proj1', type: 'personal', name: 'My Project' },
382
- sharedWithProjects: [],
383
- usedCredentials: [{ id: 'cred1', name: 'My API Key', type: 'apiKey' }],
384
- meta: { instanceId: 'abc123' },
385
- pinData: {},
386
- triggerCount: 5,
387
- unknownFutureField: 'whatever',
388
- };
389
-
390
- mockFetch.mockResolvedValueOnce({
391
- ok: true,
392
- text: async () => JSON.stringify(realN8nWorkflow),
393
- });
394
-
395
- await client.updateWorkflow('123', realN8nWorkflow as any);
396
-
397
- const putCall = mockFetch.mock.calls[0];
398
- const putBody = JSON.parse(putCall[1].body);
399
-
400
- // Request should ONLY contain fields from N8N_WORKFLOW_WRITABLE_FIELDS
401
- const sentKeys = Object.keys(putBody).sort();
402
- const allowedKeys = [...N8N_WORKFLOW_WRITABLE_FIELDS].sort();
403
-
404
- // Every sent key must be in the allowed list
405
- for (const key of sentKeys) {
406
- expect(allowedKeys).toContain(key);
407
- }
408
-
409
- // Verify exact expected keys (all writable fields that had values)
410
- expect(sentKeys).toEqual(['connections', 'name', 'nodes', 'settings', 'staticData', 'tags']);
411
- });
412
- });
413
- });
414
-
415
- // ─────────────────────────────────────────────────────────────
416
- // Schema utilities (types.ts)
417
- // ─────────────────────────────────────────────────────────────
418
-
419
- describe('pickFields utility', () => {
420
- it('picks only specified fields', () => {
421
- const obj = { a: 1, b: 2, c: 3, d: 4 };
422
- const result = pickFields(obj, ['a', 'c'] as const);
423
-
424
- expect(result).toEqual({ a: 1, c: 3 });
425
- expect(Object.keys(result)).toEqual(['a', 'c']);
426
- });
427
-
428
- it('ignores undefined values', () => {
429
- const obj = { a: 1, b: undefined, c: 3 };
430
- const result = pickFields(obj, ['a', 'b', 'c'] as const);
431
-
432
- expect(result).toEqual({ a: 1, c: 3 });
433
- expect('b' in result).toBe(false);
434
- });
435
-
436
- it('ignores fields not in object', () => {
437
- const obj = { a: 1 };
438
- const result = pickFields(obj as any, ['a', 'missing'] as const);
439
-
440
- expect(result).toEqual({ a: 1 });
441
- });
442
-
443
- it('returns empty object for empty fields array', () => {
444
- const obj = { a: 1, b: 2 };
445
- const result = pickFields(obj, [] as const);
446
-
447
- expect(result).toEqual({});
448
- });
449
- });
450
-
451
- describe('N8N_WORKFLOW_WRITABLE_FIELDS schema', () => {
452
- it('contains expected writable fields', () => {
453
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('name');
454
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('nodes');
455
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('connections');
456
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('settings');
457
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('staticData');
458
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).toContain('tags');
459
- });
460
-
461
- it('does NOT contain read-only fields', () => {
462
- const readOnlyFields = ['id', 'active', 'createdAt', 'updatedAt', 'versionId'];
463
- for (const field of readOnlyFields) {
464
- expect(N8N_WORKFLOW_WRITABLE_FIELDS).not.toContain(field);
465
- }
466
- });
467
- });