diff --git a/.gitignore b/.gitignore index 04385494854..7f89b96eb78 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,6 @@ gemini-debug.log .genkit .gemini-clipboard/ .eslintcache -evals/logs/ \ No newline at end of file +evals/logs/ + +evals/logs/ diff --git a/packages/sdk/src/agent.test.ts b/packages/sdk/src/agent.test.ts new file mode 100644 index 00000000000..d85d671b4b9 --- /dev/null +++ b/packages/sdk/src/agent.test.ts @@ -0,0 +1,47 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { GeminiCliAgent } from './agent.js'; +import { GeminiCliSession } from './session.js'; + +beforeEach(() => { + vi.restoreAllMocks(); +}); + +describe('GeminiCliSession', () => { + it('propagates errors from dynamic instructions', async () => { + vi.spyOn(GeminiCliSession.prototype, 'initialize').mockImplementation( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async function (this: any) { + this.client = { + getHistory: () => [], + async *sendMessageStream () {}, + }; + this.initialized = true; + }, + ); + + let callCount = 0; + const agent = new GeminiCliAgent({ + instructions: () => { + callCount++; + throw new Error('Dynamic instruction failure'); + }, + model: 'gemini-2.0-flash', + }); + + const stream = agent.session().sendStream('Say hello.'); + + await expect(async () => { + for await (const _event of stream) { + // consume stream + } + }).rejects.toThrow('Dynamic instruction failure'); + + expect(callCount).toBe(1); + }); +});