|
1 | 1 | import type { BaseLanguageModel } from '@langchain/core/language_models/base';
|
2 | 2 | import { FakeListChatModel } from '@langchain/core/utils/testing';
|
| 3 | +import { mock } from 'jest-mock-extended'; |
3 | 4 | import get from 'lodash/get';
|
4 |
| -import type { IDataObject, IExecuteFunctions } from 'n8n-workflow'; |
| 5 | +import type { IDataObject, IExecuteFunctions, INode } from 'n8n-workflow'; |
5 | 6 |
|
6 | 7 | import { makeZodSchemaFromAttributes } from '../helpers';
|
7 | 8 | import { InformationExtractor } from '../InformationExtractor.node';
|
@@ -88,6 +89,208 @@ describe('InformationExtractor', () => {
|
88 | 89 | });
|
89 | 90 | });
|
90 | 91 |
|
| 92 | + describe('Single Item Processing with JSON Schema from Example', () => { |
| 93 | + it('should extract information using JSON schema from example - version 1.2 (required fields)', async () => { |
| 94 | + const node = new InformationExtractor(); |
| 95 | + const inputData = [ |
| 96 | + { |
| 97 | + json: { text: 'John lives in California and has visited Los Angeles and San Francisco' }, |
| 98 | + }, |
| 99 | + ]; |
| 100 | + |
| 101 | + const mockExecuteFunctions = createExecuteFunctionsMock( |
| 102 | + { |
| 103 | + text: 'John lives in California and has visited Los Angeles and San Francisco', |
| 104 | + schemaType: 'fromJson', |
| 105 | + jsonSchemaExample: JSON.stringify({ |
| 106 | + state: 'California', |
| 107 | + cities: ['Los Angeles', 'San Francisco'], |
| 108 | + }), |
| 109 | + options: { |
| 110 | + systemPromptTemplate: '', |
| 111 | + }, |
| 112 | + }, |
| 113 | + new FakeListChatModel({ |
| 114 | + responses: [ |
| 115 | + formatFakeLlmResponse({ |
| 116 | + state: 'California', |
| 117 | + cities: ['Los Angeles', 'San Francisco'], |
| 118 | + }), |
| 119 | + ], |
| 120 | + }), |
| 121 | + inputData, |
| 122 | + ); |
| 123 | + |
| 124 | + // Mock version 1.2 to test required fields behavior |
| 125 | + mockExecuteFunctions.getNode = () => mock<INode>({ typeVersion: 1.2 }); |
| 126 | + |
| 127 | + const response = await node.execute.call(mockExecuteFunctions); |
| 128 | + |
| 129 | + expect(response).toEqual([ |
| 130 | + [ |
| 131 | + { |
| 132 | + json: { |
| 133 | + output: { |
| 134 | + state: 'California', |
| 135 | + cities: ['Los Angeles', 'San Francisco'], |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + ], |
| 140 | + ]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should extract information using JSON schema from example - version 1.1 (optional fields)', async () => { |
| 144 | + const node = new InformationExtractor(); |
| 145 | + const inputData = [{ json: { text: 'John lives in California' } }]; |
| 146 | + |
| 147 | + const mockExecuteFunctions = createExecuteFunctionsMock( |
| 148 | + { |
| 149 | + text: 'John lives in California', |
| 150 | + schemaType: 'fromJson', |
| 151 | + jsonSchemaExample: JSON.stringify({ |
| 152 | + state: 'California', |
| 153 | + cities: ['Los Angeles', 'San Francisco'], |
| 154 | + }), |
| 155 | + options: { |
| 156 | + systemPromptTemplate: '', |
| 157 | + }, |
| 158 | + }, |
| 159 | + new FakeListChatModel({ |
| 160 | + responses: [ |
| 161 | + formatFakeLlmResponse({ |
| 162 | + state: 'California', |
| 163 | + // cities field missing - should be allowed in v1.1 |
| 164 | + }), |
| 165 | + ], |
| 166 | + }), |
| 167 | + inputData, |
| 168 | + ); |
| 169 | + |
| 170 | + // Mock version 1.1 to test optional fields behavior |
| 171 | + mockExecuteFunctions.getNode = () => mock<INode>({ typeVersion: 1.1 }); |
| 172 | + |
| 173 | + const response = await node.execute.call(mockExecuteFunctions); |
| 174 | + |
| 175 | + expect(response).toEqual([ |
| 176 | + [ |
| 177 | + { |
| 178 | + json: { |
| 179 | + output: { |
| 180 | + state: 'California', |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + ], |
| 185 | + ]); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should throw error for incomplete model output in version 1.2 (required fields)', async () => { |
| 189 | + const node = new InformationExtractor(); |
| 190 | + const inputData = [{ json: { text: 'John lives in California' } }]; |
| 191 | + |
| 192 | + const mockExecuteFunctions = createExecuteFunctionsMock( |
| 193 | + { |
| 194 | + text: 'John lives in California', |
| 195 | + schemaType: 'fromJson', |
| 196 | + jsonSchemaExample: JSON.stringify({ |
| 197 | + state: 'California', |
| 198 | + cities: ['Los Angeles', 'San Francisco'], |
| 199 | + zipCode: '90210', |
| 200 | + }), |
| 201 | + options: { |
| 202 | + systemPromptTemplate: '', |
| 203 | + }, |
| 204 | + }, |
| 205 | + new FakeListChatModel({ |
| 206 | + responses: [ |
| 207 | + formatFakeLlmResponse({ |
| 208 | + state: 'California', |
| 209 | + // Missing cities and zipCode - should fail in v1.2 since all fields are required |
| 210 | + }), |
| 211 | + ], |
| 212 | + }), |
| 213 | + inputData, |
| 214 | + ); |
| 215 | + |
| 216 | + mockExecuteFunctions.getNode = () => mock<INode>({ typeVersion: 1.2 }); |
| 217 | + |
| 218 | + await expect(node.execute.call(mockExecuteFunctions)).rejects.toThrow(); |
| 219 | + }); |
| 220 | + |
| 221 | + it('should extract information using complex nested JSON schema from example', async () => { |
| 222 | + const node = new InformationExtractor(); |
| 223 | + const inputData = [ |
| 224 | + { |
| 225 | + json: { |
| 226 | + text: 'John Doe works at Acme Corp as a Software Engineer with 5 years experience', |
| 227 | + }, |
| 228 | + }, |
| 229 | + ]; |
| 230 | + |
| 231 | + const complexSchema = { |
| 232 | + person: { |
| 233 | + name: 'John Doe', |
| 234 | + company: { |
| 235 | + name: 'Acme Corp', |
| 236 | + position: 'Software Engineer', |
| 237 | + }, |
| 238 | + }, |
| 239 | + experience: { |
| 240 | + years: 5, |
| 241 | + skills: ['JavaScript', 'TypeScript'], |
| 242 | + }, |
| 243 | + }; |
| 244 | + |
| 245 | + const mockExecuteFunctions = createExecuteFunctionsMock( |
| 246 | + { |
| 247 | + text: 'John Doe works at Acme Corp as a Software Engineer with 5 years experience', |
| 248 | + schemaType: 'fromJson', |
| 249 | + jsonSchemaExample: JSON.stringify(complexSchema), |
| 250 | + options: { |
| 251 | + systemPromptTemplate: '', |
| 252 | + }, |
| 253 | + }, |
| 254 | + new FakeListChatModel({ |
| 255 | + responses: [ |
| 256 | + formatFakeLlmResponse({ |
| 257 | + person: { |
| 258 | + name: 'John Doe', |
| 259 | + company: { |
| 260 | + name: 'Acme Corp', |
| 261 | + position: 'Software Engineer', |
| 262 | + }, |
| 263 | + }, |
| 264 | + experience: { |
| 265 | + years: 5, |
| 266 | + skills: ['JavaScript', 'TypeScript'], |
| 267 | + }, |
| 268 | + }), |
| 269 | + ], |
| 270 | + }), |
| 271 | + inputData, |
| 272 | + ); |
| 273 | + |
| 274 | + mockExecuteFunctions.getNode = () => mock<INode>({ typeVersion: 1.2 }); |
| 275 | + |
| 276 | + const response = await node.execute.call(mockExecuteFunctions); |
| 277 | + |
| 278 | + expect(response[0][0].json.output).toMatchObject({ |
| 279 | + person: { |
| 280 | + name: 'John Doe', |
| 281 | + company: { |
| 282 | + name: 'Acme Corp', |
| 283 | + position: 'Software Engineer', |
| 284 | + }, |
| 285 | + }, |
| 286 | + experience: { |
| 287 | + years: 5, |
| 288 | + skills: expect.arrayContaining(['JavaScript', 'TypeScript']), |
| 289 | + }, |
| 290 | + }); |
| 291 | + }); |
| 292 | + }); |
| 293 | + |
91 | 294 | describe('Batch Processing', () => {
|
92 | 295 | it('should process multiple items in batches', async () => {
|
93 | 296 | const node = new InformationExtractor();
|
|
0 commit comments