| |
| |
| |
| |
| |
|
|
| import { execSync, spawn } from 'node:child_process'; |
| import { parse } from 'shell-quote'; |
| import { mkdirSync, writeFileSync, readFileSync } from 'node:fs'; |
| import { join, dirname } from 'node:path'; |
| import { fileURLToPath } from 'node:url'; |
| import { env } from 'node:process'; |
| import fs from 'node:fs'; |
|
|
| const __dirname = dirname(fileURLToPath(import.meta.url)); |
|
|
| function sanitizeTestName(name: string) { |
| return name |
| .toLowerCase() |
| .replace(/[^a-z0-9]/g, '-') |
| .replace(/-+/g, '-'); |
| } |
|
|
| |
| export function createToolCallErrorMessage( |
| expectedTools: string | string[], |
| foundTools: string[], |
| result: string, |
| ) { |
| const expectedStr = Array.isArray(expectedTools) |
| ? expectedTools.join(' or ') |
| : expectedTools; |
| return ( |
| `Expected to find ${expectedStr} tool call(s). ` + |
| `Found: ${foundTools.length > 0 ? foundTools.join(', ') : 'none'}. ` + |
| `Output preview: ${result ? result.substring(0, 200) + '...' : 'no output'}` |
| ); |
| } |
|
|
| |
| export function printDebugInfo( |
| rig: TestRig, |
| result: string, |
| context: Record<string, unknown> = {}, |
| ) { |
| console.error('Test failed - Debug info:'); |
| console.error('Result length:', result.length); |
| console.error('Result (first 500 chars):', result.substring(0, 500)); |
| console.error( |
| 'Result (last 500 chars):', |
| result.substring(result.length - 500), |
| ); |
|
|
| |
| Object.entries(context).forEach(([key, value]) => { |
| console.error(`${key}:`, value); |
| }); |
|
|
| |
| const allTools = rig.readToolLogs(); |
| console.error( |
| 'All tool calls found:', |
| allTools.map((t) => t.toolRequest.name), |
| ); |
|
|
| return allTools; |
| } |
|
|
| |
| export function validateModelOutput( |
| result: string, |
| expectedContent: string | (string | RegExp)[] | null = null, |
| testName = '', |
| ) { |
| |
| if (!result || result.trim().length === 0) { |
| throw new Error('Expected LLM to return some output'); |
| } |
|
|
| |
| if (expectedContent) { |
| const contents = Array.isArray(expectedContent) |
| ? expectedContent |
| : [expectedContent]; |
| const missingContent = contents.filter((content) => { |
| if (typeof content === 'string') { |
| return !result.toLowerCase().includes(content.toLowerCase()); |
| } else if (content instanceof RegExp) { |
| return !content.test(result); |
| } |
| return false; |
| }); |
|
|
| if (missingContent.length > 0) { |
| console.warn( |
| `Warning: LLM did not include expected content in response: ${missingContent.join( |
| ', ', |
| )}.`, |
| 'This is not ideal but not a test failure.', |
| ); |
| console.warn( |
| 'The tool was called successfully, which is the main requirement.', |
| ); |
| return false; |
| } else if (process.env.VERBOSE === 'true') { |
| console.log(`${testName}: Model output validated successfully.`); |
| } |
| return true; |
| } |
|
|
| return true; |
| } |
|
|
| export class TestRig { |
| bundlePath: string; |
| testDir: string | null; |
| testName?: string; |
| _lastRunStdout?: string; |
|
|
| constructor() { |
| this.bundlePath = join(__dirname, '..', 'bundle/gemini.js'); |
| this.testDir = null; |
| } |
|
|
| |
| getDefaultTimeout() { |
| if (env.CI) return 60000; |
| if (env.GEMINI_SANDBOX) return 30000; |
| return 15000; |
| } |
|
|
| setup( |
| testName: string, |
| options: { settings?: Record<string, unknown> } = {}, |
| ) { |
| this.testName = testName; |
| const sanitizedName = sanitizeTestName(testName); |
| this.testDir = join(env.INTEGRATION_TEST_FILE_DIR!, sanitizedName); |
| mkdirSync(this.testDir, { recursive: true }); |
|
|
| |
| const geminiDir = join(this.testDir, '.gemini'); |
| mkdirSync(geminiDir, { recursive: true }); |
| |
| |
| const telemetryPath = join(this.testDir, 'telemetry.log'); |
|
|
| const settings = { |
| telemetry: { |
| enabled: true, |
| target: 'local', |
| otlpEndpoint: '', |
| outfile: telemetryPath, |
| }, |
| sandbox: env.GEMINI_SANDBOX !== 'false' ? env.GEMINI_SANDBOX : false, |
| ...options.settings, |
| }; |
| writeFileSync( |
| join(geminiDir, 'settings.json'), |
| JSON.stringify(settings, null, 2), |
| ); |
| } |
|
|
| createFile(fileName: string, content: string) { |
| const filePath = join(this.testDir!, fileName); |
| writeFileSync(filePath, content); |
| return filePath; |
| } |
|
|
| mkdir(dir: string) { |
| mkdirSync(join(this.testDir!, dir), { recursive: true }); |
| } |
|
|
| sync() { |
| |
| execSync('sync', { cwd: this.testDir! }); |
| } |
|
|
| run( |
| promptOrOptions: |
| | string |
| | { prompt?: string; stdin?: string; stdinDoesNotEnd?: boolean }, |
| ...args: string[] |
| ): Promise<string> { |
| let command = `node ${this.bundlePath} --yolo`; |
| const execOptions: { |
| cwd: string; |
| encoding: 'utf-8'; |
| input?: string; |
| } = { |
| cwd: this.testDir!, |
| encoding: 'utf-8', |
| }; |
|
|
| if (typeof promptOrOptions === 'string') { |
| command += ` --prompt ${JSON.stringify(promptOrOptions)}`; |
| } else if ( |
| typeof promptOrOptions === 'object' && |
| promptOrOptions !== null |
| ) { |
| if (promptOrOptions.prompt) { |
| command += ` --prompt ${JSON.stringify(promptOrOptions.prompt)}`; |
| } |
| if (promptOrOptions.stdin) { |
| execOptions.input = promptOrOptions.stdin; |
| } |
| } |
|
|
| command += ` ${args.join(' ')}`; |
|
|
| const commandArgs = parse(command); |
| const node = commandArgs.shift() as string; |
|
|
| const child = spawn(node, commandArgs as string[], { |
| cwd: this.testDir!, |
| stdio: 'pipe', |
| }); |
|
|
| let stdout = ''; |
| let stderr = ''; |
|
|
| |
| if (execOptions.input) { |
| child.stdin!.write(execOptions.input); |
| } |
|
|
| if ( |
| typeof promptOrOptions === 'object' && |
| !promptOrOptions.stdinDoesNotEnd |
| ) { |
| child.stdin!.end(); |
| } |
|
|
| child.stdout!.on('data', (data: Buffer) => { |
| stdout += data; |
| if (env.KEEP_OUTPUT === 'true' || env.VERBOSE === 'true') { |
| process.stdout.write(data); |
| } |
| }); |
|
|
| child.stderr!.on('data', (data: Buffer) => { |
| stderr += data; |
| if (env.KEEP_OUTPUT === 'true' || env.VERBOSE === 'true') { |
| process.stderr.write(data); |
| } |
| }); |
|
|
| const promise = new Promise<string>((resolve, reject) => { |
| child.on('close', (code: number) => { |
| if (code === 0) { |
| |
| this._lastRunStdout = stdout; |
|
|
| |
| |
| let result = stdout; |
| if (env.GEMINI_SANDBOX === 'podman') { |
| |
| |
| const lines = result.split(EOL); |
| const filteredLines = []; |
| let inTelemetryObject = false; |
| let braceDepth = 0; |
|
|
| for (const line of lines) { |
| if (!inTelemetryObject && line.trim() === '{') { |
| |
| inTelemetryObject = true; |
| braceDepth = 1; |
| } else if (inTelemetryObject) { |
| |
| for (const char of line) { |
| if (char === '{') braceDepth++; |
| else if (char === '}') braceDepth--; |
| } |
|
|
| |
| if (braceDepth === 0) { |
| inTelemetryObject = false; |
| |
| continue; |
| } |
| } else { |
| |
| filteredLines.push(line); |
| } |
| } |
|
|
| result = filteredLines.join('\n'); |
| } |
| |
| if (stderr) { |
| result += `\n\nStdErr:\n${stderr}`; |
| } |
|
|
| resolve(result); |
| } else { |
| reject(new Error(`Process exited with code ${code}:\n${stderr}`)); |
| } |
| }); |
| }); |
|
|
| return promise; |
| } |
|
|
| readFile(fileName: string) { |
| const filePath = join(this.testDir!, fileName); |
| const content = readFileSync(filePath, 'utf-8'); |
| if (env.KEEP_OUTPUT === 'true' || env.VERBOSE === 'true') { |
| console.log(`--- FILE: ${filePath} ---`); |
| console.log(content); |
| console.log(`--- END FILE: ${filePath} ---`); |
| } |
| return content; |
| } |
|
|
| async cleanup() { |
| |
| if (this.testDir && !env.KEEP_OUTPUT) { |
| try { |
| execSync(`rm -rf ${this.testDir}`); |
| } catch (error) { |
| |
| if (env.VERBOSE === 'true') { |
| console.warn('Cleanup warning:', (error as Error).message); |
| } |
| } |
| } |
| } |
|
|
| async waitForTelemetryReady() { |
| |
| const logFilePath = join(this.testDir!, 'telemetry.log'); |
|
|
| if (!logFilePath) return; |
|
|
| |
| await this.poll( |
| () => { |
| if (!fs.existsSync(logFilePath)) return false; |
| try { |
| const content = readFileSync(logFilePath, 'utf-8'); |
| |
| return content.includes('"event.name"'); |
| } catch { |
| return false; |
| } |
| }, |
| 2000, |
| 100, |
| ); |
| } |
|
|
| async waitForTelemetryEvent(eventName: string, timeout?: number) { |
| if (!timeout) { |
| timeout = this.getDefaultTimeout(); |
| } |
|
|
| await this.waitForTelemetryReady(); |
|
|
| return this.poll( |
| () => { |
| const logFilePath = join(this.testDir!, 'telemetry.log'); |
|
|
| if (!logFilePath || !fs.existsSync(logFilePath)) { |
| return false; |
| } |
|
|
| const content = readFileSync(logFilePath, 'utf-8'); |
| const jsonObjects = content |
| .split(/}\n{/) |
| .map((obj, index, array) => { |
| |
| if (index > 0) obj = '{' + obj; |
| if (index < array.length - 1) obj = obj + '}'; |
| return obj.trim(); |
| }) |
| .filter((obj) => obj); |
|
|
| for (const jsonStr of jsonObjects) { |
| try { |
| const logData = JSON.parse(jsonStr); |
| if ( |
| logData.attributes && |
| logData.attributes['event.name'] === `gemini_cli.${eventName}` |
| ) { |
| return true; |
| } |
| } catch { |
| |
| } |
| } |
| return false; |
| }, |
| timeout, |
| 100, |
| ); |
| } |
|
|
| async waitForToolCall(toolName: string, timeout?: number) { |
| |
| if (!timeout) { |
| timeout = this.getDefaultTimeout(); |
| } |
|
|
| |
| await this.waitForTelemetryReady(); |
|
|
| return this.poll( |
| () => { |
| const toolLogs = this.readToolLogs(); |
| return toolLogs.some((log) => log.toolRequest.name === toolName); |
| }, |
| timeout, |
| 100, |
| ); |
| } |
|
|
| async waitForAnyToolCall(toolNames: string[], timeout?: number) { |
| |
| if (!timeout) { |
| timeout = this.getDefaultTimeout(); |
| } |
|
|
| |
| await this.waitForTelemetryReady(); |
|
|
| return this.poll( |
| () => { |
| const toolLogs = this.readToolLogs(); |
| return toolNames.some((name) => |
| toolLogs.some((log) => log.toolRequest.name === name), |
| ); |
| }, |
| timeout, |
| 100, |
| ); |
| } |
|
|
| async poll( |
| predicate: () => boolean, |
| timeout: number, |
| interval: number, |
| ): Promise<boolean> { |
| const startTime = Date.now(); |
| let attempts = 0; |
| while (Date.now() - startTime < timeout) { |
| attempts++; |
| const result = predicate(); |
| if (env.VERBOSE === 'true' && attempts % 5 === 0) { |
| console.log( |
| `Poll attempt ${attempts}: ${result ? 'success' : 'waiting...'}`, |
| ); |
| } |
| if (result) { |
| return true; |
| } |
| await new Promise((resolve) => setTimeout(resolve, interval)); |
| } |
| if (env.VERBOSE === 'true') { |
| console.log(`Poll timed out after ${attempts} attempts`); |
| } |
| return false; |
| } |
|
|
| _parseToolLogsFromStdout(stdout: string) { |
| const logs: { |
| timestamp: number; |
| toolRequest: { |
| name: string; |
| args: string; |
| success: boolean; |
| duration_ms: number; |
| }; |
| }[] = []; |
|
|
| |
| |
| |
| const toolCallPattern = |
| /body:\s*'Tool call:\s*([\w-]+)\..*?Success:\s*(\w+)\..*?Duration:\s*(\d+)ms\.'/g; |
| const matches = [...stdout.matchAll(toolCallPattern)]; |
|
|
| for (const match of matches) { |
| const toolName = match[1]; |
| const success = match[2] === 'true'; |
| const duration = parseInt(match[3], 10); |
|
|
| |
| const matchIndex = match.index || 0; |
| const contextStart = Math.max(0, matchIndex - 500); |
| const contextEnd = Math.min(stdout.length, matchIndex + 500); |
| const context = stdout.substring(contextStart, contextEnd); |
|
|
| |
| let args = '{}'; |
| const argsMatch = context.match(/function_args:\s*'([^']+)'/); |
| if (argsMatch) { |
| args = argsMatch[1]; |
| } |
|
|
| |
| |
| const nameMatch = context.match(/function_name:\s*'([\w-]+)'/); |
| const actualToolName = nameMatch ? nameMatch[1] : toolName; |
|
|
| logs.push({ |
| timestamp: Date.now(), |
| toolRequest: { |
| name: actualToolName, |
| args: args, |
| success: success, |
| duration_ms: duration, |
| }, |
| }); |
| } |
|
|
| |
| |
| if (logs.length === 0) { |
| const lines = stdout.split(EOL); |
| let currentObject = ''; |
| let inObject = false; |
| let braceDepth = 0; |
|
|
| for (const line of lines) { |
| if (!inObject && line.trim() === '{') { |
| inObject = true; |
| braceDepth = 1; |
| currentObject = line + '\n'; |
| } else if (inObject) { |
| currentObject += line + '\n'; |
|
|
| |
| for (const char of line) { |
| if (char === '{') braceDepth++; |
| else if (char === '}') braceDepth--; |
| } |
|
|
| |
| if (braceDepth === 0) { |
| inObject = false; |
| try { |
| const obj = JSON.parse(currentObject); |
|
|
| |
| if ( |
| obj.body && |
| obj.body.includes('Tool call:') && |
| obj.attributes |
| ) { |
| const bodyMatch = obj.body.match(/Tool call: (\w+)\./); |
| if (bodyMatch) { |
| logs.push({ |
| timestamp: obj.timestamp || Date.now(), |
| toolRequest: { |
| name: bodyMatch[1], |
| args: obj.attributes.function_args || '{}', |
| success: obj.attributes.success !== false, |
| duration_ms: obj.attributes.duration_ms || 0, |
| }, |
| }); |
| } |
| } else if ( |
| obj.attributes && |
| obj.attributes['event.name'] === 'gemini_cli.tool_call' |
| ) { |
| logs.push({ |
| timestamp: obj.attributes['event.timestamp'], |
| toolRequest: { |
| name: obj.attributes.function_name, |
| args: obj.attributes.function_args, |
| success: obj.attributes.success, |
| duration_ms: obj.attributes.duration_ms, |
| }, |
| }); |
| } |
| } catch { |
| |
| } |
| currentObject = ''; |
| } |
| } |
| } |
| } |
|
|
| return logs; |
| } |
|
|
| readToolLogs() { |
| |
| |
| if (env.GEMINI_SANDBOX === 'podman') { |
| |
| const logFilePath = join(this.testDir!, 'telemetry.log'); |
|
|
| if (fs.existsSync(logFilePath)) { |
| try { |
| const content = readFileSync(logFilePath, 'utf-8'); |
| if (content && content.includes('"event.name"')) { |
| |
| |
| } else if (this._lastRunStdout) { |
| |
| return this._parseToolLogsFromStdout(this._lastRunStdout); |
| } |
| } catch { |
| |
| if (this._lastRunStdout) { |
| return this._parseToolLogsFromStdout(this._lastRunStdout); |
| } |
| } |
| } else if (this._lastRunStdout) { |
| |
| return this._parseToolLogsFromStdout(this._lastRunStdout); |
| } |
| } |
|
|
| |
| const logFilePath = join(this.testDir!, 'telemetry.log'); |
|
|
| if (!logFilePath) { |
| console.warn(`TELEMETRY_LOG_FILE environment variable not set`); |
| return []; |
| } |
|
|
| |
| if (!fs.existsSync(logFilePath)) { |
| return []; |
| } |
|
|
| const content = readFileSync(logFilePath, 'utf-8'); |
|
|
| |
| |
| const jsonObjects = content |
| .split(/}\n{/) |
| .map((obj, index, array) => { |
| |
| if (index > 0) obj = '{' + obj; |
| if (index < array.length - 1) obj = obj + '}'; |
| return obj.trim(); |
| }) |
| .filter((obj) => obj); |
|
|
| const logs: { |
| toolRequest: { |
| name: string; |
| args: string; |
| success: boolean; |
| duration_ms: number; |
| }; |
| }[] = []; |
|
|
| for (const jsonStr of jsonObjects) { |
| try { |
| const logData = JSON.parse(jsonStr); |
| |
| if ( |
| logData.attributes && |
| logData.attributes['event.name'] === 'gemini_cli.tool_call' |
| ) { |
| const toolName = logData.attributes.function_name; |
| logs.push({ |
| toolRequest: { |
| name: toolName, |
| args: logData.attributes.function_args, |
| success: logData.attributes.success, |
| duration_ms: logData.attributes.duration_ms, |
| }, |
| }); |
| } |
| } catch (e) { |
| |
| if (env.VERBOSE === 'true') { |
| console.error('Failed to parse telemetry object:', e); |
| } |
| } |
| } |
|
|
| return logs; |
| } |
|
|
| readLastApiRequest(): Record<string, unknown> | null { |
| |
| const logFilePath = join(this.testDir!, 'telemetry.log'); |
|
|
| if (!logFilePath || !fs.existsSync(logFilePath)) { |
| return null; |
| } |
|
|
| const content = readFileSync(logFilePath, 'utf-8'); |
| const jsonObjects = content |
| .split(/}\n{/) |
| .map((obj, index, array) => { |
| if (index > 0) obj = '{' + obj; |
| if (index < array.length - 1) obj = obj + '}'; |
| return obj.trim(); |
| }) |
| .filter((obj) => obj); |
|
|
| let lastApiRequest = null; |
|
|
| for (const jsonStr of jsonObjects) { |
| try { |
| const logData = JSON.parse(jsonStr); |
| if ( |
| logData.attributes && |
| logData.attributes['event.name'] === 'gemini_cli.api_request' |
| ) { |
| lastApiRequest = logData; |
| } |
| } catch { |
| |
| } |
| } |
| return lastApiRequest; |
| } |
| } |
|
|