|
| 1 | +/** |
| 2 | + * Tests for #533: answer() must always return a string, never null/undefined. |
| 3 | + * |
| 4 | + * When both structured output and finalText are empty (e.g., during timeouts |
| 5 | + * or model failures), answer() previously returned null, causing downstream |
| 6 | + * callers like delegate() to throw "Delegate agent returned invalid response |
| 7 | + * (not a string)". |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, test, expect, jest, beforeEach } from '@jest/globals'; |
| 11 | +import { ProbeAgent } from '../../src/agent/ProbeAgent.js'; |
| 12 | + |
| 13 | +function createAgent(opts = {}) { |
| 14 | + return new ProbeAgent({ |
| 15 | + path: process.cwd(), |
| 16 | + model: 'test-model', |
| 17 | + ...opts, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +describe('answer() null-guard (#533)', () => { |
| 22 | + let agent; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + agent = createAgent(); |
| 26 | + jest.spyOn(agent, 'getSystemMessage').mockResolvedValue('You are a test agent.'); |
| 27 | + jest.spyOn(agent, 'prepareMessagesWithImages').mockImplementation(msgs => msgs); |
| 28 | + jest.spyOn(agent, '_buildThinkingProviderOptions').mockReturnValue(null); |
| 29 | + agent.provider = null; |
| 30 | + }); |
| 31 | + |
| 32 | + test('returns empty string when schema output and finalText are both null', async () => { |
| 33 | + jest.spyOn(agent, 'streamTextWithRetryAndFallback').mockImplementation(async () => ({ |
| 34 | + text: Promise.resolve(''), |
| 35 | + finishReason: Promise.resolve('stop'), |
| 36 | + usage: Promise.resolve({ promptTokens: 10, completionTokens: 0 }), |
| 37 | + response: { messages: Promise.resolve([]) }, |
| 38 | + experimental_providerMetadata: undefined, |
| 39 | + steps: Promise.resolve([]), |
| 40 | + // No output property — simulates no structured output |
| 41 | + })); |
| 42 | + |
| 43 | + const result = await agent.answer('test question'); |
| 44 | + expect(typeof result).toBe('string'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('returns empty string when schema present but outputObject is null', async () => { |
| 48 | + jest.spyOn(agent, 'streamTextWithRetryAndFallback').mockImplementation(async (opts) => ({ |
| 49 | + text: Promise.resolve(''), |
| 50 | + finishReason: Promise.resolve('stop'), |
| 51 | + usage: Promise.resolve({ promptTokens: 10, completionTokens: 0 }), |
| 52 | + response: { messages: Promise.resolve([]) }, |
| 53 | + experimental_providerMetadata: undefined, |
| 54 | + steps: Promise.resolve([]), |
| 55 | + result: { |
| 56 | + output: Promise.resolve(null), |
| 57 | + text: Promise.resolve(''), |
| 58 | + steps: Promise.resolve([]), |
| 59 | + }, |
| 60 | + })); |
| 61 | + |
| 62 | + // Pass a schema to trigger the schema code path |
| 63 | + const { z } = await import('zod'); |
| 64 | + const result = await agent.answer('test question', [], { |
| 65 | + schema: z.object({ answer: z.string() }), |
| 66 | + }); |
| 67 | + expect(typeof result).toBe('string'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('returns empty string when schema output throws and finalText is empty', async () => { |
| 71 | + jest.spyOn(agent, 'streamTextWithRetryAndFallback').mockImplementation(async (opts) => { |
| 72 | + // Create the rejection lazily to avoid unhandled-rejection before await |
| 73 | + const outputPromise = new Promise((_, reject) => { |
| 74 | + setTimeout(() => reject(new Error('NoObjectGeneratedError')), 0); |
| 75 | + }); |
| 76 | + // Attach a no-op catch so Node doesn't report unhandled rejection |
| 77 | + outputPromise.catch(() => {}); |
| 78 | + |
| 79 | + return { |
| 80 | + text: Promise.resolve(''), |
| 81 | + finishReason: Promise.resolve('stop'), |
| 82 | + usage: Promise.resolve({ promptTokens: 10, completionTokens: 0 }), |
| 83 | + response: { messages: Promise.resolve([]) }, |
| 84 | + experimental_providerMetadata: undefined, |
| 85 | + steps: Promise.resolve([]), |
| 86 | + result: { |
| 87 | + output: outputPromise, |
| 88 | + text: Promise.resolve(''), |
| 89 | + steps: Promise.resolve([]), |
| 90 | + }, |
| 91 | + }; |
| 92 | + }); |
| 93 | + |
| 94 | + const { z } = await import('zod'); |
| 95 | + const result = await agent.answer('test question', [], { |
| 96 | + schema: z.object({ answer: z.string() }), |
| 97 | + }); |
| 98 | + expect(typeof result).toBe('string'); |
| 99 | + }); |
| 100 | + |
| 101 | + test('returns actual text when finalText is available', async () => { |
| 102 | + jest.spyOn(agent, 'streamTextWithRetryAndFallback').mockImplementation(async () => ({ |
| 103 | + text: Promise.resolve('here is my answer'), |
| 104 | + finishReason: Promise.resolve('stop'), |
| 105 | + usage: Promise.resolve({ promptTokens: 10, completionTokens: 5 }), |
| 106 | + response: { messages: Promise.resolve([]) }, |
| 107 | + experimental_providerMetadata: undefined, |
| 108 | + steps: Promise.resolve([]), |
| 109 | + })); |
| 110 | + |
| 111 | + const result = await agent.answer('test question'); |
| 112 | + expect(result).toBe('here is my answer'); |
| 113 | + }); |
| 114 | +}); |
0 commit comments