import "#veryfront/schemas/_test-setup.ts"; /** * Init Command Tests * * Tests the init command types and options validation. */ import { assertEquals, assertExists, assertRejects, assertStringIncludes, } from "#veryfront/testing/assert.ts"; import { describe, it } from "#veryfront/testing/bdd.ts"; import { exists, makeTempDir, remove } from "#veryfront/testing/deno-compat.ts"; import { join } from "veryfront/platform/path"; import { stripAnsi } from "../../ui/ansi.ts"; import { initCommand, newProjectEnv, offerCloudProjectAfterInit } from "./init-command.ts"; import type { InitOptions, InitTemplate } from "./types.ts"; describe("InitCommand Types", () => { describe("InitTemplate", () => { const templates: InitTemplate[] = [ "ai-agent", "docs-agent", "multi-agent-system", "agentic-workflow", "coding-agent", "saas-starter", "minimal", ]; for (const template of templates) { it(`should support '${template}' template`, () => { assertEquals(template, template); }); } }); describe("InitOptions", () => { it("creates a named project beneath parentDir", async () => { const parentDir = await makeTempDir({ prefix: "veryfront-init-parent-" }); const name = `parent-target-${crypto.randomUUID()}`; try { await initCommand({ name, parentDir, template: "minimal", skipInstall: true, skipEnvPrompt: true, quiet: true, }); assertEquals(await exists(join(parentDir, name, "app")), true); assertEquals( await exists(join(parentDir, name, "package.json")), false, ); } finally { await remove(parentDir, { recursive: true }).catch(() => {}); } }); it("prints the verified URL returned by the composed deployment", async () => { const parentDir = await makeTempDir({ prefix: "veryfront-init-deploy-" }); const name = `deployed-target-${crypto.randomUUID()}`; const deployedUrl = "https://verified.example.test/app/dashboard"; const output: string[] = []; const originalLog = console.log; try { console.log = (...args: unknown[]) => output.push(args.map(String).join(" ")); await initCommand( { name, parentDir, template: "minimal", skipInstall: true, skipEnvPrompt: true, deploy: true, }, { deployProject: async (projectDir) => { assertEquals(projectDir, join(parentDir, name)); return deployedUrl; }, }, ); const expectedLiveLine = ` Live: ${deployedUrl}`; const liveLine = output.map(stripAnsi).find((line) => line === expectedLiveLine); assertEquals(await exists(join(parentDir, name, "app")), true); assertEquals(liveLine, expectedLiveLine); } finally { console.log = originalLog; await remove(parentDir, { recursive: true }).catch(() => {}); } }); it("reports deployment failure while preserving the created project", async () => { const parentDir = await makeTempDir({ prefix: "veryfront-init-deploy-failed-" }); const name = `deploy-failed-target-${crypto.randomUUID()}`; try { const result = await initCommand( { name, parentDir, template: "minimal", skipInstall: true, skipEnvPrompt: true, deploy: true, quiet: true, }, { deployProject: () => { throw new Error("sentinel deploy failure"); }, }, ); assertEquals(result.cancelled, false); if (!result.cancelled) { assertEquals(result.deployedUrl, undefined); assertEquals(result.deployment, { status: "failed", message: "Deployment failed. Your project was created locally; run veryfront deploy from the project directory after reviewing your credentials and deployment settings.", }); } assertEquals(await exists(join(parentDir, name, "app")), true); } finally { await remove(parentDir, { recursive: true }).catch(() => {}); } }); it("should allow empty options", () => { const options: InitOptions = {}; assertExists(options); }); it("should allow name option", () => { const options: InitOptions = { name: "my-project" }; assertEquals(options.name, "my-project"); }); it("should allow template option", () => { const options: InitOptions = { template: "ai-agent" }; assertEquals(options.template, "ai-agent"); }); it("should allow skipInstall option", () => { const options: InitOptions = { skipInstall: true }; assertEquals(options.skipInstall, true); }); it("should allow skipEnvPrompt option", () => { const options: InitOptions = { skipEnvPrompt: true }; assertEquals(options.skipEnvPrompt, true); }); it("should allow integrations array", () => { const options: InitOptions = { integrations: [] }; assertEquals(options.integrations?.length, 0); }); it("should allow combined options", () => { const options: InitOptions = { name: "my-ai-app", template: "ai-agent", skipInstall: false, skipEnvPrompt: false, integrations: [], }; assertEquals(options.name, "my-ai-app"); assertEquals(options.template, "ai-agent"); assertEquals(options.skipInstall, false); assertEquals(options.skipEnvPrompt, false); assertExists(options.integrations); }); it("should allow runtime option", () => { const options: InitOptions = { runtime: "deno" }; assertEquals(options.runtime, "deno"); }); it("should accept all three runtime values", () => { const node: InitOptions = { runtime: "node" }; const bun: InitOptions = { runtime: "bun" }; const deno: InitOptions = { runtime: "deno" }; assertEquals(node.runtime, "node"); assertEquals(bun.runtime, "bun"); assertEquals(deno.runtime, "deno"); }); it("rejects names that cannot be used as clean package slugs", async () => { await assertRejects( () => initCommand({ name: "Bad_Name", template: "minimal", skipInstall: true, skipEnvPrompt: true, quiet: true, }), Error, "Project name must use lowercase letters, numbers, dots, and hyphens", ); }); it("rejects invalid project names returned by the interactive wizard", async () => { await assertRejects( () => initCommand( { skipInstall: true, skipEnvPrompt: true, quiet: true, }, { shouldRunWizard: () => true, runWizard: async () => ({ projectName: "Bad_Name", template: "minimal", runtime: "node", initGit: false, skipped: false, cancelled: false, }), }, ), Error, "Project name must use lowercase letters, numbers, dots, and hyphens", ); }); }); describe("Default behaviors", () => { const options: InitOptions = {}; it("should default template to undefined when not specified", () => { assertEquals(options.template, undefined); }); it("should default skipInstall to undefined when not specified", () => { assertEquals(options.skipInstall, undefined); }); it("should default skipEnvPrompt to undefined when not specified", () => { assertEquals(options.skipEnvPrompt, undefined); }); it("should default integrations to undefined when not specified", () => { assertEquals(options.integrations, undefined); }); it("should default runtime to undefined when not specified", () => { assertEquals(options.runtime, undefined); }); }); }); describe("initCommand target directory", () => { it("rejects an existing target directory so the CLI exits non-zero", async () => { const parentDir = await makeTempDir({ prefix: "veryfront-init-existing-" }); const name = "taken"; const keepsake = join(parentDir, name, "README.md"); try { await Deno.mkdir(join(parentDir, name)); await Deno.writeTextFile(keepsake, "keep me\n"); // `veryfront init x && cd x && npm run dev` must stop at the refusal. A // printed message with a zero exit lets the chain run on in the wrong // directory, so the refusal has to surface as an error, not a return. await assertRejects( () => initCommand({ name, parentDir, template: "minimal", skipInstall: true, skipEnvPrompt: true, quiet: true, }), Error, `Directory "${name}" already contains README.md`, ); assertEquals(await Deno.readTextFile(keepsake), "keep me\n"); assertEquals(await exists(join(parentDir, name, "app")), false); } finally { await remove(parentDir, { recursive: true }).catch(() => {}); } }); }); describe("initCommand into the current directory", () => { it("refuses to overwrite files already in the directory without --force", async () => { const parentDir = await makeTempDir({ prefix: "veryfront-init-cwd-" }); const readme = join(parentDir, "README.md"); try { await Deno.writeTextFile(readme, "mine\n"); // Non-interactive `veryfront init` with no name scaffolds into the // current directory. It must hold the same line as the named path: an // existing file is refused, not replaced. await assertRejects( () => initCommand({ parentDir, template: "minimal", skipInstall: true, skipEnvPrompt: true, quiet: true, }), Error, "README.md", ); assertEquals(await Deno.readTextFile(readme), "mine\n"); assertEquals(await exists(join(parentDir, "app")), false); } finally { await remove(parentDir, { recursive: true }).catch(() => {}); } }); }); describe("offerCloudProjectAfterInit", () => { async function run( choose: () => Promise<"linked" | "declined" | "cancelled">, environment: Record = {}, ) { const logged: string[] = []; await offerCloudProjectAfterInit( "/unused", (message) => logged.push(message), choose, (name) => environment[name], ); return logged; } it("prints nothing more once the folder is linked", async () => { assertEquals(await run(() => Promise.resolve("linked")), []); }); for (const choice of ["declined", "cancelled"] as const) { it(`names both ways to get inference, in one log entry, when ${choice}`, async () => { const logged = await run(() => Promise.resolve(choice)); assertEquals(logged.length, 1); assertStringIncludes(logged[0]!, "set up inference in one of these ways"); assertStringIncludes(logged[0]!, "Veryfront Cloud AI Gateway"); assertStringIncludes(logged[0]!, "OPENAI_API_KEY"); assertEquals(logged[0]!.includes("veryfront push"), false); // init does not run dev, so it does not say to restart it. assertEquals(logged[0]!.includes("restart veryfront dev"), false); }); } it("prints no setup advice when an OpenAI key is already set", async () => { // The scaffolded agents leave their model unset, so the default OpenAI // model serves them with this key, the same check the dev banner makes. assertEquals( await run(() => Promise.resolve("declined"), { OPENAI_API_KEY: "" }), [], ); }); it("counts a key init wrote to the new .env", async () => { // A template or integration that declares OPENAI_API_KEY writes its value. const env = newProjectEnv({ OPENAI_API_KEY: "" }, () => undefined); const logged: string[] = []; await offerCloudProjectAfterInit( "/unused", (message) => logged.push(message), () => Promise.resolve("declined"), env, ); assertEquals(logged, []); }); it("ranks the shell's value first and skips written values dev would expand", () => { const shell = { OPENAI_API_KEY: "" } as Record; const env = newProjectEnv( { OPENAI_API_KEY: "", ANTHROPIC_API_KEY: " ", MISTRAL_API_KEY: "$PARENT_KEY", GOOGLE_API_KEY: "${PARENT_KEY}", }, (name) => shell[name], ); assertEquals(env("OPENAI_API_KEY"), ""); assertEquals(env("ANTHROPIC_API_KEY"), undefined); // dev expands a reference against an environment init cannot see. assertEquals(env("MISTRAL_API_KEY"), undefined); assertEquals(env("GOOGLE_API_KEY"), undefined); }); for ( const key of [ "ANTHROPIC_API_KEY", "GOOGLE_API_KEY", "GOOGLE_GENERATIVE_AI_API_KEY", "MISTRAL_API_KEY", ] ) { it(`still names both ways when only ${key}, which the scaffolded agents cannot use, is set`, async () => { const logged = await run(() => Promise.resolve("declined"), { [key]: "" }); assertEquals(logged.length, 1); assertStringIncludes(logged[0]!, "set up inference in one of these ways"); }); } it("still names both ways when the OpenAI key is blank", async () => { const logged = await run(() => Promise.resolve("declined"), { OPENAI_API_KEY: " " }); assertEquals(logged.length, 1); assertStringIncludes(logged[0]!, "set up inference in one of these ways"); }); it("names only the failure when Cloud setup throws and an OpenAI key is set", async () => { const logged = await run( () => Promise.reject(new Error("Network unavailable")), { OPENAI_API_KEY: "" }, ); assertEquals(logged, [" Cloud setup could not finish: Network unavailable"]); }); it("falls back to the trimmed written value when the shell sets none", () => { const env = newProjectEnv({ OPENAI_API_KEY: " " }, () => undefined); assertEquals(env("OPENAI_API_KEY"), ""); assertEquals(env("ANTHROPIC_API_KEY"), undefined); }); it("names the failure, then both ways to get inference, when Cloud setup throws", async () => { const logged = await run(() => Promise.reject(new Error("Network unavailable"))); assertEquals(logged.length, 2); assertEquals(logged[0], " Cloud setup could not finish: Network unavailable"); assertStringIncludes(logged[1]!, "set up inference in one of these ways"); }); });