Skip to content

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

Show thread cleanup errors on the row they belong to

aivsomkarf69f6b37a5ff
diff --git a/src/components/ThreadCleanupSettings.dom.test.ts b/src/components/ThreadCleanupSettings.dom.test.ts
new file mode 100644
index 0000000000..cf08b62607
--- /dev/null
+++ b/src/components/ThreadCleanupSettings.dom.test.ts
@@ -0,0 +1,54 @@
+// @vitest-environment happy-dom
+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 { setLocale } from "@/lib/i18n";
+import { ThreadCleanupSettings } from "./ThreadCleanupSettings";
+
+const fixture = vi.hoisted(() => ({ api: vi.fn() }));
+vi.mock("@/state/store", () => ({
+ api: fixture.api,
+ useStore: () => ({ state: { config: { threads: { maxConcurrentPerBot: 3, eventLogRetentionDays: 14, eventLogMaxBytes: 52428800 } } }, dispatch: vi.fn() }),
+}));
+
+let host: HTMLDivElement;
+let root: Root;
+const type = (id: string, value: string) => {
+ const field = host.querySelector<HTMLInputElement>(`#${id}`)!;
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(field, value);
+ flushSync(() => field.dispatchEvent(new Event("input", { bubbles: true })));
+ flushSync(() => field.dispatchEvent(new FocusEvent("focusout", { bubbles: true })));
+};
+const rowOfAlert = () => {
+ const row = host.querySelector('[role="alert"]')?.closest('[role="group"]');
+ return row ? document.getElementById(row.getAttribute("aria-labelledby")!)?.textContent : null;
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ setLocale("en");
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+ flushSync(() => root.render(createElement(ThreadCleanupSettings)));
+});
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+});
+
+describe("ThreadCleanupSettings errors", () => {
+ it("shows an invalid retention under the retention row", () => {
+ type("thread-log-retention-days", "0");
+ expect(rowOfAlert()).toBe("Delete old event logs");
+ });
+
+ it("shows a failed retention save under the retention row", async () => {
+ fixture.api.mockRejectedValue(new Error("Disk is read-only"));
+ type("thread-log-retention-days", "30");
+ await vi.waitFor(() => expect(host.querySelector('[role="alert"]')?.textContent).toBe("Disk is read-only"));
+ expect(rowOfAlert()).toBe("Delete old event logs");
+ });
+});
diff --git a/src/components/ThreadCleanupSettings.tsx b/src/components/ThreadCleanupSettings.tsx
index eefc7354d9..269060f6a6 100644
--- a/src/components/ThreadCleanupSettings.tsx
+++ b/src/components/ThreadCleanupSettings.tsx
@@ -69,6 +69,7 @@ export function ThreadCleanupSettings() {
dispatch({ type: "configStatus", config });
} catch (cause) {
setError(cause instanceof Error ? cause.message : t("settings.threadCleanup.error"));
+ setErrorKnob(knob);
} finally {
saving.current = false;
setPending(null);
@@ -170,15 +171,19 @@ export function ThreadCleanupSettings() {
);
};
+ const message = (knob: Knob) => pending === knob ? <span role="status">{t("settings.threadCleanup.saving")}</span>
+ : error && errorKnob === knob ? <span role="alert" className="text-danger">{error}</span> : undefined;
+
return (
<SettingsGroup label={t("settings.threadCleanup.title")} block="cleanup">
<SettingsRows>
- <SettingRow title={t("settings.threadCleanup.retention.title")} subtitle={confirmedRetentionDays !== null ? <>{t("settings.threadCleanup.retention.short")}{field("retention")}</> : t("settings.threadCleanup.retention.plain")}>
+ <SettingRow title={t("settings.threadCleanup.retention.title")} subtitle={confirmedRetentionDays !== null ? <>{t("settings.threadCleanup.retention.short")}{field("retention")}</> : t("settings.threadCleanup.retention.plain")}
+ message={message("retention")}>
<Switch id="thread-log-retention-enabled" checked={confirmedRetentionDays !== null} disabled={pending !== null}
aria-label={t("settings.threadCleanup.retention.title")} onClick={() => toggleRetention(confirmedRetentionDays === null)} />
</SettingRow>
<SettingRow title={t("settings.threadCleanup.cap.title")} subtitle={confirmedCapBytes !== null ? <>{t("settings.threadCleanup.cap.short")}{field("cap")}</> : t("settings.threadCleanup.cap.plain")}
- message={pending !== null ? <span role="status">{t("settings.threadCleanup.saving")}</span> : error ? <span role="alert" className="text-danger">{error}</span> : undefined}>
+ message={message("cap")}>
<Switch id="thread-log-cap-enabled" checked={confirmedCapBytes !== null} disabled={pending !== null}
aria-label={t("settings.threadCleanup.cap.title")} onClick={() => toggleCap(confirmedCapBytes === null)} />
</SettingRow>