Skip to content

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

Bot settings search: also wait for the app catalog, and look again on a new lookup

AdityaUmalef64ac0a3013f
diff --git a/src/components/bot-settings/sections.test.ts b/src/components/bot-settings/sections.test.ts
index a57f44b7f1..67abaffd2f 100644
--- a/src/components/bot-settings/sections.test.ts
+++ b/src/components/bot-settings/sections.test.ts
@@ -41,9 +41,10 @@ describe("bot settings search", () => {
});
it("keeps looking until the Email lookup has answered too", () => {
- expect(appSearchPhase({ phase: "ready", emailSettled: false })).toBe("loading");
- expect(appSearchPhase({ phase: "unknown", emailSettled: false })).toBe("loading");
- expect(appSearchPhase({ phase: "ready", emailSettled: true })).toBe("ready");
- expect(appSearchPhase({ phase: "loading", emailSettled: true })).toBe("loading");
+ expect(appSearchPhase({ phase: "ready", cardsSettled: true, emailSettled: false })).toBe("loading");
+ expect(appSearchPhase({ phase: "unknown", cardsSettled: true, emailSettled: false })).toBe("loading");
+ expect(appSearchPhase({ phase: "ready", cardsSettled: false, emailSettled: true })).toBe("loading");
+ expect(appSearchPhase({ phase: "ready", cardsSettled: true, emailSettled: true })).toBe("ready");
+ expect(appSearchPhase({ phase: "loading", cardsSettled: true, emailSettled: true })).toBe("loading");
});
});
diff --git a/src/components/bot-settings/sections.ts b/src/components/bot-settings/sections.ts
index 3ac6b9dd2e..034f7893b1 100644
--- a/src/components/bot-settings/sections.ts
+++ b/src/components/bot-settings/sections.ts
@@ -66,9 +66,9 @@ export function sectionMatches(entry: BotSectionEntry, query: string, appNames:
.some((part) => part.toLowerCase().includes(query));
}
-/** App names come from the connector inventory and the Email lookup; search keeps looking until both have answered. */
-export function appSearchPhase(apps: Pick<BotAppsState, "phase" | "emailSettled">): BotApps["phase"] {
- return apps.emailSettled ? apps.phase : "loading";
+/** App names come from the connector inventory, the app catalog and the Email lookup; search keeps looking until all have answered. */
+export function appSearchPhase(apps: Pick<BotAppsState, "phase" | "cardsSettled" | "emailSettled">): BotApps["phase"] {
+ return apps.cardsSettled && apps.emailSettled ? apps.phase : "loading";
}
/** The note shown when no section matches. Until the bot's connected apps have loaded, an app name may still match. */
diff --git a/src/components/bot-settings/useBotApps.dom.test.ts b/src/components/bot-settings/useBotApps.dom.test.ts
index d2ebc3c8ac..98f8bc1f1b 100644
--- a/src/components/bot-settings/useBotApps.dom.test.ts
+++ b/src/components/bot-settings/useBotApps.dom.test.ts
@@ -1,13 +1,17 @@
// @vitest-environment happy-dom
-// The settings search keeps looking until both app-name sources (connector inventory and Email) have answered.
+// The settings search keeps looking until every app-name source (connector inventory, app catalog, Email) has answered.
import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
-import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { setLocale } from "@/lib/i18n";
import type { Bot } from "@/state/store";
-const fixture = vi.hoisted(() => ({ answerEmail: null as null | ((ok: boolean) => void) }));
+const fixture = vi.hoisted(() => ({
+ holdCatalog: false,
+ answerCatalog: null as null | ((ok: boolean) => void),
+ answerEmail: null as null | ((ok: boolean) => void),
+}));
vi.mock("@/lib/connected-apps", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/connected-apps")>()),
preloadConnectedApps: () => Promise.resolve({ authoritative: true, services: { github: { connected: true } } }),
@@ -15,7 +19,12 @@ vi.mock("@/lib/connected-apps", async (importOriginal) => ({
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
const api = (path: string) => {
- if (path === "/api/connectors/catalog") return Promise.resolve({ cards: [] });
+ if (path === "/api/connectors/catalog") {
+ if (!fixture.holdCatalog) return Promise.resolve({ cards: [] });
+ return new Promise((resolve, reject) => {
+ fixture.answerCatalog = (ok) => (ok ? resolve({ cards: [] }) : reject(new Error("down")));
+ });
+ }
if (path === "/api/email") {
return new Promise((resolve, reject) => {
fixture.answerEmail = (ok) => (ok ? resolve({ state: "connected", accounts: [{ address: "[email protected]" }] }) : reject(new Error("down")));
@@ -29,43 +38,81 @@ vi.mock("@/state/store", async (importOriginal) => {
const { useBotApps } = await import("./useBotApps");
const { appSearchPhase } = await import("./sections");
-function Probe({ bot }: { bot: Bot }) {
- const apps = useBotApps(bot, true);
+function Probe({ bot, load }: { bot: Bot; load: boolean }) {
+ const apps = useBotApps(bot, load);
return createElement("p", null, `${appSearchPhase(apps)}:${apps.apps.map((app) => app.slug).join(",")}`);
}
-let host: HTMLDivElement;
-let root: Root;
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+const bot = { id: "scout" } as Bot;
const settle = async () => {
for (let i = 0; i < 3; i++) await new Promise((resolve) => setTimeout(resolve, 0));
};
+function render(load = true) {
+ if (!host) {
+ setLocale("en");
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+ }
+ flushSync(() => root!.render(createElement(Probe, { bot, load })));
+ return host;
+}
-beforeEach(() => {
- setLocale("en");
- fixture.answerEmail = null;
- host = document.createElement("div");
- document.body.append(host);
- root = createRoot(host);
- flushSync(() => root.render(createElement(Probe, { bot: { id: "scout" } as Bot })));
-});
afterEach(() => {
- root.unmount();
- host.remove();
+ root?.unmount();
+ host?.remove();
+ host = null;
+ root = null;
+ fixture.holdCatalog = false;
+ fixture.answerCatalog = null;
+ fixture.answerEmail = null;
});
describe("app names for the settings search", () => {
+ // Runs first: the app catalog is cached once it loads, and only a failed load is asked again.
+ it("stays loading while the app catalog has not answered, and stops when it fails", async () => {
+ fixture.holdCatalog = true;
+ const view = render();
+ await settle();
+ fixture.answerEmail?.(true);
+ await settle();
+ expect(view.textContent).toBe("loading:email,github");
+ fixture.answerCatalog?.(false);
+ await settle();
+ expect(view.textContent).toBe("ready:email,github");
+ });
+
it("stays loading while Email has not answered, even once the other apps are in", async () => {
+ const view = render();
await settle();
- expect(host.textContent).toBe("loading:github");
+ expect(view.textContent).toBe("loading:github");
fixture.answerEmail?.(true);
await settle();
- expect(host.textContent).toBe("ready:email,github");
+ expect(view.textContent).toBe("ready:email,github");
});
it("stops loading when the Email lookup fails", async () => {
+ const view = render();
await settle();
fixture.answerEmail?.(false);
await settle();
- expect(host.textContent).toBe("ready:github");
+ expect(view.textContent).toBe("ready:github");
+ });
+
+ it("is loading again while a new lookup runs after an earlier one failed", async () => {
+ const view = render();
+ await settle();
+ fixture.answerEmail?.(false);
+ await settle();
+ expect(view.textContent).toBe("ready:github");
+ render(false);
+ render(true);
+ await settle();
+ expect(view.textContent).toBe("loading:github");
+ fixture.answerEmail?.(true);
+ await settle();
+ expect(view.textContent).toBe("ready:email,github");
});
});
diff --git a/src/components/bot-settings/useBotApps.ts b/src/components/bot-settings/useBotApps.ts
index 3792c0e746..32d2c0d5da 100644
--- a/src/components/bot-settings/useBotApps.ts
+++ b/src/components/bot-settings/useBotApps.ts
@@ -73,8 +73,9 @@ export function botAppsFrom(
return { phase: "ready", apps, connectedSlugs };
}
-/** The apps plus whether the Email lookup has answered (or failed); `email` alone can't tell pending from failed. */
+/** The apps plus whether the app catalog and Email lookups have answered (or failed); the values alone can't tell pending from failed. */
export interface BotAppsState extends BotApps {
+ cardsSettled: boolean;
emailSettled: boolean;
}
@@ -83,16 +84,21 @@ export function useBotApps(bot: Bot, load = true): BotAppsState {
const [inventory, setInventory] = useState<ConnectorInventory | null>(null);
const [cards, setCards] = useState<ReadonlyMap<string, ToolkitCard>>(() => new Map());
const [email, setEmail] = useState<EmailStatus | null>(null);
+ const [cardsSettled, setCardsSettled] = useState(false);
const [emailSettled, setEmailSettled] = useState(false);
useEffect(() => {
if (!load) return;
let cancelled = false;
+ setCardsSettled(false);
+ setEmailSettled(false);
void preloadConnectedApps().then((result) => {
if (!cancelled) setInventory(result);
});
void loadAppCards().then((result) => {
- if (!cancelled) setCards(result);
+ if (cancelled) return;
+ setCards(result);
+ setCardsSettled(true);
});
void api("/api/email")
.then((result: EmailStatus) => { if (!cancelled) setEmail(result); })
@@ -103,5 +109,5 @@ export function useBotApps(bot: Bot, load = true): BotAppsState {
};
}, [load]);
- return { ...botAppsFrom(bot, inventory, cards, email), emailSettled };
+ return { ...botAppsFrom(bot, inventory, cards, email), cardsSettled, emailSettled };
}