Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; |
| 2 | import { mkdtempSync, readFileSync, writeFileSync, chmodSync, rmSync } from 'node:fs'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import { join } from 'node:path'; |
| 5 | import { ensureCliImage } from '../src/image-lifecycle'; |
| 6 | import { getRepoRoot } from '../src/config'; |
| 7 | |
| 8 | let directory: string; |
| 9 | let previousPath: string | undefined; |
| 10 | let previousActions: string | undefined; |
| 11 | |
| 12 | beforeEach(() => { |
| 13 | directory = mkdtempSync(join(tmpdir(), 'cli-image-reuse-')); |
| 14 | previousPath = process.env.PATH; |
| 15 | previousActions = process.env.GITHUB_ACTIONS; |
| 16 | process.env.PATH = `${directory}:${previousPath}`; |
| 17 | delete process.env.GITHUB_ACTIONS; |
| 18 | process.env.CLI_IMAGE_TEST_DIR = directory; |
| 19 | const docker = join(directory, 'docker'); |
| 20 | writeFileSync( |
| 21 | docker, |
| 22 | `#!/bin/sh |
| 23 | if [ "$1" = image ]; then |
| 24 | cat "$CLI_IMAGE_TEST_DIR/revision" |
| 25 | exit 0 |
| 26 | fi |
| 27 | printf '%s\\n' "$*" >> "$CLI_IMAGE_TEST_DIR/builds" |
| 28 | if [ -f "$CLI_IMAGE_TEST_DIR/fail" ]; then |
| 29 | echo 'already exists' >&2 |
| 30 | exit 1 |
| 31 | fi |
| 32 | cat "$CLI_IMAGE_TEST_DIR/source" > "$CLI_IMAGE_TEST_DIR/built-source" |
| 33 | ` |
| 34 | ); |
| 35 | chmodSync(docker, 0o755); |
| 36 | writeFileSync(join(directory, 'revision'), 'old-revision'); |
| 37 | writeFileSync(join(directory, 'source'), 'first-source'); |
| 38 | }); |
| 39 | |
| 40 | afterEach(() => { |
| 41 | if (previousPath === undefined) delete process.env.PATH; |
| 42 | else process.env.PATH = previousPath; |
| 43 | if (previousActions === undefined) delete process.env.GITHUB_ACTIONS; |
| 44 | else process.env.GITHUB_ACTIONS = previousActions; |
| 45 | delete process.env.CLI_IMAGE_TEST_DIR; |
| 46 | rmSync(directory, { recursive: true, force: true }); |
| 47 | }); |
| 48 | |
| 49 | const currentRevision = () => |
| 50 | Bun.spawnSync(['git', 'rev-parse', 'HEAD'], { |
| 51 | cwd: getRepoRoot(), |
| 52 | stdout: 'pipe', |
| 53 | stderr: 'pipe', |
| 54 | }) |
| 55 | .stdout.toString() |
| 56 | .trim(); |
| 57 | |
| 58 | const options = () => ({ repoRoot: getRepoRoot() }); |
| 59 | |
| 60 | describe('CLI image reuse', () => { |
| 61 | test('refreshes local source even when the version and revision are unchanged', async () => { |
| 62 | writeFileSync(join(directory, 'revision'), currentRevision()); |
| 63 | await ensureCliImage('0.0.0-development', options()); |
| 64 | writeFileSync(join(directory, 'source'), 'dirty-source'); |
| 65 | await ensureCliImage('0.0.0-development', options()); |
| 66 | expect(readFileSync(join(directory, 'built-source'), 'utf8')).toBe('dirty-source'); |
| 67 | expect(readFileSync(join(directory, 'builds'), 'utf8').trim().split('\n')).toHaveLength(2); |
| 68 | }); |
| 69 | |
| 70 | test('rebuilds a CI image carrying a different source revision', async () => { |
| 71 | process.env.GITHUB_ACTIONS = 'true'; |
| 72 | await ensureCliImage('0.0.0-development', options()); |
| 73 | expect(readFileSync(join(directory, 'builds'), 'utf8')).toContain( |
| 74 | `org.opencontainers.image.revision=${currentRevision()}` |
| 75 | ); |
| 76 | }); |
| 77 | |
| 78 | test('reuses the CI prebuild only for the checkout revision', async () => { |
| 79 | process.env.GITHUB_ACTIONS = 'true'; |
| 80 | writeFileSync(join(directory, 'revision'), currentRevision()); |
| 81 | await ensureCliImage('0.0.0-development', options()); |
| 82 | expect(Bun.file(join(directory, 'builds')).size).toBe(0); |
| 83 | }); |
| 84 | |
| 85 | test('does not accept a stale tag after a failed rebuild', async () => { |
| 86 | writeFileSync(join(directory, 'fail'), ''); |
| 87 | await expect(ensureCliImage('0.0.0-development', options())).rejects.toThrow( |
| 88 | 'Failed to build Docker image' |
| 89 | ); |
| 90 | }); |
| 91 | }); |