neonctl 2.22.0 → 2.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -62,21 +62,157 @@ For information about obtaining an Neon API key, see [Authentication](https://ap
62
62
 
63
63
  The Neon CLI supports autocompletion, which you can configure in a few easy steps. See [Neon CLI commands — completion](https://neon.tech/docs/reference/cli-completion) for instructions.
64
64
 
65
+ ## Linking a project
66
+
67
+ `neonctl link` is a Vercel-style command that binds the current directory to a Neon project. It picks (or creates) an organization, picks (or creates) a project, resolves the project's default branch, and writes a `.neon` file with `{ "orgId", "projectId", "branchId" }`. Subsequent commands run in this directory (or any sub-directory) automatically pick up that context.
68
+
69
+ There are three modes:
70
+
71
+ **Interactive (default)** — guided prompts for humans:
72
+
73
+ ```bash
74
+ $ neonctl link
75
+ ? Which organization would you like to link? › Personal Org (org-abc123)
76
+ ? Which project would you like to link? › + Create new project
77
+ ? Name for the new project: › my-app
78
+ ? Which region should the new project run in? › AWS US East (Ohio) (aws-us-east-2)
79
+ Created project polished-snowflake-12345678 ("my-app") in aws-us-east-2.
80
+ Linked .neon:
81
+ orgId: org-abc123
82
+ projectId: polished-snowflake-12345678
83
+ branchId: br-main-branch-87654321
84
+ ```
85
+
86
+ **Non-interactive (flags or `--params` JSON)** — for scripts and CI:
87
+
88
+ ```bash
89
+ # Link to an existing project
90
+ neonctl link --org-id org-abc123 --project-id polished-snowflake-12345678
91
+
92
+ # Create a new project and link
93
+ neonctl link --org-id org-abc123 --project-name my-app --region-id aws-us-east-2
94
+
95
+ # Same payload, one JSON blob
96
+ neonctl link --params '{"orgId":"org-abc123","projectName":"my-app","regionId":"aws-us-east-2"}'
97
+ ```
98
+
99
+ **Agent mode (`--agent`)** — a JSON state machine designed for AI coding assistants. Each invocation returns a single JSON object with a `status` discriminator describing the next step, the available options, and the exact follow-up command to run.
100
+
101
+ ```bash
102
+ $ neonctl link --agent
103
+ {
104
+ "status": "needs_org",
105
+ "instruction": "Ask the user which of these 2 organizations they want to link the current directory to. After they pick one, re-run the next_command_template with the chosen --org-id value.",
106
+ "options": [
107
+ { "id": "org-abc123", "name": "Personal Org" },
108
+ { "id": "org-team", "name": "Team Org" }
109
+ ],
110
+ "next_command_template": "neonctl link --agent --org-id <org_id>"
111
+ }
112
+
113
+ $ neonctl link --agent --org-id org-abc123
114
+ {
115
+ "status": "needs_project",
116
+ "instruction": "Ask the user whether to link to one of these 1 existing projects (use next_command_template with --project-id) or create a new project (use create_option.next_command_template).",
117
+ "options": [
118
+ { "id": "polished-snowflake-12345678", "name": "my-app" }
119
+ ],
120
+ "create_option": {
121
+ "instruction": "To create a new project, ask the user for a project name. The region can be omitted to receive a follow-up needs_project_details response that lists available regions.",
122
+ "next_command_template": "neonctl link --agent --org-id org-abc123 --project-name <name> --region-id <region_id>"
123
+ },
124
+ "next_command_template": "neonctl link --agent --org-id org-abc123 --project-id <project_id>"
125
+ }
126
+
127
+ $ neonctl link --agent --org-id org-abc123 --project-id polished-snowflake-12345678
128
+ {
129
+ "status": "linked",
130
+ "context_file": "/path/to/cwd/.neon",
131
+ "context": {
132
+ "orgId": "org-abc123",
133
+ "projectId": "polished-snowflake-12345678",
134
+ "branchId": "br-main-branch-87654321"
135
+ },
136
+ "project": { "id": "polished-snowflake-12345678" },
137
+ "message": "Linked /path/to/cwd/.neon to project polished-snowflake-12345678 (org org-abc123) on branch br-main-branch-87654321."
138
+ }
139
+ ```
140
+
141
+ The agent flow also handles project creation. If the agent sends `--project-name` without `--region-id`, the next response is `needs_project_details` with the list of supported regions.
142
+
143
+ **Organization-scoped API keys** (those created at the organization level rather than the user level) cannot list user organizations or call the regions endpoint. `link` handles this transparently:
144
+
145
+ - If the API key is org-scoped and at least one project already exists in the org, the CLI auto-detects the `org_id` from the first project. In interactive mode it prints an informational message; in `--agent` mode it skips straight to `needs_project`.
146
+ - If the API key is org-scoped and no projects exist yet, `--agent` returns a `needs_org` response with `options: []` and an instruction telling the user to find their org ID in the Neon Console. Interactive mode prints an error pointing to `--org-id`.
147
+ - When the regions endpoint is not allowed, `link` falls back to a built-in static region list.
148
+
149
+ **Agent error contract**: any unexpected failure in `--agent` mode is reported as JSON to stdout with exit code 1, so agents can always parse the response:
150
+
151
+ ```json
152
+ {
153
+ "status": "error",
154
+ "code": "CLIENT_ERROR",
155
+ "message": "user has no access to projects"
156
+ }
157
+ ```
158
+
159
+ `link` is a thin wrapper around `set-context`: both write to the same `.neon` file via a shared `applyContext` helper, so anything `link` can write, `set-context` can write too (including the newly-supported `--branch-id` flag).
160
+
161
+ ### checkout
162
+
163
+ `checkout [id|name]` pins a branch in the local context so subsequent commands target it — it's a focused helper over `set-context` for the common "switch the branch I'm working on" case. It resolves the branch (by name or id) against the project, then **heals** the `.neon` file: it always (re)writes `projectId`, `branchId`, and `orgId` (when the project has one), so a `.neon` that was missing fields or drifted ends up complete and consistent. When `orgId` isn't already known (from `--org-id` or the existing `.neon`), it's looked up from the project itself.
164
+
165
+ The branch argument is **optional**: run `neonctl checkout` with no branch in an interactive terminal to fetch the project's branches and pick one from a list. In a non-interactive context (CI or no TTY), a branch must be passed explicitly.
166
+
167
+ Branch **id vs name** is detected automatically (a `br-…` value is treated as an id):
168
+
169
+ - **id** — matched strictly by id. A non-existent id is a hard "not found" error (ids are server-assigned, so checkout never creates one).
170
+ - **name** — matched by name. If the name doesn't exist, in an interactive terminal `checkout` offers to **create** it (equivalent to `neonctl branch create --name <name>`: branched from the project's default branch with a read-write compute), then checks it out. In a non-interactive context a missing name is the usual "not found" error.
171
+
172
+ The project is resolved through the standard neonctl chain, each entry winning over the next:
173
+
174
+ 1. `--project-id <id>` flag
175
+ 2. `projectId` from the closest `.neon` file (found by walking up from the current directory — see "Where `.neon` lives" below)
176
+ 3. If still unresolved and the API key maps to exactly one project, that project is auto-detected (same behaviour as `branches` and `connection-string`)
177
+
178
+ If none of those resolve a project, `checkout` prints a telling error explaining the chain above. In an interactive terminal it then offers to run `neonctl link` in the current folder so you can pick (or create) a project on the spot; once linked, it continues and pins the requested branch. In non-interactive contexts (CI or no TTY) it exits with a non-zero code and the same guidance instead of prompting.
179
+
180
+ The resolved branch id is then written to the same `.neon` file that `link` and `set-context` use:
181
+
182
+ ```bash
183
+ $ neonctl checkout main --project-id polished-snowflake-12345678
184
+ INFO: Checked out branch br-main-branch-87654321 on project polished-snowflake-12345678. Updated /path/to/cwd/.neon.
185
+
186
+ $ cat .neon
187
+ {
188
+ "orgId": "org-abc123",
189
+ "projectId": "polished-snowflake-12345678",
190
+ "branchId": "br-main-branch-87654321"
191
+ }
192
+ ```
193
+
194
+ **Where `.neon` lives**: `link` (and `set-context`) write `.neon` into the **current working directory** by default. If an existing `.neon` is found in any parent directory, that file is reused — so commands run from a sub-directory of a linked project still pick up the project's context. To pin the location explicitly, pass `--context-file <path>`.
195
+
196
+ **`.gitignore` scaffolding**: when `.neon` is **created** for the first time, the CLI also makes sure a `.gitignore` sits alongside it listing `.neon`. If `.gitignore` doesn't exist it's created with a single `.neon` line; if it does exist, `.neon` is appended only when missing (no duplicates, your other entries are left alone). On subsequent updates to an existing `.neon`, `.gitignore` is left untouched — so if you deliberately un-ignore `.neon` (e.g. to commit shared context), the entry is not re-added on every command.
197
+
65
198
  ## Commands
66
199
 
67
- | Command | Subcommands | Description |
68
- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ---------------------------- |
69
- | [auth](https://neon.com/docs/reference/cli-auth) | | Authenticate |
70
- | [projects](https://neon.com/docs/reference/cli-projects) | `list`, `create`, `update`, `delete`, `get` | Manage projects |
71
- | [ip-allow](https://neon.com/docs/reference/cli-ip-allow) | `list`, `add`, `remove`, `reset` | Manage IP Allow |
72
- | [me](https://neon.com/docs/reference/cli-me) | | Show current user |
73
- | [branches](https://neon.com/docs/reference/cli-branches) | `list`, `create`, `rename`, `add-compute`, `set-default`, `set-expiration`, `delete`, `get` | Manage branches |
74
- | [databases](https://neon.com/docs/reference/cli-databases) | `list`, `create`, `delete` | Manage databases |
75
- | [roles](https://neon.com/docs/reference/cli-roles) | `list`, `create`, `delete` | Manage roles |
76
- | [operations](https://neon.com/docs/reference/cli-operations) | `list` | Manage operations |
77
- | [connection-string](https://neon.com/docs/reference/cli-connection-string) | | Get connection string |
78
- | [set-context](https://neon.com/docs/reference/cli-set-context) | | Set context for session |
79
- | [completion](https://neon.com/docs/reference/cli-completion) | | Generate a completion script |
200
+ | Command | Subcommands | Description |
201
+ | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ------------------------------ |
202
+ | [auth](https://neon.com/docs/reference/cli-auth) | | Authenticate |
203
+ | [projects](https://neon.com/docs/reference/cli-projects) | `list`, `create`, `update`, `delete`, `get` | Manage projects |
204
+ | [ip-allow](https://neon.com/docs/reference/cli-ip-allow) | `list`, `add`, `remove`, `reset` | Manage IP Allow |
205
+ | [me](https://neon.com/docs/reference/cli-me) | | Show current user |
206
+ | [branches](https://neon.com/docs/reference/cli-branches) | `list`, `create`, `rename`, `add-compute`, `set-default`, `set-expiration`, `delete`, `get` | Manage branches |
207
+ | [databases](https://neon.com/docs/reference/cli-databases) | `list`, `create`, `delete` | Manage databases |
208
+ | [roles](https://neon.com/docs/reference/cli-roles) | `list`, `create`, `delete` | Manage roles |
209
+ | [operations](https://neon.com/docs/reference/cli-operations) | `list` | Manage operations |
210
+ | [connection-string](https://neon.com/docs/reference/cli-connection-string) | | Get connection string |
211
+ | psql | | Connect to a database via psql |
212
+ | [set-context](https://neon.com/docs/reference/cli-set-context) | | Set context for session |
213
+ | checkout | | Pin a branch in `.neon` |
214
+ | [link](https://neon.com/docs/reference/cli-link) | | Link a directory to a project |
215
+ | [completion](https://neon.com/docs/reference/cli-completion) | | Generate a completion script |
80
216
 
81
217
  ## Global options
82
218
 
@@ -142,17 +278,19 @@ Global options are supported with any Neon CLI command.
142
278
 
143
279
  ## Contribute
144
280
 
281
+ This repo uses [pnpm](https://pnpm.io). The required version is pinned in `.tool-versions` and `package.json`'s `packageManager` field. The simplest way to get the right version is [mise](https://mise.jdx.dev): `mise install` reads `.tool-versions` and installs Node and pnpm. Alternatives: `npm install -g pnpm@9.15.9`, or [Corepack](https://nodejs.org/api/corepack.html) (`corepack enable pnpm`).
282
+
145
283
  To run the CLI locally, execute the build command after making changes:
146
284
 
147
285
  ```shell
148
- bun install
149
- bun run build
286
+ pnpm install
287
+ pnpm run build
150
288
  ```
151
289
 
152
290
  To develop continuously:
153
291
 
154
292
  ```shell
155
- bun run watch
293
+ pnpm run watch
156
294
  ```
157
295
 
158
296
  To run commands from the local build, replace the `neonctl` command with `node dist`; for example:
@@ -160,3 +298,7 @@ To run commands from the local build, replace the `neonctl` command with `node d
160
298
  ```shell
161
299
  node dist branches --help
162
300
  ```
301
+
302
+ ## Releasing
303
+
304
+ Maintainers: see [`RELEASING.md`](./RELEASING.md) for the two-stage publish flow.
@@ -0,0 +1,249 @@
1
+ import { EndpointType } from '@neondatabase/api-client';
2
+ import { isAxiosError } from 'axios';
3
+ import prompts from 'prompts';
4
+ import { retryOnLock } from '../api.js';
5
+ import { applyContext, readContextFile } from '../context.js';
6
+ import { isCi } from '../env.js';
7
+ import { log } from '../log.js';
8
+ import { fillSingleProject } from '../utils/enrichers.js';
9
+ import { looksLikeBranchId } from '../utils/formats.js';
10
+ import { handler as linkHandler } from './link.js';
11
+ // The positional is optional: omitting it in an interactive terminal opens a
12
+ // branch picker. In non-interactive contexts a missing branch is an error.
13
+ export const command = 'checkout [id|name]';
14
+ export const describe = 'Pin a branch in the local context (.neon) so subsequent commands target it';
15
+ export const builder = (argv) => argv
16
+ .usage('$0 checkout [id|name] [options]')
17
+ .positional('id', {
18
+ describe: 'Branch name or id to check out. Omit to pick interactively from the list of branches.',
19
+ type: 'string',
20
+ })
21
+ .options({
22
+ 'project-id': {
23
+ describe: 'Project ID',
24
+ type: 'string',
25
+ },
26
+ })
27
+ .example([
28
+ [
29
+ '$0 checkout',
30
+ 'Pick a branch interactively from the project in the closest .neon file',
31
+ ],
32
+ [
33
+ '$0 checkout main',
34
+ 'Pin the branch named "main" in the closest .neon file',
35
+ ],
36
+ [
37
+ '$0 checkout br-cool-snow-12345678 --project-id project-id-123',
38
+ 'Pin a branch by id for an explicit project',
39
+ ],
40
+ ]);
41
+ export const handler = async (props) => {
42
+ // Branch listing is project-scoped, so `projectId` is the only thing
43
+ // `checkout` actually needs. Resolve it through the standard chain
44
+ // (--project-id flag > .neon file > single-project auto-detect); when
45
+ // nothing resolves, fall back to an interactive `neonctl link`.
46
+ const projectId = await resolveProjectId(props);
47
+ const branchId = await resolveBranchId(props, projectId);
48
+ const orgId = await resolveOrgId(props, projectId);
49
+ // `checkout` is a thin helper over `set-context`. It fully "heals" the
50
+ // context file: it always (re)writes `projectId`, `branchId`, and `orgId`
51
+ // (when the project has one) so a `.neon` that drifted or was missing fields
52
+ // ends up complete and consistent after checkout.
53
+ applyContext(props.contextFile, {
54
+ projectId,
55
+ ...(orgId ? { orgId } : {}),
56
+ branchId,
57
+ });
58
+ log.info('Checked out branch %s on project %s%s. Updated %s.', branchId, projectId, orgId ? ` (org ${orgId})` : '', props.contextFile);
59
+ };
60
+ /**
61
+ * Resolve the branch id to check out.
62
+ *
63
+ * - Branch **id** (`br-…`): looked up by id. A non-existent id is a hard "not
64
+ * found" error — we never offer to create one, since ids are server-assigned.
65
+ * - Branch **name**: looked up by name. If it doesn't exist, in an interactive
66
+ * terminal we offer to create it (like `neonctl branch create --name <name>`);
67
+ * in a non-interactive context it's the usual "not found" error.
68
+ * - **Omitted**: open an interactive picker listing the project's branches (TTY
69
+ * only); in a non-interactive context a missing branch is a hard error.
70
+ */
71
+ const resolveBranchId = async (props, projectId) => {
72
+ const branches = (await props.apiClient.listProjectBranches({ projectId }))
73
+ .data.branches;
74
+ if (!props.id) {
75
+ return pickBranchInteractively(branches, projectId);
76
+ }
77
+ const ref = props.id;
78
+ // A `br-…` value is an id; match strictly by id and never offer to create.
79
+ if (looksLikeBranchId(ref)) {
80
+ const byId = branches.find((b) => b.id === ref);
81
+ if (byId) {
82
+ return byId.id;
83
+ }
84
+ throw new Error(notFoundMessage(ref, branches));
85
+ }
86
+ const byName = branches.find((b) => b.name === ref);
87
+ if (byName) {
88
+ return byName.id;
89
+ }
90
+ // Name not found: offer to create it interactively, mirroring `branch create`.
91
+ if (isCi() || !process.stdout.isTTY) {
92
+ throw new Error(notFoundMessage(ref, branches));
93
+ }
94
+ log.error(notFoundMessage(ref, branches));
95
+ const { create } = await prompts({
96
+ type: 'confirm',
97
+ name: 'create',
98
+ message: `Branch "${ref}" does not exist. Create it now?`,
99
+ initial: true,
100
+ });
101
+ if (!create) {
102
+ throw new Error(`Aborted: branch "${ref}" was not found and not created.`);
103
+ }
104
+ return createBranch(props, projectId, ref, branches);
105
+ };
106
+ const notFoundMessage = (ref, branches) => `Branch ${ref} not found.\nAvailable branches: ${branches
107
+ .map((b) => b.name)
108
+ .join(', ')}`;
109
+ const pickBranchInteractively = async (branches, projectId) => {
110
+ if (isCi() || !process.stdout.isTTY) {
111
+ throw new Error('No branch specified. Pass a branch name or id (e.g. `neonctl checkout main`), ' +
112
+ 'or run interactively to pick one from a list.');
113
+ }
114
+ if (branches.length === 0) {
115
+ throw new Error(`No branches found for project ${projectId}.`);
116
+ }
117
+ const defaultIndex = Math.max(0, branches.findIndex((b) => b.default));
118
+ const { branchId } = await prompts({
119
+ type: 'select',
120
+ name: 'branchId',
121
+ message: 'Which branch would you like to check out?',
122
+ choices: branches.map((b) => ({
123
+ title: `${b.default ? '✱ ' : ''}${b.name} (${b.id})`,
124
+ value: b.id,
125
+ })),
126
+ initial: defaultIndex,
127
+ });
128
+ if (!branchId) {
129
+ throw new Error('Aborted: no branch selected.');
130
+ }
131
+ return branchId;
132
+ };
133
+ /**
134
+ * Create a branch with the same defaults as `neonctl branch create --name <name>`:
135
+ * branched from the project's default branch with a read-write compute endpoint.
136
+ */
137
+ const createBranch = async (props, projectId, name, branches) => {
138
+ const defaultBranch = branches.find((b) => b.default);
139
+ if (!defaultBranch) {
140
+ throw new Error('No default branch found');
141
+ }
142
+ const { data } = await retryOnLock(() => props.apiClient.createProjectBranch(projectId, {
143
+ branch: { name, parent_id: defaultBranch.id },
144
+ endpoints: [{ type: EndpointType.ReadWrite }],
145
+ }));
146
+ if (defaultBranch.protected) {
147
+ log.warning('The parent branch is protected; a unique role password has been generated for the new branch.');
148
+ }
149
+ log.info('Created branch %s (%s).', data.branch.name, data.branch.id);
150
+ return data.branch.id;
151
+ };
152
+ /**
153
+ * Resolve the org id to heal into the context file.
154
+ *
155
+ * Prefer an org id we already know (from `--org-id`, the `.neon` file, or a
156
+ * freshly-run `link`). Otherwise look it up from the project itself so the
157
+ * `.neon` file ends up with an accurate `orgId` even when it was previously
158
+ * missing. Projects on a personal account have no org; in that case (or if the
159
+ * lookup fails for a non-auth reason) we return `undefined` and simply omit the
160
+ * field rather than failing the checkout.
161
+ */
162
+ const resolveOrgId = async (props, projectId) => {
163
+ if (props.orgId) {
164
+ return props.orgId;
165
+ }
166
+ try {
167
+ const { data } = await props.apiClient.getProject(projectId);
168
+ return data.project.org_id ?? undefined;
169
+ }
170
+ catch (err) {
171
+ if (isAxiosError(err) && err.response?.status === 401) {
172
+ throw err;
173
+ }
174
+ log.debug('checkout: could not resolve org id for project %s: %s', projectId, err instanceof Error ? err.message : String(err));
175
+ return undefined;
176
+ }
177
+ };
178
+ /**
179
+ * Resolve the project id `checkout` should target.
180
+ *
181
+ * `props.projectId` is already populated from the `--project-id` flag or the
182
+ * closest `.neon` file (via the global `enrichFromContext` middleware). When
183
+ * it's still missing we try to auto-detect a single project (same behaviour as
184
+ * `branches` / `connection-string`). If that fails we surface a telling error
185
+ * and, in an interactive terminal, offer to run `neonctl link` in the current
186
+ * folder so the user can pick a project/branch without having to re-run the
187
+ * command by hand.
188
+ */
189
+ const resolveProjectId = async (props) => {
190
+ if (props.projectId) {
191
+ return props.projectId;
192
+ }
193
+ const autoDetected = await tryAutoDetectProject(props);
194
+ if (autoDetected) {
195
+ return autoDetected;
196
+ }
197
+ const missingProjectMessage = 'Could not determine which Neon project to check out a branch from. ' +
198
+ 'Provide one via the --project-id flag ' +
199
+ 'or a .neon file (created by `neonctl link` / `neonctl set-context`).';
200
+ if (isCi() || !process.stdout.isTTY) {
201
+ throw new Error(missingProjectMessage);
202
+ }
203
+ log.error(missingProjectMessage);
204
+ const { runLink } = await prompts({
205
+ type: 'confirm',
206
+ name: 'runLink',
207
+ message: 'Run `neonctl link` in the current folder to pick a project now?',
208
+ initial: true,
209
+ });
210
+ if (!runLink) {
211
+ throw new Error('Aborted: no project selected. Re-run with --project-id or link a project first.');
212
+ }
213
+ await linkHandler({
214
+ ...props,
215
+ agent: false,
216
+ yes: false,
217
+ });
218
+ const linked = readContextFile(props.contextFile);
219
+ if (!linked.projectId) {
220
+ throw new Error('Linking did not produce a project id. Re-run `neonctl checkout` once the directory is linked.');
221
+ }
222
+ // Carry the freshly-linked org id forward so the merge below keeps it.
223
+ if (linked.orgId) {
224
+ props.orgId = linked.orgId;
225
+ }
226
+ return linked.projectId;
227
+ };
228
+ /**
229
+ * Best-effort single-project auto-detection. Returns the project id when the
230
+ * API key maps to exactly one project, or `undefined` when the project can't be
231
+ * determined unambiguously (zero or multiple projects) so the caller can fall
232
+ * back to the interactive `link` flow.
233
+ */
234
+ const tryAutoDetectProject = async (props) => {
235
+ try {
236
+ const filled = await fillSingleProject(props);
237
+ return filled.projectId;
238
+ }
239
+ catch (err) {
240
+ // `fillSingleProject` throws on "No projects found" / "Multiple projects
241
+ // found" — both mean we can't pick a project automatically. Network/auth
242
+ // errors are real and should surface to the user.
243
+ if (isAxiosError(err)) {
244
+ throw err;
245
+ }
246
+ log.debug('checkout: could not auto-detect a single project: %s', err instanceof Error ? err.message : String(err));
247
+ return undefined;
248
+ }
249
+ };
@@ -0,0 +1,170 @@
1
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { describe, expect } from 'vitest';
5
+ import { test as originalTest } from '../test_utils/fixtures';
6
+ // All tests in this file share a single temporary directory whose path is
7
+ // normalized in snapshots to `<TMP>` so absolute paths in command output stay
8
+ // stable across runs and machines.
9
+ const TEST_TMP = mkdtempSync(join(tmpdir(), 'neonctl-checkout-'));
10
+ const test = originalTest.extend({
11
+ // eslint-disable-next-line no-empty-pattern
12
+ readFile: async ({}, use) => {
13
+ await use((name) => readFileSync(name, 'utf-8'));
14
+ },
15
+ // eslint-disable-next-line no-empty-pattern
16
+ removeFile: async ({}, use) => {
17
+ await use((name) => {
18
+ try {
19
+ rmSync(name);
20
+ }
21
+ catch {
22
+ // ignore
23
+ }
24
+ });
25
+ },
26
+ // eslint-disable-next-line no-empty-pattern
27
+ tmpContext: async ({}, use) => {
28
+ await use((label, seed) => {
29
+ const dir = join(TEST_TMP, label);
30
+ mkdirSync(dir, { recursive: true });
31
+ const ctx = join(dir, '.neon');
32
+ if (seed) {
33
+ writeFileSync(ctx, JSON.stringify(seed, null, 2));
34
+ }
35
+ return ctx;
36
+ });
37
+ },
38
+ });
39
+ const parseContext = (raw) => JSON.parse(raw);
40
+ describe('checkout', () => {
41
+ test('resolves a branch by name and writes branchId to a fresh .neon', async ({ testCliCommand, readFile, tmpContext, }) => {
42
+ const ctx = tmpContext('by_name_fresh');
43
+ await testCliCommand([
44
+ 'checkout',
45
+ 'main',
46
+ '--project-id',
47
+ 'test',
48
+ '--context-file',
49
+ ctx,
50
+ ]);
51
+ expect(parseContext(readFile(ctx))).toEqual({
52
+ projectId: 'test',
53
+ branchId: 'br-main-branch-123456',
54
+ });
55
+ });
56
+ test('resolves a branch by id and writes branchId to a fresh .neon', async ({ testCliCommand, readFile, tmpContext, }) => {
57
+ const ctx = tmpContext('by_id_fresh');
58
+ await testCliCommand([
59
+ 'checkout',
60
+ 'br-sunny-branch-123456',
61
+ '--project-id',
62
+ 'test',
63
+ '--context-file',
64
+ ctx,
65
+ ]);
66
+ expect(parseContext(readFile(ctx))).toEqual({
67
+ projectId: 'test',
68
+ branchId: 'br-sunny-branch-123456',
69
+ });
70
+ });
71
+ test('preserves orgId/projectId already present in the .neon file', async ({ testCliCommand, readFile, tmpContext, }) => {
72
+ const ctx = tmpContext('preserve_org', {
73
+ orgId: 'org-keep',
74
+ projectId: 'test',
75
+ });
76
+ await testCliCommand(['checkout', 'test_branch', '--context-file', ctx]);
77
+ expect(parseContext(readFile(ctx))).toEqual({
78
+ orgId: 'org-keep',
79
+ projectId: 'test',
80
+ branchId: 'br-sunny-branch-123456',
81
+ });
82
+ });
83
+ test('heals a missing orgId by resolving it from the project', async ({ testCliCommand, readFile, tmpContext, }) => {
84
+ // The .neon only has projectId; checkout should look up the project's
85
+ // org_id and write all three fields so the context file ends up complete.
86
+ const ctx = tmpContext('heal_org', { projectId: 'test' });
87
+ await testCliCommand(['checkout', 'main', '--context-file', ctx], {
88
+ mockDir: 'checkout_heal_org',
89
+ });
90
+ expect(parseContext(readFile(ctx))).toEqual({
91
+ orgId: 'org-healed-123',
92
+ projectId: 'test',
93
+ branchId: 'br-main-branch-123456',
94
+ });
95
+ });
96
+ test('resolves projectId from the .neon file when no flag is passed', async ({ testCliCommand, readFile, tmpContext, }) => {
97
+ const ctx = tmpContext('project_from_file', { projectId: 'test' });
98
+ await testCliCommand(['checkout', 'main', '--context-file', ctx]);
99
+ expect(parseContext(readFile(ctx))).toEqual({
100
+ projectId: 'test',
101
+ branchId: 'br-main-branch-123456',
102
+ });
103
+ });
104
+ test('auto-detects the project when the API key maps to a single project', async ({ testCliCommand, readFile, tmpContext, }) => {
105
+ // No --project-id and a fresh .neon: checkout should fall
106
+ // back to single-project auto-detection (same behaviour as branches / cs).
107
+ const ctx = tmpContext('autodetect_single');
108
+ await testCliCommand(['checkout', 'main', '--context-file', ctx], {
109
+ mockDir: 'single_project',
110
+ });
111
+ expect(parseContext(readFile(ctx))).toEqual({
112
+ projectId: 'test-project-123456',
113
+ branchId: 'br-main-branch-123456',
114
+ });
115
+ });
116
+ test('fails with a telling error when no project can be resolved (non-interactive)', async ({ testCliCommand, removeFile, tmpContext, }) => {
117
+ // Fresh .neon, no --project-id, and the mock account has no projects so
118
+ // single-project auto-detection can't pick one. The forked CLI has no TTY,
119
+ // so we expect the telling error instead of a prompt.
120
+ const ctx = tmpContext('no_project');
121
+ await testCliCommand(['checkout', 'main', '--context-file', ctx], {
122
+ mockDir: 'checkout_no_project',
123
+ code: 1,
124
+ stderr: 'ERROR: Could not determine which Neon project to check out a branch from. Provide one via the --project-id flag or a .neon file (created by `neonctl link` / `neonctl set-context`).',
125
+ });
126
+ removeFile(ctx);
127
+ });
128
+ test('fails with a helpful error when the branch is not found', async ({ testCliCommand, removeFile, tmpContext, }) => {
129
+ const ctx = tmpContext('not_found');
130
+ await testCliCommand([
131
+ 'checkout',
132
+ 'does-not-exist',
133
+ '--project-id',
134
+ 'test',
135
+ '--context-file',
136
+ ctx,
137
+ ], {
138
+ code: 1,
139
+ stderr: 'ERROR: Branch does-not-exist not found. Available branches: main, test_branch, 123, test_branch_with_fixed_cu, test_branch_with_autoscaling, protected_branch',
140
+ });
141
+ removeFile(ctx);
142
+ });
143
+ test('errors when a branch id is not found (ids are never auto-created)', async ({ testCliCommand, removeFile, tmpContext, }) => {
144
+ // A `br-…` value is treated as an id and matched strictly; a non-existent
145
+ // id is a hard not-found error (no create offer, even interactively).
146
+ const ctx = tmpContext('id_not_found');
147
+ await testCliCommand([
148
+ 'checkout',
149
+ 'br-does-not-exist-123456',
150
+ '--project-id',
151
+ 'test',
152
+ '--context-file',
153
+ ctx,
154
+ ], {
155
+ code: 1,
156
+ stderr: 'ERROR: Branch br-does-not-exist-123456 not found. Available branches: main, test_branch, 123, test_branch_with_fixed_cu, test_branch_with_autoscaling, protected_branch',
157
+ });
158
+ removeFile(ctx);
159
+ });
160
+ test('errors when no branch is given in a non-interactive context', async ({ testCliCommand, removeFile, tmpContext, }) => {
161
+ // Project resolves fine (from --project-id), but no branch was passed and
162
+ // the forked CLI has no TTY, so the interactive picker is not available.
163
+ const ctx = tmpContext('no_branch');
164
+ await testCliCommand(['checkout', '--project-id', 'test', '--context-file', ctx], {
165
+ code: 1,
166
+ stderr: 'ERROR: No branch specified. Pass a branch name or id (e.g. `neonctl checkout main`), or run interactively to pick one from a list.',
167
+ });
168
+ removeFile(ctx);
169
+ });
170
+ });
@@ -3,7 +3,12 @@ import { branchIdFromProps, fillSingleProject } from '../utils/enrichers.js';
3
3
  import { writer } from '../writer.js';
4
4
  import { psql } from '../utils/psql.js';
5
5
  import { parsePITBranch } from '../utils/point_in_time.js';
6
- const SSL_MODES = ['require', 'verify-ca', 'verify-full', 'omit'];
6
+ export const SSL_MODES = [
7
+ 'require',
8
+ 'verify-ca',
9
+ 'verify-full',
10
+ 'omit',
11
+ ];
7
12
  export const command = 'connection-string [branch]';
8
13
  export const aliases = ['cs'];
9
14
  export const describe = 'Get connection string';