Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadErrorBanner.test.tsx

137 lines4.8 KB

1import { renderToStaticMarkup } from "react-dom/server";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 dismissThreadErrorBannerForSession,
6 getThreadErrorBannerKey,
7 isThreadErrorBannerDismissedForSession,
8 shouldShowThreadErrorBanner,
9 ThreadErrorBanner,
10} from "./ThreadErrorBanner";
11​
12describe("ThreadErrorBanner", () => {
13 it("stays hidden after its current error is dismissed", () => {
14 const bannerKey = getThreadErrorBannerKey("env:thread-a", "Aborted");
15 dismissThreadErrorBannerForSession(bannerKey);
16​
17 expect(
18 shouldShowThreadErrorBanner(
19 "env:thread-a",
20 "Aborted",
21 isThreadErrorBannerDismissedForSession(bannerKey),
22 ),
23 ).toBe(false);
24 });
25​
26 it("reappears when a new error arrives on the same thread", () => {
27 dismissThreadErrorBannerForSession(getThreadErrorBannerKey("env:thread-b", "Turn failed"));
28 const newErrorKey = getThreadErrorBannerKey("env:thread-b", "Provider crashed");
29​
30 expect(isThreadErrorBannerDismissedForSession(newErrorKey)).toBe(false);
31 expect(
32 shouldShowThreadErrorBanner(
33 "env:thread-b",
34 "Provider crashed",
35 isThreadErrorBannerDismissedForSession(newErrorKey),
36 ),
37 ).toBe(true);
38 });
39​
40 it("scopes dismissals to the thread that dismissed them", () => {
41 dismissThreadErrorBannerForSession(getThreadErrorBannerKey("env:thread-c", "Aborted"));
42 const otherThreadKey = getThreadErrorBannerKey("env:other-thread", "Aborted");
43​
44 expect(isThreadErrorBannerDismissedForSession(otherThreadKey)).toBe(false);
45 expect(
46 shouldShowThreadErrorBanner(
47 "env:other-thread",
48 "Aborted",
49 isThreadErrorBannerDismissedForSession(otherThreadKey),
50 ),
51 ).toBe(true);
52 });
53​
54 it("keeps a dismissal across visiting threads with no error", () => {
55 const bannerKey = getThreadErrorBannerKey("env:thread-d", "Aborted");
56 dismissThreadErrorBannerForSession(bannerKey);
57​
58 expect(shouldShowThreadErrorBanner("env:thread-d", null, false)).toBe(false);
59 expect(isThreadErrorBannerDismissedForSession(bannerKey)).toBe(true);
60 expect(
61 shouldShowThreadErrorBanner(
62 "env:thread-d",
63 "Aborted",
64 isThreadErrorBannerDismissedForSession(bannerKey),
65 ),
66 ).toBe(false);
67 });
68​
69 it("never shows a null error", () => {
70 expect(shouldShowThreadErrorBanner("env:thread-e", null, false)).toBe(false);
71 });
72​
73 it("calms a usage-limit failure to a countdown notice instead of the raw error", () => {
74 const error = "Your org has used all tokens under the current rate limit";
75 const markup = renderToStaticMarkup(
76 <ThreadErrorBanner
77 error={error}
78 usageLimitResetsAt="2099-01-01T12:00:00.000Z"
79 onDismiss={() => {}}
80 />,
81 );
82​
83 expect(markup).toContain("Reached your plan&#x27;s usage limit");
84 // The countdown owns the whole phrase so a passed window can collapse
85 // the preposition without the banner duplicating it.
86 expect(markup).toMatch(/<span[^>]*>tokens return in \d+h \d+m<\/span>/);
87 expect(markup).not.toContain(error);
88 expect(markup).not.toContain('aria-label="Dismiss error"');
89 });
90​
91 it("shows the raw error again once the limit class clears", () => {
92 const markup = renderToStaticMarkup(
93 <ThreadErrorBanner error="Provider crashed" usageLimitResetsAt={null} />,
94 );
95​
96 expect(markup).toContain("Provider crashed");
97 expect(markup).not.toContain("usage limit");
98 });
99​
100 it("keeps a passed window grammatical instead of reading 'ready soon'", () => {
101 const markup = renderToStaticMarkup(
102 <ThreadErrorBanner
103 error="Your org has used all tokens under the current rate limit"
104 usageLimitResetsAt="2020-01-01T12:00:00.000Z"
105 onDismiss={() => {}}
106 />,
107 );
108​
109 expect(markup).toContain("tokens return now");
110 expect(markup).not.toContain("ready soon");
111 });
112​
113 it("shows the raw error when the usage-limit class is replaced by another error", () => {
114 const markup = renderToStaticMarkup(
115 <ThreadErrorBanner error="Provider crashed" usageLimitResetsAt={null} />,
116 );
117​
118 expect(markup).toContain("Provider crashed");
119 expect(markup).not.toContain("Reached your plan");
120 });
121 it("aligns the warning and dismiss icons with the first line of a multi-line error", () => {
122 const markup = renderToStaticMarkup(
123 <ThreadErrorBanner
124 error={"The first error line\ncontinues on a second line"}
125 onDismiss={() => {}}
126 />,
127 );
128​
129 expect(markup).toContain('role="alert"');
130 expect(markup).toContain('aria-label="Dismiss error"');
131 expect(markup).not.toContain("controlAlignment");
132 expect(markup).toContain("flex gap-2 items-start");
133 expect(markup).toContain("min-h-7 pt-1 sm:min-h-6 sm:pt-0.5");
134 expect(markup).toContain("h-lh w-4");
135 expect(markup).toContain("h-lh self-start");
136 });
137});