From 03d0f7876078fa903e6fd72d0798231f76251be5 Mon Sep 17 00:00:00 2001 From: Bilal Godil Date: Tue, 29 Jul 2025 17:50:36 -0700 Subject: [PATCH 1/8] use mock freestyle and fix tests --- apps/backend/src/lib/email-rendering.tsx | 9 +- apps/backend/src/lib/freestyle.tsx | 10 +- .../api/v1/auth/otp/send-sign-in-code.test.ts | 23 +- .../endpoints/api/v1/auth/otp/sign-in.test.ts | 4 +- .../api/v1/auth/password/reset.test.ts | 4 +- .../v1/auth/password/send-reset-code.test.ts | 4 +- .../api/v1/auth/password/sign-up.test.ts | 2 +- .../legacy-send-verification-code.test.ts | 4 +- .../send-verification-code.test.ts | 4 +- .../endpoints/api/v1/email-themes.test.ts | 17 +- .../api/v1/internal/email-templates.test.ts | 51 ++-- .../endpoints/api/v1/internal/email.test.ts | 6 +- .../endpoints/api/v1/render-email.test.ts | 81 +----- .../endpoints/api/v1/send-email.test.ts | 250 +++++++++++++++++- .../endpoints/api/v1/unsubscribe-link.test.ts | 32 +-- docker/dependencies/freestyle-mock/Dockerfile | 229 +++++++++++----- packages/stack-shared/src/helpers/emails.ts | 2 +- 17 files changed, 479 insertions(+), 253 deletions(-) diff --git a/apps/backend/src/lib/email-rendering.tsx b/apps/backend/src/lib/email-rendering.tsx index 2f4696c12a..2b9869c3f8 100644 --- a/apps/backend/src/lib/email-rendering.tsx +++ b/apps/backend/src/lib/email-rendering.tsx @@ -65,14 +65,6 @@ export async function renderEmailWithTemplate( throw new StackAssertionError("Project is required when not in preview mode", { user, project, variables }); } - if (["development", "test"].includes(getNodeEnvironment()) && apiKey === "mock_stack_freestyle_key") { - return Result.ok({ - html: `
Mock api key detected, \n\ntemplateComponent: ${templateComponent}\n\nthemeComponent: ${themeComponent}\n\n variables: ${JSON.stringify(variables)}
`, - text: `
Mock api key detected, \n\ntemplateComponent: ${templateComponent}\n\nthemeComponent: ${themeComponent}\n\n variables: ${JSON.stringify(variables)}
`, - subject: `Mock subject, ${templateComponent.match(/]*\/>/g)?.[0]}`, - notificationCategory: "mock notification category", - }); - } const result = await bundleJavaScript({ "/utils.tsx": findComponentValueUtil, "/theme.tsx": themeComponent, @@ -123,6 +115,7 @@ export async function renderEmailWithTemplate( } const freestyle = new TracedFreestyleSandboxes({ apiKey }); const nodeModules = { + "react": "19.1.1", "@react-email/components": "0.1.1", "arktype": "2.1.20", }; diff --git a/apps/backend/src/lib/freestyle.tsx b/apps/backend/src/lib/freestyle.tsx index ab23575875..706c4320c9 100644 --- a/apps/backend/src/lib/freestyle.tsx +++ b/apps/backend/src/lib/freestyle.tsx @@ -1,11 +1,19 @@ import { traceSpan } from '@/utils/telemetry'; +import { getNodeEnvironment } from '@stackframe/stack-shared/dist/utils/env'; import { FreestyleSandboxes } from 'freestyle-sandboxes'; export class TracedFreestyleSandboxes { private freestyle: FreestyleSandboxes; constructor(options: { apiKey: string }) { - this.freestyle = new FreestyleSandboxes(options); + let baseUrl = undefined; + if (["development", "test"].includes(getNodeEnvironment()) && options.apiKey === "mock_stack_freestyle_key") { + baseUrl = "http://localhost:8119"; + } + this.freestyle = new FreestyleSandboxes({ + apiKey: options.apiKey, + baseUrl, + }); } async executeScript(script: string, options?: Parameters[1]) { diff --git a/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts b/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts index 2558fb3d40..3c02c70000 100644 --- a/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts +++ b/apps/e2e/tests/backend/endpoints/api/v1/auth/otp/send-sign-in-code.test.ts @@ -8,7 +8,7 @@ it("should send a sign-in code per e-mail", async ({ expect }) => { [ MailboxMessage { "from": "Stack Dashboard ", - "subject": "Mock subject, \\"", + "subject": "Sign in to Stack Dashboard: Your code is ", "to": ["@stack-generated.example.com>"],