Source index

test/agent.test.mjs

import test from 'node:test';
import assert from 'node:assert/strict';
import {parseRpc, ContextClient} from '../src/context.mjs';
import {localModelUrl, verifyCitations, assessGrounding, buildGuideQuery, renderPublishedPlan, joinedGuide, corpusBoundary} from '../src/agent.mjs';

test('streamable HTTP parses multi-event SSE with line breaks and selects result', () => {
  const body = 'event: ping\ndata: {"method":"ping"}\n\nevent: message\ndata: {"jsonrpc":"2.0",\ndata: "id":1,"result":{"tools":[]}}\n\n';
  assert.deepEqual(parseRpc(body, 'text/event-stream'), {jsonrpc: '2.0', id: 1, result: {tools: []}});
  assert.throws(() => parseRpc('event: ping\ndata: {"method":"ping"}\n\n', 'text/event-stream'));
});
test('endpoint and model addresses prevent credentials being sent to arbitrary hosts', () => {
  for (const url of ['https://evil.example/v1/context/organizations/test', 'https://api.sanity.io.evil.example/v1/context/organizations/test', 'http://api.sanity.io/v1/context/organizations/test']) assert.throws(() => new ContextClient({url, token: 'fake'}));
  assert.throws(() => localModelUrl('https://paid.example'));
  assert.equal(localModelUrl('http://127.0.0.1:11439'), 'http://127.0.0.1:11439/api/chat');
});
test('citation gate rejects an invented W3C reference despite a successful tool result', () => {
  const source = 'https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/';
  const fake = 'https://www.w3.org/WAI/invented/';
  assert.deepEqual(verifyCitations(`See ${source} and ${fake}`, [{content: [{type: 'text', text: JSON.stringify({references: [{url: source}]})}]}]).unsupported, [fake]);
  assert.equal(verifyCitations(`See ${source}`, [{content: [{type: 'text', text: source}]}]).unsupported.length, 0);
  assert.deepEqual(verifyCitations('See https://developer.mozilla.org/invented/', [{content: [{type: 'text', text: source}]}]).unsupported, ['https://developer.mozilla.org/invented/']);
});
test('all-error retrieval and an uncited guidance answer cannot pass grounding', () => {
  assert.equal(assessGrounding('Trap focus inside the dialog.', [], true).status, 'grounding-rejected');
  assert.equal(assessGrounding('Trap focus inside the dialog.', [{content: [{type: 'text', text: 'https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/'}]}], true).status, 'grounding-rejected');
  assert.equal(assessGrounding('UNSUPPORTED: This dataset has no color guidance.', [{content: [{type: 'text', text: '[]'}]}], true).status, 'abstained');
  assert.equal(assessGrounding('UNSUPPORTED: I cannot answer.', [], true).status, 'grounding-rejected');
});
test('narrow query adapter escapes search text and rejects a forged guide predicate', () => {
  const query = buildGuideQuery('search_guides', {symptom: 'focus ") || _type == "secret'});
  assert.ok(query.includes(JSON.stringify('focus ") || _type == "secret')));
  assert.throws(() => buildGuideQuery('read_guide', {id: 'guide-dialog"] || true'}));
  assert.ok(buildGuideQuery('read_guide', {id: 'guide-dialog'}).includes('references[0]->'));
});
test('published-plan renderer preserves source constraints and does not insert model inventions', () => {
  const guide = {_id: 'guide-dialog', title: 'Modal focus', problem: 'Tab escapes.', repairSteps: ['Contain Tab and Shift+Tab.', 'Move focus inside.'], constraints: ['aria-modal does not implement modality.'], scopeNote: 'No application was tested.', source: {title: 'Dialog Pattern', url: 'https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/', checkedAt: '2026-10-02'}, check: {procedure: ['Open the dialog.'], expected: ['Focus is inside.'], limitations: ['Not executed.']}};
  const plan = renderPublishedPlan([guide]);
  assert.ok(plan.includes(guide.constraints[0])); assert.ok(plan.includes('Suggested manual review (not performed)')); assert.ok(!plan.includes('tabindex="-1"'));
  assert.equal(joinedGuide({content: [{type: 'text', text: JSON.stringify({result: guide})}]})._id, 'guide-dialog');
  assert.equal(joinedGuide({isError: true, content: [{type: 'text', text: JSON.stringify({result: guide})}]}), null);
});
test('explicit corpus boundaries reject certification/contrast without rejecting focus guidance', () => {
  assert.ok(corpusBoundary('Can you certify my website against lawsuits?'));
  assert.ok(corpusBoundary('What is this text contrast ratio?'));
  assert.equal(corpusBoundary('How should focus stay visible under WCAG 2.2?'), null);
  assert.equal(corpusBoundary('A dialog loses focus when it closes.'), null);
});