11/** @vitest -environment node */
22import { describe , expect , it , vi } from 'vitest'
3- import { stringifyBoundedMemoryJson } from '@/lib/memory /bounded-json'
3+ import { stringifyBoundedJson } from '@/lib/core/utils /bounded-json'
44
5- describe ( 'bounded memory JSON' , ( ) => {
5+ describe ( 'bounded JSON' , ( ) => {
66 it . each ( [
77 { value : { text : 'hello' , values : [ 1 , false , null ] } } ,
88 { value : { text : 'é😀\ud800\udc00\ud800' } } ,
@@ -12,8 +12,8 @@ describe('bounded memory JSON', () => {
1212 ] ) ( 'uses the caller byte limit including UTF-8 and escaped JSON bytes' , ( { value } ) => {
1313 const json = JSON . stringify ( value )
1414 const bytes = Buffer . byteLength ( json , 'utf8' )
15- expect ( stringifyBoundedMemoryJson ( value , bytes ) ) . toBe ( json )
16- expect ( stringifyBoundedMemoryJson ( value , bytes - 1 ) ) . toBeUndefined ( )
15+ expect ( stringifyBoundedJson ( value , bytes ) ) . toBe ( json )
16+ expect ( stringifyBoundedJson ( value , bytes - 1 ) ) . toBeUndefined ( )
1717 } )
1818
1919 it ( 'rejects cycles, excessive depth, and excessive nodes' , ( ) => {
@@ -27,7 +27,7 @@ describe('bounded memory JSON', () => {
2727 Array ( 100_001 ) ,
2828 Object . fromEntries ( Array . from ( { length : 100_001 } , ( _ , index ) => [ index , undefined ] ) ) ,
2929 ] ) {
30- expect ( stringifyBoundedMemoryJson ( value , 8 * 1024 * 1024 ) ) . toBeUndefined ( )
30+ expect ( stringifyBoundedJson ( value , 8 * 1024 * 1024 ) ) . toBeUndefined ( )
3131 }
3232 } )
3333
@@ -37,7 +37,7 @@ describe('bounded memory JSON', () => {
3737 const accessor = Object . defineProperty ( { } , 'secret' , { enumerable : true , get : getter } )
3838 const custom = Object . defineProperty ( { } , 'toJSON' , { value : toJSON } )
3939 for ( const value of [ accessor , custom , { output : new Uint8Array ( [ 1 , 2 , 3 ] ) } ] ) {
40- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBeUndefined ( )
40+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBeUndefined ( )
4141 }
4242 expect ( getter ) . not . toHaveBeenCalled ( )
4343 expect ( toJSON ) . not . toHaveBeenCalled ( )
@@ -47,7 +47,7 @@ describe('bounded memory JSON', () => {
4747 const value = { output : 'x' . repeat ( 1025 ) }
4848 const serialize = vi . spyOn ( JSON , 'stringify' )
4949 try {
50- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBeUndefined ( )
50+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBeUndefined ( )
5151 expect ( serialize ) . not . toHaveBeenCalled ( )
5252 } finally {
5353 serialize . mockRestore ( )
@@ -61,7 +61,7 @@ describe('bounded memory JSON', () => {
6161 ] ) ( 'rejects escaped bytes before serializing the captured graph' , ( { value } ) => {
6262 const serialize = vi . spyOn ( JSON , 'stringify' )
6363 try {
64- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBeUndefined ( )
64+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBeUndefined ( )
6565 expect ( serialize ) . not . toHaveBeenCalled ( )
6666 } finally {
6767 serialize . mockRestore ( )
@@ -71,21 +71,21 @@ describe('bounded memory JSON', () => {
7171 it ( 'serializes the admitted descriptors without reading proxy values or toJSON' , ( ) => {
7272 const get = vi . fn ( ( ) => 'UNADMITTED' )
7373 const value = new Proxy ( { text : 'admitted' } , { get } )
74- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBe ( '{"text":"admitted"}' )
74+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBe ( '{"text":"admitted"}' )
7575 expect ( get ) . not . toHaveBeenCalled ( )
7676 } )
7777
7878 it ( 'does not read inherited numeric accessors in sparse arrays' , ( ) => {
7979 const get = vi . fn ( ( ) => 'UNADMITTED' )
8080 const prototype = Object . create ( Array . prototype , { 0 : { get } } )
8181 const value = Object . setPrototypeOf ( Array ( 1 ) , prototype )
82- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBe ( '[null]' )
82+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBe ( '[null]' )
8383 expect ( get ) . not . toHaveBeenCalled ( )
8484 } )
8585
8686 it ( 'allows repeated references without treating them as a cycle' , ( ) => {
8787 const result = { answer : 42 }
8888 const value = { rawResponse : result , modelResponse : result }
89- expect ( stringifyBoundedMemoryJson ( value , 1024 ) ) . toBe ( JSON . stringify ( value ) )
89+ expect ( stringifyBoundedJson ( value , 1024 ) ) . toBe ( JSON . stringify ( value ) )
9090 } )
9191} )
0 commit comments