|
| 1 | +import fs from 'fs' |
| 2 | +import os from 'os' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +import { API_KEY_ENV_VAR } from '@codebuff/common/constants/paths' |
| 6 | +import { CodebuffClient, type AgentDefinition } from '@codebuff/sdk' |
| 7 | +import { describe, expect, it } from 'bun:test' |
| 8 | + |
| 9 | +import base2Free from '../base2/base2-free' |
| 10 | + |
| 11 | +import type { PrintModeEvent } from '@codebuff/common/types/print-mode' |
| 12 | + |
| 13 | +describe('Gravity Index SDK E2E', () => { |
| 14 | + it( |
| 15 | + 'base2-free uses gravity_index_search for third-party service selection', |
| 16 | + async () => { |
| 17 | + const apiKey = process.env[API_KEY_ENV_VAR] |
| 18 | + if (!apiKey) { |
| 19 | + console.warn( |
| 20 | + `Skipping Gravity Index E2E: set ${API_KEY_ENV_VAR} to run.`, |
| 21 | + ) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + const tmpDir = await fs.promises.mkdtemp( |
| 26 | + path.join(os.tmpdir(), 'gravity-index-e2e-'), |
| 27 | + ) |
| 28 | + const events: PrintModeEvent[] = [] |
| 29 | + |
| 30 | + try { |
| 31 | + const client = new CodebuffClient({ |
| 32 | + apiKey, |
| 33 | + cwd: tmpDir, |
| 34 | + projectFiles: { |
| 35 | + 'package.json': JSON.stringify({ |
| 36 | + scripts: {}, |
| 37 | + dependencies: { next: '^15.0.0' }, |
| 38 | + }), |
| 39 | + }, |
| 40 | + agentDefinitions: [base2Free as AgentDefinition], |
| 41 | + handleEvent: (event) => { |
| 42 | + events.push(event) |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + const run = await client.run({ |
| 47 | + agent: base2Free.id, |
| 48 | + prompt: |
| 49 | + 'Use the Gravity Index to recommend a transactional email API for a Next.js app. Include the tracked API-key signup URL from the tool result.', |
| 50 | + maxAgentSteps: 4, |
| 51 | + }) |
| 52 | + |
| 53 | + if (run.output.type === 'error') { |
| 54 | + throw new Error(run.output.message) |
| 55 | + } |
| 56 | + |
| 57 | + const toolCalls = events.filter((event) => event.type === 'tool_call') |
| 58 | + expect( |
| 59 | + toolCalls.some( |
| 60 | + (event) => |
| 61 | + 'toolName' in event && event.toolName === 'gravity_index_search', |
| 62 | + ), |
| 63 | + ).toBe(true) |
| 64 | + |
| 65 | + const outputText = events |
| 66 | + .filter((event) => event.type === 'text') |
| 67 | + .map((event) => ('text' in event ? event.text : '')) |
| 68 | + .join('') |
| 69 | + expect(outputText).toMatch(/https:\/\/index\.trygravity\.ai\/go\//) |
| 70 | + } finally { |
| 71 | + await fs.promises.rm(tmpDir, { recursive: true, force: true }) |
| 72 | + } |
| 73 | + }, |
| 74 | + { timeout: 300_000 }, |
| 75 | + ) |
| 76 | +}) |
0 commit comments