From 90953fdab873077c0ba5f233906703c0f5d7b14b Mon Sep 17 00:00:00 2001
From: MohamedBassem <me@mbassem.com>
Date: Sun, 13 Oct 2024 16:58:16 +0000
Subject: fix(mobile): Improve connection testing accuracy

---
 apps/mobile/app/test-connection.tsx | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

(limited to 'apps/mobile/app/test-connection.tsx')

diff --git a/apps/mobile/app/test-connection.tsx b/apps/mobile/app/test-connection.tsx
index c269c55d..5639c6bd 100644
--- a/apps/mobile/app/test-connection.tsx
+++ b/apps/mobile/app/test-connection.tsx
@@ -6,6 +6,7 @@ import CustomSafeAreaView from "@/components/ui/CustomSafeAreaView";
 import { Input } from "@/components/ui/Input";
 import useAppSettings from "@/lib/settings";
 import { cn } from "@/lib/utils";
+import { z } from "zod";
 
 export default function TestConnection() {
   const { settings, isLoading } = useAppSettings();
@@ -43,15 +44,31 @@ export default function TestConnection() {
           appendText("Got the following response:");
           appendText(request.responseText);
           setStatus("error");
+          return;
         }
-        if (request.status === 200) {
+        try {
+          const schema = z.object({
+            status: z.string(),
+          });
+          const data = schema.parse(JSON.parse(request.responseText));
+          if (data.status !== "ok") {
+            appendText(`Server is not healthy: ${data.status}`);
+            setStatus("error");
+            return;
+          }
           appendText("ALL GOOD");
           setStatus("success");
+        } catch (e) {
+          appendText(`Failed to parse response as JSON: ${e}`);
+          appendText("Got the following response:");
+          appendText(request.responseText);
+          setStatus("error");
+          return;
         }
       };
 
       appendText("Using address: " + settings.address);
-      request.open("GET", `${settings.address}`);
+      request.open("GET", `${settings.address}/api/health`);
       request.send();
     }
     runTest();
-- 
cgit v1.2.3-70-g09d2

