Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,39 @@ describe('CommunityNodeTypesService', () => {
expect(detectUpdatesSpy).toHaveBeenCalled();
expect(setTimestampForRetrySpy).not.toHaveBeenCalled();
});

it('should batch IDs into chunks of 100 when updating >100 node types', async () => {
// Set up initial state so detectUpdates is called
(service as any).communityNodeTypes.set('node-1', {
name: 'node-1',
packageName: 'package-1',
npmVersion: '1.0.0',
});

const ids = Array.from({ length: 250 }, (_, i) => i + 1);

getCommunityNodeTypes.mockResolvedValue([]);

jest.spyOn(service as any, 'detectUpdates').mockResolvedValue({ typesToUpdate: ids });

await (service as any).fetchNodeTypes();

// 250 IDs should result in 3 batches
expect(getCommunityNodeTypes).toHaveBeenCalledTimes(3);

const firstCallFilters = getCommunityNodeTypes.mock.calls[0][1].filters.id.$in;
const secondCallFilters = getCommunityNodeTypes.mock.calls[1][1].filters.id.$in;
const thirdCallFilters = getCommunityNodeTypes.mock.calls[2][1].filters.id.$in;

expect(firstCallFilters).toHaveLength(100);
expect(secondCallFilters).toHaveLength(100);
expect(thirdCallFilters).toHaveLength(50);

expect(firstCallFilters[0]).toBe(1);
expect(firstCallFilters[99]).toBe(100);
expect(secondCallFilters[0]).toBe(101);
expect(thirdCallFilters[49]).toBe(250);
});
});

describe('updateCommunityNodeTypes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { buildStrapiUpdateQuery } from './strapi-utils';
const UPDATE_INTERVAL = 8 * 60 * 60 * 1000;
const RETRY_INTERVAL = 5 * 60 * 1000;

// Strapi's qs parser has an arrayLimit of 100, so we batch IDs
const STRAPI_ARRAY_LIMIT = 100;

@Service()
export class CommunityNodeTypesService {
private communityNodeTypes: Map<string, StrapiCommunityNodeType> = new Map();
Expand Down Expand Up @@ -101,8 +104,16 @@ export class CommunityNodeTypesService {
return;
}

const qs = buildStrapiUpdateQuery(typesToUpdate);
data = await getCommunityNodeTypes(environment, qs, this.config.aiNodeSdkVersion);
for (let i = 0; i < typesToUpdate.length; i += STRAPI_ARRAY_LIMIT) {
const batch = typesToUpdate.slice(i, i + STRAPI_ARRAY_LIMIT);
const qs = buildStrapiUpdateQuery(batch);
const batchData = await getCommunityNodeTypes(
environment,
qs,
this.config.aiNodeSdkVersion,
);
data.push(...batchData);
}
}

this.updateCommunityNodeTypes(data);
Expand Down