@@ -137,7 +137,11 @@ async function runGoogleVertexStreamResult(params: {
137137 return stream . result ( ) ;
138138}
139139
140- async function useGoogleAuthorizedUserCredentials ( label : string , refreshToken : string ) {
140+ async function useGoogleAuthorizedUserCredentials (
141+ label : string ,
142+ refreshToken : string ,
143+ quotaProjectId ?: string ,
144+ ) {
141145 const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , `openclaw-google-vertex-${ label } -` ) ) ;
142146 const credentialsPath = path . join ( tempDir , "application_default_credentials.json" ) ;
143147 await writeFile (
@@ -147,6 +151,7 @@ async function useGoogleAuthorizedUserCredentials(label: string, refreshToken: s
147151 client_id : "client-id" ,
148152 client_secret : "client-secret" ,
149153 refresh_token : refreshToken ,
154+ ...( quotaProjectId ? { quota_project_id : quotaProjectId } : { } ) ,
150155 } ) ,
151156 "utf8" ,
152157 ) ;
@@ -1469,6 +1474,37 @@ describe("google transport stream", () => {
14691474 expect ( tokenFetchMock ) . not . toHaveBeenCalled ( ) ;
14701475 } ) ;
14711476
1477+ it ( "never refreshes stale home ADC when the selected Cloud SDK directory has no credentials" , async ( ) => {
1478+ const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-stale-home-" ) ) ;
1479+ const homeCredentialsDir = path . join ( tempDir , "home" , ".config" , "gcloud" ) ;
1480+ await mkdir ( homeCredentialsDir , { recursive : true } ) ;
1481+ await writeFile (
1482+ path . join ( homeCredentialsDir , "application_default_credentials.json" ) ,
1483+ JSON . stringify ( {
1484+ type : "authorized_user" ,
1485+ client_id : "stale-client" ,
1486+ client_secret : "stale-secret" ,
1487+ refresh_token : "stale-refresh" ,
1488+ } ) ,
1489+ "utf8" ,
1490+ ) ;
1491+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , "" ) ;
1492+ vi . stubEnv ( "CLOUDSDK_CONFIG" , path . join ( tempDir , "missing-cloud-sdk" ) ) ;
1493+ vi . stubEnv ( "HOME" , path . join ( tempDir , "home" ) ) ;
1494+ vi . stubEnv ( "APPDATA" , "" ) ;
1495+ googleAuthGetAccessTokenMock . mockResolvedValueOnce ( "fixture-google-auth-token" ) ;
1496+ const tokenFetchMock = vi . fn ( ) ;
1497+
1498+ await expect ( resolveGoogleVertexAuthorizedUserHeaders ( tokenFetchMock ) ) . resolves . toEqual ( {
1499+ Authorization : "Bearer fixture-google-auth-token" ,
1500+ } ) ;
1501+ expect ( googleAuthMock ) . toHaveBeenCalledWith ( {
1502+ scopes : [ "https://www.googleapis.com/auth/cloud-platform" ] ,
1503+ clientOptions : { transporterOptions : { timeout : 30_000 } } ,
1504+ } ) ;
1505+ expect ( tokenFetchMock ) . not . toHaveBeenCalled ( ) ;
1506+ } ) ;
1507+
14721508 it ( "bounds Google Vertex ADC files before google-auth-library reads them" , async ( ) => {
14731509 const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-adc-file-" ) ) ;
14741510 const credentialsPath = path . join ( tempDir , "application_default_credentials.json" ) ;
@@ -1593,6 +1629,97 @@ describe("google transport stream", () => {
15931629 expect ( new Headers ( guardedInit . headers ) . has ( "x-goog-api-key" ) ) . toBe ( false ) ;
15941630 } ) ;
15951631
1632+ it . each ( [
1633+ {
1634+ scenario : "authorized-user ADC quota project" ,
1635+ credentialType : "authorized_user" ,
1636+ credentialQuotaProject : "fixture-json-billing" ,
1637+ envQuotaProject : "" ,
1638+ expectedQuotaProject : "fixture-json-billing" ,
1639+ } ,
1640+ {
1641+ scenario : "environment quota project overriding authorized-user ADC" ,
1642+ credentialType : "authorized_user" ,
1643+ credentialQuotaProject : "fixture-json-billing" ,
1644+ envQuotaProject : "fixture-env-billing" ,
1645+ expectedQuotaProject : "fixture-env-billing" ,
1646+ } ,
1647+ {
1648+ scenario : "google-auth service-account ADC quota project" ,
1649+ credentialType : "service_account" ,
1650+ credentialQuotaProject : "fixture-service-billing" ,
1651+ envQuotaProject : "" ,
1652+ expectedQuotaProject : "fixture-service-billing" ,
1653+ } ,
1654+ {
1655+ scenario : "google-auth metadata ADC environment quota project" ,
1656+ credentialType : "metadata" ,
1657+ credentialQuotaProject : "" ,
1658+ envQuotaProject : "fixture-metadata-billing" ,
1659+ expectedQuotaProject : "fixture-metadata-billing" ,
1660+ } ,
1661+ ] ) (
1662+ "forwards the $scenario on the actual Vertex request" ,
1663+ async ( { credentialType, credentialQuotaProject, envQuotaProject, expectedQuotaProject } ) => {
1664+ const tokenFetchMock = vi . fn ( ) ;
1665+ vi . stubEnv ( "GOOGLE_CLOUD_PROJECT" , "fixture-project" ) ;
1666+ vi . stubEnv ( "GOOGLE_CLOUD_LOCATION" , "global" ) ;
1667+ vi . stubEnv ( "GOOGLE_CLOUD_QUOTA_PROJECT" , envQuotaProject ) ;
1668+ if ( credentialType === "authorized_user" ) {
1669+ await useGoogleAuthorizedUserCredentials (
1670+ "quota-authorized-user" ,
1671+ "fixture-refresh-token" ,
1672+ credentialQuotaProject ,
1673+ ) ;
1674+ tokenFetchMock . mockResolvedValueOnce (
1675+ new Response ( JSON . stringify ( { access_token : "fixture-vertex-token" , expires_in : 3600 } ) , {
1676+ status : 200 ,
1677+ headers : { "content-type" : "application/json" } ,
1678+ } ) ,
1679+ ) ;
1680+ } else if ( credentialType === "service_account" ) {
1681+ const tempDir = await mkdtemp (
1682+ path . join ( os . tmpdir ( ) , "openclaw-google-vertex-quota-service-" ) ,
1683+ ) ;
1684+ const credentialsPath = path . join ( tempDir , "application_default_credentials.json" ) ;
1685+ await writeFile (
1686+ credentialsPath ,
1687+ JSON . stringify ( {
1688+ type : "service_account" ,
1689+ client_email :
"[email protected] " , 1690+ quota_project_id : credentialQuotaProject ,
1691+ } ) ,
1692+ "utf8" ,
1693+ ) ;
1694+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , credentialsPath ) ;
1695+ googleAuthGetAccessTokenMock . mockResolvedValueOnce ( "fixture-vertex-token" ) ;
1696+ } else {
1697+ const tempDir = await mkdtemp (
1698+ path . join ( os . tmpdir ( ) , "openclaw-google-vertex-quota-metadata-" ) ,
1699+ ) ;
1700+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , "" ) ;
1701+ vi . stubEnv ( "HOME" , path . join ( tempDir , "home" ) ) ;
1702+ vi . stubEnv ( "APPDATA" , "" ) ;
1703+ googleAuthGetAccessTokenMock . mockResolvedValueOnce ( "fixture-vertex-token" ) ;
1704+ }
1705+ guardedFetchMock . mockResolvedValueOnce (
1706+ buildSseResponse ( [
1707+ {
1708+ candidates : [ { content : { parts : [ { text : "ok" } ] } , finishReason : "STOP" } ] ,
1709+ } ,
1710+ ] ) ,
1711+ ) ;
1712+
1713+ await runGoogleVertexStreamResult ( { fetch : tokenFetchMock } ) ;
1714+
1715+ const guardedCall = requireMockCall ( guardedFetchMock , 0 , "guarded fetch" ) ;
1716+ expectHeaders ( requireRequestInit ( guardedCall , "guarded fetch" ) , {
1717+ Authorization : "Bearer fixture-vertex-token" ,
1718+ "x-goog-user-project" : expectedQuotaProject ,
1719+ } ) ;
1720+ } ,
1721+ ) ;
1722+
15961723 it ( "strips redundant google provider prefixes from Google Vertex model paths" , async ( ) => {
15971724 const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-prefix-" ) ) ;
15981725 vi . stubEnv ( "HOME" , path . join ( tempDir , "home" ) ) ;
0 commit comments