|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import * as fs from 'node:fs/promises' |
| 3 | +import * as path from 'node:path' |
| 4 | +import { afterAll, beforeAll, describe, expect, test } from 'vitest' |
| 5 | + |
| 6 | +const apiKey = process.env.E2B_API_KEY |
| 7 | +const domain = process.env.E2B_DOMAIN || 'e2b.app' |
| 8 | + |
| 9 | +const cliPath = path.join(process.cwd(), 'dist', 'index.js') |
| 10 | +const templateName = `cli-create-api-key-test-${Date.now()}` |
| 11 | + |
| 12 | +describe('template create cli backend integration', () => { |
| 13 | + let testDir: string |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + if (!apiKey) { |
| 17 | + throw new Error( |
| 18 | + 'E2B_API_KEY must be set to run template create backend tests' |
| 19 | + ) |
| 20 | + } |
| 21 | + testDir = await fs.mkdtemp('e2b-create-test-') |
| 22 | + await fs.writeFile( |
| 23 | + path.join(testDir, 'e2b.Dockerfile'), |
| 24 | + 'FROM ubuntu:latest\n' |
| 25 | + ) |
| 26 | + }) |
| 27 | + |
| 28 | + afterAll(async () => { |
| 29 | + if (!testDir) return |
| 30 | + runCli(['template', 'delete', '--yes', templateName]) |
| 31 | + await fs.rm(testDir, { recursive: true, force: true }) |
| 32 | + }) |
| 33 | + |
| 34 | + test( |
| 35 | + 'template create succeeds with E2B_API_KEY alone (no E2B_ACCESS_TOKEN)', |
| 36 | + { timeout: 300_000 }, |
| 37 | + () => { |
| 38 | + const result = runCli([ |
| 39 | + 'template', |
| 40 | + 'create', |
| 41 | + templateName, |
| 42 | + '--path', |
| 43 | + testDir, |
| 44 | + ]) |
| 45 | + const output = String(result.stdout || '') + String(result.stderr || '') |
| 46 | + |
| 47 | + expect(result.status, output).toBe(0) |
| 48 | + // Success marker printed by create.ts on a finished build; the failure |
| 49 | + // path prints "❌ Template build failed." instead. |
| 50 | + expect(output).toContain('✅ Building sandbox template') |
| 51 | + expect(output).not.toContain('❌ Template build failed') |
| 52 | + // Auth never fell through to the access-token error box. |
| 53 | + expect(output).not.toMatch(/You must be logged in/) |
| 54 | + } |
| 55 | + ) |
| 56 | +}) |
| 57 | + |
| 58 | +function runCli(args: string[]): ReturnType<typeof spawnSync> { |
| 59 | + // Intentionally exclude E2B_ACCESS_TOKEN from the child env so this test |
| 60 | + // verifies the API-key-only auth path end-to-end. |
| 61 | + return spawnSync('node', [cliPath, ...args], { |
| 62 | + env: { |
| 63 | + PATH: process.env.PATH, |
| 64 | + HOME: process.env.HOME, |
| 65 | + E2B_DOMAIN: domain, |
| 66 | + E2B_API_KEY: apiKey, |
| 67 | + }, |
| 68 | + encoding: 'utf8', |
| 69 | + timeout: 300_000, |
| 70 | + }) |
| 71 | +} |
0 commit comments