|
| 1 | +import { describe, it, expect, vi, beforeAll, afterAll, beforeEach, afterEach, MockInstance } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { N8nNodeLoader } from '@/loaders/node-loader'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Regression tests for issue #937: nodes-base.evaluationTrigger was silently |
| 9 | + * dropped from the database. Two compounding causes: |
| 10 | + * |
| 11 | + * 1. The node-name regex only matched `.node.js`/`.node.ts`, so enterprise |
| 12 | + * `.node.ee.js` paths produced a garbage node name and the named-export |
| 13 | + * lookup missed. |
| 14 | + * 2. The export fallback took the FIRST module export, which for |
| 15 | + * EvaluationTrigger is the constant DEFAULT_STARTING_ROW (a number), not |
| 16 | + * the node class — so a number was passed downstream and parsing failed. |
| 17 | + * |
| 18 | + * These tests exercise the REAL loader against fixture packages on disk. |
| 19 | + */ |
| 20 | +describe('N8nNodeLoader enterprise (.node.ee.js) modules', () => { |
| 21 | + let fixtureDir: string; |
| 22 | + let consoleLogSpy: MockInstance; |
| 23 | + let consoleErrorSpy: MockInstance; |
| 24 | + let consoleWarnSpy: MockInstance; |
| 25 | + |
| 26 | + const writeFixture = (relPath: string, content: string) => { |
| 27 | + const fullPath = path.join(fixtureDir, relPath); |
| 28 | + fs.mkdirSync(path.dirname(fullPath), { recursive: true }); |
| 29 | + fs.writeFileSync(fullPath, content); |
| 30 | + }; |
| 31 | + |
| 32 | + beforeAll(() => { |
| 33 | + fixtureDir = fs.mkdtempSync(path.join(os.tmpdir(), 'n8n-mcp-loader-ee-fixture-')); |
| 34 | + |
| 35 | + writeFixture('package.json', JSON.stringify({ name: 'fixture-pkg', version: '1.0.0' })); |
| 36 | + |
| 37 | + // Mirrors n8n-nodes-base EvaluationTrigger.node.ee.js: a non-class export |
| 38 | + // listed BEFORE the node class. |
| 39 | + writeFixture( |
| 40 | + 'dist/nodes/Evaluation/EvaluationTrigger/EvaluationTrigger.node.ee.js', |
| 41 | + `const DEFAULT_STARTING_ROW = 2; |
| 42 | + class EvaluationTrigger { |
| 43 | + constructor() { |
| 44 | + this.description = { name: 'evaluationTrigger', displayName: 'Evaluation Trigger', properties: [] }; |
| 45 | + } |
| 46 | + } |
| 47 | + module.exports = { DEFAULT_STARTING_ROW, EvaluationTrigger };` |
| 48 | + ); |
| 49 | + |
| 50 | + // Mirrors n8n-nodes-base Evaluation.node.ee.js: single class export. |
| 51 | + writeFixture( |
| 52 | + 'dist/nodes/Evaluation/Evaluation/Evaluation.node.ee.js', |
| 53 | + `class Evaluation { |
| 54 | + constructor() { |
| 55 | + this.description = { name: 'evaluation', displayName: 'Evaluation', properties: [] }; |
| 56 | + } |
| 57 | + } |
| 58 | + module.exports = { Evaluation };` |
| 59 | + ); |
| 60 | + |
| 61 | + // A module whose export name matches neither the file name nor a default |
| 62 | + // export, with a non-class export first: the fallback must still resolve |
| 63 | + // the class, never a constant. |
| 64 | + writeFixture( |
| 65 | + 'dist/nodes/Renamed/Renamed.node.js', |
| 66 | + `const SOME_CONSTANT = 42; |
| 67 | + class InternalName { |
| 68 | + constructor() { |
| 69 | + this.description = { name: 'renamed', displayName: 'Renamed', properties: [] }; |
| 70 | + } |
| 71 | + } |
| 72 | + module.exports = { SOME_CONSTANT, InternalName };` |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + afterAll(() => { |
| 77 | + fs.rmSync(fixtureDir, { recursive: true, force: true }); |
| 78 | + }); |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 82 | + consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 83 | + consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 84 | + }); |
| 85 | + |
| 86 | + afterEach(() => { |
| 87 | + consoleLogSpy.mockRestore(); |
| 88 | + consoleErrorSpy.mockRestore(); |
| 89 | + consoleWarnSpy.mockRestore(); |
| 90 | + }); |
| 91 | + |
| 92 | + const loadFixturePackage = async (nodePaths: string[]) => { |
| 93 | + const loader = new N8nNodeLoader(); |
| 94 | + const packageJson = { n8n: { nodes: nodePaths } }; |
| 95 | + return (loader as any).loadPackageNodes('fixture-pkg', fixtureDir, packageJson); |
| 96 | + }; |
| 97 | + |
| 98 | + it('extracts the node name from a .node.ee.js path and resolves the class by name', async () => { |
| 99 | + const results = await loadFixturePackage([ |
| 100 | + 'dist/nodes/Evaluation/EvaluationTrigger/EvaluationTrigger.node.ee.js' |
| 101 | + ]); |
| 102 | + |
| 103 | + expect(results).toHaveLength(1); |
| 104 | + expect(results[0].nodeName).toBe('EvaluationTrigger'); |
| 105 | + expect(typeof results[0].NodeClass).toBe('function'); |
| 106 | + const instance = new results[0].NodeClass(); |
| 107 | + expect(instance.description.name).toBe('evaluationTrigger'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('loads a single-export .node.ee.js module', async () => { |
| 111 | + const results = await loadFixturePackage([ |
| 112 | + 'dist/nodes/Evaluation/Evaluation/Evaluation.node.ee.js' |
| 113 | + ]); |
| 114 | + |
| 115 | + expect(results).toHaveLength(1); |
| 116 | + expect(results[0].nodeName).toBe('Evaluation'); |
| 117 | + const instance = new results[0].NodeClass(); |
| 118 | + expect(instance.description.name).toBe('evaluation'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('never resolves a non-class export as the node class', async () => { |
| 122 | + const results = await loadFixturePackage(['dist/nodes/Renamed/Renamed.node.js']); |
| 123 | + |
| 124 | + expect(results).toHaveLength(1); |
| 125 | + expect(results[0].nodeName).toBe('Renamed'); |
| 126 | + expect(typeof results[0].NodeClass).toBe('function'); |
| 127 | + const instance = new results[0].NodeClass(); |
| 128 | + expect(instance.description.name).toBe('renamed'); |
| 129 | + }); |
| 130 | +}); |
0 commit comments