From 22cdbf30079d1cbcaaddbb1a29a6f6012adc98b2 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 22 Oct 2026 10:52:48 +0000 Subject: [PATCH] Report a failed Google Doc update instead of an empty success. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Creating the document and then failing to write its content still returned 200 with success true, so the app opened a blank file. A failed batch update now returns 502 with the document link and a fixed error. Co-authored-by: Gáborjáni-Szabó Ádám --- supabase/functions/export-gdoc/auth_test.ts | 54 +++++++++++++++++++++ supabase/functions/export-gdoc/index.ts | 12 +++++ 2 files changed, 66 insertions(+) diff --git a/supabase/functions/export-gdoc/auth_test.ts b/supabase/functions/export-gdoc/auth_test.ts index 134cc38ab..b47d81d66 100644 --- a/supabase/functions/export-gdoc/auth_test.ts +++ b/supabase/functions/export-gdoc/auth_test.ts @@ -43,6 +43,60 @@ Deno.test("export-gdoc lets a valid token through to request validation", async }]); }); +Deno.test("export-gdoc reports a failed content update instead of an empty success", async () => { + await runBearerCases(handleRequest, (authorization) => new Request("http://127.0.0.1/functions/v1/export-gdoc", { + method: "POST", + headers: { Authorization: authorization!, "Content-Type": "application/json" }, + body: JSON.stringify({ mode: "brainstorm", data: { topFive: [{ rank: 1, idea: { title: "Buses" } }] }, accessToken: "test-google-token" }), + }), [{ + label: "batch update failed", + authorization: `Bearer ${VALID_TOKEN}`, + status: 502, + body: { + success: false, + error: "The Google Doc was created, but its content could not be saved. Try the export again.", + code: "DOCUMENT_UPDATE_FAILED", + documentUrl: "https://docs.google.com/document/d/doc-1/edit", + documentId: "doc-1", + }, + authCalls: 1, + }], (url) => { + if (url.href === "https://docs.googleapis.com/v1/documents") { + return Response.json({ documentId: "doc-1" }); + } + if (url.href === "https://docs.googleapis.com/v1/documents/doc-1:batchUpdate") { + return Response.json({ error: { message: "PRIVATE_GOOGLE_BODY" } }, { status: 500 }); + } + throw new Error(`Unexpected request: ${url}`); + }); +}); + +Deno.test("export-gdoc returns the document when Google accepts the content", async () => { + await runBearerCases(handleRequest, (authorization) => new Request("http://127.0.0.1/functions/v1/export-gdoc", { + method: "POST", + headers: { Authorization: authorization!, "Content-Type": "application/json" }, + body: JSON.stringify({ mode: "brainstorm", data: {}, accessToken: "test-google-token" }), + }), [{ + label: "batch update succeeded", + authorization: `Bearer ${VALID_TOKEN}`, + status: 200, + body: { + success: true, + documentUrl: "https://docs.google.com/document/d/doc-2/edit", + documentId: "doc-2", + }, + authCalls: 1, + }], (url) => { + if (url.href === "https://docs.googleapis.com/v1/documents") { + return Response.json({ documentId: "doc-2" }); + } + if (url.href === "https://docs.googleapis.com/v1/documents/doc-2:batchUpdate") { + return Response.json({ documentId: "doc-2" }); + } + throw new Error(`Unexpected request: ${url}`); + }); +}); + Deno.test("export-gdoc does not allow arbitrary LAN origins", async () => { const response = await handleRequest(new Request("http://127.0.0.1/functions/v1/export-gdoc", { method: "OPTIONS", headers: { Origin: "http://192.168.1.5:8080" }, diff --git a/supabase/functions/export-gdoc/index.ts b/supabase/functions/export-gdoc/index.ts index fb1846022..4f3206de2 100644 --- a/supabase/functions/export-gdoc/index.ts +++ b/supabase/functions/export-gdoc/index.ts @@ -911,6 +911,18 @@ export async function handleRequest(req: Request): Promise { if (!batchUpdateResponse.ok) { const errorText = await batchUpdateResponse.text(); console.error("[GDOC] Failed to update document:", errorText.slice(0, 2000)); + // The empty document already exists in the student's Drive. + // Reporting success opened that blank file as if the export worked. + return new Response(JSON.stringify({ + success: false, + error: "The Google Doc was created, but its content could not be saved. Try the export again.", + code: "DOCUMENT_UPDATE_FAILED", + documentUrl, + documentId, + }), { + status: 502, + headers: { ...corsHeaders, "Content-Type": "application/json" } + }); } }