diff options
Diffstat (limited to 'integration-tests/test-helper.js')
| -rw-r--r-- | integration-tests/test-helper.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/integration-tests/test-helper.js b/integration-tests/test-helper.js new file mode 100644 index 00000000..acc0035a --- /dev/null +++ b/integration-tests/test-helper.js @@ -0,0 +1,80 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { execSync } from 'child_process'; +import { mkdirSync, writeFileSync, readFileSync } from 'fs'; +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { env } from 'process'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +function sanitizeTestName(name) { + return name + .toLowerCase() + .replace(/[^a-z0-9]/g, '-') + .replace(/-+/g, '-'); +} + +export class TestRig { + constructor() { + this.bundlePath = join(__dirname, '..', 'bundle/gemini.js'); + this.testDir = null; + } + + setup(testName) { + this.testName = testName; + const sanitizedName = sanitizeTestName(testName); + this.testDir = join(env.INTEGRATION_TEST_FILE_DIR, sanitizedName); + mkdirSync(this.testDir, { recursive: true }); + } + + createFile(fileName, content) { + const filePath = join(this.testDir, fileName); + writeFileSync(filePath, content); + return filePath; + } + + mkdir(dir) { + mkdirSync(join(this.testDir, dir)); + } + + run(prompt, ...args) { + const output = execSync( + `node ${this.bundlePath} --yolo --prompt "${prompt}" ${args.join(' ')}`, + { + cwd: this.testDir, + encoding: 'utf-8', + }, + ); + + if (env.KEEP_OUTPUT === 'true') { + const testId = `${env.TEST_FILE_NAME.replace( + '.test.js', + '', + )}:${this.testName.replace(/ /g, '-')}`; + console.log(`--- TEST: ${testId} ---`); + console.log(output); + console.log(`--- END TEST: ${testId} ---`); + } + + return output; + } + + readFile(fileName) { + const content = readFileSync(join(this.testDir, fileName), 'utf-8'); + if (env.KEEP_OUTPUT === 'true') { + const testId = `${env.TEST_FILE_NAME.replace( + '.test.js', + '', + )}:${this.testName.replace(/ /g, '-')}`; + console.log(`--- FILE: ${testId}/${fileName} ---`); + console.log(content); + console.log(`--- END FILE: ${testId}/${fileName} ---`); + } + return content; + } +} |
