Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/cli/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,31 @@ export interface OnboardingResult {
provider: string | null;
}

function shouldSkipInteractiveOnboarding(): boolean {
const ci = process.env["CI"];
return (
process.stdin.isTTY !== true ||
process.stdout.isTTY !== true ||
(ci !== undefined && ci !== "" && ci !== "0" && ci.toLowerCase() !== "false")
);
}

function writeDefaultOnboardingPrefs(): OnboardingResult {
writePrefs({
lastAgent: null,
lastAgents: [],
lastProvider: null,
skipSplash: true,
firstRunAt: new Date().toISOString(),
});
return { agents: [], provider: null };
}

export async function runOnboarding(): Promise<OnboardingResult> {
if (shouldSkipInteractiveOnboarding()) {
return writeDefaultOnboardingPrefs();
}

p.note(
[
"Welcome to agentmemory.",
Expand Down
94 changes: 94 additions & 0 deletions test/cli-onboarding.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

const prompts = vi.hoisted(() => ({
note: vi.fn(),
multiselect: vi.fn(async () => {
throw new Error("interactive multiselect should not run in non-TTY onboarding");
}),
select: vi.fn(async () => {
throw new Error("interactive select should not run in non-TTY onboarding");
}),
confirm: vi.fn(async () => true),
isCancel: vi.fn(() => false),
cancel: vi.fn(),
log: {
warn: vi.fn(),
step: vi.fn(),
error: vi.fn(),
},
}));

vi.mock("@clack/prompts", () => prompts);
vi.mock("../src/cli/connect/index.js", () => ({
resolveAdapter: vi.fn(),
runAdapter: vi.fn(),
}));

const ORIGINAL_HOME = process.env["HOME"];
const ORIGINAL_USERPROFILE = process.env["USERPROFILE"];
const stdinTtyDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
const stdoutTtyDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "isTTY");

let sandboxHome: string;

function setTTY(value: boolean): void {
Object.defineProperty(process.stdin, "isTTY", { value, configurable: true });
Object.defineProperty(process.stdout, "isTTY", { value, configurable: true });
}

function restoreTTY(): void {
if (stdinTtyDescriptor) Object.defineProperty(process.stdin, "isTTY", stdinTtyDescriptor);
else delete (process.stdin as NodeJS.ReadStream & { isTTY?: boolean }).isTTY;
if (stdoutTtyDescriptor) Object.defineProperty(process.stdout, "isTTY", stdoutTtyDescriptor);
else delete (process.stdout as NodeJS.WriteStream & { isTTY?: boolean }).isTTY;
}

async function freshOnboarding() {
vi.resetModules();
return await import("../src/cli/onboarding.js");
}

describe("cli onboarding", () => {
beforeEach(() => {
sandboxHome = mkdtempSync(join(tmpdir(), "agentmemory-onboarding-"));
process.env["HOME"] = sandboxHome;
process.env["USERPROFILE"] = sandboxHome;
setTTY(false);
vi.clearAllMocks();
});

afterEach(() => {
restoreTTY();
if (ORIGINAL_HOME === undefined) delete process.env["HOME"];
else process.env["HOME"] = ORIGINAL_HOME;
if (ORIGINAL_USERPROFILE === undefined) delete process.env["USERPROFILE"];
else process.env["USERPROFILE"] = ORIGINAL_USERPROFILE;
rmSync(sandboxHome, { recursive: true, force: true });
});

it("does not prompt and records default preferences when onboarding runs without a TTY", async () => {
const { runOnboarding } = await freshOnboarding();

const result = await runOnboarding();

expect(result).toEqual({ agents: [], provider: null });
expect(prompts.multiselect).not.toHaveBeenCalled();
expect(prompts.select).not.toHaveBeenCalled();
expect(prompts.confirm).not.toHaveBeenCalled();

const preferencesPath = join(sandboxHome, ".agentmemory", "preferences.json");
expect(existsSync(preferencesPath)).toBe(true);
const preferences = JSON.parse(readFileSync(preferencesPath, "utf-8"));
expect(preferences).toMatchObject({
schemaVersion: 1,
lastAgent: null,
lastAgents: [],
lastProvider: null,
skipSplash: true,
});
expect(typeof preferences.firstRunAt).toBe("string");
});
});