Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

11.9 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import {
  existsSync,
  mkdirSync,
  mkdtempSync,
  readdirSync,
  readFileSync,
  rmSync,
  writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import {
  buildSandboxEnvironmentInitCommand,
  environmentInitRan,
  environmentInitReport,
  hasEnvironmentInitWork,
  type SandboxInitialization,
  toSandboxInitialization,
  withEnvironmentVariables,
} from '../environmentInit';

const sandbox = { home: '', tmp: '' };

const run = (command: string, cwd: string) => {
  const env = { ...process.env, HOME: sandbox.home, TMPDIR: sandbox.tmp };
  try {
    return {
      status: 0,
      stdout: execFileSync('bash', ['-c', command], { cwd, encoding: 'utf8', env }),
    };
  } catch (error) {
    const failure = error as { status: number; stdout: string };
    return { status: failure.status, stdout: failure.stdout };
  }
};

describe('toSandboxInitialization', () => {
  it('keeps git sources, setup and variables', () => {
    expect(
      toSandboxInitialization({
        configuration: {
          bootstrapCommand: '  pnpm install  ',
          env: { REGION: 'us' },
          sources: [
            { kind: 'git', ref: 'main', url: 'https://github.com/a/b.git' },
            { kind: 'files', uri: 's3://legacy' },
          ],
        },
        id: 'env_1',
        name: 'Tools',
      }),
    ).toEqual({
      bootstrapCommand: 'pnpm install',
      env: { REGION: 'us' },
      environmentId: 'env_1',
      name: 'Tools',
      sources: [{ ref: 'main', url: 'https://github.com/a/b.git' }],
    });
  });

  it('lays several repositories out side by side, never one inside another', () => {
    expect(
      toSandboxInitialization({
        configuration: {
          sources: [
            { kind: 'git', url: 'https://github.com/lobehub/lobehub' },
            { kind: 'git', path: 'lobe-ui', url: 'https://github.com/lobehub/lobe-ui' },
          ],
        },
        id: 'env-1',
        name: 'Two',
      })?.sources,
    ).toEqual([
      { path: 'lobehub', url: 'https://github.com/lobehub/lobehub' },
      { path: 'lobe-ui', url: 'https://github.com/lobehub/lobe-ui' },
    ]);
  });

  it('asks for nothing when the environment has neither a repository nor setup', () => {
    expect(
      toSandboxInitialization({
        configuration: { sources: [{ kind: 'files', uri: 's3://x' }] },
        id: 'env_1',
        name: 'Empty',
      }),
    ).toBeUndefined();
  });

  it('keeps an environment that only declares variables, with nothing to apply to the folder', () => {
    const init = toSandboxInitialization({
      configuration: { env: { LOBEHUB_ADMIN_URL: 'https://admin.example.com' } },
      id: 'env_1',
      name: 'lobe-admin',
    });

    expect(init).toEqual({
      env: { LOBEHUB_ADMIN_URL: 'https://admin.example.com' },
      environmentId: 'env_1',
      name: 'lobe-admin',
      sources: [],
    });
    expect(hasEnvironmentInitWork(init!)).toBe(false);
  });
});

describe('withEnvironmentVariables', () => {
  let work: string;

  beforeEach(() => {
    work = mkdtempSync(join(tmpdir(), 'lobe-env-vars-'));
    sandbox.home = work;
    sandbox.tmp = work;
  });

  afterEach(() => {
    rmSync(work, { force: true, recursive: true });
  });

  it('exports the variables into the command and whatever it starts', () => {
    const command = withEnvironmentVariables(
      'bash -c "printenv LOBEHUB_ADMIN_URL"; echo "$REGION"',
      {
        LOBEHUB_ADMIN_URL: 'https://admin.example.com',
        REGION: 'us',
      },
    );

    expect(run(command, work).stdout).toBe('https://admin.example.com\nus\n');
  });

  it('keeps values literal, quotes and substitutions included', () => {
    const value = `it's $(echo pwned) \`id\` $HOME`;

    expect(
      run(withEnvironmentVariables('printf %s "$TRICKY"', { TRICKY: value }), work).stdout,
    ).toBe(value);
  });

  it('lets the command override a variable for itself', () => {
    expect(
      run(withEnvironmentVariables('REGION=eu; echo "$REGION"', { REGION: 'us' }), work).stdout,
    ).toBe('eu\n');
  });

  it('skips names a shell cannot export and leaves the command alone without variables', () => {
    expect(withEnvironmentVariables('ls', { 'BAD-NAME': 'x' })).toBe('ls');
    expect(withEnvironmentVariables('ls', undefined)).toBe('ls');
  });
});

describe('buildSandboxEnvironmentInitCommand', () => {
  let root: string;
  let repo: string;
  let work: string;

  beforeEach(() => {
    root = mkdtempSync(join(tmpdir(), 'lobe-env-init-'));
    repo = join(root, 'repo');
    work = join(root, 'work');
    mkdirSync(repo);
    mkdirSync(work);
    sandbox.home = join(root, 'home');
    sandbox.tmp = join(root, 'tmp');
    mkdirSync(sandbox.home);
    mkdirSync(sandbox.tmp);
    const git = (...args: string[]) =>
      execFileSync('git', args, {
        cwd: repo,
        env: {
          ...process.env,
          GIT_AUTHOR_EMAIL: 't@t',
          GIT_AUTHOR_NAME: 't',
          GIT_COMMITTER_EMAIL: 't@t',
          GIT_COMMITTER_NAME: 't',
        },
      });
    git('init', '-q', '-b', 'main');
    writeFileSync(join(repo, 'README.md'), 'hello');
    git('add', '.');
    git('commit', '-qm', 'init');
  });

  afterEach(() => rmSync(root, { force: true, recursive: true }));

  const init = (overrides: Partial<SandboxInitialization> = {}): SandboxInitialization => ({
    bootstrapCommand: 'echo "$REGION" > setup.txt',
    env: { REGION: 'eu' },
    environmentId: 'env_1',
    name: "Bob's tools",
    sources: [{ url: `file://${repo}` }],
    ...overrides,
  });

  it('clones the repository with its history and runs setup with the variables, leaving nothing of its own', () => {
    const result = run(buildSandboxEnvironmentInitCommand(init()), work);

    expect(result.status).toBe(0);
    expect(environmentInitRan(result.stdout)).toBe(true);
    expect(environmentInitReport(result.stdout)).toBe(
      `Initialized from the environment Bob's tools.`,
    );
    expect(readFileSync(join(work, 'README.md'), 'utf8')).toBe('hello');
    expect(existsSync(join(work, '.git'))).toBe(true);
    expect(readFileSync(join(work, 'setup.txt'), 'utf8').trim()).toBe('eu');
    // Only what the repository and setup put there: no marker beside them.
    expect(readdirSync(work).sort()).toEqual(['.git', 'README.md', 'setup.txt']);
    expect(execFileSync('git', ['status', '--porcelain'], { cwd: work, encoding: 'utf8' })).toBe(
      '?? setup.txt\n',
    );
  });

  it('does nothing and prints nothing once the folder holds the checkout', () => {
    const command = buildSandboxEnvironmentInitCommand(init());
    run(command, work);
    rmSync(join(work, 'setup.txt'));

    const again = run(command, work);

    expect(again).toEqual({ status: 0, stdout: '' });
    expect(existsSync(join(work, 'setup.txt'))).toBe(false);
  });

  // A project checkout removed while its sandbox lived on is made again empty;
  // the record of the first clone used to keep it empty for good.
  it('clones again into a checkout folder that was emptied after it was initialized', () => {
    const command = buildSandboxEnvironmentInitCommand(init());
    run(command, work);
    rmSync(work, { force: true, recursive: true });
    mkdirSync(work);

    const again = run(command, work);

    expect(environmentInitRan(again.stdout)).toBe(true);
    expect(readFileSync(join(work, 'README.md'), 'utf8')).toBe('hello');
  });

  it('does not retry a failed clone just because its folder is still empty', () => {
    const command = buildSandboxEnvironmentInitCommand(
      init({ bootstrapCommand: undefined, sources: [{ url: `file://${root}/missing` }] }),
    );

    expect(environmentInitReport(run(command, work).stdout)).toContain('failed');
    expect(run(command, work).stdout).toBe('');
  });

  it('leaves a folder that already holds files alone, silently', () => {
    writeFileSync(join(work, 'mine.txt'), 'work');

    const result = run(buildSandboxEnvironmentInitCommand(init()), work);

    expect(result).toEqual({ status: 0, stdout: '' });
    expect(existsSync(join(work, 'README.md'))).toBe(false);
  });

  it('runs a setup that writes nothing into the folder once, not on every call', () => {
    // A global install leaves the folder empty, and each tool call starts a
    // fresh service: the record in the sandbox is what stops a rerun.
    const command = buildSandboxEnvironmentInitCommand(
      init({ bootstrapCommand: 'echo ran >> "$HOME/setup-runs"', sources: [] }),
    );

    expect(environmentInitRan(run(command, work).stdout)).toBe(true);
    expect(run(command, work).stdout).toBe('');
    expect(readFileSync(join(sandbox.home, 'setup-runs'), 'utf8')).toBe('ran\n');
    expect(readdirSync(work)).toEqual([]);
  });

  it('reports a failed setup once rather than retrying it on every call', () => {
    const command = buildSandboxEnvironmentInitCommand(
      init({ bootstrapCommand: 'exit 7', sources: [] }),
    );

    expect(environmentInitReport(run(command, work).stdout)).toContain('failed (exit 7)');
    expect(run(command, work).stdout).toBe('');
  });

  it('initializes another empty folder with the same environment on its own', () => {
    const command = buildSandboxEnvironmentInitCommand(init());
    const other = join(root, 'other');
    mkdirSync(other);

    run(command, work);

    expect(environmentInitRan(run(command, other).stdout)).toBe(true);
    expect(existsSync(join(other, 'README.md'))).toBe(true);
  });

  it('leaves no staging copy of the repository behind, whether setup succeeds or fails', () => {
    run(buildSandboxEnvironmentInitCommand(init()), work);
    const failing = join(root, 'failing');
    mkdirSync(failing);
    run(buildSandboxEnvironmentInitCommand(init({ bootstrapCommand: 'exit 3' })), failing);

    expect(readdirSync(sandbox.tmp)).toEqual([]);
  });

  it('gives git the clone credential for the clones alone, and keeps it out of the checkout', () => {
    const result = run(
      buildSandboxEnvironmentInitCommand(
        init({
          bootstrapCommand: 'echo "[$GIT_CONFIG_VALUE_0]" > seen.txt',
          cloneCredential: {
            header: 'Authorization: Basic c2VjcmV0',
            urlPrefix: 'https://github.com/',
          },
        }),
      ),
      work,
    );

    expect(environmentInitReport(result.stdout)).toContain('Initialized');
    // Setup, which runs after the clones, cannot read it back.
    expect(readFileSync(join(work, 'seen.txt'), 'utf8').trim()).toBe('[]');
    expect(readFileSync(join(work, '.git', 'config'), 'utf8')).not.toContain('c2VjcmV0');
    expect(result.stdout).not.toContain('c2VjcmV0');
  });

  it('gives git one credential per repository when there are several, and unsets them all', () => {
    const command = buildSandboxEnvironmentInitCommand(
      init({
        bootstrapCommand: 'echo "[$GIT_CONFIG_COUNT$GIT_CONFIG_VALUE_1]" > seen.txt',
        cloneCredentials: [
          { header: 'Authorization: Basic b25l', urlPrefix: 'https://github.com/acme/one' },
          { header: 'Authorization: Basic dHdv', urlPrefix: 'https://github.com/acme/two' },
        ],
      }),
    );

    expect(command).toContain('GIT_CONFIG_COUNT=2');
    expect(command).toContain('http.https://github.com/acme/two.extraHeader');
    const result = run(command, work);
    expect(environmentInitReport(result.stdout)).toContain('Initialized');
    expect(readFileSync(join(work, 'seen.txt'), 'utf8').trim()).toBe('[]');
  });

  it('keeps the variables out of the shell that runs afterwards', () => {
    run(buildSandboxEnvironmentInitCommand(init()), work);

    expect(
      run(`${buildSandboxEnvironmentInitCommand(init())}; echo "[$REGION]"`, work).stdout,
    ).toBe('[]\n');
  });

  it('refuses a source path that leaves the working directory', () => {
    const result = run(
      buildSandboxEnvironmentInitCommand(
        init({ sources: [{ path: '../out', url: `file://${repo}` }] }),
      ),
      work,
    );

    expect(result.stdout).toContain('unusable path');
    expect(existsSync(join(root, 'out'))).toBe(false);
  });
});