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,280 @@
|
||||
import type { IExecuteFunctions } from 'n8n-workflow';
|
||||
import nock from 'nock';
|
||||
|
||||
import * as getPaginated from '../../../actions/extraction/getPaginated.operation';
|
||||
import { ERROR_MESSAGES } from '../../../constants';
|
||||
import * as GenericFunctions from '../../../GenericFunctions';
|
||||
import * as transport from '../../../transport';
|
||||
import { createMockExecuteFunction } from '../helpers';
|
||||
|
||||
const baseNodeParameters = {
|
||||
resource: 'extraction',
|
||||
operation: 'getPaginated',
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
sessionMode: 'existing',
|
||||
additionalFields: {},
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
modelResponse:
|
||||
'{"items": [{"title": "Item 1", "price": "$10.99"}, {"title": "Item 2", "price": "$20.99"}]}',
|
||||
},
|
||||
};
|
||||
|
||||
const mockJsonSchema =
|
||||
'{"type":"object","properties":{"title":{"type":"string"},"price":{"type":"string"}}}';
|
||||
|
||||
jest.mock('../../../transport', () => {
|
||||
const originalModule = jest.requireActual<typeof transport>('../../../transport');
|
||||
return {
|
||||
...originalModule,
|
||||
apiRequest: jest.fn(async (method: string, endpoint: string) => {
|
||||
// For paginated extraction requests
|
||||
if (endpoint.includes('/paginated-extraction')) {
|
||||
return mockResponse;
|
||||
}
|
||||
|
||||
// For session deletion
|
||||
if (method === 'DELETE' && endpoint.includes('/sessions/')) {
|
||||
return { status: 'success' };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock('../../../GenericFunctions', () => {
|
||||
const originalModule = jest.requireActual<typeof GenericFunctions>('../../../GenericFunctions');
|
||||
return {
|
||||
...originalModule,
|
||||
createSessionAndWindow: jest.fn().mockImplementation(async () => {
|
||||
return {
|
||||
sessionId: 'new-session-123',
|
||||
windowId: 'new-window-123',
|
||||
};
|
||||
}),
|
||||
shouldCreateNewSession: jest.fn().mockImplementation(function (
|
||||
this: IExecuteFunctions,
|
||||
index: number,
|
||||
) {
|
||||
const sessionMode = this.getNodeParameter('sessionMode', index) as string;
|
||||
return sessionMode === 'new';
|
||||
}),
|
||||
validateAirtopApiResponse: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe('Test Airtop, getPaginated operation', () => {
|
||||
beforeAll(() => {
|
||||
nock.disableNetConnect();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
nock.restore();
|
||||
jest.unmock('../../../transport');
|
||||
jest.unmock('../../../GenericFunctions');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should extract data with minimal parameters', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'Extract all product titles and prices',
|
||||
};
|
||||
|
||||
const result = await getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/paginated-extraction',
|
||||
{
|
||||
prompt: 'Extract all product titles and prices',
|
||||
configuration: {},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should extract data with output schema', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'Extract all product titles and prices',
|
||||
additionalFields: {
|
||||
outputSchema: mockJsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/paginated-extraction',
|
||||
{
|
||||
prompt: 'Extract all product titles and prices',
|
||||
configuration: {
|
||||
outputSchema: mockJsonSchema,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
['auto', 'accurate', 'cost-efficient'].forEach((interactionMode) => {
|
||||
it(`interactionMode > Should extract data with '${interactionMode}' mode`, async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'Extract all product titles and prices',
|
||||
additionalFields: {
|
||||
interactionMode,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/paginated-extraction',
|
||||
{
|
||||
prompt: 'Extract all product titles and prices',
|
||||
configuration: {
|
||||
interactionMode,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
['auto', 'paginated', 'infinite-scroll'].forEach((paginationMode) => {
|
||||
it(`paginationMode > Should extract data with '${paginationMode}' mode`, async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'Extract all product titles and prices',
|
||||
additionalFields: {
|
||||
paginationMode,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/paginated-extraction',
|
||||
{
|
||||
prompt: 'Extract all product titles and prices',
|
||||
configuration: {
|
||||
paginationMode,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should extract data using a new session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionMode: 'new',
|
||||
autoTerminateSession: true,
|
||||
url: 'https://example.com',
|
||||
prompt: 'Extract all product titles and prices',
|
||||
};
|
||||
|
||||
const result = await getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(2); // One for extraction, one for session deletion
|
||||
expect(transport.apiRequest).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'POST',
|
||||
'/sessions/new-session-123/windows/new-window-123/paginated-extraction',
|
||||
{
|
||||
prompt: 'Extract all product titles and prices',
|
||||
configuration: {},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should throw error when 'sessionId' is empty and session mode is 'existing'", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionId: '',
|
||||
prompt: 'Extract data',
|
||||
};
|
||||
|
||||
await expect(
|
||||
getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0),
|
||||
).rejects.toThrow(ERROR_MESSAGES.SESSION_ID_REQUIRED);
|
||||
});
|
||||
|
||||
it("should throw error when 'windowId' is empty and session mode is 'existing'", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
windowId: '',
|
||||
prompt: 'Extract data',
|
||||
};
|
||||
|
||||
await expect(
|
||||
getPaginated.execute.call(createMockExecuteFunction(nodeParameters), 0),
|
||||
).rejects.toThrow(ERROR_MESSAGES.WINDOW_ID_REQUIRED);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,283 @@
|
||||
import nock from 'nock';
|
||||
|
||||
import * as query from '../../../actions/extraction/query.operation';
|
||||
import { ERROR_MESSAGES } from '../../../constants';
|
||||
import * as GenericFunctions from '../../../GenericFunctions';
|
||||
import * as transport from '../../../transport';
|
||||
import { createMockExecuteFunction } from '../helpers';
|
||||
|
||||
const baseNodeParameters = {
|
||||
resource: 'extraction',
|
||||
operation: 'query',
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
sessionMode: 'existing',
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
modelResponse: {
|
||||
answer: 'The page contains 5 products with prices ranging from $10.99 to $50.99',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockJsonSchema =
|
||||
'{"type":"object","properties":{"productCount":{"type":"number"},"priceRange":{"type":"object"}}}';
|
||||
|
||||
jest.mock('../../../transport', () => {
|
||||
const originalModule = jest.requireActual<typeof transport>('../../../transport');
|
||||
return {
|
||||
...originalModule,
|
||||
apiRequest: jest.fn(async function (method: string, endpoint: string) {
|
||||
if (method === 'DELETE' && endpoint.includes('/sessions/')) {
|
||||
return { status: 'success' };
|
||||
}
|
||||
return mockResponse;
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock('../../../GenericFunctions', () => {
|
||||
const originalModule = jest.requireActual<typeof GenericFunctions>('../../../GenericFunctions');
|
||||
return {
|
||||
...originalModule,
|
||||
createSessionAndWindow: jest.fn().mockImplementation(async () => {
|
||||
return {
|
||||
sessionId: 'new-session-456',
|
||||
windowId: 'new-win-456',
|
||||
};
|
||||
}),
|
||||
shouldCreateNewSession: jest.fn().mockImplementation(function (this: any) {
|
||||
const sessionMode = this.getNodeParameter('sessionMode', 0);
|
||||
return sessionMode === 'new';
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
describe('Test Airtop, query page operation', () => {
|
||||
beforeAll(() => {
|
||||
nock.disableNetConnect();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
nock.restore();
|
||||
jest.unmock('../../../transport');
|
||||
jest.unmock('../../../GenericFunctions');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should query the page with minimal parameters using existing session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
};
|
||||
|
||||
const result = await query.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/page-query',
|
||||
{
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
configuration: {
|
||||
experimental: {
|
||||
includeVisualAnalysis: 'disabled',
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should query the page with output schema using existing session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
additionalFields: {
|
||||
outputSchema: mockJsonSchema,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await query.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/page-query',
|
||||
{
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
configuration: {
|
||||
outputSchema: mockJsonSchema,
|
||||
experimental: {
|
||||
includeVisualAnalysis: 'disabled',
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should query the page using a new session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionMode: 'new',
|
||||
url: 'https://example.com',
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
autoTerminateSession: true,
|
||||
};
|
||||
|
||||
const result = await query.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(2); // One for query, one for session deletion
|
||||
expect(transport.apiRequest).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'POST',
|
||||
'/sessions/new-session-456/windows/new-win-456/page-query',
|
||||
{
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
configuration: {
|
||||
experimental: {
|
||||
includeVisualAnalysis: 'disabled',
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should throw error when 'sessionId' is empty in 'existing' session mode", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionId: '',
|
||||
prompt: 'Query data',
|
||||
};
|
||||
|
||||
await expect(query.execute.call(createMockExecuteFunction(nodeParameters), 0)).rejects.toThrow(
|
||||
ERROR_MESSAGES.SESSION_ID_REQUIRED,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error when 'windowId' is empty in 'existing' session mode", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
windowId: '',
|
||||
prompt: 'Query data',
|
||||
};
|
||||
|
||||
await expect(query.execute.call(createMockExecuteFunction(nodeParameters), 0)).rejects.toThrow(
|
||||
ERROR_MESSAGES.WINDOW_ID_REQUIRED,
|
||||
);
|
||||
});
|
||||
|
||||
it("should query the page with 'includeVisualAnalysis' enabled", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'List the colors of the products on the page',
|
||||
additionalFields: {
|
||||
includeVisualAnalysis: true,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await query.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/page-query',
|
||||
{
|
||||
prompt: 'List the colors of the products on the page',
|
||||
configuration: {
|
||||
experimental: {
|
||||
includeVisualAnalysis: 'enabled',
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should query the page with 'includeVisualAnalysis' disabled", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
additionalFields: {
|
||||
includeVisualAnalysis: false,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await query.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/page-query',
|
||||
{
|
||||
prompt: 'How many products are on the page and what is their price range?',
|
||||
configuration: {
|
||||
experimental: {
|
||||
includeVisualAnalysis: 'disabled',
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
import nock from 'nock';
|
||||
|
||||
import * as scrape from '../../../actions/extraction/scrape.operation';
|
||||
import { ERROR_MESSAGES } from '../../../constants';
|
||||
import * as GenericFunctions from '../../../GenericFunctions';
|
||||
import * as transport from '../../../transport';
|
||||
import { createMockExecuteFunction } from '../helpers';
|
||||
|
||||
const baseNodeParameters = {
|
||||
resource: 'extraction',
|
||||
operation: 'scrape',
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
sessionMode: 'existing',
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
content: '<html><body>Scraped content</body></html>',
|
||||
},
|
||||
};
|
||||
|
||||
jest.mock('../../../transport', () => {
|
||||
const originalModule = jest.requireActual<typeof transport>('../../../transport');
|
||||
return {
|
||||
...originalModule,
|
||||
apiRequest: jest.fn(async function (method: string, endpoint: string) {
|
||||
if (method === 'DELETE' && endpoint.includes('/sessions/')) {
|
||||
return { status: 'success' };
|
||||
}
|
||||
return mockResponse;
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock('../../../GenericFunctions', () => {
|
||||
const originalModule = jest.requireActual<typeof GenericFunctions>('../../../GenericFunctions');
|
||||
return {
|
||||
...originalModule,
|
||||
createSessionAndWindow: jest.fn().mockImplementation(async () => {
|
||||
return {
|
||||
sessionId: 'new-session-456',
|
||||
windowId: 'new-win-456',
|
||||
};
|
||||
}),
|
||||
shouldCreateNewSession: jest.fn().mockImplementation(function (this: any) {
|
||||
const sessionMode = this.getNodeParameter('sessionMode', 0);
|
||||
return sessionMode === 'new';
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
describe('Test Airtop, scrape operation', () => {
|
||||
beforeAll(() => {
|
||||
nock.disableNetConnect();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
nock.restore();
|
||||
jest.unmock('../../../transport');
|
||||
jest.unmock('../../../GenericFunctions');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should scrape content with minimal parameters using existing session', async () => {
|
||||
const result = await scrape.execute.call(createMockExecuteFunction(baseNodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/scrape-content',
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should scrape content with additional parameters using existing session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
additionalFields: {
|
||||
waitForSelector: '.product-list',
|
||||
waitForTimeout: 5000,
|
||||
},
|
||||
};
|
||||
|
||||
const result = await scrape.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).not.toHaveBeenCalled();
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledWith(
|
||||
'POST',
|
||||
'/sessions/test-session-123/windows/win-123/scrape-content',
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
sessionId: 'test-session-123',
|
||||
windowId: 'win-123',
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should scrape content using a new session', async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionMode: 'new',
|
||||
url: 'https://example.com',
|
||||
autoTerminateSession: true,
|
||||
};
|
||||
|
||||
const result = await scrape.execute.call(createMockExecuteFunction(nodeParameters), 0);
|
||||
|
||||
expect(GenericFunctions.shouldCreateNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(GenericFunctions.createSessionAndWindow).toHaveBeenCalledTimes(1);
|
||||
expect(transport.apiRequest).toHaveBeenCalledTimes(2); // One for scrape, one for session deletion
|
||||
expect(transport.apiRequest).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'POST',
|
||||
'/sessions/new-session-456/windows/new-win-456/scrape-content',
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
json: {
|
||||
data: mockResponse.data,
|
||||
},
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should throw error when sessionId is empty in 'existing' session mode", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
sessionId: '',
|
||||
};
|
||||
|
||||
await expect(scrape.execute.call(createMockExecuteFunction(nodeParameters), 0)).rejects.toThrow(
|
||||
ERROR_MESSAGES.SESSION_ID_REQUIRED,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error when windowId is empty in 'existing' session mode", async () => {
|
||||
const nodeParameters = {
|
||||
...baseNodeParameters,
|
||||
windowId: '',
|
||||
};
|
||||
|
||||
await expect(scrape.execute.call(createMockExecuteFunction(nodeParameters), 0)).rejects.toThrow(
|
||||
ERROR_MESSAGES.WINDOW_ID_REQUIRED,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user