11import type { EvalFunction } from 'mathjs' ;
22
33import { LruCache } from '@apify/datastructures' ;
4- import { calculateDefaultMemoryFromExpression , DEFAULT_MEMORY_MBYTES_MAX_CHARS } from '@apify/math-utils' ;
4+ import { calculateRunDynamicMemory , DEFAULT_MEMORY_MBYTES_MAX_CHARS } from '@apify/math-utils' ;
55
66describe ( 'calculateDefaultMemoryFromExpression' , ( ) => {
77 const emptyContext = { input : { } , runOptions : { } } ;
@@ -10,7 +10,7 @@ describe('calculateDefaultMemoryFromExpression', () => {
1010 it ( 'correctly calculates and rounds memory from one-line expression' , ( ) => {
1111 const context = { input : { size : 10 } , runOptions : { } } ;
1212 // 10 * 1024 = 10240. log2(10240) ~ 13.32. round(13.32) -> 2^13 = 8192
13- const result = calculateDefaultMemoryFromExpression ( 'input.size * 1024' , context ) ;
13+ const result = calculateRunDynamicMemory ( 'input.size * 1024' , context ) ;
1414 expect ( result ) . toBe ( 8192 ) ;
1515 } ) ;
1616
@@ -22,34 +22,34 @@ describe('calculateDefaultMemoryFromExpression', () => {
2222 baseVal * multVal
2323 ` ;
2424 // 10 * 1024 = 10240. Rounds to 8192.
25- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
25+ const result = calculateRunDynamicMemory ( expr , context ) ;
2626 expect ( result ) . toBe ( 8192 ) ;
2727 } ) ;
2828
2929 it ( 'correctly accesses runOptions from the context' , ( ) => {
3030 const context = { input : { } , runOptions : { timeoutSecs : 60 } } ;
3131 const expr = 'runOptions.timeoutSecs * 100' ; // 60 * 100 = 6000
3232 // log2(6000) ~ 12.55. round(13) -> 2^13 = 8192
33- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
33+ const result = calculateRunDynamicMemory ( expr , context ) ;
3434 expect ( result ) . toBe ( 8192 ) ;
3535 } ) ;
3636
3737 it ( 'correctly handles a single number expression' , ( ) => {
38- const result = calculateDefaultMemoryFromExpression ( '2048' , emptyContext ) ;
38+ const result = calculateRunDynamicMemory ( '2048' , emptyContext ) ;
3939 expect ( result ) . toBe ( 2048 ) ;
4040 } ) ;
4141
4242 it ( 'correctly handles expressions with custom get() function' , ( ) => {
4343 const context = { input : { nested : { value : 20 } } , runOptions : { } } ;
4444 const expr = "get(input, 'nested.value', 10) * 50" ; // 20 * 50 = 1000
45- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
45+ const result = calculateRunDynamicMemory ( expr , context ) ;
4646 expect ( result ) . toBe ( 1024 ) ;
4747 } ) ;
4848
4949 it ( 'should use get() default value when path is invalid' , ( ) => {
5050 const context = { input : { user : { } } , runOptions : { } } ;
5151 const expr = "get(input, 'user.settings.memory', 512)" ;
52- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
52+ const result = calculateRunDynamicMemory ( expr , context ) ;
5353 expect ( result ) . toBe ( 512 ) ;
5454 } ) ;
5555
@@ -78,7 +78,7 @@ describe('calculateDefaultMemoryFromExpression', () => {
7878 '$desc' ,
7979 ( { expression } ) => {
8080 // in case operation is not supported, mathjs will throw
81- expect ( calculateDefaultMemoryFromExpression ( expression , context ) ) . toBeDefined ( ) ;
81+ expect ( calculateRunDynamicMemory ( expression , context ) ) . toBeDefined ( ) ;
8282 } ,
8383 ) ;
8484 } ) ;
@@ -88,87 +88,87 @@ describe('calculateDefaultMemoryFromExpression', () => {
8888 it ( 'should throw error if variable doesn\'t start with .runOptions or .input' , ( ) => {
8989 const context = { input : { } , runOptions : { memoryMbytes : 16 } } ;
9090 const expr = '{{unexistingVariable}} * 1024' ;
91- expect ( ( ) => calculateDefaultMemoryFromExpression ( expr , context ) )
91+ expect ( ( ) => calculateRunDynamicMemory ( expr , context ) )
9292 . toThrow ( `Invalid variable '{{unexistingVariable}}' in expression. Variables must start with 'input.' or 'runOptions.'.` ) ;
9393 } ) ;
9494
9595 it ( 'correctly replaces {{runOptions.variable}} with valid runOptions.variable' , ( ) => {
9696 const context = { input : { } , runOptions : { memoryMbytes : 16 } } ;
9797 const expr = '{{runOptions.memoryMbytes}} * 1024' ;
98- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
98+ const result = calculateRunDynamicMemory ( expr , context ) ;
9999 expect ( result ) . toBe ( 16384 ) ;
100100 } ) ;
101101
102102 it ( 'correctly replaces {{input.variable}} with valid input.variable' , ( ) => {
103103 const context = { input : { value : 16 } , runOptions : { } } ;
104104 const expr = '{{input.value}} * 1024' ;
105- const result = calculateDefaultMemoryFromExpression ( expr , context ) ;
105+ const result = calculateRunDynamicMemory ( expr , context ) ;
106106 expect ( result ) . toBe ( 16384 ) ;
107107 } ) ;
108108
109109 it ( 'should throw error if runOptions variable is invalid' , ( ) => {
110110 const context = { input : { value : 16 } , runOptions : { } } ;
111111 const expr = '{{runOptions.customVariable}} * 1024' ;
112- expect ( ( ) => calculateDefaultMemoryFromExpression ( expr , context ) )
112+ expect ( ( ) => calculateRunDynamicMemory ( expr , context ) )
113113 . toThrow ( `Invalid variable '{{runOptions.customVariable}}' in expression. Only the following runOptions are allowed:` ) ;
114114 } ) ;
115115 } ) ;
116116
117117 describe ( 'Rounding Logic' , ( ) => {
118118 it ( 'should round down (e.g., 10240 -> 8192)' , ( ) => {
119119 // 2^13 = 8192, 2^14 = 16384.
120- const result = calculateDefaultMemoryFromExpression ( '10240' , emptyContext ) ;
120+ const result = calculateRunDynamicMemory ( '10240' , emptyContext ) ;
121121 expect ( result ) . toBe ( 8192 ) ;
122122 } ) ;
123123
124124 it ( 'should round up (e.g., 13000 -> 16384)' , ( ) => {
125125 // 13000 is closer to 16384 than 8192.
126- const result = calculateDefaultMemoryFromExpression ( '13000' , emptyContext ) ;
126+ const result = calculateRunDynamicMemory ( '13000' , emptyContext ) ;
127127 expect ( result ) . toBe ( 16384 ) ;
128128 } ) ;
129129
130130 it ( 'should clamp to the minimum memory limit if the result is too low' , ( ) => {
131- const result = calculateDefaultMemoryFromExpression ( '64' , emptyContext ) ;
131+ const result = calculateRunDynamicMemory ( '64' , emptyContext ) ;
132132 expect ( result ) . toBe ( 128 ) ;
133133 } ) ;
134134
135135 it ( 'should clamp to the maximum memory limit if the result is too high' , ( ) => {
136- const result = calculateDefaultMemoryFromExpression ( '100000' , emptyContext ) ;
136+ const result = calculateRunDynamicMemory ( '100000' , emptyContext ) ;
137137 expect ( result ) . toBe ( 32768 ) ;
138138 } ) ;
139139 } ) ;
140140
141141 describe ( 'Invalid/Error Handling' , ( ) => {
142142 it ( 'should throw an error if expression length is greater than DEFAULT_MEMORY_MBYTES_MAX_CHARS' , ( ) => {
143143 const expr = '1' . repeat ( DEFAULT_MEMORY_MBYTES_MAX_CHARS + 1 ) ; // Assuming max length is 1000
144- expect ( ( ) => calculateDefaultMemoryFromExpression ( expr , emptyContext ) )
144+ expect ( ( ) => calculateRunDynamicMemory ( expr , emptyContext ) )
145145 . toThrow ( `The defaultMemoryMbytes expression is too long. Max length is ${ DEFAULT_MEMORY_MBYTES_MAX_CHARS } characters.` ) ;
146146 } ) ;
147147
148148 it ( 'should throw an error for invalid syntax' , ( ) => {
149149 const expr = '1 +* 2' ;
150- expect ( ( ) => calculateDefaultMemoryFromExpression ( expr , emptyContext ) )
150+ expect ( ( ) => calculateRunDynamicMemory ( expr , emptyContext ) )
151151 . toThrow ( ) ;
152152 } ) ;
153153
154154 it ( 'should throw error if result is 0' , ( ) => {
155- expect ( ( ) => calculateDefaultMemoryFromExpression ( '10 - 10' , emptyContext ) ) . toThrow ( `Calculated memory value must be a positive number, greater than 0, got: 0` ) ;
155+ expect ( ( ) => calculateRunDynamicMemory ( '10 - 10' , emptyContext ) ) . toThrow ( `Calculated memory value must be a positive number, greater than 0, got: 0` ) ;
156156 } ) ;
157157
158158 it ( 'should throw error if result is negative' , ( ) => {
159- expect ( ( ) => calculateDefaultMemoryFromExpression ( '5 - 10' , emptyContext ) ) . toThrow ( `Calculated memory value must be a positive number, greater than 0, got: -5` ) ;
159+ expect ( ( ) => calculateRunDynamicMemory ( '5 - 10' , emptyContext ) ) . toThrow ( `Calculated memory value must be a positive number, greater than 0, got: -5` ) ;
160160 } ) ;
161161
162162 it ( 'should throw error if result is NaN' , ( ) => {
163- expect ( ( ) => calculateDefaultMemoryFromExpression ( '0 / 0' , emptyContext ) ) . toThrow ( 'Failed to round number to a power of 2.' ) ;
163+ expect ( ( ) => calculateRunDynamicMemory ( '0 / 0' , emptyContext ) ) . toThrow ( 'Failed to round number to a power of 2.' ) ;
164164 } ) ;
165165
166166 it ( 'should throw error if result is a non-numeric (string)' , ( ) => {
167- expect ( ( ) => calculateDefaultMemoryFromExpression ( "'hello'" , emptyContext ) ) . toThrow ( 'Failed to round number to a power of 2.' ) ;
167+ expect ( ( ) => calculateRunDynamicMemory ( "'hello'" , emptyContext ) ) . toThrow ( 'Failed to round number to a power of 2.' ) ;
168168 } ) ;
169169
170170 it ( 'should throw error when disabled functionality of MathJS is used' , ( ) => {
171- expect ( ( ) => calculateDefaultMemoryFromExpression ( 'evaluate(512)' , emptyContext ) ) . toThrow ( 'Function evaluate is disabled' ) ;
171+ expect ( ( ) => calculateRunDynamicMemory ( 'evaluate(512)' , emptyContext ) ) . toThrow ( 'Function evaluate is disabled' ) ;
172172 } ) ;
173173 } ) ;
174174
@@ -185,20 +185,20 @@ describe('calculateDefaultMemoryFromExpression', () => {
185185 expect ( cache . length ( ) ) . toBe ( 0 ) ;
186186
187187 // First call - cache miss
188- const result1 = calculateDefaultMemoryFromExpression ( expr , context , { cache } ) ;
188+ const result1 = calculateRunDynamicMemory ( expr , context , { cache } ) ;
189189 expect ( result1 ) . toBe ( 8192 ) ;
190190 expect ( cache . length ( ) ) . toBe ( 1 ) ; // Expression is now cached
191191
192192 // Second call - cache hit
193- const result2 = calculateDefaultMemoryFromExpression ( expr , context , { cache } ) ;
193+ const result2 = calculateRunDynamicMemory ( expr , context , { cache } ) ;
194194 expect ( result2 ) . toBe ( 8192 ) ;
195195 expect ( cache . length ( ) ) . toBe ( 1 ) ; // Cache length is unchanged
196196 } ) ;
197197
198198 it ( 'should cache different expressions separately' , ( ) => {
199199 const expr2 = 'input.size * 2048' ; // 10 * 2048 = 20480 -> 16384
200- calculateDefaultMemoryFromExpression ( expr , context , { cache } ) ;
201- calculateDefaultMemoryFromExpression ( expr2 , context , { cache } ) ;
200+ calculateRunDynamicMemory ( expr , context , { cache } ) ;
201+ calculateRunDynamicMemory ( expr2 , context , { cache } ) ;
202202 expect ( cache . length ( ) ) . toBe ( 2 ) ;
203203 } ) ;
204204 } ) ;
0 commit comments