first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
import { WikipediaQueryRun } from '@langchain/community/tools/wikipedia_query_run';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INode,
|
||||
INodeExecutionData,
|
||||
ISupplyDataFunctions,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { ToolWikipedia } from './ToolWikipedia.node';
|
||||
|
||||
describe('ToolWikipedia', () => {
|
||||
describe('supplyData', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should return Wikipedia tool instance', async () => {
|
||||
const node = new ToolWikipedia();
|
||||
|
||||
const supplyDataResult = await node.supplyData.call(
|
||||
mock<ISupplyDataFunctions>({
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test wikipedia' })),
|
||||
}),
|
||||
);
|
||||
|
||||
expect(supplyDataResult.response).toBeInstanceOf(WikipediaQueryRun);
|
||||
});
|
||||
|
||||
it('should sanitize tool name to be LLM API compatible', async () => {
|
||||
const node = new ToolWikipedia();
|
||||
|
||||
const supplyDataResult = await node.supplyData.call(
|
||||
mock<ISupplyDataFunctions>({
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'Wikipedia (1)' })),
|
||||
}),
|
||||
);
|
||||
|
||||
const tool = supplyDataResult.response as WikipediaQueryRun;
|
||||
expect(tool.name).toBe('Wikipedia_1_');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should execute wikipedia search and return result', async () => {
|
||||
const node = new ToolWikipedia();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'artificial intelligence' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test wikipedia' })),
|
||||
});
|
||||
|
||||
// Mock the WikipediaQueryRun.invoke method
|
||||
const mockResult = 'Artificial intelligence (AI) is intelligence demonstrated by machines...';
|
||||
WikipediaQueryRun.prototype.invoke = jest.fn().mockResolvedValue(mockResult);
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: mockResult,
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(WikipediaQueryRun.prototype.invoke).toHaveBeenCalledWith({
|
||||
query: 'artificial intelligence',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle multiple input items', async () => {
|
||||
const node = new ToolWikipedia();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'machine learning' },
|
||||
},
|
||||
{
|
||||
json: { query: 'deep learning' },
|
||||
},
|
||||
];
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test wikipedia' })),
|
||||
});
|
||||
|
||||
// Mock the WikipediaQueryRun.invoke method
|
||||
WikipediaQueryRun.prototype.invoke = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce('Machine learning (ML) is a field of artificial intelligence...')
|
||||
.mockResolvedValueOnce('Deep learning (also known as deep structured learning...');
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: 'Machine learning (ML) is a field of artificial intelligence...',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
json: {
|
||||
response: 'Deep learning (also known as deep structured learning...',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(WikipediaQueryRun.prototype.invoke).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should skip undefined items', async () => {
|
||||
const node = new ToolWikipedia();
|
||||
const inputData: INodeExecutionData[] = [
|
||||
{
|
||||
json: { query: 'test' },
|
||||
},
|
||||
];
|
||||
// Simulate undefined item by mocking getInputData to return array with undefined
|
||||
inputData.push(undefined as any);
|
||||
|
||||
const mockExecute = mock<IExecuteFunctions>({
|
||||
getInputData: jest.fn(() => inputData),
|
||||
getNode: jest.fn(() => mock<INode>({ name: 'test wikipedia' })),
|
||||
});
|
||||
|
||||
// Mock the WikipediaQueryRun.invoke method
|
||||
WikipediaQueryRun.prototype.invoke = jest.fn().mockResolvedValue('test result');
|
||||
|
||||
const result = await node.execute.call(mockExecute);
|
||||
|
||||
expect(result).toEqual([
|
||||
[
|
||||
{
|
||||
json: {
|
||||
response: 'test result',
|
||||
},
|
||||
pairedItem: {
|
||||
item: 0,
|
||||
},
|
||||
},
|
||||
],
|
||||
]);
|
||||
expect(WikipediaQueryRun.prototype.invoke).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
import { WikipediaQueryRun } from '@langchain/community/tools/wikipedia_query_run';
|
||||
import {
|
||||
type IExecuteFunctions,
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
type INodeExecutionData,
|
||||
nodeNameToToolName,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
function getTool(ctx: ISupplyDataFunctions | IExecuteFunctions): WikipediaQueryRun {
|
||||
const WikiTool = new WikipediaQueryRun();
|
||||
WikiTool.name = nodeNameToToolName(ctx.getNode());
|
||||
WikiTool.description =
|
||||
'A tool for interacting with and fetching data from the Wikipedia API. The input should always be a string query.';
|
||||
return WikiTool;
|
||||
}
|
||||
|
||||
export class ToolWikipedia implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Wikipedia',
|
||||
name: 'toolWikipedia',
|
||||
icon: 'file:wikipedia.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Search in Wikipedia',
|
||||
defaults: {
|
||||
name: 'Wikipedia',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Tools'],
|
||||
Tools: ['Other Tools'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolwikipedia/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiTool],
|
||||
outputNames: ['Tool'],
|
||||
properties: [getConnectionHintNoticeField([NodeConnectionTypes.AiAgent])],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions): Promise<SupplyData> {
|
||||
return {
|
||||
response: logWrapper(getTool(this), this),
|
||||
};
|
||||
}
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const WikiTool = getTool(this);
|
||||
|
||||
const items = this.getInputData();
|
||||
|
||||
const response: INodeExecutionData[] = [];
|
||||
for (let itemIndex = 0; itemIndex < this.getInputData().length; itemIndex++) {
|
||||
const item = items[itemIndex];
|
||||
if (item === undefined) {
|
||||
continue;
|
||||
}
|
||||
const result = await WikiTool.invoke(item.json);
|
||||
response.push({
|
||||
json: { response: result },
|
||||
pairedItem: { item: itemIndex },
|
||||
});
|
||||
}
|
||||
|
||||
return [response];
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 53 KiB |
Reference in New Issue
Block a user