Files
colbymchenry__codegraph/__tests__/cli-install-init.test.ts
Colby McHenry 7672b92a95 feat(cli): install --init and init --yes for a one-shot, non-interactive bootstrap (#1578)
A fresh container or CI job had to run two commands — `codegraph install
--yes` and then `codegraph init` — and the second could still stop on a
prompt (the gitignored-child-repos offer, the watch-fallback offer). The
installer deliberately never indexes on its own (a surprise index of
$HOME is exactly what `init` refuses), so the missing piece was an
explicit opt-in, not a behavior change.

- `codegraph install -i/--init` runs the `init` flow in the current
  directory after the agents are wired — also when nothing was wired
  (`--target none`, no agents detected), since the installer returns
  normally there. It shares every guard with `codegraph init`: an
  unsafe root (home directory, filesystem root, a parent of home) is
  refused with exit code 1 and no implied `--force`; an
  already-initialized project just says so and exits 0. Ignored by
  `--print-config` / `--refresh`, which return before the install.
- `codegraph init -y/--yes` makes every post-index offer
  non-interactive: the ignored-repos offer prints its one-line opt-in
  snippet instead of prompting (the non-TTY behavior), and the
  watch-fallback offer takes its `yes` default.
- The `init` action body is now `runInit()`, shared by both commands;
  the plain `init` path is behavior-identical.
- The post-install "Next: index a project" note mentions the flag;
  README gets the flag row + a `--yes --init` example.

Tests (`__tests__/cli-install-init.test.ts`, against the built binary,
stdin closed, `--target none` so no agent config is touched):
`install --yes --target none --init` builds `.codegraph/codegraph.db`;
the same on an already-initialized project reports it and exits 0; at
the filesystem root it refuses with exit 1 and writes nothing; `init
--yes` completes with stdin closed; both flags appear in `--help`.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LxZj6W6Y1SHXwvpT3uwJpK
2026-08-22 10:52:12 -07:00

109 lines
4.1 KiB
TypeScript

/**
* `codegraph install --init` and `codegraph init --yes` (#1578): the one-shot,
* non-interactive "wire agents + build this project's index" bootstrap a fresh
* container / CI job needs.
*
* Exercised end-to-end against the built binary so the CLI wiring (the shared
* `runInit` flow, the flag plumbing, exit codes) is what's covered. Every run
* uses `--target none`, so the installer touches no agent config on the
* machine running the suite; the only side effect is the temp project's
* `.codegraph/`.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { execFileSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
const BIN = path.resolve(__dirname, '../dist/bin/codegraph.js');
interface RunResult {
status: number;
stdout: string;
stderr: string;
}
/** Run the CLI with stdin closed — a prompt that blocks would hang / fail here. */
function runCodegraph(args: string[], cwd: string): RunResult {
try {
const stdout = execFileSync(process.execPath, [BIN, ...args], {
cwd,
encoding: 'utf-8',
env: {
...process.env,
CODEGRAPH_NO_DAEMON: '1',
CODEGRAPH_TELEMETRY: '0',
DO_NOT_TRACK: '1',
NO_COLOR: '1',
},
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 120_000,
});
return { status: 0, stdout, stderr: '' };
} catch (err) {
const e = err as { status?: number | null; stdout?: string | Buffer; stderr?: string | Buffer };
return {
status: e.status ?? -1,
stdout: String(e.stdout ?? ''),
stderr: String(e.stderr ?? ''),
};
}
}
describe('codegraph install --init / init --yes (#1578)', () => {
let tempDir: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-install-init-'));
fs.writeFileSync(
path.join(tempDir, 'a.ts'),
`export function greet(name: string) { return hello(name); }\n` +
`export function hello(n: string) { return 'hi ' + n; }\n`,
);
});
afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('install --yes --target none --init builds the current project\'s index in one command', () => {
const r = runCodegraph(['install', '--yes', '--target', 'none', '--init'], tempDir);
expect(r.status, r.stdout + r.stderr).toBe(0);
// The installer ran (and had nothing to wire) …
expect(r.stdout).toContain('No agent targets selected');
// … and the init ran afterwards, in cwd.
expect(r.stdout).toContain(`Initialized in ${fs.realpathSync(tempDir)}`);
expect(fs.existsSync(path.join(tempDir, '.codegraph', 'codegraph.db'))).toBe(true);
});
it('install --init on an already-initialized project reports that and still exits 0', () => {
expect(runCodegraph(['init', '--yes'], tempDir).status).toBe(0);
const r = runCodegraph(['install', '--yes', '--target', 'none', '--init'], tempDir);
expect(r.status, r.stdout + r.stderr).toBe(0);
expect(r.stdout).toContain('Already initialized');
});
it('install --init refuses an unsafe root (filesystem root) with exit code 1, like init does', () => {
// `/` (or the drive root on Windows) is the canonical unsafe root: the
// refusal fires before anything is created, so nothing is written there.
const root = path.parse(process.cwd()).root;
const r = runCodegraph(['install', '--yes', '--target', 'none', '--init'], root);
expect(r.status).toBe(1);
expect(r.stdout).toContain('Refusing to initialize');
expect(fs.existsSync(path.join(root, '.codegraph'))).toBe(false);
});
it('init --yes runs non-interactively with stdin closed and builds the index', () => {
const r = runCodegraph(['init', '--yes'], tempDir);
expect(r.status, r.stdout + r.stderr).toBe(0);
expect(r.stdout).toContain('Initialized in');
expect(fs.existsSync(path.join(tempDir, '.codegraph', 'codegraph.db'))).toBe(true);
});
it('documents the new flags in --help', () => {
expect(runCodegraph(['init', '--help'], tempDir).stdout).toMatch(/-y, --yes\b/);
expect(runCodegraph(['install', '--help'], tempDir).stdout).toMatch(/-i, --init\b/);
});
});