From 08c0368bad53cf017cc69f5cfb4d6695faba7aa9 Mon Sep 17 00:00:00 2001 From: Matthew Meszaros Date: Sun, 1 Nov 2026 12:10:15 +0100 Subject: [PATCH] feat: refresh visible mailbox health and error reports from current server evidence every minute and on stale tab return --- docs/content/docs/guides/mailboxes.mdx | 2 + .../analytics/accountStatusRecovery.test.tsx | 131 ++++++++++++++++++ .../hooks/app/analytics/useAccountStatus.ts | 5 +- .../hooks/app/analytics/useAccountStatuses.ts | 3 + 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 web/src/lib/api/hooks/app/analytics/accountStatusRecovery.test.tsx diff --git a/docs/content/docs/guides/mailboxes.mdx b/docs/content/docs/guides/mailboxes.mdx index 73b8b832a..ee3ceb84f 100644 --- a/docs/content/docs/guides/mailboxes.mdx +++ b/docs/content/docs/guides/mailboxes.mdx @@ -642,6 +642,8 @@ The Accounts list groups mailboxes as **Healthy** (sending normally), **Warming* Repeated reports of the same error code count once toward the health score, at their highest unresolved severity, rather than making one problem look like several. The drawer groups identical reports while keeping different error details and sending versus sync failures separate. Connection titles name Gmail, Microsoft Outlook or the mail server. **Reported** shows the stored report time: connection and temporary provider-limit observations refresh it, while other error categories may retain their original report time. It is not proof of an ongoing outage. +While visible, the Accounts list and mailbox drawer refresh their health and error reports every minute as well as on relevant live events. Returning to a tab refreshes stale reports too. These reads show the server's latest recovery evidence; they do not reconnect a mailbox or retry a send. A failed status refresh does not count as recovery. + A later completed sync automatically clears earlier temporary sync errors. A provider-confirmed send clears earlier temporary sending errors, including provider throttling and quota warnings. Recovered reports no longer appear under **Needs attention** or reduce the health score. Older reports that do not identify which operation failed clear once both sending and sync have succeeded after the report; existing reports with that evidence are cleared on upgrade too. A newer failure remains visible, and simply waiting or completing a queued task does not count as recovery. Recoverable connection failures retry automatically while the mailbox is on; a connection warning alone is not a reason to reconnect. A temporary connection, server or throttling failure is not shown at all while the affected sync or sending has still succeeded within the last two hours, because the mailbox is working. It appears only when the failure lasts longer than that. Authentication, credential and policy errors retain their own instructions, and a switched-off mailbox must be switched back on before it can retry. A send safety hold is different from a retryable connection failure. If Warmbly cannot confirm whether the provider accepted a message, sending pauses to avoid retrying a message that might already have been sent. The mailbox is shown as needing attention even without a separate connection warning, and its health details explain the hold. A successful inbox sync does not confirm a send. The hold clears on its own and no customer action is needed. After ten minutes Warmbly sets the uncertain email aside for good: a campaign email waits for confirmation for that one contact while the mailbox sends to everyone else, and a Unibox, placement-test or Warmbly Cloud send simply never runs again. A send that never reached the worker is handed back after 30 minutes so the step can go out fresh. A late confirmed result still updates the email automatically. Only legacy sends without durable execution authorization need provider-confirmed resolution. See [send safety](/development/send-safety/). diff --git a/web/src/lib/api/hooks/app/analytics/accountStatusRecovery.test.tsx b/web/src/lib/api/hooks/app/analytics/accountStatusRecovery.test.tsx new file mode 100644 index 000000000..5b64997aa --- /dev/null +++ b/web/src/lib/api/hooks/app/analytics/accountStatusRecovery.test.tsx @@ -0,0 +1,131 @@ +import type { PropsWithChildren } from "react"; +import { act, cleanup, renderHook } from "@testing-library/react"; +import { focusManager, QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import getAccountStatus from "@/lib/api/client/app/analytics/getAccountStatus"; +import getAccountStatuses from "@/lib/api/client/app/analytics/getAccountStatuses"; +import type AccountStatus from "@/lib/api/models/app/analytics/AccountStatus"; +import useAccountStatus from "./useAccountStatus"; +import useAccountStatuses from "./useAccountStatuses"; + +vi.mock("@/lib/api/client/app/analytics/getAccountStatus", () => ({ default: vi.fn() })); +vi.mock("@/lib/api/client/app/analytics/getAccountStatuses", () => ({ default: vi.fn() })); + +function mailbox(recovered: boolean): AccountStatus { + return { + id: "mailbox", email: "mailbox@example.test", provider: "smtp_imap", status: "active", + last_synced_at: recovered ? new Date() : new Date(Date.now() - 3 * 60 * 60_000), + health: { status: recovered ? "healthy" : "warning", score: recovered ? 100 : 80 }, + errors: recovered ? [] : [{ + id: "failure", error_code: "SERVER_UNREACHABLE", severity: "WARNING", operation: "sync", + title: "Mail server sync connection failed", message: "Automatic retry", created_at: new Date(), + }], + daily_usage: { date: "2026-11-01", campaign_sent: 0, campaign_limit: 10 }, in_campaign: false, + }; +} + +function harness() { + const client = new QueryClient({ defaultOptions: { queries: { + retry: false, gcTime: Infinity, staleTime: 30_000, refetchOnWindowFocus: false, + } } }); + const wrapper = ({ children }: PropsWithChildren) => {children}; + const ids = ["mailbox"]; + const hook = renderHook(({ enabled }) => ({ + drawer: useAccountStatus(enabled ? "mailbox" : ""), list: useAccountStatuses(ids, { enabled }), + }), { wrapper, initialProps: { enabled: true } }); + return { client, ...hook }; +} + +beforeEach(() => { + vi.useFakeTimers(); + vi.clearAllMocks(); + focusManager.setFocused(true); + vi.mocked(getAccountStatus).mockResolvedValue(mailbox(false)); + vi.mocked(getAccountStatuses).mockResolvedValue([mailbox(false)]); +}); + +afterEach(() => { + cleanup(); + focusManager.setFocused(undefined); + vi.useRealTimers(); +}); + +describe("mailbox status freshness without realtime events", () => { + it("replaces a recovered warning and its old success time in both open views", async () => { + const { client, result, unmount } = harness(); + await act(() => vi.advanceTimersByTimeAsync(1)); + expect(result.current.drawer.data?.errors).toHaveLength(1); + expect(result.current.list.data[0]?.errors).toHaveLength(1); + + const recovered = mailbox(true); + vi.mocked(getAccountStatus).mockResolvedValue(recovered); + vi.mocked(getAccountStatuses).mockResolvedValue([recovered]); + await act(() => vi.advanceTimersByTimeAsync(59_000)); + expect(getAccountStatus).toHaveBeenCalledTimes(1); + expect(getAccountStatuses).toHaveBeenCalledTimes(1); + await act(() => vi.advanceTimersByTimeAsync(1_001)); + expect(result.current.drawer.data?.errors).toEqual([]); + expect(result.current.drawer.data?.last_synced_at).toEqual(recovered.last_synced_at); + expect(result.current.list.data[0]?.errors).toEqual([]); + expect(result.current.list.data[0]?.last_synced_at).toEqual(recovered.last_synced_at); + unmount(); client.clear(); + }); + + it("retains a reported failure until the server confirms recovery, and stops after unmount", async () => { + const { client, result, unmount } = harness(); + await act(() => vi.advanceTimersByTimeAsync(60_002)); + expect(getAccountStatus).toHaveBeenCalledTimes(2); + expect(getAccountStatuses).toHaveBeenCalledTimes(2); + expect(result.current.drawer.data?.errors).toHaveLength(1); + expect(result.current.list.data[0]?.errors).toHaveLength(1); + unmount(); + await act(() => vi.advanceTimersByTimeAsync(120_000)); + expect(getAccountStatus).toHaveBeenCalledTimes(2); + expect(getAccountStatuses).toHaveBeenCalledTimes(2); + client.clear(); + }); + + it("pauses periodic reads while hidden and refreshes immediately on return", async () => { + const { client, result, unmount } = harness(); + await act(() => vi.advanceTimersByTimeAsync(1)); + act(() => focusManager.setFocused(false)); + const recovered = mailbox(true); + vi.mocked(getAccountStatus).mockResolvedValue(recovered); + vi.mocked(getAccountStatuses).mockResolvedValue([recovered]); + await act(() => vi.advanceTimersByTimeAsync(60_001)); + expect(getAccountStatus).toHaveBeenCalledTimes(1); + expect(getAccountStatuses).toHaveBeenCalledTimes(1); + act(() => focusManager.setFocused(true)); + await act(() => vi.advanceTimersByTimeAsync(1)); + expect(result.current.drawer.data?.errors).toEqual([]); + expect(result.current.list.data[0]?.errors).toEqual([]); + unmount(); client.clear(); + }); + + it("keeps the last report when a refresh fails instead of assuming recovery", async () => { + const { client, result, unmount } = harness(); + await act(() => vi.advanceTimersByTimeAsync(1)); + vi.mocked(getAccountStatus).mockRejectedValue(new Error("Status unavailable")); + vi.mocked(getAccountStatuses).mockRejectedValue(new Error("Status unavailable")); + await act(() => vi.advanceTimersByTimeAsync(60_001)); + expect(result.current.drawer.isError).toBe(true); + expect(result.current.list.isError).toBe(true); + expect(result.current.drawer.data?.errors).toHaveLength(1); + expect(result.current.list.data[0]?.errors).toHaveLength(1); + expect(result.current.list.observations[0]?.current).toBe(false); + unmount(); client.clear(); + }); + + it("respects disabled status reads during timer and focus changes", async () => { + const { client, rerender, unmount } = harness(); + await act(() => vi.advanceTimersByTimeAsync(1)); + rerender({ enabled: false }); + act(() => focusManager.setFocused(false)); + await act(() => vi.advanceTimersByTimeAsync(60_001)); + act(() => focusManager.setFocused(true)); + await act(() => vi.advanceTimersByTimeAsync(60_001)); + expect(getAccountStatus).toHaveBeenCalledTimes(1); + expect(getAccountStatuses).toHaveBeenCalledTimes(1); + unmount(); client.clear(); + }); +}); diff --git a/web/src/lib/api/hooks/app/analytics/useAccountStatus.ts b/web/src/lib/api/hooks/app/analytics/useAccountStatus.ts index 3a3786299..fa96edbe0 100644 --- a/web/src/lib/api/hooks/app/analytics/useAccountStatus.ts +++ b/web/src/lib/api/hooks/app/analytics/useAccountStatus.ts @@ -6,5 +6,8 @@ export default function useAccountStatus(id: string) { queryKey: ["analytics", "accounts", id], queryFn: () => getAccountStatus(id), enabled: !!id, - }) + // Routine recovery persists without a realtime event. + refetchInterval: 60_000, + refetchOnWindowFocus: true, + }); } diff --git a/web/src/lib/api/hooks/app/analytics/useAccountStatuses.ts b/web/src/lib/api/hooks/app/analytics/useAccountStatuses.ts index bfd442504..fc1279176 100644 --- a/web/src/lib/api/hooks/app/analytics/useAccountStatuses.ts +++ b/web/src/lib/api/hooks/app/analytics/useAccountStatuses.ts @@ -49,6 +49,9 @@ export default function useAccountStatuses(emailIds: string[], options?: { enabl queryKey: ["analytics", "accounts", "list", ids, sourceRevision], queryFn: ({ signal }: { signal: AbortSignal }) => getAccountStatuses(ids, signal), enabled: ids.length > 0 && enabled, + // Routine recovery persists without a realtime event. + refetchInterval: 60_000, + refetchOnWindowFocus: true, })), [chunks, enabled, sourceRevision], );