import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from "node:fs"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { PassThrough } from "node:stream"; import { fileURLToPath } from "node:url"; import type { ComputeSettings, ConfigureRealtimeInput, CreateBranchInput, CreateBucketInput, CreateCredentialInput, DeployFunctionInput, GetConnectionUriInput, NeonAuthSnapshot, NeonBranchSnapshot, NeonBranchStorageSnapshot, NeonBucketSnapshot, NeonCredentialMeta, NeonCredentialReveal, NeonCredentialSecret, NeonCustomDomainSnapshot, NeonDataApiSnapshot, NeonDatabaseSnapshot, NeonEndpointSnapshot, NeonFunctionDeploymentSnapshot, NeonFunctionSnapshot, NeonProjectSnapshot, NeonRealtimeSecret, NeonRealtimeSnapshot, NeonRoleSnapshot, NeonTriggerSnapshot, } from "@neon/config"; import { resolveConfig } from "@neon/config"; import { loadConfigFromFile, type NeonApi } from "@neon/config-runtime"; import { recordCredentialInputs } from "@neon-internals/cli-core/auth_selection"; import { unzipSync } from "fflate"; import stripAnsi from "strip-ansi"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import type { ConfigProps } from "./config.js"; import { applyCmd, applyPolicyOnCreate, createBranchFromPolicyOnCheckout, initCmd, planCmd, status, } from "./config.js"; const PROJECT_ID = "patient-art-12345"; const BRANCH_ID = "br-snowy-frost-12345"; const BRANCH_NAME = "main"; const CHILD_ID = "br-staging-frost-99999"; const CHILD_NAME = "staging"; /** * Full {@link NeonApi} implementation backed by fixed in-memory state for one * project + one default branch. The handful of methods that `inspect` / `plan` / * `apply` exercise return real data; everything else throws so an unexpected call * fails loudly instead of silently passing. `enableNeonAuth` records its calls so * a test can assert `apply` actually mutated remote state. */ class FakeNeonApi implements NeonApi { readonly enableNeonAuthCalls: { projectId: string; branchId: string }[] = []; readonly deployBranchFunctionCalls: { projectId: string; branchId: string; slug: string; input: DeployFunctionInput; }[] = []; readonly enableRealtimeCalls: Array<{ projectId: string; branchId: string; input?: ConfigureRealtimeInput; }> = []; private realtime: NeonRealtimeSnapshot = { enabled: false, pending: false, }; /** Functions materialized by a deploy, keyed by slug (Neon creates on first deploy). */ private readonly functions = new Map(); private readonly customDomains = new Map< string, NeonCustomDomainSnapshot >(); async listProjects(): Promise { throw new Error("not implemented"); } async getProject(projectId: string): Promise { return { id: projectId, name: "dev-project", regionId: "aws-us-east-1", pgVersion: 17, }; } async createProject(): Promise { throw new Error("not implemented"); } async updateProject(): Promise { throw new Error("not implemented"); } async listBranches(projectId: string): Promise { void projectId; return [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: false, }, ]; } async createBranch( projectId: string, input: CreateBranchInput, ): Promise<{ branch: NeonBranchSnapshot; endpoints: NeonEndpointSnapshot[]; }> { void projectId; void input; throw new Error("not implemented"); } async updateBranch(): Promise { throw new Error("not implemented"); } async listEndpoints(projectId: string): Promise { void projectId; return [ { id: "ep-fake-1", branchId: BRANCH_ID, type: "read_write", autoscalingLimitMinCu: 0.25, autoscalingLimitMaxCu: 0.25, suspendTimeout: "5m", }, ]; } async updateEndpoint( projectId: string, endpointId: string, settings: ComputeSettings, ): Promise { void projectId; void endpointId; void settings; throw new Error("not implemented"); } async listBranchRoles( projectId: string, branchId: string, ): Promise { void projectId; return [{ name: "neondb_owner", branchId, protected: false }]; } async listBranchDatabases( projectId: string, branchId: string, ): Promise { void projectId; return [{ name: "neondb", branchId, ownerName: "neondb_owner" }]; } async getConnectionUri( projectId: string, input: GetConnectionUriInput, ): Promise<{ uri: string }> { void projectId; return { uri: `postgresql://${input.roleName}:pw@${BRANCH_ID}.fake.neon.tech/${input.databaseName}?sslmode=require`, }; } async getNeonAuth(): Promise { return null; } async enableNeonAuth( projectId: string, branchId: string, ): Promise { this.enableNeonAuthCalls.push({ projectId, branchId }); return { projectId: "auth-project", jwksUrl: "https://auth.fake.neon.tech/.well-known/jwks.json", baseUrl: "https://auth.fake.neon.tech", }; } async getNeonDataApi(): Promise { return null; } async enableProjectBranchDataApi(): Promise { throw new Error("not implemented"); } async updateProjectBranchDataApi(): Promise { throw new Error("not implemented"); } async deleteProjectBranchDataApi(): Promise {} async getProjectBranchRealtime(): Promise { return structuredClone(this.realtime); } async getProjectBranchRealtimeSecret(): Promise { return { secret: "nrt_live_1_cli-config-test", pending: false }; } async enableProjectBranchRealtime( projectId: string, branchId: string, input?: ConfigureRealtimeInput, ): Promise { this.enableRealtimeCalls.push({ projectId, branchId, input }); this.realtime = { enabled: true, pending: false, invocationUrl: "wss://realtime.fake.neon.tech/v1", ...(input?.allowedOrigins !== undefined ? { allowedOrigins: [...input.allowedOrigins] } : {}), }; } async disableProjectBranchRealtime(): Promise { this.realtime = { enabled: false, pending: false }; } async listBranchBuckets(): Promise { return []; } async createBranchBucket( projectId: string, branchId: string, input: CreateBucketInput, ): Promise { void projectId; void branchId; return { name: input.name, accessLevel: input.accessLevel ?? "private", }; } async deleteBranchBucket(): Promise { throw new Error("not implemented"); } async listBranchFunctions(): Promise { return [...this.functions.values()]; } async deleteBranchFunction(): Promise { throw new Error("not implemented"); } async deployBranchFunction( projectId: string, branchId: string, slug: string, input: DeployFunctionInput, ): Promise { this.deployBranchFunctionCalls.push({ projectId, branchId, slug, input, }); // Neon creates the function on its first deployment — mirror that so a later // `listBranchFunctions` (used to resolve the invocation URL) sees it. if (!this.functions.has(slug)) { this.functions.set(slug, { id: `fn-${slug}`, slug, name: slug, invocationUrl: `https://${branchId}.fake.neon.tech/functions/${slug}`, }); } const fn = this.functions.get(slug); if (fn) { fn.activeDeploymentId = 1; fn.currentDeployment = { id: 1, status: "completed" }; } return { id: 1, status: "completed" }; } async getBranchFunction( _projectId: string, _branchId: string, slug: string, ): Promise { const fn = this.functions.get(slug); if (!fn) { throw new Error(`Function ${slug} was not found.`); } return fn; } async updateBranchFunction( _projectId: string, _branchId: string, slug: string, input: { name: string }, ): Promise { const fn = this.functions.get(slug); if (!fn) { throw new Error(`Function ${slug} was not found.`); } fn.name = input.name; return fn; } async listBranchTriggers(): Promise { return []; } async createBranchTrigger(): Promise { throw new Error("not implemented"); } async updateBranchTrigger(): Promise { throw new Error("not implemented"); } async deleteBranchTrigger(): Promise { throw new Error("not implemented"); } async listBranchCustomDomains( _projectId: string, _branchId: string, ): Promise { return [...this.customDomains.values()]; } async registerBranchCustomDomain( _projectId: string, _branchId: string, input: { domain: string; functionSlug: string }, ): Promise { const snapshot: NeonCustomDomainSnapshot = { domain: input.domain, entityType: "function", entityId: input.functionSlug, cnameTarget: "custom-domains.fake.neon.tech", }; this.customDomains.set(input.domain, snapshot); return snapshot; } async deleteBranchCustomDomain( _projectId: string, _branchId: string, domain: string, ): Promise { this.customDomains.delete(domain); } seedCustomDomain(snapshot: NeonCustomDomainSnapshot): void { this.customDomains.set(snapshot.domain, snapshot); } async getAiGatewayEnabled(): Promise { return false; } async enableAiGateway(): Promise { throw new Error("not implemented"); } async disableAiGateway(): Promise { throw new Error("not implemented"); } async createCredential( _projectId: string, branchId: string, input: CreateCredentialInput, ): Promise { return { tokenId: "cred-fake-0000", tokenIdShort: "credfake0000", apiToken: "nt_live_credfake0000_secret", s3SecretAccessKey: "s3secret".padEnd(64, "0"), scopes: input.scopes, branchId, createdAt: "2026-01-01T00:00:00Z", }; } async listCredentials(): Promise { return []; } async revealCredential(): Promise { throw new Error("not implemented"); } async revokeCredential(): Promise { return; } async getProjectBranchStorage(): Promise { return { s3Endpoint: "https://fake.storage.neon.tech", region: "us-east-1", forcePathStyle: true, }; } } class ChildBranchNeonApi extends FakeNeonApi { override async listBranches( projectId: string, ): Promise { void projectId; return [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: false, }, { id: CHILD_ID, name: CHILD_NAME, isDefault: false, protected: false, parentId: BRANCH_ID, }, ]; } override async listEndpoints( projectId: string, ): Promise { const [main] = await super.listEndpoints(projectId); return [ main, { ...main, id: "ep-fake-child", branchId: CHILD_ID, }, ]; } } /** * Minimal stand-in for the neonctl `Api` client. Only `listProjectBranches` is * exercised (by `branchIdFromProps`, to resolve the branch name to its id); * anything else is absent and would throw if touched. */ /** `apply` always fails partway through — used to prove `deploy.after` does not run then. */ class BuildingFunctionNeonApi extends FakeNeonApi { override async getBranchFunction( projectId: string, branchId: string, slug: string, ): Promise { const fn = await super.getBranchFunction(projectId, branchId, slug); return { ...fn, currentDeployment: { id: 1, status: "building" } }; } } class RejectAuthNeonApi extends FakeNeonApi { override async enableNeonAuth(): Promise { throw new Error("Neon Auth enablement rejected (test)"); } } class ProtectedBranchNeonApi extends FakeNeonApi { override async listBranches(): Promise { return [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: true, }, ]; } } const fakeApiClient = { listProjectBranches: async () => ({ data: { branches: [ { id: BRANCH_ID, name: BRANCH_NAME, default: true, }, ], }, }), }; /** Capture writer output (the writer respects `props.out`). */ const captureOut = (): { stream: PassThrough; read: () => string } => { const stream = new PassThrough(); let buffer = ""; stream.on("data", (chunk: Buffer) => { buffer += chunk.toString(); }); return { stream, read: () => buffer }; }; const baseProps = ( api: FakeNeonApi, out: PassThrough, ): ConfigProps & { runtimeApi: NeonApi; out: PassThrough } => ({ apiClient: fakeApiClient as never, apiKey: "", apiHost: "https://console.neon.tech/api/v2", contextFile: "", output: "json", projectId: PROJECT_ID, branch: BRANCH_NAME, runtimeApi: api, // Off by default in tests so the apply/plan assertions don't trigger the bundled env // pull (which writes a .env to cwd). The dedicated env-pull tests opt back in. envPull: false, out, }); describe("config commands", () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), "neonctl-config-")); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); const writeConfig = (body: string): string => { const path = join(cwd, "neon.ts"); writeFileSync(path, body); return path; }; it("status returns the live project + branch state with a resolved neon.ts-shaped config", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); await status(baseProps(api, stream)); const parsed = JSON.parse(read()); expect(parsed.project.id).toBe(PROJECT_ID); expect(parsed.branch.id).toBe(BRANCH_ID); expect(parsed.branch.name).toBe(BRANCH_NAME); // The `config` column is the resolved neon.ts-shaped view, not the raw `{}`: the fake // branch has compute settings (so a `branch.postgres` section) and no auth/dataApi. expect(parsed.config.branch.postgres.computeSettings).toMatchObject({ autoscalingLimitMaxCu: 0.25, suspendTimeout: "5m", }); expect(parsed.config.auth).toBeUndefined(); expect(parsed.config.dataApi).toBeUndefined(); expect(parsed.config.preview).toBeUndefined(); }); it("status prints a readable summary in table output", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); await status({ ...baseProps(api, stream), output: "table" }); const text = read(); expect(text).toMatch(/^Status$/m); expect(text).toMatch( new RegExp(`^Branch\\s+${BRANCH_NAME} \\(${BRANCH_ID}\\)$`, "m"), ); expect(text).toMatch(/^Compute\s+0\.25 CU$/m); expect(text).toMatch(/^Scale To Zero\s+5m$/m); expect(text).not.toContain("{"); expect(text).not.toMatch(/^Realtime/m); }); it("status reports Realtime and its allowed origins", async () => { const api = new FakeNeonApi(); await api.enableProjectBranchRealtime(PROJECT_ID, BRANCH_ID, { allowedOrigins: [ "https://app.example.com", "http://localhost:3000", ], }); const json = captureOut(); await status(baseProps(api, json.stream)); expect(JSON.parse(json.read()).config.preview.realtime).toEqual({ allowedOrigins: [ "https://app.example.com", "http://localhost:3000", ], }); const table = captureOut(); await status({ ...baseProps(api, table.stream), output: "table" }); const text = table.read(); expect(text).toMatch(/^Realtime\s+enabled$/m); expect(text).toMatch( /^Realtime Origins\s+https:\/\/app\.example\.com, http:\/\/localhost:3000$/m, ); }); it("status reports Realtime that allows every origin", async () => { const api = new FakeNeonApi(); await api.enableProjectBranchRealtime(PROJECT_ID, BRANCH_ID); const json = captureOut(); await status(baseProps(api, json.stream)); expect(JSON.parse(json.read()).config.preview.realtime).toBe(true); const table = captureOut(); await status({ ...baseProps(api, table.stream), output: "table" }); const text = table.read(); expect(text).toMatch(/^Realtime\s+enabled$/m); expect(text).toMatch(/^Realtime Origins\s+any$/m); }); it("status --config-json prints only the neon.ts-shaped config to stdout", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); // Capture process.stdout (the --config-json path writes there directly). const chunks: string[] = []; const orig = process.stdout.write.bind(process.stdout); process.stdout.write = ((chunk: string | Uint8Array) => { chunks.push(chunk.toString()); return true; }) as typeof process.stdout.write; try { await status({ ...baseProps(api, stream), configJson: true }); } finally { process.stdout.write = orig; } // The writer (table/json output) is NOT used; only stdout JSON. expect(read()).toBe(""); const json = JSON.parse(chunks.join("")); // No project/branch envelope — just the config. expect(json.project).toBeUndefined(); expect(json.branch.postgres.computeSettings.suspendTimeout).toBe("5m"); }); it("status --current-branch prints only the pinned branch from .neon and never touches the API", async () => { const { stream, read } = captureOut(); const contextFile = join(cwd, ".neon"); writeFileSync( contextFile, JSON.stringify({ orgId: "org-x", projectId: "proj-x", branch: "my-feature", }), ); // Any API access in this mode is a bug: the branch comes purely from `.neon`. const throwingApi = new Proxy( {}, { get() { throw new Error( "apiClient must not be called for --current-branch", ); }, }, ); const chunks: string[] = []; const orig = process.stdout.write.bind(process.stdout); process.stdout.write = ((chunk: string | Uint8Array) => { chunks.push(chunk.toString()); return true; }) as typeof process.stdout.write; try { await status({ ...baseProps(new FakeNeonApi(), stream), apiClient: throwingApi as never, contextFile, currentBranch: true, }); } finally { process.stdout.write = orig; } // Branch present: exactly the branch name + newline, exit 0 (like `git // branch --show-current` for this case); the writer is unused. expect(chunks.join("")).toBe("my-feature\n"); expect(read()).toBe(""); }); it("status --current-branch prints nothing to stdout, hints on stderr, and exits non-zero when no branch is pinned", async () => { const { stream } = captureOut(); const contextFile = join(cwd, ".neon"); // A linked project but no pinned branch — the unset case. writeFileSync( contextFile, JSON.stringify({ orgId: "org-x", projectId: "proj-x" }), ); const stdoutChunks: string[] = []; const stderrChunks: string[] = []; const origOut = process.stdout.write.bind(process.stdout); const origErr = process.stderr.write.bind(process.stderr); const origExitCode = process.exitCode; process.stdout.write = ((chunk: string | Uint8Array) => { stdoutChunks.push(chunk.toString()); return true; }) as typeof process.stdout.write; process.stderr.write = ((chunk: string | Uint8Array) => { stderrChunks.push(chunk.toString()); return true; }) as typeof process.stderr.write; try { await status({ ...baseProps(new FakeNeonApi(), stream), contextFile, currentBranch: true, }); // stdout stays empty; the hint goes to stderr only. expect(stdoutChunks.join("")).toBe(""); expect(stderrChunks.join("")).toContain( "Run `neon checkout `", ); // Non-zero exit (grep-style) so a shell prompt can guard on it directly. expect(process.exitCode).toBe(1); } finally { process.stdout.write = origOut; process.stderr.write = origErr; // Restore so a failing exit code doesn't leak into the vitest process. process.exitCode = origExitCode; } }); it("status --current-branch treats a missing .neon as no branch (empty stdout, non-zero exit)", async () => { const { stream } = captureOut(); const contextFile = join(cwd, "does-not-exist", ".neon"); const stdoutChunks: string[] = []; const orig = process.stdout.write.bind(process.stdout); const origExitCode = process.exitCode; process.stdout.write = ((chunk: string | Uint8Array) => { stdoutChunks.push(chunk.toString()); return true; }) as typeof process.stdout.write; try { await status({ ...baseProps(new FakeNeonApi(), stream), contextFile, currentBranch: true, }); expect(stdoutChunks.join("")).toBe(""); expect(process.exitCode).toBe(1); } finally { process.stdout.write = orig; process.exitCode = origExitCode; } }); it("plan is a dry run whose applied list includes the auth service change", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig("export default { auth: {} };\n"); await planCmd({ ...baseProps(api, stream), config }); const result = JSON.parse(read()); expect(result.dryRun).toBe(true); const authChange = result.applied.find( (change: { identifier: string }) => change.identifier === "auth", ); expect(authChange).toBeDefined(); expect(authChange.kind).toBe("service"); // A dry run never mutates remote state. expect(api.enableNeonAuthCalls).toHaveLength(0); }); it("apply actually enables Neon Auth and is not a dry run", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig("export default { auth: {} };\n"); await applyCmd({ ...baseProps(api, stream), config }); const result = JSON.parse(read()); expect(result.dryRun).toBe(false); expect(api.enableNeonAuthCalls).toHaveLength(1); expect(api.enableNeonAuthCalls[0]).toEqual({ projectId: PROJECT_ID, branchId: BRANCH_ID, }); }); it("plans and applies Realtime through the config runtime", async () => { const api = new FakeNeonApi(); const config = writeConfig( 'export default { preview: { realtime: { allowedOrigins: ["https://app.example.com"] } } };\n', ); const planned = captureOut(); await planCmd({ ...baseProps(api, planned.stream), config }); const plan = JSON.parse(planned.read()); expect(plan.dryRun).toBe(true); expect(plan.applied).toEqual( expect.arrayContaining([ expect.objectContaining({ action: "create", identifier: "realtime", }), ]), ); expect(api.enableRealtimeCalls).toHaveLength(0); const applied = captureOut(); await applyCmd({ ...baseProps(api, applied.stream), config }); expect(JSON.parse(applied.read()).dryRun).toBe(false); expect(api.enableRealtimeCalls).toEqual([ { projectId: PROJECT_ID, branchId: BRANCH_ID, input: { allowedOrigins: ["https://app.example.com"] }, }, ]); }); it("runs deploy.before and deploy.after (Preview lifecycle hooks) around a real apply", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const calls: { phase: string; ctx: unknown }[] = []; (globalThis as Record).__deployHookCalls = calls; const config = writeConfig(`export default { experimental: { hooks: { deploy: { before: (ctx) => { globalThis.__deployHookCalls.push({ phase: "before", ctx }); }, after: (ctx) => { globalThis.__deployHookCalls.push({ phase: "after", ctx }); }, }, }, }, };\n`); try { await applyCmd({ ...baseProps(api, stream), config, runtimeApi: api, }); } finally { delete (globalThis as Record).__deployHookCalls; } expect(calls.map((c) => c.phase)).toEqual(["before", "after"]); const before = calls[0].ctx as { branch: { id: string; name: string; created: boolean }; event: { type: string }; }; expect(before.branch).toEqual( expect.objectContaining({ id: BRANCH_ID, name: BRANCH_NAME, created: false, }), ); expect(before.event).toEqual({ type: "neon-deploy" }); const after = calls[1].ctx as { branch: { id: string }; env: { postgres: { databaseUrl: string; databaseUrlUnpooled: string }; }; result: { dryRun: boolean }; event: { type: string }; }; expect(after.branch.id).toBe(BRANCH_ID); expect(after.event).toEqual({ type: "neon-deploy" }); expect(after.result.dryRun).toBe(false); // Resolved via the injected `runtimeApi` (FakeNeonApi), never a real network call. expect(after.env.postgres.databaseUrl).toContain("neondb_owner"); expect(after.env.postgres.databaseUrlUnpooled).toContain( "neondb_owner", ); }); it("does not run deploy.before when a protected-branch apply is refused", async () => { const api = new ProtectedBranchNeonApi(); const { stream } = captureOut(); const calls: string[] = []; (globalThis as Record).__deployHookCalls = calls; const config = writeConfig(`export default { auth: {}, experimental: { hooks: { deploy: { before: () => { globalThis.__deployHookCalls.push("before"); } } }, }, };\n`); try { await expect( applyCmd({ ...baseProps(api, stream), config, runtimeApi: api, }), ).rejects.toThrow( `Branch "${BRANCH_NAME}" is protected. Re-run with --allow-protected (or -y) to apply to it.`, ); } finally { delete (globalThis as Record).__deployHookCalls; } expect(calls).toEqual([]); expect(api.enableNeonAuthCalls).toEqual([]); }); it("does not run deploy.after when apply throws (deploy.before still ran)", async () => { const api = new RejectAuthNeonApi(); const { stream } = captureOut(); const calls: { phase: string }[] = []; (globalThis as Record).__deployHookCalls = calls; const config = writeConfig(`export default { auth: {}, experimental: { hooks: { deploy: { before: (ctx) => { globalThis.__deployHookCalls.push({ phase: "before" }); }, after: (ctx) => { globalThis.__deployHookCalls.push({ phase: "after" }); }, }, }, }, };\n`); try { await expect( applyCmd({ ...baseProps(api, stream), config, runtimeApi: api, }), ).rejects.toThrow(); } finally { delete (globalThis as Record).__deployHookCalls; } expect(calls.map((c) => c.phase)).toEqual(["before"]); }); it("apply deploys a function via neonctl's own bundler", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); // A real handler module on disk: applyCmd's injected bundler (bundleEntry + // zipBundle) actually runs esbuild against this source and zips the output, so // it must be valid TypeScript that esbuild can bundle. const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); const config = writeConfig( `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } } } };\n`, ); await applyCmd({ ...baseProps(api, stream), config }); // The function was new (listBranchFunctions returns []), so apply both creates // it and deploys code to it. We assert the deploy carried a real bundle. expect(api.deployBranchFunctionCalls).toHaveLength(1); const call = api.deployBranchFunctionCalls[0]; expect(call.projectId).toBe(PROJECT_ID); expect(call.branchId).toBe(BRANCH_ID); expect(call.slug).toBe("hello"); expect( (await api.getBranchFunction(PROJECT_ID, BRANCH_ID, "hello")).name, ).toBe("Hello"); // The bundle must be the real ZIP produced by neonctl's bundleEntry + zipBundle // (NOT config-runtime's own esbuild default bundler). A non-empty Uint8Array // whose first two bytes are the ZIP local-file-header magic ("PK") proves a real // archive was built by neonctl's injected FunctionBundler. const { bundle } = call.input; expect(bundle).toBeInstanceOf(Uint8Array); expect(bundle.byteLength).toBeGreaterThan(0); expect(bundle[0]).toBe(0x50); // 'P' expect(bundle[1]).toBe(0x4b); // 'K' }); it("apply warns for preview tuning gated on parentId", async () => { const api = new ChildBranchNeonApi(); const { stream } = captureOut(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); const config = writeConfig( `export default { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } }, branch: (branch) => branch.parentId ? { preview: { functions: { hello: { runtime: 'nodejs24' } } } } : {} };\n`, ); const childApiClient = { listProjectBranches: async () => ({ data: { branches: [ { id: BRANCH_ID, name: BRANCH_NAME, default: true, protected: false, }, { id: CHILD_ID, name: CHILD_NAME, default: false, protected: false, parent_id: BRANCH_ID, }, ], }, }), }; const stderrChunks: string[] = []; const origErr = process.stderr.write.bind(process.stderr); process.stderr.write = ((chunk: string | Uint8Array) => { stderrChunks.push(chunk.toString()); return origErr(chunk); }) as typeof process.stderr.write; try { await applyCmd({ ...baseProps(api, stream), apiClient: childApiClient as never, branch: CHILD_NAME, config, }); expect(stderrChunks.join("")).toContain( "branch.preview.functions → branch.functions", ); } finally { process.stderr.write = origErr; } }); it("apply ships a bundler none directory without esbuild flattening", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const outDir = join(cwd, "build-output"); mkdirSync(outDir); writeFileSync( join(outDir, "index.mjs"), "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync(join(outDir, "chunk.mjs"), "export const x = 1;\n"); const config = writeConfig( `export default { preview: { functions: { app: { name: 'App', source: ${JSON.stringify( outDir, )}, bundler: 'none' } } } };\n`, ); await applyCmd({ ...baseProps(api, stream), config }); expect(api.deployBranchFunctionCalls).toHaveLength(1); const entries = unzipSync( api.deployBranchFunctionCalls[0].input.bundle, ); expect(Object.keys(entries).sort()).toEqual(["chunk.mjs", "index.mjs"]); }); it("apply esbuilds a directory source from index.ts, ignoring a broken index.js", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const outDir = join(cwd, "fn-src"); mkdirSync(outDir); writeFileSync( join(outDir, "index.ts"), "export default { fetch() { return new Response('from-ts'); } };\n", ); writeFileSync(join(outDir, "index.js"), "export default {\n"); const config = writeConfig( `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( outDir, )} } } } };\n`, ); await applyCmd({ ...baseProps(api, stream), config }); expect(api.deployBranchFunctionCalls).toHaveLength(1); const entries = unzipSync( api.deployBranchFunctionCalls[0].input.bundle, ); expect(Object.keys(entries)).toEqual(["index.mjs"]); expect(new TextDecoder().decode(entries["index.mjs"])).toContain( "from-ts", ); }); it("apply rejects bundler none when the directory is only TypeScript", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const outDir = join(cwd, "ts-only"); mkdirSync(outDir); writeFileSync(join(outDir, "index.ts"), "export default {};\n"); const config = writeConfig( `export default { preview: { functions: { app: { name: 'App', source: ${JSON.stringify( outDir, )}, bundler: 'none' } } } };\n`, ); await expect( applyCmd({ ...baseProps(api, stream), config }), ).rejects.toThrow(/bundler is "none".*TypeScript/); }); it("--env loads a .env file into the environment before evaluating neon.ts", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); // The function's env value reads process.env.RESEND_API_KEY — which is only present if // the --env file is loaded before the policy is evaluated. const config = writeConfig( `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )}, env: { resendApiKey: process.env.RESEND_API_KEY } } } } };\n`, ); const envFile = join(cwd, ".env.deploy"); writeFileSync(envFile, "RESEND_API_KEY=re_from_file\n"); try { await applyCmd({ ...baseProps(api, stream), config, env: envFile }); } finally { delete process.env.RESEND_API_KEY; } expect(api.deployBranchFunctionCalls).toHaveLength(1); expect(api.deployBranchFunctionCalls[0].input.environment).toEqual({ resendApiKey: "re_from_file", }); }); it("apply surfaces each deployed function invocation URL in the output", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); const config = writeConfig( `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } } } };\n`, ); // Human-readable (table) output so we exercise the dedicated Function URLs table; the // JSON path already carries the URL inside the raw applied-change details. await applyCmd({ ...baseProps(api, stream), output: "table", config }); const out = read(); expect(out).toContain("Function URLs"); expect(out).toContain( `https://${BRANCH_ID}.fake.neon.tech/functions/hello`, ); }); describe("waiting for function builds", () => { const writeHelloConfig = () => { const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); return writeConfig( `export default { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } } };\n`, ); }; const captureStderr = async (run: () => Promise) => { const chunks: string[] = []; const origErr = process.stderr.write.bind(process.stderr); process.stderr.write = ((chunk: string | Uint8Array) => { chunks.push(chunk.toString()); return true; }) as typeof process.stderr.write; try { await run(); } finally { process.stderr.write = origErr; } return chunks.join(""); }; const withPollTimeout = async ( run: () => Promise, ): Promise => { const prev = { interval: process.env.NEON_FUNCTIONS_POLL_INTERVAL_MS, timeout: process.env.NEON_FUNCTIONS_POLL_TIMEOUT_MS, }; process.env.NEON_FUNCTIONS_POLL_INTERVAL_MS = "5"; process.env.NEON_FUNCTIONS_POLL_TIMEOUT_MS = "40"; try { return await run(); } finally { for (const [key, value] of [ ["NEON_FUNCTIONS_POLL_INTERVAL_MS", prev.interval], ["NEON_FUNCTIONS_POLL_TIMEOUT_MS", prev.timeout], ] as const) { if (value === undefined) delete process.env[key]; else process.env[key] = value; } } }; it("names the functions it waits for, and fails when a build does not finish", async () => { const { stream } = captureOut(); const config = writeHelloConfig(); let failure: unknown; const stderr = await withPollTimeout(() => captureStderr(async () => { try { await applyCmd({ ...baseProps(new BuildingFunctionNeonApi(), stream), config, }); } catch (err) { failure = err; } }), ); expect(stderr).toContain( "Waiting for Function builds to finish: hello…", ); expect(String(failure)).toMatch( /"hello" deployment 1 did not complete within/, ); }); it("--no-wait returns while the build is still running", async () => { const { stream, read } = captureOut(); const config = writeHelloConfig(); const stderr = await withPollTimeout(() => captureStderr(() => applyCmd({ ...baseProps(new BuildingFunctionNeonApi(), stream), config, wait: false, }), ), ); expect(stderr).not.toContain("Waiting for Function builds"); expect(read()).toContain( `https://${BRANCH_ID}.fake.neon.tech/functions/hello`, ); }); }); it("keeps the changes table minimal and lists function URLs out of the table (regression)", async () => { // Regression: a deployed function's change details carry a long `invocationUrl`. We used // to JSON.stringify the whole details object into a "Details" table column, which blew the // ASCII table out to ~190 columns so its borders wrapped and misaligned in a normal // terminal. The changes table is now minimal (action/kind/identifier only) and the URLs // are printed as a plain list below it, so nothing long ever lands in a table cell. const api = new FakeNeonApi(); const { stream, read } = captureOut(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); const config = writeConfig( `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } } } };\n`, ); await applyCmd({ ...baseProps(api, stream), output: "table", config }); const out = stripAnsi(read()); const invocationUrl = `https://${BRANCH_ID}.fake.neon.tech/functions/hello`; // The changes table never carries a Details column or the raw details blob. const [appliedSection, functionSection = ""] = out.split("Function URLs"); expect(appliedSection).toContain("Applied changes"); expect(appliedSection).not.toContain("Details"); expect(appliedSection).not.toContain('{"slug"'); expect(appliedSection).not.toContain(invocationUrl); // The URL is listed (not tabulated) below, as a copy-pasteable bullet. expect(functionSection).toContain(`• hello: ${invocationUrl}`); // No rendered line is absurdly wide. Pre-fix the function detail row was ~190 cols; // a 120-col ceiling fails loudly if a long value ever leaks back into a table cell. const widest = Math.max(...out.split("\n").map((line) => line.length)); expect(widest).toBeLessThan(120); }); it("reports the services a policy utilizes (Postgres always on) in the plan output", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig( "export default { auth: {}, dataApi: true, preview: { aiGateway: true, buckets: { uploads: {} } } };\n", ); await planCmd({ ...baseProps(api, stream), config }); const result = JSON.parse(read()); // Postgres is always first; each declared service follows in a stable order. The AI // Gateway is listed even though it never produces a plan step (it's credential-gated). expect(result.services).toEqual([ "Postgres", "Neon Auth", "Data API", "Object Storage", "AI Gateway", ]); }); it("plans and applies the AI Gateway on a Free-plan project without consulting the plan", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig( "export default { preview: { aiGateway: true } };\n", ); let planLookups = 0; const apiClient = { ...fakeApiClient, getProject: async () => { planLookups += 1; return { data: { project: { id: PROJECT_ID, owner: { subscription_type: "free_v3" }, }, }, }; }, }; const props = { ...baseProps(api, stream), apiClient: apiClient as never, }; await planCmd({ ...props, config }); await applyCmd({ ...props, config }); expect(read()).toContain('"AI Gateway"'); expect(planLookups).toBe(0); expect(process.exitCode ?? 0).toBe(0); }); it('prints a "Utilized services" summary below the plan table (human output)', async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig( "export default { auth: {}, preview: { aiGateway: true } };\n", ); await planCmd({ ...baseProps(api, stream), output: "table", config }); const out = read(); expect(out).toContain("Planned changes"); expect(out).toContain( "Utilized services: Postgres, Neon Auth, AI Gateway", ); }); it("still lists utilized services when the branch already matches (no changes)", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); // Empty policy: nothing to apply, so the plan table is empty — but the summary still // shows Postgres so the command never looks like it did nothing meaningful. const config = writeConfig("export default {};\n"); await applyCmd({ ...baseProps(api, stream), output: "table", config }); expect(read()).toContain("Utilized services: Postgres"); }); it("plans a custom-domain register without inventing a CNAME target", async () => { const api = new FakeNeonApi(); const { stream, read } = captureOut(); const config = writeConfig( 'export default { preview: { functions: { hello: { name: "Hello", source: "./hello.ts", customDomains: ["docs.example.com"] } } } };\n', ); await planCmd({ ...baseProps(api, stream), output: "table", config }); const out = read(); expect(out).toContain("domain docs.example.com -> hello"); expect(out).not.toContain("CNAME docs.example.com"); }); it("plans no static custom domain on a non-default branch", async () => { const devId = "br-sunny-glade-12345"; const api = new FakeNeonApi(); api.listBranches = async () => [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: false, }, { id: devId, name: "dev", isDefault: false, protected: false, }, ]; api.listEndpoints = async () => [ { id: "ep-fake-1", branchId: BRANCH_ID, type: "read_write", autoscalingLimitMinCu: 0.25, autoscalingLimitMaxCu: 0.25, suspendTimeout: "5m", }, { id: "ep-fake-2", branchId: devId, type: "read_write", autoscalingLimitMinCu: 0.25, autoscalingLimitMaxCu: 0.25, suspendTimeout: "5m", }, ]; const { stream, read } = captureOut(); const config = writeConfig( 'export default { preview: { functions: { hello: { name: "Hello", source: "./hello.ts", customDomains: ["docs.example.com"] } } } };\n', ); await planCmd({ ...baseProps(api, stream), branch: devId, output: "table", config, }); const out = stripAnsi(read()); expect(out).toContain("function hello"); expect(out).not.toContain("domain docs.example.com"); }); it("prints CNAME for an already-registered declared domain on plan", async () => { const api = new FakeNeonApi(); api.seedCustomDomain({ domain: "docs.example.com", entityType: "function", entityId: "hello", cnameTarget: "edge.neon.tech", }); const { stream, read } = captureOut(); const config = writeConfig( 'export default { preview: { functions: { hello: { name: "Hello", source: "./hello.ts", customDomains: ["docs.example.com"] } } } };\n', ); await planCmd({ ...baseProps(api, stream), output: "table", config }); expect(read()).toContain("CNAME docs.example.com -> edge.neon.tech"); }); it("exits 1 when plan hits a blocking custom-domain conflict", async () => { const origExitCode = process.exitCode; process.exitCode = 0; try { const api = new FakeNeonApi(); api.seedCustomDomain({ domain: "docs.example.com", entityType: "bucket", entityId: "uploads", cnameTarget: "edge.neon.tech", }); const { stream, read } = captureOut(); const config = writeConfig( 'export default { preview: { functions: { hello: { name: "Hello", source: "./hello.ts", customDomains: ["docs.example.com"] } } } };\n', ); await planCmd({ ...baseProps(api, stream), output: "table", config, }); const out = read(); expect(out).toContain("customDomain"); expect(out).not.toContain("re-run with --update-existing to apply"); expect(out).not.toContain("CNAME"); expect(process.exitCode).toBe(1); process.exitCode = 0; const jsonOut = captureOut(); await planCmd({ ...baseProps(api, jsonOut.stream), output: "json", config, }); const result = JSON.parse(jsonOut.read()); expect(result.conflicts[0].field).toBe("customDomain"); expect(result.customDomains).toEqual([ { domain: "docs.example.com", slug: "hello" }, ]); expect(process.exitCode).toBe(1); } finally { process.exitCode = origExitCode; } }); it("exits 1 for a blocking domain even when the hostname contains updateexisting", async () => { const origExitCode = process.exitCode; process.exitCode = 0; try { const api = new FakeNeonApi(); api.seedCustomDomain({ domain: "updateexisting.example.com", entityType: "bucket", entityId: "uploads", cnameTarget: "edge.neon.tech", }); const { stream, read } = captureOut(); const config = writeConfig( 'export default { preview: { functions: { hello: { name: "Hello", source: "./hello.ts", customDomains: ["updateexisting.example.com"] } } } };\n', ); await planCmd({ ...baseProps(api, stream), output: "table", config, }); const out = read(); expect(out).toContain("customDomain"); expect(out).not.toContain("re-run with --update-existing to apply"); expect(process.exitCode).toBe(1); } finally { process.exitCode = origExitCode; } }); it("plan exits 0 for a non-domain conflict (no read-write endpoint)", async () => { const origExitCode = process.exitCode; try { const api = new FakeNeonApi(); api.listEndpoints = async () => []; const config = writeConfig( "export default { branch: () => ({ postgres: { computeSettings: { autoscalingLimitMaxCu: 2 } } }) };\n", ); for (const output of ["table", "json", "yaml"] as const) { process.exitCode = 0; const { stream, read } = captureOut(); await planCmd({ ...baseProps(api, stream), output, config, }); const out = read(); if (output === "json") { const result = JSON.parse(out); expect(result.conflicts[0].field).toBe("endpoint"); } else { expect(out).toMatch(/endpoint/); } expect(process.exitCode ?? 0).toBe(0); } } finally { process.exitCode = origExitCode; } }); it("pulls the branch env into a local .env after a successful apply (like link/checkout)", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); // Empty policy: apply provisions nothing, but the bundled env pull still writes the // branch's connection strings to a local .env so the branch is usable for local dev. const config = writeConfig("export default {};\n"); await applyCmd({ ...baseProps(api, stream), output: "table", config, cwd, envPull: true, }); const envPath = join(cwd, ".env.local"); expect(existsSync(envPath)).toBe(true); expect(readFileSync(envPath, "utf8")).toContain("DATABASE_URL="); }); it("pulls Realtime env after provisioning it", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const config = writeConfig( "export default { preview: { realtime: true } };\n", ); await applyCmd({ ...baseProps(api, stream), output: "table", config, cwd, envPull: true, }); const env = readFileSync(join(cwd, ".env.local"), "utf8"); expect(env).toContain( "NEON_REALTIME_URL=wss://realtime.fake.neon.tech/v1", ); expect(env).toContain( "NEON_REALTIME_SECRET=nrt_live_1_cli-config-test", ); }); it("lists the project's branches once per step of an apply with its env pull", async () => { let runtimeBranchListings = 0; const api = new (class extends FakeNeonApi { override async listBranches( projectId: string, ): Promise { runtimeBranchListings += 1; return super.listBranches(projectId); } })(); const { stream } = captureOut(); const config = writeConfig("export default {};\n"); let cliBranchListings = 0; const apiClient = { listProjectBranches: async () => { cliBranchListings += 1; return fakeApiClient.listProjectBranches(); }, }; await applyCmd({ ...baseProps(api, stream), apiClient: apiClient as never, output: "table", config, cwd, envPull: true, }); expect(readFileSync(join(cwd, ".env.local"), "utf8")).toContain( "DATABASE_URL=", ); // The CLI resolves the branch once, for the apply; the env pull reuses it. expect(cliBranchListings).toBe(1); // The apply, the env pull's policy check, and its env fetch. expect(runtimeBranchListings).toBe(3); }); it("skips the env pull after apply when --no-env-pull is set", async () => { const api = new FakeNeonApi(); const { stream } = captureOut(); const config = writeConfig("export default {};\n"); await applyCmd({ ...baseProps(api, stream), output: "table", config, cwd, envPull: false, }); // Nothing written: --no-env-pull leaves the working tree untouched. expect(existsSync(join(cwd, ".env.local"))).toBe(false); expect(existsSync(join(cwd, ".env"))).toBe(false); }); }); /** * A branch that has something worth seeding: Neon Auth and the Data API on, one bucket, one * deployed function, and the branch marked protected. */ class LoadedBranchNeonApi extends FakeNeonApi { override async listBranches(): Promise { return [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: true, }, ]; } override async getNeonAuth(): Promise { return { projectId: "auth-project", jwksUrl: "https://auth.fake.neon.tech/.well-known/jwks.json", baseUrl: "https://auth.fake.neon.tech", }; } override async getNeonDataApi(): Promise { return { url: "https://dataapi.fake.neon.tech", status: "ready" }; } override async listBranchBuckets(): Promise { return [ // Hyphenated on purpose: a real Neon bucket name is not a JS identifier, and a // bare `user-uploads:` key would be a syntax error in the emitted policy. { name: "user-uploads", accessLevel: "public_read" }, { name: "backups", accessLevel: "private" }, ]; } override async listBranchFunctions(): Promise { return [ { id: "fn-resize", slug: "resize", name: "Resize Image", invocationUrl: "https://fake.neon.tech/functions/resize", }, ]; } } describe("config init --services data-api", () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), "neonctl-init-data-api-")); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); /** A branch whose Data API verifies Clerk tokens and has no Neon Auth. */ class ExternalDataApiNeonApi extends FakeNeonApi { override async listBranches(): Promise { return [ { id: BRANCH_ID, name: BRANCH_NAME, isDefault: true, protected: false, }, ]; } override async getNeonAuth(): Promise { return null; } override async getNeonDataApi(): Promise { return { url: "https://dataapi.fake.neon.tech", status: "ready", authProvider: "external", jwksUrl: "https://idp.example.com/.well-known/jwks.json", providerName: "Clerk", }; } } class UnreadableDataApiNeonApi extends LoadedBranchNeonApi { override async getNeonDataApi(): Promise { throw new Error("data api read failed"); } } const linkedProps = (api: NeonApi) => ({ cwd, install: false, services: ["data-api"], apiClient: fakeApiClient as never, projectId: PROJECT_ID, runtimeApi: api, }); it("declares an external Data API the way the branch has it, without Neon Auth", async () => { await initCmd(linkedProps(new ExternalDataApiNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: false,"); expect(source).not.toContain("dataApi: true"); expect(source).toContain('authProvider: "external",'); expect(source).toContain( 'jwksUrl: "https://idp.example.com/.well-known/jwks.json",', ); expect(source).toContain('providerName: "Clerk",'); }); it("declares a Neon Auth Data API as before", async () => { await initCmd(linkedProps(new LoadedBranchNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: true,"); expect(source).toContain("dataApi: true,"); expect(source).not.toContain("authProvider"); }); it("writes the Neon Auth scaffold when no project is linked", async () => { await initCmd({ cwd, install: false, services: ["data-api"] }); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: true,"); expect(source).toContain("dataApi: true,"); }); it("falls back to the Neon Auth scaffold when the branch cannot be read", async () => { await initCmd(linkedProps(new UnreadableDataApiNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: true,"); expect(source).toContain("dataApi: true,"); }); describe("without the auth middleware (standalone `config init`)", () => { let server: Server; let apiHost: string; const requestedPaths: string[] = []; beforeEach(async () => { requestedPaths.length = 0; server = createServer((req, res) => { requestedPaths.push(req.url ?? ""); res.writeHead(200, { "content-type": "application/json" }); res.end( JSON.stringify({ branches: [ { id: BRANCH_ID, name: BRANCH_NAME, default: true }, ], }), ); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve), ); const { port } = server.address() as AddressInfo; apiHost = `http://127.0.0.1:${port}/api/v2`; }); afterEach(async () => { recordCredentialInputs({ apiKeyFlag: "", apiKeyEnv: "", profileEnv: "", profileFlag: "", configDir: "", }); await new Promise((resolve) => server.close(() => resolve())); }); it("reads the linked branch with the API key it was given and declares the external Data API", async () => { recordCredentialInputs({ apiKeyFlag: "napi_standalone", apiKeyEnv: "", profileEnv: "", profileFlag: "", configDir: "", }); await initCmd({ cwd, install: false, services: ["data-api"], projectId: PROJECT_ID, apiHost, configDir: cwd, runtimeApi: new ExternalDataApiNeonApi(), }); expect( requestedPaths.some((path) => path.includes(PROJECT_ID)), ).toBe(true); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain('authProvider: "external",'); expect(source).not.toContain("auth: true,"); expect(source).not.toContain("dataApi: true"); }); it("never starts a login when no credential is on hand, and keeps the Neon Auth scaffold", async () => { recordCredentialInputs({ apiKeyFlag: "", apiKeyEnv: "", profileEnv: "", profileFlag: "", configDir: "", }); await initCmd({ cwd, install: false, services: ["data-api"], projectId: PROJECT_ID, apiHost, configDir: cwd, runtimeApi: new ExternalDataApiNeonApi(), }); expect(requestedPaths).toEqual([]); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: true,"); expect(source).toContain("dataApi: true,"); }); }); }); describe("config init --from-branch", () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), "neonctl-from-branch-")); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); const seedProps = (api: NeonApi) => ({ cwd, install: false, fromBranch: true, apiClient: fakeApiClient as never, projectId: PROJECT_ID, runtimeApi: api, }); it("declares the services the branch actually has", async () => { await initCmd(seedProps(new LoadedBranchNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("auth: true,"); expect(source).toContain("dataApi: true,"); expect(source).toContain('"user-uploads": { access: "public_read" },'); expect(source).toContain('backups: { access: "private" },'); expect(source).toContain( `Seeded by \`neon config init --from-branch\` from ${BRANCH_NAME}`, ); }); it("lists deployed functions as a comment, since source cannot round-trip", async () => { await initCmd(seedProps(new LoadedBranchNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("// main has 1 deployed function."); expect(source).toContain( '// resize: { name: "Resize Image", source: "./resize.ts" },', ); // Commented out, so the policy does not declare a function with no source on disk. expect(source).not.toMatch(/^\s+functions: \{/m); // And no handler is invented for it. expect(existsSync(join(cwd, "resize.ts"))).toBe(false); }); it("reports `protected` as a comment instead of declaring it", async () => { await initCmd(seedProps(new LoadedBranchNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("// main is protected on Neon."); expect(source).not.toMatch(/protected: true/); }); it("carries the branch's compute settings into the policy closure", async () => { await initCmd(seedProps(new FakeNeonApi())); const source = readFileSync(join(cwd, "neon.ts"), "utf8"); expect(source).toContain("branch: () => ({"); expect(source).toContain("autoscalingLimitMinCu: 0.25,"); expect(source).toContain('suspendTimeout: "5m",'); // The AI Gateway has no readable per-branch state, so it is never declared — only // mentioned in the header comment as something to add by hand. expect(source).not.toMatch(/^\s+aiGateway: true,/m); expect(source).toContain( "// `aiGateway: true` if the policy should declare it.", ); }); // A seeded policy that doesn't load is worse than no seed: the emitted values come from // live state, so a mis-serialized compute setting or bucket name only fails when // `config plan` imports the file. Load it through the CLI's own loader. // // The temp project lives under the package's `node_modules` so jiti resolves // `@neon/config/v1` by walking up as it would in a user's project, and so a directory // left by a crashed run can never be committed. it("writes a policy that loads and resolves", async () => { const packageRoot = fileURLToPath(new URL("../../", import.meta.url)); const project = mkdtempSync( join(packageRoot, "node_modules", ".neon-from-branch-"), ); try { await initCmd({ ...seedProps(new LoadedBranchNeonApi()), cwd: project, }); const { config } = await loadConfigFromFile({ path: join(project, "neon.ts"), }); const resolved = resolveConfig(config, { name: "preview", exists: false, isDefault: false, }); expect(resolved.authEnabled).toBe(true); expect(resolved.dataApiEnabled).toBe(true); expect(resolved.preview?.buckets).toEqual([ { name: "user-uploads", access: "public_read" }, { name: "backups", access: "private" }, ]); expect(resolved.preview?.functions).toEqual([]); expect(resolved.postgres?.computeSettings).toMatchObject({ autoscalingLimitMinCu: 0.25, suspendTimeout: "5m", }); // Read but deliberately not declared. expect(resolved.protected).toBeUndefined(); } finally { rmSync(project, { recursive: true, force: true }); } }); it("leaves an existing neon.ts untouched without calling the API", async () => { const original = "export default { auth: true };\n"; writeFileSync(join(cwd, "neon.ts"), original); // `runtimeApi` is omitted: a read would have to build a real adapter and fail. await initCmd({ cwd, install: false, fromBranch: true, apiClient: fakeApiClient as never, projectId: PROJECT_ID, }); expect(readFileSync(join(cwd, "neon.ts"), "utf8")).toBe(original); }); it("fails with guidance when no project is resolved", async () => { await expect( initCmd({ cwd, install: false, fromBranch: true }), ).rejects.toThrow(/--from-branch needs a project/); expect(existsSync(join(cwd, "neon.ts"))).toBe(false); }); }); /** First `listBranches` is the CLI GA warning; apply lists again afterwards. */ class ApplyWarningListFailsNeonApi extends FakeNeonApi { private listCalls = 0; override async listBranches( projectId: string, ): Promise { this.listCalls += 1; if (this.listCalls === 1) { throw new Error("warning list failed"); } return super.listBranches(projectId); } } describe("applyPolicyOnCreate", () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), "neonctl-create-")); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); it("applies the neon.ts policy to the new branch when one is present", async () => { const api = new FakeNeonApi(); writeFileSync(join(cwd, "neon.ts"), "export default { auth: {} };\n"); await applyPolicyOnCreate({ projectId: PROJECT_ID, branchId: BRANCH_ID, runtimeApi: api, cwd, }); expect(api.enableNeonAuthCalls).toHaveLength(1); }); it("is a no-op when there is no neon.ts on the path", async () => { const api = new FakeNeonApi(); await applyPolicyOnCreate({ projectId: PROJECT_ID, branchId: BRANCH_ID, runtimeApi: api, cwd, // empty temp dir, no neon.ts }); expect(api.enableNeonAuthCalls).toHaveLength(0); }); it("fails before applying when --env names a missing file", async () => { const api = new FakeNeonApi(); writeFileSync(join(cwd, "neon.ts"), "export default { auth: {} };\n"); await expect( applyPolicyOnCreate({ projectId: PROJECT_ID, branchId: BRANCH_ID, runtimeApi: api, cwd, env: join(cwd, "missing.env"), }), ).rejects.toThrow(/Env file not found/); expect(api.enableNeonAuthCalls).toEqual([]); }); it("loads --env into process.env before evaluating function env", async () => { const api = new FakeNeonApi(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync( join(cwd, "neon.ts"), `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )}, env: { resendApiKey: process.env.RESEND_API_KEY } } } } };\n`, ); const envFile = join(cwd, ".env.deploy"); writeFileSync(envFile, "RESEND_API_KEY=re_from_file\n"); delete process.env.RESEND_API_KEY; try { await applyPolicyOnCreate({ projectId: PROJECT_ID, branchId: BRANCH_ID, runtimeApi: api, cwd, env: envFile, }); } finally { delete process.env.RESEND_API_KEY; } expect(api.deployBranchFunctionCalls).toHaveLength(1); expect(api.deployBranchFunctionCalls[0].input.environment).toEqual({ resendApiKey: "re_from_file", }); }); it("still applies when the GA warning list fails", async () => { const api = new ApplyWarningListFailsNeonApi(); writeFileSync(join(cwd, "neon.ts"), "export default { auth: {} };\n"); await applyPolicyOnCreate({ projectId: PROJECT_ID, branchId: BRANCH_ID, runtimeApi: api, cwd, }); expect(api.enableNeonAuthCalls).toHaveLength(1); }); }); const NEW_BRANCH_ID = "br-fresh-dawn-98765"; const NEW_BRANCH_NAME = "feature-policy"; /** Tunes compute only on a *new* branch — the "configure new branches" policy shape. */ const NEW_BRANCH_POLICY = "export default { branch: (branch) => (branch.exists ? {} : " + "{ postgres: { computeSettings: { autoscalingLimitMaxCu: 2 } } }) };\n"; /** Enables a service, which can only be provisioned once the branch exists. */ const AUTH_POLICY = "export default { auth: {} };\n"; /** What Neon rejects a plan-gated compute setting with (seen live on a Free-plan project). */ const SUSPEND_REJECTED = 'HTTP 412. Neon API said: "modifying the suspend interval is not permitted on this account".'; const AUTH_REJECTED = "Neon Auth is not available on this account"; const newBranchEndpoint = ( settings?: ComputeSettings, ): NeonEndpointSnapshot => ({ id: "ep-fresh-1", branchId: NEW_BRANCH_ID, type: "read_write", autoscalingLimitMinCu: settings?.autoscalingLimitMinCu ?? 0.25, autoscalingLimitMaxCu: settings?.autoscalingLimitMaxCu ?? 0.25, suspendTimeout: settings?.suspendTimeout ?? "5m", }); /** * Branch creation succeeds and, like Neon, the branch comes up with whatever the create call * carried. Records the create input and any compute update so a test can tell "the policy was * applied at creation" apart from "applied afterwards" apart from "nothing happened". */ class CreateBranchNeonApi extends FakeNeonApi { readonly createBranchCalls: CreateBranchInput[] = []; readonly updateEndpointCalls: { endpointId: string; settings: ComputeSettings; }[] = []; /** The branch `createBranch` made, so subsequent listings see it like Neon would. */ private created: NeonBranchSnapshot | undefined; /** Compute the branch actually came up on — the create call's, unless it was ignored. */ private compute: ComputeSettings | undefined; /** Whether the create call's settings take effect, as they do on Neon. */ protected get appliesCreateSettings(): boolean { return true; } override async listBranches( projectId: string, ): Promise { const branches = await super.listBranches(projectId); return this.created ? [...branches, this.created] : branches; } override async createBranch( projectId: string, input: CreateBranchInput, ): Promise<{ branch: NeonBranchSnapshot; endpoints: NeonEndpointSnapshot[]; }> { void projectId; this.createBranchCalls.push(input); const applied = this.appliesCreateSettings; this.created = { id: NEW_BRANCH_ID, name: input.name, isDefault: false, protected: applied && input.protected === true, parentId: input.parentId, ...(applied && input.expiresAt ? { expiresAt: input.expiresAt } : {}), }; if (applied) this.compute = input.computeSettings; return { branch: this.created, endpoints: [newBranchEndpoint(this.compute)], }; } override async listEndpoints( projectId: string, ): Promise { return [ ...(await super.listEndpoints(projectId)), newBranchEndpoint(this.compute), ]; } override async updateEndpoint( projectId: string, endpointId: string, settings: ComputeSettings, ): Promise { void projectId; this.updateEndpointCalls.push({ endpointId, settings }); this.compute = settings; return newBranchEndpoint(settings); } } /** Accepts the create-time settings but ignores them, leaving drift for the push to fix. */ class IgnoresCreateSettingsNeonApi extends CreateBranchNeonApi { protected override get appliesCreateSettings(): boolean { return false; } } /** Creation succeeds, then Neon refuses to provision the service the policy declares. */ class CreateBranchThenRejectNeonApi extends CreateBranchNeonApi { override async enableNeonAuth(): Promise { throw new Error(AUTH_REJECTED); } } /** Third `listBranches` is the CLI GA warning after create+push already listed. */ class WarningListFailsNeonApi extends CreateBranchNeonApi { private listCalls = 0; override async listBranches( projectId: string, ): Promise { this.listCalls += 1; if (this.listCalls >= 3) { throw new Error("warning list failed"); } return super.listBranches(projectId); } } class PartialCreateThenWarningListFailsNeonApi extends CreateBranchThenRejectNeonApi { private listCalls = 0; override async listBranches( projectId: string, ): Promise { this.listCalls += 1; if (this.listCalls >= 3) { throw new Error("warning list failed"); } return super.listBranches(projectId); } } /** Neon rejects a setting the create call carried, so no branch is created at all. */ class RejectCreateNeonApi extends CreateBranchNeonApi { override async createBranch(): Promise<{ branch: NeonBranchSnapshot; endpoints: NeonEndpointSnapshot[]; }> { throw new Error(SUSPEND_REJECTED); } } describe("createBranchFromPolicyOnCheckout", () => { let cwd: string; beforeEach(() => { cwd = mkdtempSync(join(tmpdir(), "neonctl-create-policy-")); }); afterEach(() => { rmSync(cwd, { recursive: true, force: true }); }); it("creates the branch with the policy's settings on the create call", async () => { const api = new CreateBranchNeonApi(); writeFileSync(join(cwd, "neon.ts"), NEW_BRANCH_POLICY); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); expect(api.createBranchCalls).toEqual([ { name: NEW_BRANCH_NAME, // The policy names no parent, so the branch comes off the project default. parentId: BRANCH_ID, computeSettings: { autoscalingLimitMaxCu: 2 }, }, ]); // Applied by the creation itself, so nothing follows it. expect(api.updateEndpointCalls).toEqual([]); }); it("falls back to updating the branch when the create call's settings don't take", async () => { const api = new IgnoresCreateSettingsNeonApi(); writeFileSync(join(cwd, "neon.ts"), NEW_BRANCH_POLICY); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); expect(api.updateEndpointCalls).toEqual([ { endpointId: "ep-fresh-1", settings: { autoscalingLimitMaxCu: 2 }, }, ]); }); it("fails outright, creating nothing, when Neon rejects a setting", async () => { // Settings ride along on the create call, so a rejected one fails before a branch // exists — no id to hand back, nothing half-configured to explain. const api = new RejectCreateNeonApi(); writeFileSync(join(cwd, "neon.ts"), NEW_BRANCH_POLICY); await expect( createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }), ).rejects.toThrow(SUSPEND_REJECTED); }); it("returns the created branch id and the reason when a service fails", async () => { // Services are provisioned against an existing branch, so this failure still lands // after creation. The id must come back to the caller (checkout pins it) rather than // being lost with the thrown error. const api = new CreateBranchThenRejectNeonApi(); writeFileSync(join(cwd, "neon.ts"), AUTH_POLICY); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created?.branchId).toBe(NEW_BRANCH_ID); expect(created?.policyFailure).toContain(AUTH_REJECTED); }); it("keeps the created branch id when the GA warning list fails", async () => { const api = new WarningListFailsNeonApi(); writeFileSync(join(cwd, "neon.ts"), NEW_BRANCH_POLICY); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); }); it("keeps the original policy failure when the GA warning list fails", async () => { const api = new PartialCreateThenWarningListFailsNeonApi(); writeFileSync(join(cwd, "neon.ts"), AUTH_POLICY); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created?.branchId).toBe(NEW_BRANCH_ID); expect(created?.policyFailure).toContain(AUTH_REJECTED); }); it("returns null when there is no neon.ts on the path", async () => { const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: new CreateBranchNeonApi(), cwd, // empty temp dir, no neon.ts }); expect(created).toBeNull(); }); it("fails before creating a branch when --env names a missing file", async () => { const api = new CreateBranchNeonApi(); writeFileSync(join(cwd, "neon.ts"), NEW_BRANCH_POLICY); await expect( createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, env: join(cwd, "missing.env"), }), ).rejects.toThrow(/Env file not found/); expect(api.createBranchCalls).toEqual([]); }); it("loads --env into process.env before evaluating function env", async () => { const api = new CreateBranchNeonApi(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync( join(cwd, "neon.ts"), `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )}, env: { resendApiKey: process.env.RESEND_API_KEY } } } } };\n`, ); const envFile = join(cwd, ".env.deploy"); writeFileSync(envFile, "RESEND_API_KEY=re_from_file\n"); delete process.env.RESEND_API_KEY; try { const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, env: envFile, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); } finally { delete process.env.RESEND_API_KEY; } expect(api.deployBranchFunctionCalls).toHaveLength(1); expect(api.deployBranchFunctionCalls[0].input.environment).toEqual({ resendApiKey: "re_from_file", }); }); it("deploys the function on checkout without registering static customDomains", async () => { const api = new CreateBranchNeonApi(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync( join(cwd, "neon.ts"), `export default { preview: { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )}, customDomains: ["docs.example.com"] } } } };\n`, ); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); expect(api.deployBranchFunctionCalls).toHaveLength(1); expect( await api.listBranchCustomDomains(PROJECT_ID, NEW_BRANCH_ID), ).toEqual([]); }); it("registers a tuning custom domain on checkout of a non-default branch", async () => { const api = new CreateBranchNeonApi(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync( join(cwd, "neon.ts"), `export default { preview: { functions: { hello: { name: "Hello", source: ${JSON.stringify(source)}, customDomains: ["docs.example.com"], }, }, }, branch: (branch) => ({ preview: { functions: { hello: branch.isDefault ? {} : { customDomains: ["preview.example.com"] }, }, }, }), }; `, ); const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); expect(api.deployBranchFunctionCalls).toHaveLength(1); expect( await api.listBranchCustomDomains(PROJECT_ID, NEW_BRANCH_ID), ).toEqual([ { domain: "preview.example.com", entityType: "function", entityId: "hello", cnameTarget: "custom-domains.fake.neon.tech", }, ]); }); it("warns for preview tuning gated on parentId during checkout create", async () => { const api = new CreateBranchNeonApi(); const source = join(cwd, "hello.ts"); writeFileSync( source, "export default { fetch() { return new Response('ok'); } };\n", ); writeFileSync( join(cwd, "neon.ts"), `export default { functions: { hello: { name: 'Hello', source: ${JSON.stringify( source, )} } }, branch: (branch) => branch.parentId ? { preview: { functions: { hello: { runtime: 'nodejs24' } } } } : {} };\n`, ); const stderrChunks: string[] = []; const origErr = process.stderr.write.bind(process.stderr); process.stderr.write = ((chunk: string | Uint8Array) => { stderrChunks.push(chunk.toString()); return origErr(chunk); }) as typeof process.stderr.write; try { const created = await createBranchFromPolicyOnCheckout({ projectId: PROJECT_ID, branchName: NEW_BRANCH_NAME, runtimeApi: api, cwd, }); expect(created).toEqual({ branchId: NEW_BRANCH_ID }); expect(stderrChunks.join("")).toContain( "branch.preview.functions → branch.functions", ); } finally { process.stderr.write = origErr; } }); });