Spaces:
Running
Running
File size: 2,899 Bytes
6c30253 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import test from 'node:test';
import assert from 'node:assert/strict';
import { parseDocumentRegions } from '../src/document-parsing.js';
test('parses document regions across multiple page images', () => {
const text = `image_index=0 title [58, 78, 677, 100]
Stellar engines and Dyson bubbles can be stable
image_index=0 text [58, 116, 235, 131]
Colin R. McInnes*
image_index=1 equation [100, 200, 800, 300]
E = mc^2`;
assert.deepEqual(parseDocumentRegions(text, 2), [
{ imageId: 0, label: 'title', type: 'box', coordinates: [58, 78, 677, 100], content: 'Stellar engines and Dyson bubbles can be stable' },
{ imageId: 0, label: 'text', type: 'box', coordinates: [58, 116, 235, 131], content: 'Colin R. McInnes*' },
{ imageId: 1, label: 'equation', type: 'box', coordinates: [100, 200, 800, 300], content: 'E = mc^2' },
]);
});
test('preserves multiline region content', () => {
const [region] = parseDocumentRegions('image_index=0 code [0, 0, 500, 500]\nconst x = 1;\nreturn x;', 1);
assert.equal(region.content, 'const x = 1;\nreturn x;');
});
test('accepts harmless header whitespace', () => {
const parsed = parseDocumentRegions(`image_index = 0 title [ 58,78, 677 , 100 ]
Liquid Foundation Model`, 1);
assert.equal(parsed.length, 1);
assert.deepEqual(parsed[0].coordinates, [58, 78, 677, 100]);
});
test('accepts model-chosen labels and zero-to-one coordinates', () => {
const parsed = parseDocumentRegions(`image_index=0 Quoted label / callout [0.318, 0.045, 0.707, 0.067]
The second image shows the cows in their natural habitat.
image_index=0 image [202, 78, 797, 486]
Diagram of Liquid Foundation Model architecture.`, 1);
assert.deepEqual(parsed, [
{ imageId: 0, label: 'Quoted label / callout', type: 'box', coordinates: [318, 45, 707, 67], content: 'The second image shows the cows in their natural habitat.' },
{ imageId: 0, label: 'image', type: 'box', coordinates: [202, 78, 797, 486], content: 'Diagram of Liquid Foundation Model architecture.' },
]);
});
test('keeps complete regions when the final generated region is truncated', () => {
const parsed = parseDocumentRegions(`image_index=0 title [58, 78, 677, 100]
Liquid Foundation Model
image_index=0 text [58, 116, 530, 155]
Model architecture description
image_index=0 table [`, 1);
assert.equal(parsed.length, 2);
assert.equal(parsed[1].content, 'Model architecture description');
});
test('rejects malformed document layouts', () => {
assert.equal(parseDocumentRegions('ordinary answer', 1), null);
assert.equal(parseDocumentRegions('image_index=1 text [0, 0, 1, 1]\ncontent', 1), null);
assert.equal(parseDocumentRegions('image_index=0 text [10, 10, 5, 20]\ncontent', 1), null);
assert.equal(parseDocumentRegions('image_index=0 text [0, nope, 1, 1]\ncontent', 1), null);
assert.equal(parseDocumentRegions('image_index=0 text [0, 0, 1, 1]', 1), null);
});
|