From 3b58745db1f584cab8dc020299407c76bf2bf48f Mon Sep 17 00:00:00 2001 From: Matt Rubens <2600+mrubens@users.noreply.github.com> Date: Sun, 2 Aug 2026 00:58:11 -0400 Subject: [PATCH] fix: isolate install-after-auth route tests from developer .env.local The tests stubbed process.env.R_APP_URL/R_PUBLIC_URL, but the route resolves the public URL through the dotenvx-backed web Env proxy, which reads the repo-root .env.local directly - so a developer-local R_PUBLIC_URL (e.g. an ngrok host) leaked into the redirect assertions. Mock the Env accessor with a test-owned object instead, keeping the real getPublicAppUrl preference logic under test. --- .../__tests__/route.test.ts | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/apps/web/src/app/api/slack/install-after-auth/__tests__/route.test.ts b/apps/web/src/app/api/slack/install-after-auth/__tests__/route.test.ts index 62cdef591..73db8a666 100644 --- a/apps/web/src/app/api/slack/install-after-auth/__tests__/route.test.ts +++ b/apps/web/src/app/api/slack/install-after-auth/__tests__/route.test.ts @@ -4,6 +4,21 @@ import { createServerCaller } from '@/trpc/server'; import { GET } from '../route'; +// getCallbackHost rewrites internal request origins (localhost) to the +// configured public app URL, which it reads through the dotenvx-backed Env +// proxy. The proxy resolves values from the repo-root .env.local file, so a +// developer-local R_APP_URL/R_PUBLIC_URL (e.g. an ngrok host) would leak into +// redirect assertions even with process.env stubbed. Replace Env with a +// test-owned object so redirects always resolve against localhost. +const { envState } = vi.hoisted(() => ({ + envState: {} as { R_APP_URL: string; R_PUBLIC_URL?: string }, +})); + +vi.mock('@/lib/server/env', async (importOriginal) => ({ + ...(await importOriginal()), + Env: envState, +})); + vi.mock('@/trpc/server', () => ({ createServerCaller: vi.fn(), })); @@ -13,16 +28,10 @@ const mockInstallation = vi.fn(); const mockConnectApp = vi.fn(); describe('GET /api/slack/install-after-auth', () => { - // getCallbackHost rewrites internal request origins (localhost) to the - // configured public app URL, so machine-level R_APP_URL/R_PUBLIC_URL (e.g. an - // ngrok host in the shell) leak into redirect assertions. Pin them per test. - const originalAppUrl = process.env.R_APP_URL; - const originalPublicUrl = process.env.R_PUBLIC_URL; - beforeEach(() => { vi.clearAllMocks(); - process.env.R_APP_URL = 'http://localhost:13000'; - delete process.env.R_PUBLIC_URL; + envState.R_APP_URL = 'http://localhost:13000'; + delete envState.R_PUBLIC_URL; mockCreateServerCaller.mockResolvedValue({ slack: { installation: mockInstallation, @@ -31,20 +40,6 @@ describe('GET /api/slack/install-after-auth', () => { } as never); }); - afterEach(() => { - if (originalAppUrl === undefined) { - delete process.env.R_APP_URL; - } else { - process.env.R_APP_URL = originalAppUrl; - } - - if (originalPublicUrl === undefined) { - delete process.env.R_PUBLIC_URL; - } else { - process.env.R_PUBLIC_URL = originalPublicUrl; - } - }); - it('redirects to the requested path when Slack is already installed', async () => { mockInstallation.mockResolvedValue({ id: 'slack-installation-1' });