Skip to content

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

Prevent UI crashes on invalid snapshots

amaurymagalhaesce39da34a562
diff --git a/src/state/store.test.ts b/src/state/store.test.ts
index ec559bb743..4fcb4e4c35 100644
--- a/src/state/store.test.ts
+++ b/src/state/store.test.ts
@@ -1,5 +1,5 @@
// @vitest-environment happy-dom
-import { createElement } from "react";
+import { Component, createElement, type ReactNode } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -2847,6 +2847,11 @@ describe("restoring the open conversation", () => {
});
describe("the provider's own effect", () => {
+ class TestErrorBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
+ state = { failed: false };
+ static getDerivedStateFromError() { return { failed: true }; }
+ render() { return this.state.failed ? createElement("div", { id: "snapshot-crash" }) : this.props.children; }
+ }
// The provider opens the one stream and pulls the snapshot itself, so the fixture answers both.
class FixtureEventSource {
static opened: FixtureEventSource[] = [];
@@ -2906,7 +2911,7 @@ describe("restoring the open conversation", () => {
container = document.createElement("div");
document.body.append(container);
root = createRoot(container);
- flushSync(() => root!.render(createElement(StoreProvider, null, createElement(Probe))));
+ flushSync(() => root!.render(createElement(TestErrorBoundary, null, createElement(StoreProvider, null, createElement(Probe)))));
await settle();
const [stream] = FixtureEventSource.opened;
expect(FixtureEventSource.opened).toHaveLength(1);
@@ -2915,6 +2920,31 @@ describe("restoring the open conversation", () => {
};
const hello = () => ({ kind: "hello", resumed: false, cursor: "run:0" });
+ it("keeps the UI mounted when a reconnect receives a malformed bots snapshot", async () => {
+ const { stream } = await boot();
+ stream.send(hello());
+ await settle();
+ expect(seen.selectedId).toBe("bot-1");
+ vi.stubGlobal("fetch", () => Promise.resolve(new Response("{}")));
+ stream.send(hello());
+ await settle();
+ expect(container?.querySelector("#snapshot-crash")).toBeNull();
+ expect(seen.selectedId).toBe("bot-1");
+ });
+
+ it("keeps the UI mounted when a reconnect receives a malformed routines snapshot", async () => {
+ const { stream } = await boot();
+ stream.send(hello());
+ await settle();
+ vi.stubGlobal("fetch", (path: string) => Promise.resolve(new Response(JSON.stringify(
+ path.startsWith("/api/bots?") ? { bots: [first, second], groups: [room] } : {},
+ ))));
+ stream.send(hello());
+ await settle();
+ expect(container?.querySelector("#snapshot-crash")).toBeNull();
+ expect(seen.routinesLoadState).toBe("error");
+ });
+
it("the provider remembers each non-empty selection and never an empty one", async () => {
const { stored, stream } = await boot();
diff --git a/src/state/store.tsx b/src/state/store.tsx
index f3dfd54cf8..0b4adddc2a 100644
--- a/src/state/store.tsx
+++ b/src/state/store.tsx
@@ -2976,6 +2976,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {
key: "routines",
request: async () => {
const since = Date.now() - ROUTINE_RUNS_WINDOW_MS, { routines, runs } = await api(`/api/routines?since=${since}`);
+ if (!Array.isArray(routines) || !Array.isArray(runs)) throw new Error("Invalid routines snapshot");
return () => rawDispatch({ type: "routinesHydrated", routines, runs, since });
},
},
@@ -3055,6 +3056,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {
.catch(() => {});
const chat = () =>
api(`/api/bots?messages=${MESSAGE_PAGE_SIZE}`).then(({ bots, groups, sections, computerControl, botQueuedMessages }) => {
+ if (!Array.isArray(bots) || (groups != null && !Array.isArray(groups))) throw new Error("Invalid bots snapshot");
if (!alive) return;
rawDispatch({
type: "hydrate",