Skip to content

Commit 32b9c38

Browse files
committed
Enforce API key and backend check in SDK e2e tests
1 parent 4f29807 commit 32b9c38

17 files changed

+199
-96
lines changed

sdk/e2e/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,24 @@ bun run test:e2e && bun run test:integration && bun run test:unit:e2e
9696
## Prerequisites
9797

9898
- **API Key**: Set `CODEBUFF_API_KEY` environment variable for E2E and integration tests
99-
- Tests skip gracefully if API key is not set
99+
- Tests require the API key and will fail fast if it is not set.
100100

101101
## Writing Tests
102102

103103
### E2E Test Pattern
104104
```typescript
105105
import { describe, test, expect, beforeAll } from 'bun:test'
106106
import { CodebuffClient } from '../../src/client'
107-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, DEFAULT_AGENT, DEFAULT_TIMEOUT } from '../utils'
107+
import { EventCollector, getApiKey, isAuthError, DEFAULT_AGENT, DEFAULT_TIMEOUT } from '../utils'
108108

109109
describe('E2E: My Test', () => {
110110
let client: CodebuffClient
111111

112112
beforeAll(() => {
113-
if (skipIfNoApiKey()) return
114113
client = new CodebuffClient({ apiKey: getApiKey() })
115114
})
116115

117116
test('does something', async () => {
118-
if (skipIfNoApiKey()) return
119117
const collector = new EventCollector()
120118

121119
const result = await client.run({

sdk/e2e/custom-agents/api-integration-agent.e2e.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
* Agent that fetches from external APIs demonstrating API integration patterns.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88
import { z } from 'zod/v4'
99

1010
import { CodebuffClient, getCustomToolDefinition } from '../../src'
11-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, DEFAULT_TIMEOUT } from '../utils'
11+
import {
12+
EventCollector,
13+
getApiKey,
14+
isAuthError,
15+
ensureBackendConnection,
16+
DEFAULT_TIMEOUT,
17+
} from '../utils'
1218

1319
import type { AgentDefinition } from '../../src'
1420

@@ -87,14 +93,16 @@ Summarize the response data clearly.`,
8793
})
8894

8995
beforeAll(() => {
90-
if (skipIfNoApiKey()) return
9196
client = new CodebuffClient({ apiKey: getApiKey() })
9297
})
9398

99+
beforeEach(async () => {
100+
await ensureBackendConnection()
101+
})
102+
94103
test(
95104
'fetches mock API data and summarizes response',
96105
async () => {
97-
if (skipIfNoApiKey()) return
98106

99107
const collector = new EventCollector()
100108

@@ -121,7 +129,6 @@ Summarize the response data clearly.`,
121129
test(
122130
'handles API errors gracefully',
123131
async () => {
124-
if (skipIfNoApiKey()) return
125132

126133
const collector = new EventCollector()
127134

sdk/e2e/custom-agents/database-query-agent.e2e.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
* Agent with mock SQL execution tool demonstrating database integration patterns.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88
import { z } from 'zod/v4'
99

1010
import { CodebuffClient, getCustomToolDefinition } from '../../src'
11-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, MOCK_DATABASE, DEFAULT_TIMEOUT } from '../utils'
11+
import {
12+
EventCollector,
13+
getApiKey,
14+
isAuthError,
15+
ensureBackendConnection,
16+
MOCK_DATABASE,
17+
DEFAULT_TIMEOUT,
18+
} from '../utils'
1219

1320
import type { AgentDefinition } from '../../src'
1421

@@ -57,14 +64,16 @@ Always format query results in a readable way.`,
5764
})
5865

5966
beforeAll(() => {
60-
if (skipIfNoApiKey()) return
6167
client = new CodebuffClient({ apiKey: getApiKey() })
6268
})
6369

70+
beforeEach(async () => {
71+
await ensureBackendConnection()
72+
})
73+
6474
test(
6575
'executes SELECT query and returns results',
6676
async () => {
67-
if (skipIfNoApiKey()) return
6877

6978
const collector = new EventCollector()
7079

@@ -96,7 +105,6 @@ Always format query results in a readable way.`,
96105
test(
97106
'handles query with WHERE clause',
98107
async () => {
99-
if (skipIfNoApiKey()) return
100108

101109
const collector = new EventCollector()
102110

sdk/e2e/custom-agents/weather-agent.e2e.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
* Custom agent with a get_weather custom tool demonstrating custom tool integration.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88
import { z } from 'zod/v4'
99

1010
import { CodebuffClient, getCustomToolDefinition } from '../../src'
11-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, MOCK_WEATHER_DATA, DEFAULT_TIMEOUT } from '../utils'
11+
import {
12+
EventCollector,
13+
getApiKey,
14+
isAuthError,
15+
ensureBackendConnection,
16+
MOCK_WEATHER_DATA,
17+
DEFAULT_TIMEOUT,
18+
} from '../utils'
1219

1320
import type { AgentDefinition } from '../../src'
1421

@@ -49,14 +56,16 @@ Always report the temperature and conditions clearly.`,
4956
})
5057

5158
beforeAll(() => {
52-
if (skipIfNoApiKey()) return
5359
client = new CodebuffClient({ apiKey: getApiKey() })
5460
})
5561

62+
beforeEach(async () => {
63+
await ensureBackendConnection()
64+
})
65+
5666
test(
5767
'custom weather tool is called and returns data',
5868
async () => {
59-
if (skipIfNoApiKey()) return
6069

6170
const collector = new EventCollector()
6271

@@ -93,7 +102,6 @@ Always report the temperature and conditions clearly.`,
93102
test(
94103
'custom tool handles unknown city gracefully',
95104
async () => {
96-
if (skipIfNoApiKey()) return
97105

98106
const collector = new EventCollector()
99107

sdk/e2e/features/knowledge-files.e2e.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@
44
* Tests knowledgeFiles injection for providing context to the agent.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88

99
import { CodebuffClient } from '../../src/client'
10-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, DEFAULT_AGENT, DEFAULT_TIMEOUT } from '../utils'
10+
import {
11+
EventCollector,
12+
getApiKey,
13+
isAuthError,
14+
ensureBackendConnection,
15+
DEFAULT_AGENT,
16+
DEFAULT_TIMEOUT,
17+
} from '../utils'
1118

1219
describe('Features: Knowledge Files', () => {
1320
let client: CodebuffClient
1421

1522
beforeAll(() => {
16-
if (skipIfNoApiKey()) return
1723
client = new CodebuffClient({ apiKey: getApiKey() })
1824
})
1925

26+
beforeEach(async () => {
27+
await ensureBackendConnection()
28+
})
29+
2030
test(
2131
'agent uses injected knowledge files',
2232
async () => {
23-
if (skipIfNoApiKey()) return
2433

2534
const collector = new EventCollector()
2635

@@ -46,7 +55,6 @@ describe('Features: Knowledge Files', () => {
4655
test(
4756
'multiple knowledge files are accessible',
4857
async () => {
49-
if (skipIfNoApiKey()) return
5058

5159
const collector = new EventCollector()
5260

sdk/e2e/features/max-agent-steps.e2e.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@
44
* Tests the maxAgentSteps option for limiting agent execution.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88

99
import { CodebuffClient } from '../../src/client'
10-
import { EventCollector, getApiKey, skipIfNoApiKey, isAuthError, DEFAULT_AGENT, DEFAULT_TIMEOUT } from '../utils'
10+
import {
11+
EventCollector,
12+
getApiKey,
13+
isAuthError,
14+
ensureBackendConnection,
15+
DEFAULT_AGENT,
16+
DEFAULT_TIMEOUT,
17+
} from '../utils'
1118

1219
describe('Features: Max Agent Steps', () => {
1320
let client: CodebuffClient
1421

1522
beforeAll(() => {
16-
if (skipIfNoApiKey()) return
1723
client = new CodebuffClient({ apiKey: getApiKey() })
1824
})
1925

26+
beforeEach(async () => {
27+
await ensureBackendConnection()
28+
})
29+
2030
test(
2131
'run completes with maxAgentSteps set',
2232
async () => {
23-
if (skipIfNoApiKey()) return
2433

2534
const collector = new EventCollector()
2635

@@ -42,7 +51,6 @@ describe('Features: Max Agent Steps', () => {
4251
test(
4352
'low maxAgentSteps still allows simple responses',
4453
async () => {
45-
if (skipIfNoApiKey()) return
4654

4755
const collector = new EventCollector()
4856

sdk/e2e/features/project-files.e2e.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
* Tests projectFiles injection for providing file context to the agent.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88

99
import { CodebuffClient } from '../../src/client'
1010
import {
1111
EventCollector,
1212
getApiKey,
13-
skipIfNoApiKey,
1413
isAuthError,
14+
ensureBackendConnection,
1515
SAMPLE_PROJECT_FILES,
1616
DEFAULT_AGENT,
1717
DEFAULT_TIMEOUT,
@@ -21,14 +21,16 @@ describe('Features: Project Files', () => {
2121
let client: CodebuffClient
2222

2323
beforeAll(() => {
24-
if (skipIfNoApiKey()) return
2524
client = new CodebuffClient({ apiKey: getApiKey() })
2625
})
2726

27+
beforeEach(async () => {
28+
await ensureBackendConnection()
29+
})
30+
2831
test(
2932
'agent can reference injected project files',
3033
async () => {
31-
if (skipIfNoApiKey()) return
3234

3335
const collector = new EventCollector()
3436

@@ -58,7 +60,6 @@ describe('Features: Project Files', () => {
5860
test(
5961
'agent can analyze content of project files',
6062
async () => {
61-
if (skipIfNoApiKey()) return
6263

6364
const collector = new EventCollector()
6465

sdk/e2e/integration/connection-check.integration.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44
* Tests the checkConnection() method of CodebuffClient.
55
*/
66

7-
import { describe, test, expect, beforeAll } from 'bun:test'
7+
import { describe, test, expect, beforeAll, beforeEach } from 'bun:test'
88

99
import { CodebuffClient } from '../../src/client'
10-
import { getApiKey, skipIfNoApiKey } from '../utils'
10+
import { getApiKey, ensureBackendConnection } from '../utils'
1111

1212
describe('Integration: Connection Check', () => {
1313
let client: CodebuffClient
1414

1515
beforeAll(() => {
16-
if (skipIfNoApiKey()) return
1716
client = new CodebuffClient({ apiKey: getApiKey() })
1817
})
1918

19+
beforeEach(async () => {
20+
await ensureBackendConnection()
21+
})
22+
2023
test('checkConnection returns true when backend is reachable', async () => {
21-
if (skipIfNoApiKey()) return
2224

2325
const isConnected = await client.checkConnection()
2426
expect(isConnected).toBe(true)
2527
})
2628

2729
test('checkConnection returns boolean', async () => {
28-
if (skipIfNoApiKey()) return
2930

3031
const result = await client.checkConnection()
3132
expect(typeof result).toBe('boolean')

0 commit comments

Comments
 (0)