1- import { afterEach , beforeEach , expect , test } from "bun:test" ;
1+ import { afterEach , beforeEach , expect , spyOn , test } from "bun:test" ;
22import { Window } from "happy-dom" ;
33import { act } from "react" ;
44import type { Root } from "react-dom/client" ;
55import { LanguageProvider } from "../src/i18n/provider" ;
66import AddProviderModal from "../src/components/AddProviderModal" ;
7+ import { OAUTH_LOGIN_POLL_INTERVAL_MS } from "../src/components/use-add-provider-oauth" ;
78
89/**
910 * The add-provider OAuth pane renders the authorization URL so a user whose
@@ -23,6 +24,7 @@ let host: HTMLElement;
2324let root : Root | null = null ;
2425let originalFetch : typeof globalThis . fetch ;
2526let pendingLogins : Array < ( url : string ) => void > = [ ] ;
27+ let oauthStatus : { loggedIn : boolean ; error ?: string } = { loggedIn : false } ;
2628
2729const PRESETS = [
2830 { id : "claude" , label : "Claude" , adapter : "anthropic" , baseUrl : "https://api.anthropic.com" , auth : "oauth" , oauthProvider : "claude" } ,
@@ -43,6 +45,7 @@ beforeEach(() => {
4345 ( globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT ?: boolean } ) . IS_REACT_ACT_ENVIRONMENT = true ;
4446
4547 pendingLogins = [ ] ;
48+ oauthStatus = { loggedIn : false } ;
4649 Object . defineProperty ( globalThis , "fetch" , {
4750 configurable : true ,
4851 value : async ( input : RequestInfo | URL , init ?: RequestInit ) => {
@@ -56,7 +59,7 @@ beforeEach(() => {
5659 pendingLogins . push ( ( authUrl : string ) => resolve ( Response . json ( { url : authUrl } ) ) ) ;
5760 } ) ;
5861 }
59- if ( url . pathname === "/api/oauth/status" ) return Response . json ( { loggedIn : false } ) ;
62+ if ( url . pathname === "/api/oauth/status" ) return Response . json ( oauthStatus ) ;
6063 return Response . json ( { } ) ;
6164 } ,
6265 } ) ;
@@ -78,13 +81,13 @@ afterEach(async () => {
7881 await win . happyDOM ?. close ?.( ) ;
7982} ) ;
8083
81- async function mountModal ( ) {
84+ async function mountModal ( onAdded : ( name : string ) => void = ( ) => { } ) {
8285 const { createRoot } = await import ( "react-dom/client" ) ;
8386 await act ( async ( ) => {
8487 root = createRoot ( host ) ;
8588 root . render (
8689 < LanguageProvider >
87- < AddProviderModal apiBase = "" existingNames = { [ ] } initialTier = "paid" onClose = { ( ) => { } } onAdded = { ( ) => { } } />
90+ < AddProviderModal apiBase = "" existingNames = { [ ] } initialTier = "paid" onClose = { ( ) => { } } onAdded = { onAdded } />
8891 </ LanguageProvider > ,
8992 ) ;
9093 } ) ;
@@ -167,3 +170,46 @@ test("a late URL for an abandoned provider cannot overwrite the one already show
167170 expect ( host . querySelector ( ".login-url-block-text" ) ?. textContent ) . toBe ( B_URL ) ;
168171 expect ( host . textContent ) . not . toContain ( A_URL ) ;
169172} ) ;
173+
174+ test ( "a login error wins over a retained OAuth credential" , async ( ) => {
175+ const added : string [ ] = [ ] ;
176+ oauthStatus = {
177+ loggedIn : true ,
178+ error : "The credential was saved, but the provider entry was not written." ,
179+ } ;
180+ const realSetTimeout = globalThis . setTimeout ;
181+ const timeoutSpy = spyOn ( globalThis , "setTimeout" ) . mockImplementation ( ( (
182+ callback : ( ...args : unknown [ ] ) => void ,
183+ delay ?: number ,
184+ ...args : unknown [ ]
185+ ) => {
186+ if ( delay === OAUTH_LOGIN_POLL_INTERVAL_MS ) {
187+ queueMicrotask ( ( ) => callback ( ...args ) ) ;
188+ return 0 as unknown as ReturnType < typeof setTimeout > ;
189+ }
190+ return realSetTimeout ( callback , delay , ...args ) ;
191+ } ) as typeof setTimeout ) ;
192+
193+ try {
194+ await mountModal ( name => added . push ( name ) ) ;
195+ await act ( async ( ) => {
196+ clickByText ( "Claude" ) ;
197+ await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
198+ } ) ;
199+ await act ( async ( ) => {
200+ clickByText ( "Log in with Claude" ) ;
201+ await new Promise ( ( r ) => setTimeout ( r , 20 ) ) ;
202+ } ) ;
203+
204+ expect ( pendingLogins ) . toHaveLength ( 1 ) ;
205+ await act ( async ( ) => {
206+ pendingLogins . shift ( ) ! ( A_URL ) ;
207+ await new Promise ( ( r ) => setTimeout ( r , 40 ) ) ;
208+ } ) ;
209+
210+ expect ( added ) . toEqual ( [ ] ) ;
211+ expect ( host . textContent ) . toContain ( "provider entry was not written" ) ;
212+ } finally {
213+ timeoutSpy . mockRestore ( ) ;
214+ }
215+ } ) ;
0 commit comments