88 readAuthProfilesForAgent ,
99 setupAuthTestEnv ,
1010} from "../../test/helpers/auth-wizard.js" ;
11+ import { ensureAuthProfileStore } from "../agents/auth-profiles/store.js" ;
1112import type { OAuthCredentials } from "../llm/utils/oauth/types.js" ;
1213import {
1314 applyAuthProfileConfig ,
@@ -62,6 +63,13 @@ function expectFields(value: unknown, expected: Record<string, unknown>, label =
6263 return record ;
6364}
6465
66+ function readEffectiveAuthProfiles ( agentDir : string ) {
67+ return ensureAuthProfileStore ( agentDir , {
68+ readOnly : true ,
69+ syncExternalCli : false ,
70+ } ) ;
71+ }
72+
6573describe ( "writeOAuthCredentials" , ( ) => {
6674 const lifecycle = createAuthTestLifecycle ( [
6775 "OPENCLAW_STATE_DIR" ,
@@ -125,19 +133,23 @@ describe("writeOAuthCredentials", () => {
125133 } ) ;
126134
127135 for ( const dir of [ mainAgentDir , kidAgentDir ] ) {
128- const persistedStore = await readAuthProfilesForAgent < {
129- profiles ?: Record < string , OAuthCredentials & { type ?: string } > ;
130- } > ( dir ) ;
131- expectFields ( persistedStore . profiles ?. [ "openai:default" ] , {
136+ const effectiveStore = readEffectiveAuthProfiles ( dir ) ;
137+ expectFields ( effectiveStore . profiles ?. [ "openai:default" ] , {
132138 refresh : "refresh-sync" ,
133139 access : "access-sync" ,
134140 type : "oauth" ,
135141 } ) ;
136142 }
137- const inheritedSiblingStore = await readAuthProfilesForAgent < {
143+ const inheritedSiblingStore = readEffectiveAuthProfiles ( workerAgentDir ) ;
144+ expectFields ( inheritedSiblingStore . profiles ?. [ "openai:default" ] , {
145+ refresh : "refresh-sync" ,
146+ access : "access-sync" ,
147+ type : "oauth" ,
148+ } ) ;
149+ const persistedSiblingStore = await readAuthProfilesForAgent < {
138150 profiles ?: Record < string , OAuthCredentials & { type ?: string } > ;
139151 } > ( workerAgentDir ) ;
140- expect ( inheritedSiblingStore . profiles ) . toEqual ( { } ) ;
152+ expect ( persistedSiblingStore . profiles ) . toEqual ( { } ) ;
141153 } ) ;
142154
143155 it ( "writes OAuth credentials only to target dir by default" , async ( ) => {
@@ -160,9 +172,7 @@ describe("writeOAuthCredentials", () => {
160172
161173 await writeOAuthCredentials ( "openai" , creds , kidAgentDir ) ;
162174
163- const kidParsed = await readAuthProfilesForAgent < {
164- profiles ?: Record < string , OAuthCredentials & { type ?: string } > ;
165- } > ( kidAgentDir ) ;
175+ const kidParsed = readEffectiveAuthProfiles ( kidAgentDir ) ;
166176 expectFields ( kidParsed . profiles ?. [ "openai:default" ] , {
167177 access : "access-kid" ,
168178 type : "oauth" ,
@@ -239,16 +249,20 @@ describe("upsertApiKeyProfile secret refs", () => {
239249 agentDir : string ,
240250 profileId : string ,
241251 ) : Promise < AuthProfileEntry | undefined > {
242- const parsed = await readAuthProfilesForAgent < {
243- profiles ?: Record < string , AuthProfileEntry > ;
244- } > ( agentDir ) ;
245- return parsed . profiles ?. [ profileId ] ;
252+ const parsed = readEffectiveAuthProfiles ( agentDir ) ;
253+ const profile = parsed . profiles [ profileId ] ;
254+ if ( ! profile || profile . type !== "api_key" ) {
255+ return undefined ;
256+ }
257+ return {
258+ ...( profile . key !== undefined ? { key : profile . key } : { } ) ,
259+ ...( profile . keyRef !== undefined ? { keyRef : profile . keyRef } : { } ) ,
260+ ...( profile . metadata !== undefined ? { metadata : profile . metadata } : { } ) ,
261+ } ;
246262 }
247263
248264 async function readProfileIds ( agentDir : string ) : Promise < string [ ] > {
249- const parsed = await readAuthProfilesForAgent < {
250- profiles ?: Record < string , AuthProfileEntry > ;
251- } > ( agentDir ) ;
265+ const parsed = readEffectiveAuthProfiles ( agentDir ) ;
252266 return Object . keys ( parsed . profiles ?? { } ) . toSorted ( ) ;
253267 }
254268
0 commit comments