Skip to content

Commit b341b07

Browse files
committed
adding tests
1 parent 55ff73e commit b341b07

2 files changed

Lines changed: 175 additions & 0 deletions

File tree

test/gridsetProcessor.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,91 @@ describe('GridsetProcessor', () => {
8383
}
8484
});
8585
});
86+
87+
describe('saveModifiedTree', () => {
88+
const tempOutputPath = path.join(__dirname, 'temp_gridset_modified.gridset');
89+
const tempSaveFromTreePath = path.join(__dirname, 'temp_gridset_saveFromTree.gridset');
90+
91+
afterEach(async () => {
92+
if (fs.existsSync(tempOutputPath)) {
93+
fs.unlinkSync(tempOutputPath);
94+
}
95+
if (fs.existsSync(tempSaveFromTreePath)) {
96+
fs.unlinkSync(tempSaveFromTreePath);
97+
}
98+
});
99+
100+
it('should preserve original file size better than saveFromTree', async () => {
101+
const processor = new GridsetProcessor();
102+
103+
// Load the original file
104+
const fileBuffer = fs.readFileSync(exampleFile);
105+
const tree = await processor.loadIntoTree(fileBuffer);
106+
const originalSize = fileBuffer.length;
107+
108+
// Save using saveModifiedTree
109+
await processor.saveModifiedTree(exampleFile, tree, tempOutputPath);
110+
const modifiedSize = fs.statSync(tempOutputPath).size;
111+
112+
// Save using saveFromTree for comparison
113+
await processor.saveFromTree(tree, tempSaveFromTreePath);
114+
const saveFromTreeSize = fs.statSync(tempSaveFromTreePath).size;
115+
116+
// saveModifiedTree should preserve file size much better than saveFromTree
117+
expect(modifiedSize).toBeGreaterThan(saveFromTreeSize);
118+
119+
// saveModifiedTree should be at least 80% of original size (preserves most assets)
120+
expect(modifiedSize / originalSize).toBeGreaterThan(0.8);
121+
});
122+
123+
it('should produce a valid loadable gridset', async () => {
124+
const processor = new GridsetProcessor();
125+
126+
// Load the original file
127+
const fileBuffer = fs.readFileSync(exampleFile);
128+
const tree = await processor.loadIntoTree(fileBuffer);
129+
const originalPageCount = Object.keys(tree.pages).length;
130+
131+
// Save using saveModifiedTree
132+
await processor.saveModifiedTree(exampleFile, tree, tempOutputPath);
133+
134+
// Load the saved file
135+
const savedBuffer = fs.readFileSync(tempOutputPath);
136+
const savedTree = await processor.loadIntoTree(savedBuffer);
137+
138+
// Verify the saved tree has the same pages
139+
expect(Object.keys(savedTree.pages).length).toBe(originalPageCount);
140+
expect(savedTree.rootId).toBe(tree.rootId);
141+
});
142+
143+
it('should handle empty tree by copying original', async () => {
144+
const processor = new GridsetProcessor();
145+
146+
// Create an empty tree
147+
const emptyTree: AACTree = {
148+
pages: {},
149+
rootId: null,
150+
toolbarId: null,
151+
dashboardId: null,
152+
metadata: {},
153+
addPage() {
154+
throw new Error('Not implemented');
155+
},
156+
getPage() {
157+
return undefined;
158+
},
159+
traverse() {
160+
// Empty - nothing to traverse
161+
},
162+
};
163+
164+
// Save using saveModifiedTree
165+
await processor.saveModifiedTree(exampleFile, emptyTree, tempOutputPath);
166+
167+
// Verify the file was copied (same size)
168+
const originalSize = fs.statSync(exampleFile).size;
169+
const copiedSize = fs.statSync(tempOutputPath).size;
170+
expect(copiedSize).toBe(originalSize);
171+
});
172+
});
86173
});

test/obfProcessor.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)