@@ -37,4 +37,92 @@ describe('OBFProcessor', () => {
3737 }
3838 }
3939 } ) ;
40+
41+ describe ( 'saveModifiedTree' , ( ) => {
42+ const tempOutputPath = path . join ( __dirname , 'temp_obz_modified.obz' ) ;
43+ const tempSaveFromTreePath = path . join ( __dirname , 'temp_obz_saveFromTree.obz' ) ;
44+
45+ afterEach ( async ( ) => {
46+ const fs = await import ( 'fs' ) ;
47+ if ( fs . existsSync ( tempOutputPath ) ) {
48+ fs . unlinkSync ( tempOutputPath ) ;
49+ }
50+ if ( fs . existsSync ( tempSaveFromTreePath ) ) {
51+ fs . unlinkSync ( tempSaveFromTreePath ) ;
52+ }
53+ } ) ;
54+
55+ it ( 'should preserve original file size better than saveFromTree for OBZ files' , async ( ) => {
56+ const processor = new ObfProcessor ( ) ;
57+ const fs = await import ( 'fs' ) ;
58+
59+ // Load the original file
60+ const tree = await processor . loadIntoTree ( obzPath ) ;
61+ const originalSize = fs . statSync ( obzPath ) . size ;
62+
63+ // Save using saveModifiedTree
64+ await processor . saveModifiedTree ( obzPath , tree , tempOutputPath ) ;
65+ const modifiedSize = fs . statSync ( tempOutputPath ) . size ;
66+
67+ // Save using saveFromTree for comparison
68+ await processor . saveFromTree ( tree , tempSaveFromTreePath ) ;
69+ const saveFromTreeSize = fs . statSync ( tempSaveFromTreePath ) . size ;
70+
71+ // saveModifiedTree should preserve file size much better than saveFromTree
72+ expect ( modifiedSize ) . toBeGreaterThan ( saveFromTreeSize ) ;
73+
74+ // saveModifiedTree should be at least 80% of original size (preserves most assets)
75+ expect ( modifiedSize / originalSize ) . toBeGreaterThan ( 0.8 ) ;
76+ } ) ;
77+
78+ it ( 'should produce a valid loadable OBZ file' , async ( ) => {
79+ const processor = new ObfProcessor ( ) ;
80+ const fs = await import ( 'fs' ) ;
81+
82+ // Load the original file
83+ const tree = await processor . loadIntoTree ( obzPath ) ;
84+ const originalPageCount = Object . keys ( tree . pages ) . length ;
85+
86+ // Save using saveModifiedTree
87+ await processor . saveModifiedTree ( obzPath , tree , tempOutputPath ) ;
88+
89+ // Load the saved file
90+ const savedTree = await processor . loadIntoTree ( tempOutputPath ) ;
91+
92+ // Verify the saved tree has the same pages
93+ expect ( Object . keys ( savedTree . pages ) . length ) . toBe ( originalPageCount ) ;
94+ expect ( savedTree . rootId ) . toBe ( tree . rootId ) ;
95+ } ) ;
96+
97+ it ( 'should handle empty tree by copying original' , async ( ) => {
98+ const processor = new ObfProcessor ( ) ;
99+ const fs = await import ( 'fs' ) ;
100+
101+ // Create an empty tree
102+ const emptyTree : AACTree = {
103+ pages : { } ,
104+ rootId : null ,
105+ toolbarId : null ,
106+ dashboardId : null ,
107+ metadata : { } ,
108+ addPage ( ) {
109+ throw new Error ( 'Not implemented' ) ;
110+ } ,
111+ getPage ( ) {
112+ return undefined ;
113+ } ,
114+ traverse ( ) {
115+ // Empty - nothing to traverse
116+ } ,
117+ } ;
118+
119+ // Save using saveModifiedTree
120+ await processor . saveModifiedTree ( obzPath , emptyTree , tempOutputPath ) ;
121+
122+ // Verify the file was copied (same size)
123+ const originalSize = fs . statSync ( obzPath ) . size ;
124+ const copiedSize = fs . statSync ( tempOutputPath ) . size ;
125+ expect ( copiedSize ) . toBe ( originalSize ) ;
126+ } ) ;
127+ } ) ;
40128} ) ;
0 commit comments