|
| 1 | +import { QueryClientProvider } from "@tanstack/react-query"; |
| 2 | +import { RouterProvider } from "@tanstack/react-router"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { queryClient } from "@/query-client"; |
| 5 | +import { router } from "@/router"; |
| 6 | + |
| 7 | +/** |
| 8 | + * The whole app as main.tsx mounts it, on the real route tree (app/src/routeTree.gen.ts), for |
| 9 | + * desktop-versions.test.tsx to bundle and load in a real browser. The server is a stand-in for an |
| 10 | + * OpenBot that runs `window.openbotServer.version`: OpenBot Cloud (`hostedSignIn`), or OpenBot on |
| 11 | + * this computer (`localEvaluation`). Whether a desktop app is around the page is the test's |
| 12 | + * `__TAURI_INTERNALS__`. Anything else the server is asked is the API's plain not found, and its |
| 13 | + * path is kept in `window.unanswered` for the test to print. |
| 14 | + */ |
| 15 | +type Server = { version: string; cloud: boolean }; |
| 16 | +const server = (window as unknown as { openbotServer: Server }).openbotServer; |
| 17 | +const ORGANIZATION = "/api/v2/organizations/default"; |
| 18 | +const unanswered: string[] = []; |
| 19 | +(window as unknown as { unanswered: string[] }).unanswered = unanswered; |
| 20 | + |
| 21 | +function problem(status: number, code: string): Response { |
| 22 | + return new Response(JSON.stringify({ status, code, title: code }), { |
| 23 | + status, |
| 24 | + headers: { "content-type": "application/problem+json" }, |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +const realFetch = globalThis.fetch; |
| 29 | +globalThis.fetch = Object.assign(answer, { preconnect: realFetch.preconnect }); |
| 30 | + |
| 31 | +async function answer( |
| 32 | + input: RequestInfo | URL, |
| 33 | + init?: RequestInit, |
| 34 | +): Promise<Response> { |
| 35 | + const url = new URL( |
| 36 | + input instanceof Request ? input.url : String(input), |
| 37 | + location.origin, |
| 38 | + ); |
| 39 | + const method = ( |
| 40 | + init?.method ?? (input instanceof Request ? input.method : "GET") |
| 41 | + ).toUpperCase(); |
| 42 | + const path = url.pathname; |
| 43 | + if (path === `${ORGANIZATION}/deployment`) |
| 44 | + return Response.json({ |
| 45 | + generativeUi: true, |
| 46 | + selfHostBanner: false, |
| 47 | + localEvaluation: !server.cloud, |
| 48 | + hostedSignIn: server.cloud, |
| 49 | + edition: "personal", |
| 50 | + }); |
| 51 | + if (path === `${ORGANIZATION}/people/me`) |
| 52 | + return Response.json({ |
| 53 | + person: { |
| 54 | + id: "per_sofia", |
| 55 | + email: server.cloud ? "sofia@example.com" : "dev@openbot.local", |
| 56 | + name: "Sofía", |
| 57 | + needsName: false, |
| 58 | + image: null, |
| 59 | + role: "admin", |
| 60 | + onboarding: { completedAt: "2026-11-01T00:00:00.000Z", step: 0 }, |
| 61 | + }, |
| 62 | + openbotVersion: server.version, |
| 63 | + }); |
| 64 | + if (method === "GET" && path === `${ORGANIZATION}/bots`) |
| 65 | + return Response.json({ bots: [], nextCursor: null }); |
| 66 | + if (method === "GET" && path === `${ORGANIZATION}/conversations`) |
| 67 | + return Response.json({ conversations: [], nextCursor: null }); |
| 68 | + unanswered.push(`${method} ${path}`); |
| 69 | + return problem(404, "NOT_FOUND"); |
| 70 | +} |
| 71 | + |
| 72 | +/** No socket: nothing here answers one. */ |
| 73 | +class OfflineWebSocket extends EventTarget { |
| 74 | + static readonly CONNECTING = 0; |
| 75 | + static readonly OPEN = 1; |
| 76 | + static readonly CLOSING = 2; |
| 77 | + static readonly CLOSED = 3; |
| 78 | + readyState = 3; |
| 79 | + onclose = null; |
| 80 | + onerror = null; |
| 81 | + onmessage = null; |
| 82 | + onopen = null; |
| 83 | + close() {} |
| 84 | + send() {} |
| 85 | +} |
| 86 | +globalThis.WebSocket = OfflineWebSocket as unknown as typeof WebSocket; |
| 87 | + |
| 88 | +const root = document.getElementById("root"); |
| 89 | +if (root) |
| 90 | + createRoot(root).render( |
| 91 | + <QueryClientProvider client={queryClient}> |
| 92 | + <RouterProvider router={router} context={{ queryClient }} /> |
| 93 | + </QueryClientProvider>, |
| 94 | + ); |
0 commit comments