|
| 1 | +/** |
| 2 | + * @file TypedNetworkObserver.test.ts |
| 3 | + * @description Contract tests for the 6-step LLM extractor. Uses a |
| 4 | + * mocked LLM to assert: structured-output parsing, ID generation |
| 5 | + * format, all-bank routing through the observer, validation rejection |
| 6 | + * of malformed output, and code-fence tolerance. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { TypedNetworkObserver, type ITypedExtractionLLM } from '../TypedNetworkObserver.js'; |
| 11 | + |
| 12 | +function mockLLM(response: string): ITypedExtractionLLM { |
| 13 | + return { invoke: async () => response }; |
| 14 | +} |
| 15 | + |
| 16 | +describe('TypedNetworkObserver', () => { |
| 17 | + it('parses valid LLM output into TypedFact[]', async () => { |
| 18 | + const llm = mockLLM(JSON.stringify({ |
| 19 | + facts: [{ |
| 20 | + text: 'Berlin is in Germany', |
| 21 | + bank: 'WORLD', |
| 22 | + temporal: { mention: '2026-04-26T10:00:00Z' }, |
| 23 | + participants: [], |
| 24 | + reasoning_markers: [], |
| 25 | + entities: ['Berlin', 'Germany'], |
| 26 | + confidence: 1.0, |
| 27 | + }], |
| 28 | + })); |
| 29 | + const obs = new TypedNetworkObserver({ llm }); |
| 30 | + const facts = await obs.extract('User: Where is Berlin? Assistant: In Germany.', 'session-1'); |
| 31 | + expect(facts).toHaveLength(1); |
| 32 | + expect(facts[0].bank).toBe('WORLD'); |
| 33 | + expect(facts[0].entities).toContain('Berlin'); |
| 34 | + expect(facts[0].id).toBe('session-1-fact-0'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('generates sequential IDs for multiple facts', async () => { |
| 38 | + const llm = mockLLM(JSON.stringify({ |
| 39 | + facts: [ |
| 40 | + { text: 'A', bank: 'WORLD', temporal: { mention: '2026-04-26T10:00:00Z' }, participants: [], reasoning_markers: [], entities: [], confidence: 1.0 }, |
| 41 | + { text: 'B', bank: 'EXPERIENCE', temporal: { mention: '2026-04-26T10:01:00Z' }, participants: [], reasoning_markers: [], entities: [], confidence: 1.0 }, |
| 42 | + { text: 'C', bank: 'OPINION', temporal: { mention: '2026-04-26T10:02:00Z' }, participants: [], reasoning_markers: [], entities: [], confidence: 0.7 }, |
| 43 | + ], |
| 44 | + })); |
| 45 | + const obs = new TypedNetworkObserver({ llm }); |
| 46 | + const facts = await obs.extract('text', 'sx'); |
| 47 | + expect(facts.map((f) => f.id)).toEqual(['sx-fact-0', 'sx-fact-1', 'sx-fact-2']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('routes facts into all four banks', async () => { |
| 51 | + const llm = mockLLM(JSON.stringify({ |
| 52 | + facts: [ |
| 53 | + { text: 'World', bank: 'WORLD', temporal: { mention: 'now' }, participants: [], reasoning_markers: [], entities: [], confidence: 1.0 }, |
| 54 | + { text: 'Exp', bank: 'EXPERIENCE', temporal: { mention: 'now' }, participants: [], reasoning_markers: [], entities: [], confidence: 1.0 }, |
| 55 | + { text: 'Op', bank: 'OPINION', temporal: { mention: 'now' }, participants: [], reasoning_markers: [], entities: [], confidence: 0.5 }, |
| 56 | + { text: 'Obs', bank: 'OBSERVATION', temporal: { mention: 'now' }, participants: [], reasoning_markers: [], entities: [], confidence: 1.0 }, |
| 57 | + ], |
| 58 | + })); |
| 59 | + const obs = new TypedNetworkObserver({ llm }); |
| 60 | + const facts = await obs.extract('text', 's1'); |
| 61 | + const banks = facts.map((f) => f.bank); |
| 62 | + expect(banks).toEqual(['WORLD', 'EXPERIENCE', 'OPINION', 'OBSERVATION']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('snake_case → camelCase translation for reasoning_markers', async () => { |
| 66 | + const llm = mockLLM(JSON.stringify({ |
| 67 | + facts: [{ |
| 68 | + text: 'Because the user prefers TypeScript, we use Bun', |
| 69 | + bank: 'EXPERIENCE', |
| 70 | + temporal: { mention: '2026-04-26T10:00:00Z' }, |
| 71 | + participants: [], |
| 72 | + reasoning_markers: ['Because', 'we use'], |
| 73 | + entities: ['TypeScript', 'Bun'], |
| 74 | + confidence: 1.0, |
| 75 | + }], |
| 76 | + })); |
| 77 | + const obs = new TypedNetworkObserver({ llm }); |
| 78 | + const facts = await obs.extract('text', 's1'); |
| 79 | + expect(facts[0].reasoningMarkers).toEqual(['Because', 'we use']); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws on missing required field (zod validation)', async () => { |
| 83 | + const llm = mockLLM('{"facts": [{"text": ""}]}'); |
| 84 | + const obs = new TypedNetworkObserver({ llm }); |
| 85 | + await expect(obs.extract('blah', 'session-2')).rejects.toThrow(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('throws on unknown bank label', async () => { |
| 89 | + const llm = mockLLM(JSON.stringify({ |
| 90 | + facts: [{ |
| 91 | + text: 'foo', |
| 92 | + bank: 'FOO', |
| 93 | + temporal: { mention: 'now' }, |
| 94 | + participants: [], |
| 95 | + reasoning_markers: [], |
| 96 | + entities: [], |
| 97 | + confidence: 1.0, |
| 98 | + }], |
| 99 | + })); |
| 100 | + const obs = new TypedNetworkObserver({ llm }); |
| 101 | + await expect(obs.extract('text', 's1')).rejects.toThrow(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('throws on confidence outside [0, 1]', async () => { |
| 105 | + const llm = mockLLM(JSON.stringify({ |
| 106 | + facts: [{ |
| 107 | + text: 'foo', |
| 108 | + bank: 'OPINION', |
| 109 | + temporal: { mention: 'now' }, |
| 110 | + participants: [], |
| 111 | + reasoning_markers: [], |
| 112 | + entities: [], |
| 113 | + confidence: 1.5, |
| 114 | + }], |
| 115 | + })); |
| 116 | + const obs = new TypedNetworkObserver({ llm }); |
| 117 | + await expect(obs.extract('text', 's1')).rejects.toThrow(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('tolerates triple-backtick code fence around JSON', async () => { |
| 121 | + const llm = mockLLM('```json\n{"facts": []}\n```'); |
| 122 | + const obs = new TypedNetworkObserver({ llm }); |
| 123 | + const facts = await obs.extract('text', 's1'); |
| 124 | + expect(facts).toEqual([]); |
| 125 | + }); |
| 126 | + |
| 127 | + it('tolerates bare backticks without language tag', async () => { |
| 128 | + const llm = mockLLM('```\n{"facts": []}\n```'); |
| 129 | + const obs = new TypedNetworkObserver({ llm }); |
| 130 | + const facts = await obs.extract('text', 's1'); |
| 131 | + expect(facts).toEqual([]); |
| 132 | + }); |
| 133 | + |
| 134 | + it('passes maxTokens and temperature to the LLM', async () => { |
| 135 | + let capturedArgs: { maxTokens: number; temperature: number } | undefined; |
| 136 | + const llm: ITypedExtractionLLM = { |
| 137 | + invoke: async (args) => { |
| 138 | + capturedArgs = { maxTokens: args.maxTokens, temperature: args.temperature }; |
| 139 | + return JSON.stringify({ facts: [] }); |
| 140 | + }, |
| 141 | + }; |
| 142 | + const obs = new TypedNetworkObserver({ llm, maxTokens: 8192, temperature: 0.2 }); |
| 143 | + await obs.extract('text', 's1'); |
| 144 | + expect(capturedArgs?.maxTokens).toBe(8192); |
| 145 | + expect(capturedArgs?.temperature).toBe(0.2); |
| 146 | + }); |
| 147 | + |
| 148 | + it('default maxTokens=4096, temperature=0', async () => { |
| 149 | + let capturedArgs: { maxTokens: number; temperature: number } | undefined; |
| 150 | + const llm: ITypedExtractionLLM = { |
| 151 | + invoke: async (args) => { |
| 152 | + capturedArgs = { maxTokens: args.maxTokens, temperature: args.temperature }; |
| 153 | + return JSON.stringify({ facts: [] }); |
| 154 | + }, |
| 155 | + }; |
| 156 | + const obs = new TypedNetworkObserver({ llm }); |
| 157 | + await obs.extract('text', 's1'); |
| 158 | + expect(capturedArgs?.maxTokens).toBe(4096); |
| 159 | + expect(capturedArgs?.temperature).toBe(0); |
| 160 | + }); |
| 161 | +}); |
0 commit comments