From 97cb58ff42b3af739447a11aa29fddefcff4577b Mon Sep 17 00:00:00 2001 From: oabrivard Date: Mon, 23 Mar 2026 10:33:00 +0100 Subject: [PATCH] fix: improve type safety and error handling in generation UAT --- e2e/tests/generation-live.spec.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/e2e/tests/generation-live.spec.ts b/e2e/tests/generation-live.spec.ts index ba14102..c3044c7 100644 --- a/e2e/tests/generation-live.spec.ts +++ b/e2e/tests/generation-live.spec.ts @@ -9,6 +9,7 @@ */ import { test, expect } from '@playwright/test'; +import type { Page } from '@playwright/test'; import * as dotenv from 'dotenv'; import * as path from 'path'; import { loginAsUser } from '../helpers/auth'; @@ -20,7 +21,7 @@ const OPENAI_KEY = process.env.OPENAI_TEST_API_KEY; /** Helper to make an authenticated API call from the browser context. */ async function apiCall( - page: any, + page: Page, method: string, url: string, body?: object, @@ -47,7 +48,17 @@ async function apiCall( options.body = JSON.stringify(body); } const resp = await fetch(url, options); - const data = await resp.json().catch(() => null); + const text = await resp.text(); + let data: unknown = null; + if (text) { + try { + data = JSON.parse(text); + } catch { + throw new Error( + `Expected JSON from ${method} ${url} (status ${resp.status}), got: ${text.slice(0, 200)}` + ); + } + } return { status: resp.status, data }; }, { method, url, body }, @@ -56,7 +67,7 @@ async function apiCall( /** Wait for SSE generation to complete, return the synthesis_id. */ async function waitForGenerationComplete( - page: any, + page: Page, jobId: string, timeoutMs = 120_000, ): Promise { @@ -73,8 +84,12 @@ async function waitForGenerationComplete( es.addEventListener('complete', (event: MessageEvent) => { clearTimeout(timer); es.close(); - const parsed = JSON.parse(event.data); - resolve(parsed.synthesis_id); + try { + const parsed = JSON.parse(event.data); + resolve(parsed.synthesis_id); + } catch { + reject(new Error(`Invalid JSON in complete event: ${event.data}`)); + } }); es.addEventListener('error', (event: MessageEvent) => { clearTimeout(timer);