import test from 'node:test'; import assert from 'node:assert/strict'; import { parseGroundingResponse } from '../src/grounding.js'; test('parses normalized boxes and points', () => { assert.deepEqual(parseGroundingResponse(JSON.stringify([ { image_id: 0, bbox_2d: [10, 20, 500, 600], label: 'cat' }, { image_id: 1, point_2d: [750, 125], label: 'nose' }, ]), 2), [ { imageId: 0, label: 'cat', type: 'box', coordinates: [10, 20, 500, 600] }, { imageId: 1, label: 'nose', type: 'point', coordinates: [750, 125] }, ]); }); test('accepts a valid empty grounding response', () => { assert.deepEqual(parseGroundingResponse('[]', 1), []); }); test('renders bare four-number lists as boxes on the first image', () => { assert.deepEqual(parseGroundingResponse('Detected regions: [10, 20, 500, 600] and [600, 100, 900, 800].', 2), [ { imageId: 0, label: 'Bounding box', type: 'box', coordinates: [10, 20, 500, 600] }, { imageId: 0, label: 'Bounding box 2', type: 'box', coordinates: [600, 100, 900, 800] }, ]); }); test('normalizes zero-to-one boxes in structured and bare output', () => { assert.deepEqual(parseGroundingResponse('[{"image_id":0,"label":"cat","bbox_2d":[0.1,0.2,0.8,0.9]}]', 1), [ { imageId: 0, label: 'cat', type: 'box', coordinates: [100, 200, 800, 900] }, ]); assert.deepEqual(parseGroundingResponse('box: [0.125, 0.25, 0.75, 1]', 1), [ { imageId: 0, label: 'Bounding box', type: 'box', coordinates: [125, 250, 750, 1000] }, ]); }); test('does not treat fenced or unrelated JSON as grounding', () => { assert.equal(parseGroundingResponse('```json\n[]\n```', 1), null); assert.equal(parseGroundingResponse('{"answer": 4}', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat","bbox_2d":[0,0,1,1],"score":1}]', 1), null); }); test('rejects invalid image references and coordinates', () => { assert.equal(parseGroundingResponse('[{"image_id":1,"label":"cat","bbox_2d":[0,0,10,10]}]', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat","point_2d":[1.5,2]}]', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat","point_2d":[1001,2]}]', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat","bbox_2d":[20,20,10,30]}]', 1), null); }); test('requires one and only one supported geometry', () => { assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat"}]', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"cat","point_2d":[1,2],"bbox_2d":[0,0,2,3]}]', 1), null); assert.equal(parseGroundingResponse('[{"image_id":0,"label":"","point_2d":[1,2]}]', 1), null); });