Skip to content

Commit b6d611a

Browse files
committed
fix(web): keep usage-limit snooze actions available
1 parent 54983e3 commit b6d611a

2 files changed

Lines changed: 110 additions & 11 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { RunId } from "@t3tools/contracts";
2+
import { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
5+
6+
import "~/index.css";
7+
import { ComposerBannerStack } from "./ComposerBannerStack";
8+
import { usageLimitRecoveryBannerItem } from "./UsageLimitRecoveryBanner";
9+
10+
const runId = RunId.make("limited-run");
11+
const resetAt = "2026-10-19T12:00:00.000Z";
12+
const onChange = vi.fn(async () => {});
13+
let root: Root;
14+
let container: HTMLDivElement;
15+
16+
beforeEach(() => {
17+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
18+
vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] });
19+
vi.setSystemTime(new Date("2026-10-19T11:59:00.000Z"));
20+
onChange.mockClear();
21+
container = document.createElement("div");
22+
document.body.append(container);
23+
root = createRoot(container);
24+
});
25+
26+
afterEach(async () => {
27+
await act(() => root.unmount());
28+
container.remove();
29+
vi.useRealTimers();
30+
vi.unstubAllGlobals();
31+
});
32+
33+
async function renderBanner(
34+
overrides: Partial<Parameters<typeof usageLimitRecoveryBannerItem>[0]> = {},
35+
) {
36+
const item = usageLimitRecoveryBannerItem({
37+
runId,
38+
resetAt,
39+
stoppedAt: "2026-10-19T11:00:00.000Z",
40+
snoozedUntil: resetAt,
41+
recovery: { runId, resetAt, autoResume: true, snooze: true },
42+
onChange,
43+
...overrides,
44+
});
45+
await act(() => root.render(<ComposerBannerStack items={[item]} />));
46+
}
47+
48+
function button(label: string) {
49+
const match = [...container.querySelectorAll("button")].find(
50+
(node) => node.textContent === label,
51+
);
52+
expect(match, `Expected a ${label} button`).toBeDefined();
53+
return match!;
54+
}
55+
56+
it("lets a snoozed thread wake without cancelling auto-resume", async () => {
57+
await renderBanner();
58+
expect(button("Cancel auto-resume").disabled).toBe(false);
59+
await act(() => button("Wake now").click());
60+
expect(onChange).toHaveBeenCalledExactlyOnceWith({ runId, resetAt, snooze: false });
61+
});
62+
63+
it("keeps snooze available independently of auto-resume", async () => {
64+
await renderBanner({
65+
snoozedUntil: null,
66+
recovery: { runId, resetAt, autoResume: false, snooze: false },
67+
});
68+
await act(() => button("Snooze until reset").click());
69+
expect(onChange).toHaveBeenCalledExactlyOnceWith({ runId, resetAt, snooze: true });
70+
onChange.mockClear();
71+
await renderBanner();
72+
await act(() => button("Cancel auto-resume").click());
73+
expect(onChange).toHaveBeenCalledExactlyOnceWith({ runId, resetAt, autoResume: false });
74+
});
75+
76+
it("stops offering Wake now when the reset passes without a server update", async () => {
77+
await renderBanner();
78+
button("Wake now");
79+
await act(async () => vi.advanceTimersByTime(60_001));
80+
expect(button("Snooze until reset").disabled).toBe(true);
81+
expect(container.textContent).not.toContain("Wake now");
82+
await act(() => button("Snooze until reset").click());
83+
expect(onChange).not.toHaveBeenCalled();
84+
});
85+
86+
it("does not treat a persisted, expired snooze as active on mount", async () => {
87+
vi.setSystemTime(new Date("2026-10-19T12:01:00.000Z"));
88+
await renderBanner();
89+
expect(button("Snooze until reset").disabled).toBe(true);
90+
expect(container.textContent).not.toContain("Wake now");
91+
});
92+
93+
it("does not wake a snooze belonging to another run", async () => {
94+
await renderBanner({
95+
recovery: { runId: RunId.make("older-run"), resetAt, autoResume: true, snooze: true },
96+
});
97+
expect(button("Resume at reset").disabled).toBe(false);
98+
expect(button("Snooze until reset").disabled).toBe(false);
99+
expect(container.textContent).not.toContain("Wake now");
100+
});

‎apps/web/src/components/chat/UsageLimitRecoveryBanner.tsx‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: R
5252
recovery.resetAt === resetAt &&
5353
resetAt !== null &&
5454
snoozedUntil !== null &&
55-
Date.parse(snoozedUntil) === Date.parse(resetAt);
55+
Date.parse(snoozedUntil) === Date.parse(resetAt) &&
56+
Date.parse(snoozedUntil) > nowMs;
5657
async function toggle(action: "resume" | "snooze") {
5758
if (resetAt === null) return;
5859
if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
@@ -87,16 +88,14 @@ function RecoveryActions({ runId, resetAt, recovery, snoozedUntil, onChange }: R
8788
<Button size="xs" variant="ghost" disabled={pending} onClick={() => void toggle("resume")}>
8889
{pending ? "Saving..." : scheduled ? "Cancel auto-resume" : "Resume at reset"}
8990
</Button>
90-
{!snoozed ? (
91-
<Button
92-
size="xs"
93-
variant="ghost"
94-
disabled={pending || Date.parse(resetAt!) <= nowMs}
95-
onClick={() => void toggle("snooze")}
96-
>
97-
{pending ? "Saving..." : "Snooze until reset"}
98-
</Button>
99-
) : null}
91+
<Button
92+
size="xs"
93+
variant="ghost"
94+
disabled={pending || (!snoozed && Date.parse(resetAt!) <= nowMs)}
95+
onClick={() => void toggle("snooze")}
96+
>
97+
{pending ? "Saving..." : snoozed ? "Wake now" : "Snooze until reset"}
98+
</Button>
10099
</>
101100
);
102101
}

0 commit comments

Comments
 (0)