22// validation errors, and protocol response shapes.
33import { describe , expect , it , vi } from "vitest" ;
44import { ErrorCodes } from "../../../packages/gateway-protocol/src/index.js" ;
5+ import {
6+ clearRuntimeAuthProfileStoreSnapshots ,
7+ replaceRuntimeAuthProfileStoreSnapshots ,
8+ } from "../../agents/auth-profiles.js" ;
59import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
610import { createDeferred } from "../../test-utils/deferred.js" ;
711import { withEnvAsync } from "../../test-utils/env.js" ;
@@ -21,6 +25,21 @@ const withoutOpenAIEnvAuth = async <T>(run: () => Promise<T>): Promise<T> =>
2125 run ,
2226 ) ;
2327
28+ function createDemoOAuthStore ( params : { access : string ; expires : number } ) {
29+ return {
30+ version : 1 as const ,
31+ profiles : {
32+ "demo-provider:oauth" : {
33+ type : "oauth" as const ,
34+ provider : "demo-provider" ,
35+ access : params . access ,
36+ refresh : "refresh-token" ,
37+ expires : params . expires ,
38+ } ,
39+ } ,
40+ } ;
41+ }
42+
2443function requestModelsList ( params : {
2544 view : "configured" | "all" ;
2645 respond ?: ReturnType < typeof vi . fn > ;
@@ -488,18 +507,12 @@ describe("models.list", () => {
488507 agentEnv : "main" ,
489508 } ,
490509 async ( state ) => {
491- await state . writeAuthProfiles ( {
492- version : 1 ,
493- profiles : {
494- "demo-provider:expired" : {
495- type : "oauth" ,
496- provider : "demo-provider" ,
497- access : "expired-access" ,
498- refresh : "refresh-token" ,
499- expires : Date . now ( ) - 60_000 ,
500- } ,
501- } ,
502- } ) ;
510+ await state . writeAuthProfiles (
511+ createDemoOAuthStore ( {
512+ access : "expired-access" ,
513+ expires : Date . now ( ) - 60_000 ,
514+ } ) ,
515+ ) ;
503516
504517 const { request, respond } = requestModelsList ( {
505518 view : "all" ,
@@ -528,6 +541,64 @@ describe("models.list", () => {
528541 ) ;
529542 } ) ;
530543
544+ it ( "uses refreshed persisted OAuth when the runtime auth snapshot is stale" , async ( ) => {
545+ await withOpenClawTestState (
546+ {
547+ layout : "state-only" ,
548+ prefix : "openclaw-models-list-stale-runtime-profile-" ,
549+ agentEnv : "main" ,
550+ } ,
551+ async ( state ) => {
552+ const agentDir = state . agentDir ( ) ;
553+ await state . writeAuthProfiles (
554+ createDemoOAuthStore ( {
555+ access : "refreshed-access" ,
556+ expires : Date . now ( ) + 60 * 60_000 ,
557+ } ) ,
558+ ) ;
559+ replaceRuntimeAuthProfileStoreSnapshots ( [
560+ {
561+ agentDir,
562+ store : createDemoOAuthStore ( {
563+ access : "expired-access" ,
564+ expires : Date . now ( ) - 60_000 ,
565+ } ) ,
566+ } ,
567+ ] ) ;
568+
569+ try {
570+ const { request, respond } = requestModelsList ( {
571+ view : "all" ,
572+ loadGatewayModelCatalog : vi . fn ( ( ) =>
573+ Promise . resolve ( [
574+ { id : "demo-model" , name : "Demo Model" , provider : "demo-provider" } ,
575+ ] ) ,
576+ ) ,
577+ reqId : "req-models-list-stale-runtime-profile" ,
578+ } ) ;
579+ await request ;
580+
581+ expect ( respond ) . toHaveBeenCalledWith (
582+ true ,
583+ {
584+ models : [
585+ {
586+ id : "demo-model" ,
587+ name : "Demo Model" ,
588+ provider : "demo-provider" ,
589+ available : true ,
590+ } ,
591+ ] ,
592+ } ,
593+ undefined ,
594+ ) ;
595+ } finally {
596+ clearRuntimeAuthProfileStoreSnapshots ( ) ;
597+ }
598+ } ,
599+ ) ;
600+ } ) ;
601+
531602 it ( "marks env SecretRef-backed auth profiles available" , async ( ) => {
532603 await withOpenClawTestState (
533604 {
0 commit comments