|
1 | | -import * as envModule from '@codebuff/internal/env' |
2 | | -import { describe, expect, it, spyOn } from 'bun:test' |
| 1 | +import { describe, expect, it, spyOn, beforeEach, afterEach, mock } from 'bun:test' |
3 | 2 | import { applyPatch } from 'diff' |
4 | 3 |
|
| 4 | +// Mock the benchify module to simulate missing API key |
| 5 | +mock.module('benchify', () => ({ |
| 6 | + Benchify: class MockBenchify { |
| 7 | + constructor() {} |
| 8 | + runFixer() { |
| 9 | + return Promise.resolve([]) |
| 10 | + } |
| 11 | + } |
| 12 | +})) |
| 13 | + |
5 | 14 | import { processStrReplace } from '../process-str-replace' |
6 | 15 | import { mockFileContext } from './test-utils' |
7 | 16 | import { |
@@ -487,47 +496,46 @@ describe('Benchify resilience', () => { |
487 | 496 | }) |
488 | 497 |
|
489 | 498 | it('should fall back gracefully when Benchify is disabled', async () => { |
490 | | - // Test with no API key - mock the entire env module |
491 | | - const mockEnv = { |
492 | | - BENCHIFY_API_KEY: undefined, |
493 | | - // Add other required env properties that might be accessed |
494 | | - PORT: 3001, |
495 | | - OPEN_ROUTER_API_KEY: 'mock-key', |
496 | | - RELACE_API_KEY: 'mock-key', |
497 | | - LINKUP_API_KEY: 'mock-key', |
498 | | - } as any |
499 | | - Object.defineProperty(envModule, 'env', { |
500 | | - get: () => mockEnv, |
501 | | - configurable: true, |
502 | | - }) |
503 | | - |
504 | | - const result = await executeBatchStrReplaces({ |
505 | | - deferredStrReplaces: [ |
506 | | - { |
507 | | - toolCall: { |
508 | | - toolName: 'str_replace' as const, |
509 | | - toolCallId: 'test-call', |
510 | | - input: { |
511 | | - path: 'test.ts', |
512 | | - replacements: [{ old: 'old', new: 'new', allowMultiple: false }], |
| 499 | + // Mock the process.env to simulate missing BENCHIFY_API_KEY |
| 500 | + const originalEnv = process.env.BENCHIFY_API_KEY |
| 501 | + delete process.env.BENCHIFY_API_KEY |
| 502 | + |
| 503 | + try { |
| 504 | + const result = await executeBatchStrReplaces({ |
| 505 | + deferredStrReplaces: [ |
| 506 | + { |
| 507 | + toolCall: { |
| 508 | + toolName: 'str_replace' as const, |
| 509 | + toolCallId: 'test-call', |
| 510 | + input: { |
| 511 | + path: 'test.ts', |
| 512 | + replacements: [ |
| 513 | + { old: 'old', new: 'new', allowMultiple: false }, |
| 514 | + ], |
| 515 | + }, |
513 | 516 | }, |
514 | 517 | }, |
515 | | - }, |
516 | | - ], |
517 | | - toolCalls: [], |
518 | | - toolResults: [], |
519 | | - ws: {} as any, |
520 | | - fileContext: mockFileContext, |
521 | | - agentStepId: 'test-step', |
522 | | - clientSessionId: 'test-session', |
523 | | - userInputId: 'test-input', |
524 | | - onResponseChunk: () => {}, |
525 | | - state: { messages: [] }, |
526 | | - userId: 'test-user', |
527 | | - }) |
528 | | - |
529 | | - // Should complete without error even when Benchify is unavailable |
530 | | - expect(result).toBeUndefined() // Function returns void |
| 518 | + ], |
| 519 | + toolCalls: [], |
| 520 | + toolResults: [], |
| 521 | + ws: {} as any, |
| 522 | + fileContext: mockFileContext, |
| 523 | + agentStepId: 'test-step', |
| 524 | + clientSessionId: 'test-session', |
| 525 | + userInputId: 'test-input', |
| 526 | + onResponseChunk: () => {}, |
| 527 | + state: { messages: [] }, |
| 528 | + userId: 'test-user', |
| 529 | + }) |
| 530 | + |
| 531 | + // Should complete without error even when Benchify is unavailable |
| 532 | + expect(result).toBeUndefined() // Function returns void |
| 533 | + } finally { |
| 534 | + // Restore the original environment variable |
| 535 | + if (originalEnv !== undefined) { |
| 536 | + process.env.BENCHIFY_API_KEY = originalEnv |
| 537 | + } |
| 538 | + } |
531 | 539 | }) |
532 | 540 |
|
533 | 541 | describe('Batch str_replace integration tests', () => { |
|
0 commit comments