-
Notifications
You must be signed in to change notification settings - Fork 1.5k
test: add env and database vitest coverage #1834
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
Open
sypham98-prog
wants to merge
2
commits into
CapSoftware:main
Choose a base branch
from
sypham98-prog:codex/test-env-database-bootstrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+286
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,24 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { hashPassword, verifyPassword } from "./crypto"; | ||
|
|
||
| describe("password hashing", () => { | ||
| it("verifies the original password and rejects a different one", async () => { | ||
| const hash = await hashPassword("correct horse battery staple"); | ||
|
|
||
| expect(hash).not.toBe("correct horse battery staple"); | ||
| expect(await verifyPassword(hash, "correct horse battery staple")).toBe( | ||
| true, | ||
| ); | ||
| expect(await verifyPassword(hash, "wrong password")).toBe(false); | ||
| }); | ||
|
|
||
| it("rejects empty hash and password inputs", async () => { | ||
| const hash = await hashPassword("non-empty password"); | ||
|
|
||
| expect(await verifyPassword("", "non-empty password")).toBe(false); | ||
| expect(await verifyPassword(hash, "")).toBe(false); | ||
| await expect(hashPassword("")).rejects.toThrow( | ||
| "Cannot hash empty or null password", | ||
| ); | ||
| }); | ||
| }); |
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
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,8 @@ | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| environment: "node", | ||
| include: ["**/*.test.ts"], | ||
| }, | ||
| }); |
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,38 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const originalEnv = { ...process.env }; | ||
|
|
||
| function resetEnv() { | ||
| for (const key of Object.keys(process.env)) { | ||
| delete process.env[key]; | ||
| } | ||
| Object.assign(process.env, originalEnv); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| resetEnv(); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| describe("buildEnv", () => { | ||
| it("uses WEB_URL as the public web URL fallback", async () => { | ||
| process.env.WEB_URL = "https://cap.example"; | ||
| delete process.env.NEXT_PUBLIC_WEB_URL; | ||
|
|
||
| const { buildEnv } = await import("./build"); | ||
|
|
||
| expect(buildEnv.NEXT_PUBLIC_WEB_URL).toBe("https://cap.example"); | ||
| }); | ||
|
|
||
| it("caches the parsed environment after first access", async () => { | ||
| process.env.WEB_URL = "https://first.example"; | ||
|
|
||
| const { buildEnv } = await import("./build"); | ||
|
|
||
| expect(buildEnv.NEXT_PUBLIC_WEB_URL).toBe("https://first.example"); | ||
|
|
||
| process.env.WEB_URL = "https://second.example"; | ||
|
|
||
| expect(buildEnv.NEXT_PUBLIC_WEB_URL).toBe("https://first.example"); | ||
| }); | ||
| }); |
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
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,50 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const originalEnv = { ...process.env }; | ||
|
|
||
| function resetEnv() { | ||
| for (const key of Object.keys(process.env)) { | ||
| delete process.env[key]; | ||
| } | ||
| Object.assign(process.env, originalEnv); | ||
| } | ||
|
|
||
| function setRequiredServerEnv() { | ||
| process.env.DATABASE_URL = "mysql://user:password@localhost:3306/cap"; | ||
| process.env.WEB_URL = "https://cap.example"; | ||
| process.env.NEXTAUTH_SECRET = "test-secret"; | ||
| process.env.NEXTAUTH_URL = "https://cap.example"; | ||
| process.env.CAP_AWS_BUCKET = "cap-test-bucket"; | ||
| process.env.CAP_AWS_REGION = "us-east-1"; | ||
| process.env.NODE_ENV = "test"; | ||
| delete process.env.S3_PATH_STYLE; | ||
| delete process.env.CAP_VIDEOS_DEFAULT_PUBLIC; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| resetEnv(); | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| describe("serverEnv", () => { | ||
| it("maps CAP_AWS_ENDPOINT to both S3 endpoint aliases", async () => { | ||
| setRequiredServerEnv(); | ||
| process.env.CAP_AWS_ENDPOINT = "https://s3.example"; | ||
|
|
||
| const { serverEnv } = await import("./server"); | ||
|
|
||
| const env = serverEnv(); | ||
| expect(env.S3_PUBLIC_ENDPOINT).toBe("https://s3.example"); | ||
| expect(env.S3_INTERNAL_ENDPOINT).toBe("https://s3.example"); | ||
| }); | ||
|
|
||
| it("parses boolean string defaults for server settings", async () => { | ||
| setRequiredServerEnv(); | ||
|
|
||
| const { serverEnv } = await import("./server"); | ||
|
|
||
| const env = serverEnv(); | ||
| expect(env.S3_PATH_STYLE).toBe(true); | ||
| expect(env.CAP_VIDEOS_DEFAULT_PUBLIC).toBe(true); | ||
| }); | ||
| }); | ||
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,8 @@ | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| environment: "node", | ||
| include: ["**/*.test.ts"], | ||
| }, | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S3_PATH_STYLEandCAP_VIDEOS_DEFAULT_PUBLICtake their schema-level defaults (true), yetsetRequiredServerEnv()never unsets those variables.originalEnvis captured at module-load time, so if the developer's or CI environment already has either var set (e.g.S3_PATH_STYLE=false),...process.envinexperimental__runtimeEnvwill propagate that value and override the Zod default, causing the test to fail with no obvious cause.Prompt To Fix With AI
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f8c9fcc. setRequiredServerEnv now deletes S3_PATH_STYLE and CAP_VIDEOS_DEFAULT_PUBLIC before importing serverEnv, so the boolean default assertions are isolated from any ambient developer or CI environment. Verification passed: corepack pnpm --filter @cap/env test; corepack pnpm --filter @cap/database test; corepack pnpm exec biome check on the touched test config and package files.