// The assistant protocol: how the document goes out and how ops come back. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { contextForAssistant, docForAssistant, parseReply, validateOp, ASSISTANT_INSTRUCTIONS } from '../src/suggestions/protocol.ts'; import { SAMPLE } from '../demo/sample.ts'; import { stateOf } from './harness.ts'; test('the document renders as id-tagged blocks', () => { const s = stateOf(SAMPLE); const text = docForAssistant(s.doc, 'The Lantern Protocol'); assert.match(text, /^# The Lantern Protocol\n/); assert.match(text, /\[s-intro\] ## Introduction\n/); assert.match(text, /\[b-intro-1\] A lantern is a very small/); assert.match(text, /\[b-model-code\] ```json\n\{\n/); assert.match(text, /\[b-model-dl\] resource: The URI the lantern is about\.\n\s+lit: When/); assert.match(text, /\[b-pub-list\] 1\. Confirm/); assert.match(text, /\[b-model-3\] A lantern is stale when the current time is later than \. Stale/); assert.ok(ASSISTANT_INSTRUCTIONS.includes('"op":"replace"')); }); test('replies parse from fenced, bare, or prose-wrapped JSON, and bad ops are reported', () => { const fenced = parseReply('Here you go:\n```json\n{"message":"Two things.","ops":[{"op":"delete","block":"b1","find":"very "},{"op":"nope","block":"b1"}]}\n```'); assert.equal(fenced.reply.message, 'Two things.'); assert.equal(fenced.reply.ops.length, 1); assert.deepEqual(fenced.errors, ['op 2: unknown op "nope"']); const bare = parseReply('{"ops":[{"op":"comment","block":"b1","text":"hm"}]}'); assert.equal(bare.reply.message, '1 suggestion.'); const prose = parseReply('Just a thought, no changes.'); assert.equal(prose.reply.message, 'Just a thought, no changes.'); assert.deepEqual(prose.reply.ops, []); const broken = parseReply('{"message": "oops", "ops": [}'); assert.equal(broken.reply.ops.length, 0); assert.ok(broken.errors[0]!.startsWith('invalid JSON')); }); test('validateOp checks shapes', () => { assert.ok('op' in validateOp({ op: 'replace', block: 'b', find: 'a', with: 'b' })); assert.deepEqual(validateOp({ op: 'replace', block: 'b', find: 'a' }), { error: 'replace needs a string "with"' }); assert.deepEqual(validateOp({ op: 'format', block: 'b', find: 'a', mark: 'underline' }), { error: 'format: unknown mark "underline"' }); assert.deepEqual(validateOp({ op: 'insert-block', text: 'x', type: 'table' }), { error: 'insert-block: unknown type "table"' }); assert.deepEqual(validateOp({ op: 'delete', block: 'b', find: 'a', nth: 0 }), { error: 'delete: nth must be a positive number' }); assert.deepEqual(validateOp('nope'), { error: 'not an object' }); }); test('outline notes are marked in the text, and context goes beside it without ids', () => { const s = stateOf({ type: 'doc', content: [ { type: 'section', attrs: { id: 's1', anchor: 's1', anchorTouched: false }, content: [ { type: 'sectionTitle', content: [{ type: 'text', text: 'Why' }] }, { type: 'paragraph', attrs: { id: 'p1', outline: true }, content: [{ type: 'text', text: 'Open with the problem.' }] }, { type: 'paragraph', attrs: { id: 'p2' }, content: [{ type: 'text', text: 'Prose.' }] }, ], }, ], }); const text = docForAssistant(s.doc); assert.match(text, /\[p1\] \(outline\) Open with the problem\./); assert.match(text, /\[p2\] Prose\./); assert.equal(contextForAssistant([]), ''); const ctx = contextForAssistant([{ label: 'Chapter 2: The Argument', text: 'Here it goes.' }, { label: 'Note: Polycentricity', text: 'Ostrom says…' }]); assert.match(ctx, /^For context only\./); assert.match(ctx, /### Chapter 2: The Argument\n\nHere it goes\./); assert.match(ctx, /### Note: Polycentricity\n\nOstrom says…/); assert.ok(ASSISTANT_INSTRUCTIONS.includes('(outline)')); assert.ok(ASSISTANT_INSTRUCTIONS.includes('"outline":true')); });