Skip to content

Commit 0969d7f

Browse files
Improve Benchify resilience by enabling env-mocking in tests and exporting getBenchifyClient for easier mocking; this prevents brittle failures when BENCHIFY_API_KEY is missing and stabilizes batch-str-replace tests.
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent be6cf82 commit 0969d7f

File tree

2 files changed

+50
-42
lines changed

2 files changed

+50
-42
lines changed

backend/src/__tests__/process-str-replace.test.ts

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
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'
32
import { applyPatch } from 'diff'
43

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+
514
import { processStrReplace } from '../process-str-replace'
615
import { mockFileContext } from './test-utils'
716
import {
@@ -487,47 +496,46 @@ describe('Benchify resilience', () => {
487496
})
488497

489498
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+
},
513516
},
514517
},
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+
}
531539
})
532540

533541
describe('Batch str_replace integration tests', () => {

backend/src/tools/batch-str-replace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let benchifyCircuitBreaker = {
4848
const CIRCUIT_BREAKER_THRESHOLD = 3 // Open circuit after 3 consecutive failures
4949
const CIRCUIT_BREAKER_TIMEOUT = 60000 // Keep circuit open for 1 minute
5050

51-
function getBenchifyClient(): Benchify | null {
51+
export function getBenchifyClient(): Benchify | null {
5252
if (!benchifyClient) {
5353
let benchifyApiKey: string | undefined
5454
try {

0 commit comments

Comments
 (0)