Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(live): ask whether the sign-in ended when the stream stays down

KunjShah95c1d3f44eea7a
diff --git a/src/components/LiveConnectionStrip.test.ts b/src/components/LiveConnectionStrip.test.ts
index 6db752cfe4..0e6f9989e0 100644
--- a/src/components/LiveConnectionStrip.test.ts
+++ b/src/components/LiveConnectionStrip.test.ts
@@ -2,6 +2,10 @@
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
+
+const session = vi.hoisted(() => ({ noteRefusedRequest: vi.fn() }));
+vi.mock("@/lib/session-ended", () => ({ noteRefusedRequest: session.noteRefusedRequest }));
+
import { LiveConnectionStrip, RECONNECTING_AFTER_MS, CONNECTION_HELP_AFTER_MS } from "./LiveConnectionStrip";
import { t } from "@/lib/i18n";
import { markCloudAsleep, resetCloudPowerForTests } from "@/lib/cloud-power";
@@ -12,6 +16,7 @@ let root: Root;
let container: HTMLDivElement;
beforeEach(() => {
vi.useFakeTimers();
+ session.noteRefusedRequest.mockClear();
container = document.createElement("div");
root = createRoot(container);
});
@@ -67,6 +72,32 @@ it("does not offer a network retry while offline or while a Cloud is asleep", as
expect(container.textContent).toBe("");
});
+it("asks whether the sign-in ended once the stream is late, so a refused stream can show the pairing page", async () => {
+ draw(false);
+ await act(async () => { await vi.advanceTimersByTimeAsync(RECONNECTING_AFTER_MS); });
+ expect(session.noteRefusedRequest).toHaveBeenCalledOnce();
+ draw(true);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ expect(session.noteRefusedRequest).toHaveBeenCalledOnce();
+});
+
+it("asks again when the help text appears, in case the first probe could not reach the server", async () => {
+ draw(false);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ expect(session.noteRefusedRequest).toHaveBeenCalledTimes(2);
+});
+
+it("never asks an offline device or a sleeping Cloud", async () => {
+ draw(false);
+ act(() => { window.dispatchEvent(new Event("offline")); });
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ expect(session.noteRefusedRequest).not.toHaveBeenCalled();
+ act(() => { window.dispatchEvent(new Event("online")); markCloudAsleep(); });
+ draw(false);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ expect(session.noteRefusedRequest).not.toHaveBeenCalled();
+});
+
it("cancels delayed help when an initial connection recovers", async () => {
draw(false, false);
await act(async () => { await vi.advanceTimersByTimeAsync(RECONNECTING_AFTER_MS); });
diff --git a/src/components/LiveConnectionStrip.tsx b/src/components/LiveConnectionStrip.tsx
index 2bb5e15dba..b22da6a724 100644
--- a/src/components/LiveConnectionStrip.tsx
+++ b/src/components/LiveConnectionStrip.tsx
@@ -3,6 +3,7 @@ import { CloudOff, Loader2 } from "lucide-react";
import { cloudAsleep, onCloudPower } from "@/lib/cloud-power";
import { t } from "@/lib/i18n";
import { retryLiveEvents } from "@/lib/live-events";
+import { noteRefusedRequest } from "@/lib/session-ended";
export const RECONNECTING_AFTER_MS = 4_000;
export const CONNECTION_HELP_AFTER_MS = 20_000;
@@ -27,11 +28,15 @@ export function LiveConnectionStrip({ connected, hydrated }: { connected: boolea
useEffect(() => {
setLate(false);
setHelp(false);
- if (connected) return;
+ if (connected || asleep || !online) return;
+ // The stream is a browser EventSource, so a refused handshake looks like any other drop. Ask the session endpoint
+ // once it is late: an ended sign-in then shows the pairing page instead of "Connecting…" forever. Neither a sleeping
+ // Cloud nor an offline device is woken just to ask.
+ const probe = setTimeout(() => noteRefusedRequest(), RECONNECTING_AFTER_MS);
const timer = setTimeout(() => setLate(true), RECONNECTING_AFTER_MS);
- const helpTimer = setTimeout(() => setHelp(true), CONNECTION_HELP_AFTER_MS);
- return () => { clearTimeout(timer); clearTimeout(helpTimer); };
- }, [connected]);
+ const helpTimer = setTimeout(() => { setHelp(true); noteRefusedRequest(); }, CONNECTION_HELP_AFTER_MS);
+ return () => { clearTimeout(probe); clearTimeout(timer); clearTimeout(helpTimer); };
+ }, [connected, asleep, online]);
if (asleep) return null;
const notice = !online ? "offline" : !connected && late && (hydrated || help) ? "reconnecting" : null;
if (!notice) return null;