Skip to content

Commit 8d38abd

Browse files
committed
Add verification
1 parent 1a8a584 commit 8d38abd

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

wasm/nutrient/benchmark.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ function renderAnnotation(annotationId) {
173173
}
174174
}
175175

176+
// `getContentTree` retrieves the page text and accessibility tree for a given page index.
176177
function getContentTree(pageIndex) {
177178
const result = Module.dispatchCommand(
178179
JSON.stringify({
@@ -184,6 +185,41 @@ function getContentTree(pageIndex) {
184185
if (result && result.hasError()) {
185186
throw new Error("Failed to get content tree: " + result.getErrorMessage());
186187
}
188+
189+
return getSingleJsonReplyFromResult(result);
190+
}
191+
192+
function verifyContentTree(contentTree) {
193+
if (
194+
!contentTree ||
195+
!Array.isArray(contentTree.nodes) ||
196+
contentTree.nodes.length === 0
197+
) {
198+
throw new Error("Content tree is empty or not an array");
199+
}
200+
}
201+
202+
function getAnnotations() {
203+
const result = Module.dispatchCommand(
204+
JSON.stringify({
205+
type: "get_annotations",
206+
skip_attachments: true,
207+
})
208+
);
209+
210+
if (result && result.hasError()) {
211+
throw new Error("Failed to get annotations: " + result.getErrorMessage());
212+
}
213+
214+
const annotations = [];
215+
for (let i = 0; i < result.getRepliesCount(); i++) {
216+
if (result.hasJSONReply(i)) {
217+
annotations.push(JSON.parse(result.getJSONReply(i)));
218+
} else {
219+
throw new Error(`Expected JSON reply for annotation ${i}`);
220+
}
221+
}
222+
return annotations;
187223
}
188224

189225
// Page width and height for the assets/example.pdf document.
@@ -209,12 +245,18 @@ class Benchmark {
209245
openDocument("/document.pdf");
210246

211247
importInstantJSON(Module.annotations);
248+
const annotations = getAnnotations();
249+
if (annotations.length !== 9) {
250+
throw new Error(`Expected 9 annotations, but got ${annotations.length}`);
251+
}
212252

213253
var renderScale = 0.3;
214254
renderPage(0, PAGE_WIDTH * renderScale, PAGE_HEIGHT * renderScale);
215255
renderPage(1, PAGE_WIDTH * renderScale, PAGE_HEIGHT * renderScale);
216256

217-
getContentTree(0);
218-
getContentTree(1);
257+
const contentTree0 = getContentTree(0);
258+
verifyContentTree(contentTree0);
259+
const contentTree1 = getContentTree(1);
260+
verifyContentTree(contentTree1);
219261
}
220262
}

0 commit comments

Comments
 (0)