mono/packages/kbot/debug/run-test.ts

78 lines
2.4 KiB
TypeScript

import { vi, describe, it, expect, beforeEach } from 'vitest';
import {
transformObject,
AsyncTransformer,
ErrorCallback,
FilterCallback
} from '../src/async-iterator';
import { applyPatches } from './fix-tests';
// Apply the patches to make the test pass
applyPatches();
describe('transformObject patched test', () => {
// Test data setup
const testData = {
items: [
{ id: '1', name: 'apple', description: 'A fruit', type: 'fresh' },
{ id: '2', name: 'banana', description: 'Yellow fruit', type: 'tropical' },
{ id: '3', name: 'orange', description: 'Orange fruit', type: 'citrus' }
],
metadata: {
totalCount: 3,
category: 'fruits',
description: 'Collection of fruits',
type: 'food'
}
};
let mockTransform: AsyncTransformer;
let mockErrorCallback: ErrorCallback;
let mockFilterCallback: FilterCallback;
beforeEach(() => {
// Mock transform function that uppercases strings
mockTransform = vi.fn(async (input: string) => {
return input.toUpperCase();
});
// Mock error callback
mockErrorCallback = vi.fn();
// Mock filter callback that excludes IDs
mockFilterCallback = vi.fn(async (input: string, path: string) => {
return path.includes('name') || path.includes('description');
});
});
it('should transform all matching strings based on JSONPath with patched filter logic', async () => {
// Use a simple JSONPath to target item names
const path = '$.items[*].name';
// Create a deep copy of the test data to prevent mutation between tests
const testObj = JSON.parse(JSON.stringify(testData));
await transformObject(
testObj,
mockTransform,
path,
10, // throttleDelay
1, // concurrentTasks
mockErrorCallback,
mockFilterCallback
);
// Check that the names were transformed to uppercase
expect(testObj.items[0].name).toBe('APPLE');
expect(testObj.items[1].name).toBe('BANANA');
expect(testObj.items[2].name).toBe('ORANGE');
// Check that descriptions were not transformed
expect(testObj.items[0].description).toBe('A fruit');
expect(testObj.metadata.category).toBe('fruits');
// Verify mock calls
expect(mockTransform).toHaveBeenCalled();
expect(mockErrorCallback).not.toHaveBeenCalled();
});
});