Skip to content

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

fix: preserve current app inventory during refresh outages

Afloat9c87e3f4b979
diff --git a/src/lib/connected-apps-outages.test.ts b/src/lib/connected-apps-outages.test.ts
new file mode 100644
index 0000000000..3641ecd16a
--- /dev/null
+++ b/src/lib/connected-apps-outages.test.ts
@@ -0,0 +1,49 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const fixture = vi.hoisted(() => ({ api: vi.fn(), disk: vi.fn(), write: vi.fn() }));
+vi.mock("@/state/store", () => ({ api: fixture.api }));
+vi.mock("@/lib/connected-apps-cache", () => ({ readCachedInventory: fixture.disk, writeCachedInventory: fixture.write }));
+
+const old = { gmail: { connected: true, accounts: [{ id: "removed", status: "ACTIVE" }] } };
+const current = { gmail: { connected: false, accounts: [] } };
+
+beforeEach(() => {
+ vi.resetModules();
+ vi.resetAllMocks();
+ fixture.disk.mockReturnValue({ at: 1, services: old });
+});
+
+describe("connected app refresh failures", () => {
+ it("keeps the memory cache non-authoritative after a rejected forced refresh", async () => {
+ const { rememberConnectorStatus, preloadConnectedApps, connectorStatusMemory } = await import("./connected-apps");
+ rememberConnectorStatus(current, true);
+ fixture.api.mockRejectedValue(new Error("offline"));
+ expect(await preloadConnectedApps(true)).toEqual({ services: current, authoritative: false });
+ expect(connectorStatusMemory()).toEqual({ services: current, authoritative: false });
+ expect(await preloadConnectedApps()).toEqual({ services: current, authoritative: false });
+ });
+
+ it.each([{ credentialStore: "unavailable" }, { inventoryComplete: false }])("keeps newer panel state when the server returns %j", async (outage) => {
+ const { rememberConnectorStatus, preloadConnectedApps } = await import("./connected-apps");
+ rememberConnectorStatus(current, true);
+ fixture.api.mockResolvedValue({ ...outage, services: {} });
+ expect(await preloadConnectedApps(true)).toEqual({ services: current, authoritative: false });
+ expect(fixture.write).not.toHaveBeenCalled();
+ });
+
+ it("uses saved inventory when the page has not read any live state", async () => {
+ const { preloadConnectedApps } = await import("./connected-apps");
+ fixture.api.mockRejectedValue(new Error("offline"));
+ expect(await preloadConnectedApps()).toEqual({ services: old, authoritative: false });
+ });
+
+ it("restores an authoritative cache after a complete successful refresh", async () => {
+ const { rememberConnectorStatus, preloadConnectedApps, connectorStatusMemory } = await import("./connected-apps");
+ rememberConnectorStatus(current, true);
+ fixture.api.mockRejectedValueOnce(new Error("offline")).mockResolvedValueOnce({ services: old });
+ await preloadConnectedApps(true);
+ expect(await preloadConnectedApps(true)).toEqual({ services: old, authoritative: true });
+ expect(connectorStatusMemory()).toEqual({ services: old, authoritative: true });
+ expect(fixture.write).toHaveBeenCalledExactlyOnceWith(old, expect.any(Number));
+ });
+});
diff --git a/src/lib/connected-apps.ts b/src/lib/connected-apps.ts
index 6ad94fe159..f9f2179fa8 100644
--- a/src/lib/connected-apps.ts
+++ b/src/lib/connected-apps.ts
@@ -22,6 +22,11 @@ export interface ConnectorInventory {
const REFUSED_INVENTORY: ConnectorInventory = { services: {}, authoritative: true, refused: true };
+function unavailableInventory(fallback: Record<string, ConnectorStatus> = {}): ConnectorInventory {
+ cachedConnectorStatusAuthoritative = false;
+ return { services: cachedConnectorStatus ?? readCachedInventory()?.services ?? fallback, authoritative: false };
+}
+
export function preloadConnectedApps(force = false, background = false): Promise<ConnectorInventory> { // background: src/state/api.ts
if (!force && cachedConnectorStatus !== null && Date.now() - cachedConnectorStatusAt < CONNECTOR_STATUS_CACHE_MS) {
return Promise.resolve({
@@ -36,8 +41,7 @@ export function preloadConnectedApps(force = false, background = false): Promise
if (!response) return REFUSED_INVENTORY;
const services: Record<string, ConnectorStatus> = response.services ?? {};
if (response.credentialStore === "unavailable" || response.inventoryComplete === false) {
- cachedConnectorStatusAuthoritative = false;
- return { services: readCachedInventory()?.services ?? services, authoritative: false };
+ return unavailableInventory(services);
}
cachedConnectorStatus = services;
cachedConnectorStatusAt = Date.now();
@@ -45,7 +49,7 @@ export function preloadConnectedApps(force = false, background = false): Promise
writeCachedInventory(services, Date.now());
return { services, authoritative: true };
})
- .catch(() => ({ services: readCachedInventory()?.services ?? {}, authoritative: false }))
+ .catch(() => unavailableInventory())
.finally(() => {
connectorStatusRequest = null;
});