|
1 | 1 | import { describe, expect, it } from 'vitest'; |
2 | 2 |
|
3 | | -import { getActorDefinitionStorageFieldNames } from '../../src/utils/actor.js'; |
| 3 | +import { ensureOutputWithinCharLimit, getActorDefinitionStorageFieldNames } from '../../src/utils/actor.js'; |
4 | 4 |
|
5 | 5 | describe('getActorDefinitionStorageFieldNames', () => { |
6 | 6 | it('should return an array of field names from a single view (display.properties and transformation.fields)', () => { |
@@ -98,3 +98,40 @@ describe('getActorDefinitionStorageFieldNames', () => { |
98 | 98 | expect(result.sort()).toEqual(['bar', 'baz', 'foo']); |
99 | 99 | }); |
100 | 100 | }); |
| 101 | + |
| 102 | +describe('ensureOutputWithinCharLimit', () => { |
| 103 | + it('should return all items when limit is high', () => { |
| 104 | + const items = [ |
| 105 | + { id: 1, name: 'Item 1', value: 'test' }, |
| 106 | + { id: 2, name: 'Item 2', value: 'test' }, |
| 107 | + ]; |
| 108 | + const charLimit = JSON.stringify(items).length; |
| 109 | + const result = ensureOutputWithinCharLimit(items, [], charLimit); |
| 110 | + expect(result).toEqual(items); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should use important fields when all items exceed limit', () => { |
| 114 | + const items = [ |
| 115 | + { id: 1, name: 'Item 1', description: 'Very long description that makes this item exceed the limit', extra: 'unnecessary data' }, |
| 116 | + { id: 2, name: 'Item 2', description: 'Another long description', extra: 'more unnecessary data' }, |
| 117 | + ]; |
| 118 | + const importantFields = ['id', 'name']; |
| 119 | + const charLimit = 100; // Very small limit |
| 120 | + const result = ensureOutputWithinCharLimit(items, importantFields, charLimit); |
| 121 | + expect(result).toEqual([ |
| 122 | + { id: 1, name: 'Item 1' }, |
| 123 | + { id: 2, name: 'Item 2' }, |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should remove all items when limit is extremely small', () => { |
| 128 | + const items = [ |
| 129 | + { id: 1, name: 'Item 1' }, |
| 130 | + { id: 2, name: 'Item 2' }, |
| 131 | + ]; |
| 132 | + const charLimit = 10; // Extremely small limit - even empty array JSON "[]" is 2 chars |
| 133 | + const result = ensureOutputWithinCharLimit(items, [], charLimit); |
| 134 | + expect(result).toEqual([]); |
| 135 | + expect(JSON.stringify(result).length).toBeLessThanOrEqual(charLimit); |
| 136 | + }); |
| 137 | +}); |
0 commit comments