-
Notifications
You must be signed in to change notification settings - Fork 1
DR-6844 Various create-db updates #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3d7e8af
chore: flags destructured into `flags.ts`
aidankmcalister a5a6f22
chore: extract command handlers into dedicated modules
aidankmcalister 106e839
chore: consolidate output formatting logic
aidankmcalister ac61937
chore: added JSDoc comments
aidankmcalister 9d394ef
fix: flag comment github issue added
aidankmcalister 209b247
feat: more rigorous testing with minimal db creations
aidankmcalister 86ba884
chore: folder restructure
aidankmcalister 0ce245c
chore: version updates
aidankmcalister 2df4a28
fix: coderabbit comments resolved
aidankmcalister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { execa } from "execa"; | ||
| import { fileURLToPath } from "url"; | ||
| import path from "path"; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const CLI_PATH = path.resolve(__dirname, "../dist/cli.mjs"); | ||
|
|
||
| const runCli = async ( | ||
| args: string[] = [], | ||
| options: { env?: Record<string, string>; timeout?: number } = {} | ||
| ) => { | ||
| const result = await execa("node", [CLI_PATH, ...args], { | ||
| env: { ...process.env, ...options.env }, | ||
| reject: false, | ||
| timeout: options.timeout ?? 20000, | ||
| }); | ||
| return { | ||
| ...result, | ||
| all: result.stdout + result.stderr, | ||
| }; | ||
| }; | ||
|
|
||
| // ============================================================================ | ||
| // UNIT TESTS - No database creation, tests CLI structure and help only | ||
| // ============================================================================ | ||
|
|
||
| describe("CLI help and version", () => { | ||
| it("displays help with --help flag", async () => { | ||
| const result = await runCli(["--help"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.all).toContain("create-db"); | ||
| expect(result.all).toContain("Create a new Prisma Postgres database"); | ||
| expect(result.all).toContain("regions"); | ||
| }); | ||
|
|
||
| it("displays help with -h flag", async () => { | ||
| const result = await runCli(["-h"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.all).toContain("create-db"); | ||
| }); | ||
|
|
||
| it("displays version with --version flag", async () => { | ||
| const result = await runCli(["--version"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout).toMatch(/\d+\.\d+\.\d+/); | ||
| }); | ||
|
|
||
| it("displays version with -V flag", async () => { | ||
| const result = await runCli(["-V"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout).toMatch(/\d+\.\d+\.\d+/); | ||
| }); | ||
|
|
||
| it("displays create command help", async () => { | ||
| const result = await runCli(["create", "--help"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.all).toContain("--region"); | ||
| expect(result.all).toContain("--interactive"); | ||
| expect(result.all).toContain("--json"); | ||
| expect(result.all).toContain("--env"); | ||
| }); | ||
|
|
||
| it("displays regions command help", async () => { | ||
| const result = await runCli(["regions", "--help"]); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.all).toContain("List available Prisma Postgres regions"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("CLI error handling", () => { | ||
| it("fails with invalid region (no DB created)", async () => { | ||
| const result = await runCli(["--region", "invalid-region"]); | ||
| expect(result.exitCode).not.toBe(0); | ||
| }, 10000); | ||
|
|
||
| it("shows error for unknown command", async () => { | ||
| const result = await runCli(["unknown-command"]); | ||
| expect(result.exitCode).not.toBe(0); | ||
| }); | ||
|
|
||
| it("shows error for unknown flag", async () => { | ||
| const result = await runCli(["--unknown-flag"]); | ||
| expect(result.exitCode).not.toBe(0); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { CreateFlags, type CreateFlagsInput } from "../src/cli/flags.js"; | ||
| import { RegionSchema } from "../src/types.js"; | ||
|
|
||
| describe("CreateFlags schema", () => { | ||
| describe("region field", () => { | ||
| it("accepts valid region IDs", () => { | ||
| const validRegions = [ | ||
| "us-east-1", | ||
| "us-west-1", | ||
| "eu-central-1", | ||
| "eu-west-3", | ||
| "ap-southeast-1", | ||
| "ap-northeast-1", | ||
| ]; | ||
|
|
||
| for (const region of validRegions) { | ||
| const result = CreateFlags.safeParse({ region }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.region).toBe(region); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("rejects invalid region IDs", () => { | ||
| const result = CreateFlags.safeParse({ region: "invalid-region" }); | ||
| expect(result.success).toBe(false); | ||
| }); | ||
|
|
||
| it("allows undefined region", () => { | ||
| const result = CreateFlags.safeParse({}); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.region).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("interactive field", () => { | ||
| it("defaults to false", () => { | ||
| const result = CreateFlags.safeParse({}); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.interactive).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts true", () => { | ||
| const result = CreateFlags.safeParse({ interactive: true }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.interactive).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts false", () => { | ||
| const result = CreateFlags.safeParse({ interactive: false }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.interactive).toBe(false); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("json field", () => { | ||
| it("defaults to false", () => { | ||
| const result = CreateFlags.safeParse({}); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.json).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts true", () => { | ||
| const result = CreateFlags.safeParse({ json: true }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.json).toBe(true); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("env field", () => { | ||
| it("accepts string path", () => { | ||
| const result = CreateFlags.safeParse({ env: ".env" }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.env).toBe(".env"); | ||
| } | ||
| }); | ||
|
|
||
| it("accepts full path", () => { | ||
| const result = CreateFlags.safeParse({ env: "/path/to/.env.local" }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.env).toBe("/path/to/.env.local"); | ||
| } | ||
| }); | ||
|
|
||
| it("allows undefined", () => { | ||
| const result = CreateFlags.safeParse({}); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.env).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("userAgent field", () => { | ||
| it("accepts custom user agent string", () => { | ||
| const result = CreateFlags.safeParse({ userAgent: "myapp/1.0.0" }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.userAgent).toBe("myapp/1.0.0"); | ||
| } | ||
| }); | ||
|
|
||
| it("allows undefined", () => { | ||
| const result = CreateFlags.safeParse({}); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.userAgent).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("combined fields", () => { | ||
| it("parses all fields together", () => { | ||
| const input = { | ||
| region: "eu-central-1", | ||
| interactive: true, | ||
| json: false, | ||
| env: ".env.local", | ||
| userAgent: "test/2.0.0", | ||
| }; | ||
|
|
||
| const result = CreateFlags.safeParse(input); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data).toEqual({ | ||
| region: "eu-central-1", | ||
| interactive: true, | ||
| json: false, | ||
| env: ".env.local", | ||
| userAgent: "test/2.0.0", | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it("applies defaults for missing optional fields", () => { | ||
| const result = CreateFlags.safeParse({ region: "us-east-1" }); | ||
| expect(result.success).toBe(true); | ||
| if (result.success) { | ||
| expect(result.data.region).toBe("us-east-1"); | ||
| expect(result.data.interactive).toBe(false); | ||
| expect(result.data.json).toBe(false); | ||
| expect(result.data.env).toBeUndefined(); | ||
| expect(result.data.userAgent).toBeUndefined(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("type inference", () => { | ||
| it("CreateFlagsInput type matches schema output", () => { | ||
| const input: CreateFlagsInput = { | ||
| region: "us-east-1", | ||
| interactive: false, | ||
| json: true, | ||
| env: ".env", | ||
| userAgent: "test/1.0", | ||
| }; | ||
|
|
||
| const result = CreateFlags.parse(input); | ||
| expect(result.region).toBe(input.region); | ||
| expect(result.interactive).toBe(input.interactive); | ||
| expect(result.json).toBe(input.json); | ||
| expect(result.env).toBe(input.env); | ||
| expect(result.userAgent).toBe(input.userAgent); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("RegionSchema", () => { | ||
| it("validates all supported regions", () => { | ||
| const regions = [ | ||
| "us-east-1", | ||
| "us-west-1", | ||
| "eu-central-1", | ||
| "eu-west-3", | ||
| "ap-southeast-1", | ||
| "ap-northeast-1", | ||
| ]; | ||
|
|
||
| for (const region of regions) { | ||
| expect(RegionSchema.safeParse(region).success).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it("rejects unsupported regions", () => { | ||
| const invalidRegions = [ | ||
| "us-east-2", | ||
| "eu-west-1", | ||
| "ap-south-1", | ||
| "sa-east-1", | ||
| "", | ||
| "invalid", | ||
| ]; | ||
|
|
||
| for (const region of invalidRegions) { | ||
| expect(RegionSchema.safeParse(region).success).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| it("rejects non-string values", () => { | ||
| expect(RegionSchema.safeParse(123).success).toBe(false); | ||
| expect(RegionSchema.safeParse(null).success).toBe(false); | ||
| expect(RegionSchema.safeParse(undefined).success).toBe(false); | ||
| expect(RegionSchema.safeParse({}).success).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.