1+ // @vitest -environment jsdom
2+
13import {
24 EnvironmentId ,
35 ProviderDriverKind ,
46 ProviderInstanceId ,
57 type ServerProvider ,
68} from "@t3tools/contracts" ;
79import type { LimitPresentations } from "@t3tools/shared/usageLimits" ;
8- import { act , type PropsWithChildren , type ReactElement } from "react" ;
9- import { create , type ReactTestRenderer } from "react-test-renderer " ;
10+ import { act } from "react" ;
11+ import { createRoot , type Root } from "react-dom/client " ;
1012import { afterEach , beforeEach , expect , it , vi } from "vite-plus/test" ;
1113
1214const consume = vi . hoisted ( ( ) => vi . fn ( ) ) ;
13-
1415vi . mock ( "../../state/session" , ( ) => ( {
1516 useEnvironmentScope : ( ) => true ,
1617 readEnvironmentScope : ( ) => true ,
@@ -20,43 +21,6 @@ vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => consume }
2021vi . mock ( "../../hooks/useSettings" , ( ) => ( { usePrimarySettings : ( ) => "24h" } ) ) ;
2122vi . mock ( "../chat/ProviderInstanceIcon" , ( ) => ( { ProviderInstanceIcon : ( ) => null } ) ) ;
2223vi . mock ( "../settings/RedactedSensitiveText" , ( ) => ( { RedactedSensitiveText : ( ) => null } ) ) ;
23- vi . mock ( "../ui/button" , ( ) => ( { Button : "button" } ) ) ;
24- vi . mock ( "../ui/tooltip" , ( ) => ( { Tooltip : "div" , TooltipPopup : "div" , TooltipTrigger : "div" } ) ) ;
25- // Popovers and dialogs portal and animate; these keep only the open state the segment owns.
26- vi . mock ( "../ui/popover" , async ( ) => {
27- const React = await import ( "react" ) ;
28- const Open = React . createContext ( { open : false , toggle : ( ) => { } } ) ;
29- return {
30- Popover : ( {
31- open,
32- onOpenChange,
33- children,
34- } : PropsWithChildren < { open : boolean ; onOpenChange : ( open : boolean ) => void } > ) => {
35- const value = React . useMemo (
36- ( ) => ( { open, toggle : ( ) => onOpenChange ( ! open ) } ) ,
37- [ open , onOpenChange ] ,
38- ) ;
39- return < Open . Provider value = { value } > { children } </ Open . Provider > ;
40- } ,
41- PopoverTrigger : ( {
42- render,
43- children,
44- } : PropsWithChildren < { render : ReactElement < { onClick ?: ( ) => void } > } > ) =>
45- React . cloneElement ( render , { onClick : React . useContext ( Open ) . toggle } , children ) ,
46- PopoverPopup : ( { children } : PropsWithChildren ) =>
47- React . useContext ( Open ) . open ? < div > { children } </ div > : null ,
48- } ;
49- } ) ;
50- vi . mock ( "../ui/alert-dialog" , ( ) => ( {
51- AlertDialog : ( { open, children } : PropsWithChildren < { open : boolean } > ) =>
52- open ? < div role = "dialog" > { children } </ div > : null ,
53- AlertDialogPopup : "div" ,
54- AlertDialogHeader : "div" ,
55- AlertDialogTitle : "div" ,
56- AlertDialogDescription : "div" ,
57- AlertDialogFooter : "div" ,
58- AlertDialogClose : "div" ,
59- } ) ) ;
6024
6125import { UsageLimitsPooled } from "./UsageLimitsPooled" ;
6226
@@ -102,61 +66,72 @@ const presentations: LimitPresentations = new Map([
10266 ] ,
10367] ) ;
10468
105- let renderer : ReactTestRenderer ;
106-
107- const button = ( label : string ) =>
108- renderer . root . find ( ( node ) => node . type === "button" && node . children . includes ( label ) ) ;
109- const segment = ( ) =>
110- renderer . root . find (
111- ( node ) => node . type === "button" && node . props [ "aria-label" ] ?. startsWith ( "Alpha:" ) ,
112- ) ;
113- const text = ( node : ReactTestRenderer [ "root" ] | string ) : string =>
114- typeof node === "string" ? node : node . children . map ( text ) . join ( "" ) ;
115- const status = ( ) =>
116- renderer . root
117- . findAll ( ( node ) => node . props . role === "status" )
118- . map ( text )
119- . join ( " " ) ;
120-
121- /** Confirms a reset whose answer the test releases, closing the popover the way a click does. */
122- async function confirmHeldReset ( ) {
123- let answer ! : ( result : unknown ) => void ;
124- consume . mockReturnValue ( new Promise ( ( resolve ) => ( answer = resolve ) ) ) ;
125- await act ( ( ) => {
126- renderer = create ( < UsageLimitsPooled presentations = { presentations } now = { 0 } /> ) ;
127- } ) ;
128- await act ( ( ) => segment ( ) . props . onClick ( ) ) ;
129- await act ( ( ) => button ( "Use reset" ) . props . onClick ( ) ) ;
130- await act ( ( ) => button ( "Use credit" ) . props . onClick ( ) ) ;
131- return answer ;
132- }
69+ let root : Root ;
70+ let container : HTMLDivElement ;
13371
134- beforeEach ( ( ) => {
72+ beforeEach ( async ( ) => {
13573 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
74+ vi . stubGlobal (
75+ "ResizeObserver" ,
76+ class {
77+ observe ( ) { }
78+ disconnect ( ) { }
79+ unobserve ( ) { }
80+ } ,
81+ ) ;
13682 consume . mockReset ( ) ;
83+ container = document . createElement ( "div" ) ;
84+ document . body . append ( container ) ;
85+ root = createRoot ( container ) ;
86+ await act ( ( ) => root . render ( < UsageLimitsPooled presentations = { presentations } now = { 0 } /> ) ) ;
13787} ) ;
13888
13989afterEach ( async ( ) => {
140- await act ( ( ) => renderer . unmount ( ) ) ;
90+ await act ( ( ) => root . unmount ( ) ) ;
91+ container . remove ( ) ;
14192 vi . unstubAllGlobals ( ) ;
14293} ) ;
14394
144- it ( "shows the pending reset after its popover closes, then the outcome" , async ( ) => {
145- const answer = await confirmHeldReset ( ) ;
146- expect ( consume ) . toHaveBeenCalledOnce ( ) ;
147- expect ( status ( ) ) . toBe ( "Alpha Using reset credit…" ) ;
148-
149- await act ( ( ) => segment ( ) . props . onClick ( ) ) ;
150- expect ( button ( "Using…" ) . props . disabled ) . toBe ( true ) ;
151-
152- await act ( async ( ) => answer ( { _tag : "Success" , value : { outcome : "reset" } } ) ) ;
153- expect ( status ( ) ) . toBe ( "Alpha Reset applied. Your windows have cleared." ) ;
154- } ) ;
95+ async function click ( label : string ) {
96+ const button = Array . from ( document . querySelectorAll ( "button" ) ) . find (
97+ ( node ) => node . textContent === label || node . getAttribute ( "aria-label" ) ?. startsWith ( label ) ,
98+ ) ;
99+ expect ( button ) . toBeDefined ( ) ;
100+ await act ( ( ) => button ! . click ( ) ) ;
101+ }
155102
156- it ( "replaces the pending reset with the failure" , async ( ) => {
157- const answer = await confirmHeldReset ( ) ;
158- await act ( async ( ) =>
159- answer ( { _tag : "Failure" , cause : { error : new Error ( "Codex rejected the credit." ) } } ) ,
103+ it . each ( [
104+ {
105+ label : "success" ,
106+ result : { _tag : "Success" , value : { outcome : "reset" } } ,
107+ text : "Reset applied. Your windows have cleared." ,
108+ } ,
109+ {
110+ label : "failure" ,
111+ result : { _tag : "Failure" , cause : { error : new Error ( "Codex rejected the credit." ) } } ,
112+ text : "Codex rejected the credit." ,
113+ } ,
114+ ] ) ( "keeps pending feedback outside the closed popup until $label" , async ( { result, text } ) => {
115+ let answer ! : ( result : unknown ) => void ;
116+ consume . mockReturnValue ( new Promise ( ( resolve ) => ( answer = resolve ) ) ) ;
117+ await click ( "Alpha:" ) ;
118+ await click ( "Use reset" ) ;
119+ expect ( consume ) . not . toHaveBeenCalled ( ) ;
120+ await click ( "Cancel" ) ;
121+ expect ( consume ) . not . toHaveBeenCalled ( ) ;
122+ await click ( "Alpha:" ) ;
123+ await click ( "Use reset" ) ;
124+ await click ( "Use credit" ) ;
125+ expect ( consume ) . toHaveBeenCalledOnce ( ) ;
126+ expect ( document . querySelector ( '[role="alertdialog"]' ) ) . toBeNull ( ) ;
127+ expect ( document . querySelector ( '[data-slot="popover-popup"]' ) ) . toBeNull ( ) ;
128+ const status = document . querySelector ( '[role="status"]' ) ;
129+ expect ( status ?. textContent ) . toBe ( "Alpha Using reset credit…" ) ;
130+ await click ( "Alpha:" ) ;
131+ const using = Array . from ( document . querySelectorAll ( "button" ) ) . find (
132+ ( node ) => node . textContent === "Using…" ,
160133 ) ;
161- expect ( status ( ) ) . toBe ( "Alpha Codex rejected the credit." ) ;
134+ expect ( using?. disabled ) . toBe ( true ) ;
135+ await act ( async ( ) => answer ( result ) ) ;
136+ expect ( status ?. textContent ) . toBe ( `Alpha ${ text } ` ) ;
162137} ) ;
0 commit comments