diff options
| author | Sandy Tao <[email protected]> | 2025-07-14 21:44:07 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-15 04:44:07 +0000 |
| commit | 886faa2990b191a0dbd01dc58a88e09e342886b5 (patch) | |
| tree | b22e1c3b95efe0678b86bfa1d959f17c9032b28e /packages/core/src/services/loopDetectionService.test.ts | |
| parent | 734da8b9d24ab7e2cfcfe80e43f061734b7ae6ba (diff) | |
Log the 2 types of loop detection (#4193)
Diffstat (limited to 'packages/core/src/services/loopDetectionService.test.ts')
| -rw-r--r-- | packages/core/src/services/loopDetectionService.test.ts | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/packages/core/src/services/loopDetectionService.test.ts b/packages/core/src/services/loopDetectionService.test.ts index 915bedc0..f0d76166 100644 --- a/packages/core/src/services/loopDetectionService.test.ts +++ b/packages/core/src/services/loopDetectionService.test.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { describe, it, expect, beforeEach } from 'vitest'; +import { describe, it, expect, beforeEach, vi } from 'vitest'; import { LoopDetectionService } from './loopDetectionService.js'; import { GeminiEventType, @@ -12,15 +12,26 @@ import { ServerGeminiToolCallRequestEvent, } from '../core/turn.js'; import { ServerGeminiStreamEvent } from '../core/turn.js'; +import { Config } from '../config/config.js'; +import * as loggers from '../telemetry/loggers.js'; + +vi.mock('../telemetry/loggers.js', () => ({ + logLoopDetected: vi.fn(), +})); const TOOL_CALL_LOOP_THRESHOLD = 5; const CONTENT_LOOP_THRESHOLD = 10; describe('LoopDetectionService', () => { let service: LoopDetectionService; + let mockConfig: Config; beforeEach(() => { - service = new LoopDetectionService(); + mockConfig = { + getTelemetryEnabled: () => true, + } as unknown as Config; + service = new LoopDetectionService(mockConfig); + vi.clearAllMocks(); }); const createToolCallRequestEvent = ( @@ -48,6 +59,7 @@ describe('LoopDetectionService', () => { for (let i = 0; i < TOOL_CALL_LOOP_THRESHOLD - 1; i++) { expect(service.addAndCheck(event)).toBe(false); } + expect(loggers.logLoopDetected).not.toHaveBeenCalled(); }); it(`should detect a loop on the TOOL_CALL_LOOP_THRESHOLD-th identical call`, () => { @@ -56,6 +68,7 @@ describe('LoopDetectionService', () => { service.addAndCheck(event); } expect(service.addAndCheck(event)).toBe(true); + expect(loggers.logLoopDetected).toHaveBeenCalledTimes(1); }); it('should detect a loop on subsequent identical calls', () => { @@ -64,6 +77,7 @@ describe('LoopDetectionService', () => { service.addAndCheck(event); } expect(service.addAndCheck(event)).toBe(true); + expect(loggers.logLoopDetected).toHaveBeenCalledTimes(2); }); it('should not detect a loop for different tool calls', () => { @@ -91,6 +105,7 @@ describe('LoopDetectionService', () => { for (let i = 0; i < CONTENT_LOOP_THRESHOLD - 1; i++) { expect(service.addAndCheck(event)).toBe(false); } + expect(loggers.logLoopDetected).not.toHaveBeenCalled(); }); it(`should detect a loop on the CONTENT_LOOP_THRESHOLD-th identical content string`, () => { @@ -99,6 +114,7 @@ describe('LoopDetectionService', () => { service.addAndCheck(event); } expect(service.addAndCheck(event)).toBe(true); + expect(loggers.logLoopDetected).toHaveBeenCalledTimes(1); }); it('should not detect a loop for different content strings', () => { @@ -108,6 +124,7 @@ describe('LoopDetectionService', () => { expect(service.addAndCheck(event1)).toBe(false); expect(service.addAndCheck(event2)).toBe(false); } + expect(loggers.logLoopDetected).not.toHaveBeenCalled(); }); }); |
