Files
codegraph/__tests__/cli-install-init.test.ts
T
Colby MchenryandGitHub 0d17dfd6a8 feat(cli): install --init and init --yes for a one-shot, non-interactive bootstrap (#1578) (#1595)
Fixes #1578.

## What was wrong

Bootstrapping CodeGraph in a fresh environment — the issue's case is a throwaway container per AI session — took two commands, `codegraph install --yes` and then `codegraph init`, and the second one could still stop on a prompt (the gitignored-child-repos offer, the watch-fallback offer on WSL/`/mnt`). There was no way to wire agents and build the project's index in one non-interactive line.

The installer's "never index implicitly" rule is deliberate (a surprise index of `$HOME` is exactly what `init` refuses), so the gap is an explicit opt-in, not a change in default behavior.

## What this does

- **`codegraph install -i, --init`** — after wiring the agents, runs the `init` flow in the current directory. It also runs when nothing was wired (`--target none`, no agents detected), since the installer returns normally in that case. Every `init` guard applies: a home directory / filesystem root / parent of home is **refused with exit code 1** (no implied `--force`), and an already-initialized project just reports that and exits 0. `--print-config` and `--refresh` return before the install, so `--init` is a no-op with them.
- **`codegraph init -y, --yes`** — non-interactive: the ignored-repos offer prints its one-line `includeIgnored` opt-in snippet instead of prompting (the existing non-TTY behavior), and the watch-fallback offer takes its `yes` default. `install --init` passes `--yes` through, so `codegraph install --yes --init` is a fully unattended bootstrap.
- The `init` action body becomes `runInit()`, shared by both commands. The plain `init` path is behavior-identical (same refusal, already-initialized notice, supervised index, telemetry, offers, outro).
- The post-install "Next: index a project" note gains one line mentioning `--init`; README gets the flag row and a `--yes --init` example.

On the reporter's other observation — `install --yes` skipping the "install the CLI on your PATH" step: that's by design for scripted use (it assumes the CLI is already present), and the `bunx @colbymchenry/codegraph serve --mcp` MCP entry they found is the self-contained alternative. Not changed here.

## Tests

`__tests__/cli-install-init.test.ts` — end-to-end against the built binary with stdin closed (a blocking prompt would fail), always `--target none` so the suite never touches an agent config on the host:

- `install --yes --target none --init` → exit 0, installer reports nothing to wire, `Initialized in <tmp>`, `.codegraph/codegraph.db` exists;
- the same on an already-initialized project → `Already initialized`, exit 0;
- the same at the filesystem root → exit 1, `Refusing to initialize`, nothing written;
- `init --yes` with stdin closed → exit 0, index built;
- `init --help` lists `-y, --yes`, `install --help` lists `-i, --init`.

`npx vitest run __tests__/installer-targets.test.ts __tests__/upgrade.test.ts` → 283 passed, 3 skipped. Full `npm test` → see the checks on this PR / below.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01LxZj6W6Y1SHXwvpT3uwJpK
2026-08-26 10:38:21 -05: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/);
});
});