llm-complete-openrouter-data-integrity.test.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
3 days ago
| 1 | /** |
| 2 | * Tier 5 — DATA INTEGRITY: the OpenRouter lane must transmit and return content exactly. |
| 3 | * |
| 4 | * Goal: prove the request body faithfully carries the caller's system/user text, model, |
| 5 | * and max_tokens with no mutation, ordering change, or truncation, and that the response |
| 6 | * content is returned verbatim (modulo the documented trim). Covers Unicode, very large |
| 7 | * inputs, and multi-line content. Network is mocked. |
| 8 | */ |
| 9 | import { describe, it, beforeEach, afterEach } from 'node:test'; |
| 10 | import assert from 'node:assert'; |
| 11 | import { completeChat } from '../lib/llm-complete.mjs'; |
| 12 | |
| 13 | const ORIG = { ...process.env }; |
| 14 | const origFetch = globalThis.fetch; |
| 15 | |
| 16 | const CHAT_ENV_KEYS = [ |
| 17 | 'OPENAI_API_KEY', |
| 18 | 'ANTHROPIC_API_KEY', |
| 19 | 'DEEPINFRA_API_KEY', |
| 20 | 'OPENROUTER_API_KEY', |
| 21 | 'OPENROUTER_CHAT_MODEL', |
| 22 | 'OPENROUTER_SITE_URL', |
| 23 | 'OPENROUTER_APP_TITLE', |
| 24 | 'KNOWTATION_CHAT_PROVIDER', |
| 25 | 'KNOWTATION_CHAT_PREFER_ANTHROPIC', |
| 26 | ]; |
| 27 | |
| 28 | function clearChatEnv() { |
| 29 | for (const k of CHAT_ENV_KEYS) delete process.env[k]; |
| 30 | } |
| 31 | |
| 32 | function restoreEnv() { |
| 33 | for (const k of CHAT_ENV_KEYS) { |
| 34 | if (ORIG[k] === undefined) delete process.env[k]; |
| 35 | else process.env[k] = ORIG[k]; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | function captureBody() { |
| 40 | const ref = {}; |
| 41 | globalThis.fetch = async (url, init) => { |
| 42 | ref.url = String(url); |
| 43 | ref.body = JSON.parse(init.body); |
| 44 | ref.headers = init.headers; |
| 45 | return { ok: true, json: async () => ({ choices: [{ message: { content: 'ack' } }] }) }; |
| 46 | }; |
| 47 | return ref; |
| 48 | } |
| 49 | |
| 50 | describe('OpenRouter lane — data integrity', () => { |
| 51 | beforeEach(() => { |
| 52 | clearChatEnv(); |
| 53 | process.env.KNOWTATION_CHAT_PROVIDER = 'openrouter'; |
| 54 | process.env.OPENROUTER_API_KEY = 'or-data'; |
| 55 | }); |
| 56 | |
| 57 | afterEach(() => { |
| 58 | globalThis.fetch = origFetch; |
| 59 | restoreEnv(); |
| 60 | }); |
| 61 | |
| 62 | it('sends system then user messages in order, content byte-for-byte', async () => { |
| 63 | const ref = captureBody(); |
| 64 | const system = 'SYSTEM-PROMPT-αβγ\nline2'; |
| 65 | const user = 'USER-PROMPT-😀 with "quotes", <tags>, and \\backslashes\\'; |
| 66 | await completeChat({}, { system, user }); |
| 67 | assert.deepStrictEqual(ref.body.messages, [ |
| 68 | { role: 'system', content: system }, |
| 69 | { role: 'user', content: user }, |
| 70 | ]); |
| 71 | }); |
| 72 | |
| 73 | it('preserves a very large note body without truncation', async () => { |
| 74 | const ref = captureBody(); |
| 75 | const big = 'x'.repeat(200_000); |
| 76 | await completeChat({}, { system: 's', user: big }); |
| 77 | assert.strictEqual(ref.body.messages[1].content.length, 200_000); |
| 78 | assert.strictEqual(ref.body.messages[1].content, big); |
| 79 | }); |
| 80 | |
| 81 | it('carries the exact custom max_tokens value', async () => { |
| 82 | const ref = captureBody(); |
| 83 | await completeChat({}, { system: 's', user: 'u', maxTokens: 777 }); |
| 84 | assert.strictEqual(ref.body.max_tokens, 777); |
| 85 | }); |
| 86 | |
| 87 | it('returns the completion content verbatim apart from outer-whitespace trim', async () => { |
| 88 | globalThis.fetch = async () => ({ |
| 89 | ok: true, |
| 90 | json: async () => ({ choices: [{ message: { content: '\n\tInner spaces kept 🚀\n' } }] }), |
| 91 | }); |
| 92 | const out = await completeChat({}, { system: 's', user: 'u' }); |
| 93 | assert.strictEqual(out, 'Inner spaces kept 🚀'); |
| 94 | }); |
| 95 | |
| 96 | it('serialises a JSON-bearing user payload safely (no double-encoding corruption)', async () => { |
| 97 | const ref = captureBody(); |
| 98 | const user = JSON.stringify({ a: 1, b: ['two', { c: '日本語' }] }); |
| 99 | await completeChat({}, { system: 's', user }); |
| 100 | assert.strictEqual(ref.body.messages[1].content, user); |
| 101 | assert.deepStrictEqual(JSON.parse(ref.body.messages[1].content), { |
| 102 | a: 1, |
| 103 | b: ['two', { c: '日本語' }], |
| 104 | }); |
| 105 | }); |
| 106 | }); |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
3 days ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
3 days ago