From 599f418da01530fb44334a4b7050c16cd708ca78 Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Wed, 3 Sep 2025 15:56:20 -0700 Subject: [PATCH] Final tests fix --- apps/backend/src/lib/workflows.tsx | 86 +++++++++++++---------- apps/e2e/tests/backend/backend-helpers.ts | 14 +--- apps/e2e/tests/backend/workflows.test.ts | 6 ++ 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/apps/backend/src/lib/workflows.tsx b/apps/backend/src/lib/workflows.tsx index e1e302f37..227d2516f 100644 --- a/apps/backend/src/lib/workflows.tsx +++ b/apps/backend/src/lib/workflows.tsx @@ -139,17 +139,23 @@ async function compileWorkflow(tenancy: Tenancy, workflowId: string): Promise { + console.log(`Compiling workflow ${workflowId}...`); const compiledCodeResult = await compileWorkflowSource(workflow.tsSource); if (compiledCodeResult.status === "error") { return Result.error({ compileError: `Failed to compile workflow: ${compiledCodeResult.error}` }); } + console.log(`Compiled workflow source for ${workflowId}, running compilation trigger...`, { compiledCodeResult }); + const compileTriggerResult = await triggerWorkflowRaw(tenancy, compiledCodeResult.data, { type: "compile", }); if (compileTriggerResult.status === "error") { return Result.error({ compileError: `Failed to initialize workflow: ${compileTriggerResult.error}` }); } + + console.log(`Compilation trigger result:`, { compileTriggerResult }); + const compileTriggerOutputResult = compileTriggerResult.data; if (typeof compileTriggerOutputResult !== "object" || !compileTriggerOutputResult || !("triggerOutput" in compileTriggerOutputResult)) { captureError("workflows-compile-trigger-output", new StackAssertionError(`Failed to parse compile trigger output`, { compileTriggerOutputResult })); @@ -161,6 +167,8 @@ async function compileWorkflow(tenancy: Tenancy, workflowId: string): Promise> { - const workflowToken = generateSecureRandomString(); - const workflowTriggerToken = await globalPrismaClient.workflowTriggerToken.create({ - data: { - expiresAt: new Date(Date.now() + 1000 * 35), - tenancyId: tenancy.id, - tokenHash: await hashWorkflowTriggerToken(workflowToken), - }, - }); - - const tokenRefreshInterval = setInterval(() => { - runAsynchronously(async () => { - await globalPrismaClient.workflowTriggerToken.update({ - where: { - tenancyId_id: { - tenancyId: tenancy.id, - id: workflowTriggerToken.id, - }, - }, - data: { expiresAt: new Date(Date.now() + 1000 * 35) }, - }); - }); - }, 10_000); - - try { - const freestyle = new Freestyle(); - const freestyleRes = await freestyle.executeScript(compiledWorkflowCode, { - envVars: { - STACK_WORKFLOW_TRIGGER_DATA: JSON.stringify(trigger), - NEXT_PUBLIC_STACK_PROJECT_ID: tenancy.project.id, - NEXT_PUBLIC_STACK_API_URL: getEnvVariable("NEXT_PUBLIC_STACK_API_URL").replace("http://localhost", "http://host.docker.internal"), // the replace is a hardcoded hack for the Freestyle mock server - NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY: "", - STACK_SECRET_SERVER_KEY: "", - STACK_WORKFLOW_TOKEN_SECRET: workflowToken, + return await traceSpan({ description: `triggerWorkflowRaw ${trigger.type}` }, async () => { + const workflowToken = generateSecureRandomString(); + const workflowTriggerToken = await globalPrismaClient.workflowTriggerToken.create({ + data: { + expiresAt: new Date(Date.now() + 1000 * 35), + tenancyId: tenancy.id, + tokenHash: await hashWorkflowTriggerToken(workflowToken), }, - nodeModules: Object.fromEntries(Object.entries(externalPackages).map(([packageName, version]) => [packageName, version])), }); - return Result.map(freestyleRes, (data) => data.result); - } finally { - clearInterval(tokenRefreshInterval); - } + + const tokenRefreshInterval = setInterval(() => { + runAsynchronously(async () => { + await globalPrismaClient.workflowTriggerToken.update({ + where: { + tenancyId_id: { + tenancyId: tenancy.id, + id: workflowTriggerToken.id, + }, + }, + data: { expiresAt: new Date(Date.now() + 1000 * 35) }, + }); + }); + }, 10_000); + + try { + const freestyle = new Freestyle(); + const freestyleRes = await freestyle.executeScript(compiledWorkflowCode, { + envVars: { + STACK_WORKFLOW_TRIGGER_DATA: JSON.stringify(trigger), + NEXT_PUBLIC_STACK_PROJECT_ID: tenancy.project.id, + NEXT_PUBLIC_STACK_API_URL: getEnvVariable("NEXT_PUBLIC_STACK_API_URL").replace("http://localhost", "http://host.docker.internal"), // the replace is a hardcoded hack for the Freestyle mock server + NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY: "", + STACK_SECRET_SERVER_KEY: "", + STACK_WORKFLOW_TOKEN_SECRET: workflowToken, + }, + nodeModules: Object.fromEntries(Object.entries(externalPackages).map(([packageName, version]) => [packageName, version])), + }); + return Result.map(freestyleRes, (data) => data.result); + } finally { + clearInterval(tokenRefreshInterval); + } + }); } async function createScheduledTrigger(tenancy: Tenancy, workflowId: string, trigger: WorkflowTrigger, scheduledAt: Date) { diff --git a/apps/e2e/tests/backend/backend-helpers.ts b/apps/e2e/tests/backend/backend-helpers.ts index 066fac459..88085031d 100644 --- a/apps/e2e/tests/backend/backend-helpers.ts +++ b/apps/e2e/tests/backend/backend-helpers.ts @@ -218,20 +218,12 @@ export namespace Auth { }); expect(response).toMatchInlineSnapshot(` NiceResponse { - "status": 401, - "body": { - "code": "ADMIN_ACCESS_TOKEN_EXPIRED", - "details": { "expired_at_millis": 1756938402000 }, - "error": "Admin access token has expired. Please refresh it and try again. (The access token expired at 2025-09-03T22:26:42.000Z.)", - }, - "headers": Headers { - "x-stack-known-error": "ADMIN_ACCESS_TOKEN_EXPIRED", -