@@ -3,6 +3,7 @@ import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js
33import {
44 ensureProfileForTailscaleIdentity ,
55 getUserProfileListItem ,
6+ syncGitHubIdentity ,
67} from "../state/user-profiles.js" ;
78import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js" ;
89import { ControlUiGitHubError } from "./control-ui-github-api.js" ;
@@ -15,6 +16,17 @@ function githubResponse(body: unknown, status = 200, headers: Record<string, str
1516 } ) ;
1617}
1718
19+ function githubBodyReadFailure ( ) {
20+ return new Response (
21+ new ReadableStream ( {
22+ start ( controller ) {
23+ controller . error ( new Error ( "body read failed" ) ) ;
24+ } ,
25+ } ) ,
26+ { status : 200 } ,
27+ ) ;
28+ }
29+
1830function accessAssertion ( issuer : unknown ) : string {
1931 const payload = Buffer . from ( JSON . stringify ( { iss : issuer } ) ) . toString ( "base64url" ) ;
2032 return `header.${ payload } .signature` ;
@@ -250,6 +262,23 @@ describe("authenticated GitHub identity sync", () => {
250262251263 githubIdentity : { login : "steipete" } ,
252264 } ) ;
265+
266+ fetchMock
267+ . mockResolvedValueOnce (
268+ githubResponse ( {
269+ id : 58493 ,
270+ 271+ idp : { type : "github" } ,
272+ } ) ,
273+ )
274+ . mockResolvedValueOnce ( githubResponse ( { id : 58493 , login : "steipete-renamed" } ) ) ;
275+ await expect ( cloudflareSync ( { } ) ?.( ) ) . resolves . toMatchObject ( {
276+ profileId : result ! . profileId ,
277+ } ) ;
278+ expect ( getUserProfileListItem ( result ! . profileId ) . githubIdentity ) . toMatchObject ( {
279+ login : "steipete-renamed" ,
280+ } ) ;
281+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 4 ) ;
253282 } ) ;
254283 } ) ;
255284
@@ -352,39 +381,116 @@ describe("authenticated GitHub identity sync", () => {
352381 "x-ratelimit-remaining" : "0" ,
353382 } ) ,
354383 } ,
384+ { name : "GitHub upstream failure" , githubResult : githubResponse ( { } , 503 ) } ,
355385 { name : "GitHub network failure" , githubError : new Error ( "network unavailable" ) } ,
356- ] ) ( "preserves prior identity after a $name" , async ( { githubResult, githubError } ) => {
357- await withOpenClawTestState ( { scenario : "minimal" } , async ( ) => {
358- const fetchMock = vi
359- . spyOn ( globalThis , "fetch" )
360- . mockResolvedValueOnce (
386+ ] ) (
387+ "reattaches the exact cached verified identity after a $name" ,
388+ async ( { githubResult, githubError } ) => {
389+ await withOpenClawTestState ( { scenario : "minimal" } , async ( ) => {
390+ const fetchMock = vi
391+ . spyOn ( globalThis , "fetch" )
392+ . mockResolvedValueOnce (
393+ githubResponse ( {
394+ id : 58493 ,
395+ 396+ idp : { type : "github" } ,
397+ } ) ,
398+ )
399+ . mockResolvedValueOnce ( githubResponse ( { id : 58493 , login : "steipete" } ) ) ;
400+ const first = await cloudflareSync ( { } ) ?.( ) ;
401+ fetchMock . mockResolvedValueOnce (
361402 githubResponse ( {
362403 id : 58493 ,
363404364405 idp : { type : "github" } ,
365406 } ) ,
366- )
367- . mockResolvedValueOnce ( githubResponse ( { id : 58493 , login : "steipete" } ) ) ;
368- const first = await cloudflareSync ( { } ) ?.( ) ;
369- fetchMock . mockResolvedValueOnce (
370- githubResponse ( {
371- id : 58493 ,
372- 373- idp : { type : "github" } ,
374- } ) ,
375- ) ;
376- if ( githubError ) {
377- fetchMock . mockRejectedValueOnce ( githubError ) ;
378- } else {
379- fetchMock . mockResolvedValueOnce ( githubResult ! ) ;
380- }
407+ ) ;
408+ if ( githubError ) {
409+ fetchMock . mockRejectedValueOnce ( githubError ) ;
410+ } else {
411+ fetchMock . mockResolvedValueOnce ( githubResult ! ) ;
412+ }
381413
382- await expect ( cloudflareSync ( { } ) ?.( ) ) . rejects . toThrow ( ) ;
383- expect ( getUserProfileListItem ( first ! . profileId ) . githubIdentity ) . toMatchObject ( {
384- login : "steipete" ,
414+ await expect ( cloudflareSync ( { principal :
"[email protected] " } ) ?.
( ) ) . resolves . toEqual ( first ) ; 415+ expect ( getUserProfileListItem ( first ! . profileId ) . githubIdentity ) . toMatchObject ( {
416+ login : "steipete" ,
417+ } ) ;
418+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 4 ) ;
385419 } ) ;
386- } ) ;
387- } ) ;
420+ } ,
421+ ) ;
422+
423+ it . each ( [
424+ { name : "malformed GitHub response" , githubStatus : 200 , expectedStatus : 502 , malformed : true } ,
425+ {
426+ name : "GitHub body read failure" ,
427+ githubStatus : 200 ,
428+ expectedStatus : 502 ,
429+ bodyReadFailure : true ,
430+ } ,
431+ { name : "non-retryable GitHub request" , githubStatus : 400 , expectedStatus : 502 } ,
432+ { name : "unauthorized GitHub account" , githubStatus : 401 } ,
433+ { name : "GitHub permission denial" , githubStatus : 403 } ,
434+ { name : "deleted GitHub account" , githubStatus : 404 } ,
435+ { name : "different cached account" , githubStatus : 429 , accessAccountId : 99999 } ,
436+ { name :
"different cached email" , githubStatus :
429 , principal :
"[email protected] " } , 437+ {
438+ name : "email and account on different profiles" ,
439+ githubStatus : 429 ,
440+ accessAccountId : 99999 ,
441+ otherProfile : true ,
442+ } ,
443+ { name : "missing cached identity" , githubStatus : 429 , seedCache : false } ,
444+ ] ) (
445+ "fails closed for a $name" ,
446+ async ( {
447+ githubStatus,
448+ expectedStatus,
449+ malformed,
450+ bodyReadFailure,
451+ accessAccountId,
452+ principal,
453+ otherProfile,
454+ seedCache,
455+ } ) => {
456+ await withOpenClawTestState ( { scenario : "minimal" } , async ( ) => {
457+ if ( seedCache !== false ) {
458+ syncGitHubIdentity ( {
459+ identity : { accountId : 58493 , login : "steipete" } ,
460+ authenticationAlias :
{ kind :
"email" , email :
"[email protected] " } , 461+ } ) ;
462+ }
463+ if ( otherProfile ) {
464+ syncGitHubIdentity ( {
465+ identity : { accountId : 99999 , login : "mallory" } ,
466+ authenticationAlias :
{ kind :
"email" , email :
"[email protected] " } , 467+ } ) ;
468+ }
469+ const authenticatedEmail = principal ?? "[email protected] " ; 470+ const fetchMock = vi
471+ . spyOn ( globalThis , "fetch" )
472+ . mockResolvedValueOnce (
473+ githubResponse ( {
474+ id : accessAccountId ?? 58493 ,
475+ email : authenticatedEmail ,
476+ idp : { type : "github" } ,
477+ } ) ,
478+ )
479+ . mockResolvedValueOnce (
480+ bodyReadFailure
481+ ? githubBodyReadFailure ( )
482+ : malformed
483+ ? new Response ( "{" , { status : githubStatus } )
484+ : githubResponse ( { } , githubStatus ) ,
485+ ) ;
486+
487+ await expect ( cloudflareSync ( { principal : authenticatedEmail } ) ?.( ) ) . rejects . toMatchObject ( {
488+ statusCode : expectedStatus ?? githubStatus ,
489+ } satisfies Partial < ControlUiGitHubError > ) ;
490+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
491+ } ) ;
492+ } ,
493+ ) ;
388494
389495 it ( "redacts the Access assertion from network failures and retries on the same connection" , async ( ) => {
390496 await withOpenClawTestState ( { scenario : "minimal" } , async ( ) => {
0 commit comments