|
| 1 | +import { |
| 2 | + assertSelfHostPreflight, |
| 3 | + formatSelfHostPreflightError, |
| 4 | + preflightEnv, |
| 5 | + type SelfHostPreflightProblem, |
| 6 | +} from "../../src/selfhost/preflight"; |
| 7 | + |
| 8 | +describe("self-host environment preflight (#2080)", () => { |
| 9 | + it("returns every missing required value at once for the first-run setup path", () => { |
| 10 | + const result = preflightEnv({}); |
| 11 | + |
| 12 | + expect(result).toEqual({ |
| 13 | + ok: false, |
| 14 | + problems: [ |
| 15 | + expect.objectContaining({ var: "REDIS_URL" }), |
| 16 | + expect.objectContaining({ var: "SELFHOST_SETUP_TOKEN" }), |
| 17 | + expect.objectContaining({ var: "PUBLIC_API_ORIGIN" }), |
| 18 | + ], |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("trims values, passes configured GitHub App installs, and accepts postgres URLs", () => { |
| 23 | + expect( |
| 24 | + preflightEnv({ |
| 25 | + REDIS_URL: " redis://redis:6379 ", |
| 26 | + GITHUB_APP_ID: " 123 ", |
| 27 | + DATABASE_URL: " postgres://gittensory:secret@postgres:5432/gittensory ", |
| 28 | + }), |
| 29 | + ).toEqual({ ok: true, problems: [] }); |
| 30 | + |
| 31 | + expect( |
| 32 | + preflightEnv({ |
| 33 | + REDIS_URL: "redis://redis:6379", |
| 34 | + GITHUB_APP_ID: "123", |
| 35 | + DATABASE_URL: "postgresql://gittensory:secret@postgres:5432/gittensory", |
| 36 | + }), |
| 37 | + ).toEqual({ ok: true, problems: [] }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("requires setup-wizard vars only when neither a GitHub App nor Orb broker enrollment is configured", () => { |
| 41 | + expect( |
| 42 | + preflightEnv({ |
| 43 | + REDIS_URL: "redis://redis:6379", |
| 44 | + SELFHOST_SETUP_TOKEN: "setup-secret", |
| 45 | + PUBLIC_API_ORIGIN: "https://selfhost.example", |
| 46 | + }), |
| 47 | + ).toEqual({ ok: true, problems: [] }); |
| 48 | + |
| 49 | + expect( |
| 50 | + preflightEnv({ |
| 51 | + REDIS_URL: "redis://redis:6379", |
| 52 | + ORB_ENROLLMENT_SECRET: "orb-secret", |
| 53 | + }), |
| 54 | + ).toEqual({ ok: true, problems: [] }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("flags blank values and invalid DATABASE_URL while never echoing supplied secrets", () => { |
| 58 | + const result = preflightEnv({ |
| 59 | + REDIS_URL: " ", |
| 60 | + SELFHOST_SETUP_TOKEN: "secret-setup-token", |
| 61 | + PUBLIC_API_ORIGIN: "https://selfhost.example", |
| 62 | + DATABASE_URL: "sqlite:///tmp/gittensory.sqlite?password=super-secret-db", |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result).toEqual({ |
| 66 | + ok: false, |
| 67 | + problems: [ |
| 68 | + expect.objectContaining({ var: "REDIS_URL" }), |
| 69 | + expect.objectContaining({ var: "DATABASE_URL" }), |
| 70 | + ], |
| 71 | + }); |
| 72 | + const serialized = JSON.stringify(result); |
| 73 | + expect(serialized).not.toContain("secret-setup-token"); |
| 74 | + expect(serialized).not.toContain("super-secret-db"); |
| 75 | + expect(serialized).not.toContain("sqlite:///tmp"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("formats all problems with names and actionable hints", () => { |
| 79 | + const problems: SelfHostPreflightProblem[] = [ |
| 80 | + { var: "REDIS_URL", message: "Set REDIS_URL to Redis." }, |
| 81 | + { var: "PUBLIC_API_ORIGIN", message: "Set PUBLIC_API_ORIGIN to HTTPS." }, |
| 82 | + ]; |
| 83 | + |
| 84 | + expect(formatSelfHostPreflightError(problems)).toBe( |
| 85 | + "Self-host environment preflight failed:\n" + |
| 86 | + "- REDIS_URL: Set REDIS_URL to Redis.\n" + |
| 87 | + "- PUBLIC_API_ORIGIN: Set PUBLIC_API_ORIGIN to HTTPS.", |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it("asserts the preflight result for the boot path", () => { |
| 92 | + expect(() => |
| 93 | + assertSelfHostPreflight({ |
| 94 | + REDIS_URL: "redis://redis:6379", |
| 95 | + GITHUB_APP_ID: "123", |
| 96 | + }), |
| 97 | + ).not.toThrow(); |
| 98 | + |
| 99 | + expect(() => assertSelfHostPreflight({ DATABASE_URL: "mysql://db/app" })).toThrow( |
| 100 | + /Self-host environment preflight failed:\n- REDIS_URL: .*SELFHOST_SETUP_TOKEN.*PUBLIC_API_ORIGIN.*DATABASE_URL:/s, |
| 101 | + ); |
| 102 | + }); |
| 103 | +}); |
0 commit comments