gram/packages/cli/tests/interactive.test.ts
abiwab b66b785353 feat(cli): show and override the AI model on every AI command
`import`, `db lint` and `db enrich` now print the provider, model and where
the choice came from before calling anything, and accept --model, --provider
and --pick-model to change it for a single run without touching the config.

Split resolveAiSelection (the choice) from buildAiModel (the construction) so
what is displayed cannot drift from what is called, and route all three
commands through one resolveAiForCommand helper. The interactive picker moves
out of init.ts, which now consumes it instead of keeping its own copy with
hardcoded provider labels.

Also hardens credential isolation one layer further out than audit finding
0-a: ai.apiKey, ai.model and ai.baseUrl belong to the provider they were
written for, and are now dropped when the effective provider contradicts
config.ai.provider — whether through --provider or through defu() merging a
global ai block into a project that selects a different provider.

And adds canPrompt(), which requires both stdin and stdout to be a terminal:
`gram import -o out.gram` without --yes used to render the review prompt and
then block forever in a scripted run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 14:43:37 +02:00

46 lines
1.4 KiB
TypeScript

import { describe, it, expect, afterEach } from "bun:test";
import { canPrompt } from "../src/core/interactive";
// Commands used to gate their prompts on `process.stdout.isTTY` alone. Under
// `gram import … < /dev/null` — or any scripted run with stdin piped — stdout
// is still a terminal, so the prompt rendered and then waited forever on input
// that could never arrive. Both ends have to be a terminal.
const original = {
stdin: process.stdin.isTTY,
stdout: process.stdout.isTTY,
};
function withTty(stdin: boolean, stdout: boolean, fn: () => void) {
process.stdin.isTTY = stdin;
process.stdout.isTTY = stdout;
try {
fn();
} finally {
process.stdin.isTTY = original.stdin;
process.stdout.isTTY = original.stdout;
}
}
describe("canPrompt", () => {
afterEach(() => {
process.stdin.isTTY = original.stdin;
process.stdout.isTTY = original.stdout;
});
it("allows prompting when both ends are a terminal", () => {
withTty(true, true, () => expect(canPrompt()).toBe(true));
});
it("refuses when stdin is redirected, even though stdout is a terminal", () => {
withTty(false, true, () => expect(canPrompt()).toBe(false));
});
it("refuses when stdout is piped", () => {
withTty(true, false, () => expect(canPrompt()).toBe(false));
});
it("refuses when neither end is a terminal", () => {
withTty(false, false, () => expect(canPrompt()).toBe(false));
});
});