Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

config.test.ts

54 lines1.9 KB

1import { afterEach, beforeEach, expect, it, vi } from "vitest"
2import { runtimeConfig } from "../../../server/config"
3​
4// Keep these checks independent of developer credentials and local env files.
5vi.mock("node:process", () => {
6 const loadEnvFile = vi.fn()
7 return { loadEnvFile, default: { loadEnvFile } }
8})
9​
10beforeEach(() => {
11 vi.stubEnv("DATABASE_URL", "postgresql://user:password@localhost/demo")
12 vi.stubEnv("TEAM_COOKIE_SECRET", "test-identity-secret")
13 for (const name of [
14 "NEON_REALTIME_URL",
15 "NEON_REALTIME_SECRET",
16 "REALTIME_PUBLIC_URL",
17 "REALTIME_SECRET",
18 ])
19 vi.stubEnv(name, undefined)
20})
21afterEach(() => vi.unstubAllEnvs())
22​
23it("uses the Realtime variables emitted by neon env pull", () => {
24 vi.stubEnv("NEON_REALTIME_URL", "wss://realtime.example.com")
25 vi.stubEnv("NEON_REALTIME_SECRET", "published-secret")
26 const config = runtimeConfig()
27 expect(config.realtimeUrl).toBe("wss://realtime.example.com")
28 expect(config.secret).toBe("published-secret")
29 expect(config.databaseName).toBe("demo")
30})
31​
32it("keeps deployments using the previous variable names working", () => {
33 vi.stubEnv("REALTIME_PUBLIC_URL", "wss://previous.example.com")
34 vi.stubEnv("REALTIME_SECRET", "previous-secret")
35 expect(runtimeConfig()).toMatchObject({
36 realtimeUrl: "wss://previous.example.com",
37 secret: "previous-secret",
38 })
39})
40​
41it("prefers CLI variables when both names are supplied", () => {
42 vi.stubEnv("NEON_REALTIME_URL", "wss://realtime.example.com")
43 vi.stubEnv("NEON_REALTIME_SECRET", "published-secret")
44 vi.stubEnv("REALTIME_PUBLIC_URL", "wss://previous.example.com")
45 vi.stubEnv("REALTIME_SECRET", "previous-secret")
46 expect(runtimeConfig()).toMatchObject({
47 realtimeUrl: "wss://realtime.example.com",
48 secret: "published-secret",
49 })
50})
51​
52it("reports the canonical variable name when Realtime is unconfigured", () => {
53 expect(() => runtimeConfig()).toThrow("Missing NEON_REALTIME_URL")
54})