11// Anthropic OAuth tests cover token exchange and refresh behavior.
22import { get } from "node:http" ;
33import { afterEach , describe , expect , it , vi } from "vitest" ;
4- import { __testing , anthropicOAuthProvider , refreshAnthropicToken } from "./anthropic.js" ;
4+ import { anthropicOAuthProvider , refreshAnthropicToken , testing } from "./anthropic.js" ;
55
66afterEach ( ( ) => {
77 vi . unstubAllGlobals ( ) ;
@@ -117,34 +117,37 @@ describe("Anthropic OAuth token responses", () => {
117117
118118describe ( "Anthropic OAuth callback host" , ( ) => {
119119 it ( "rejects non-loopback callback bind hosts" , ( ) => {
120- expect ( ( ) =>
121- __testing . resolveCallbackHost ( { OPENCLAW_OAUTH_CALLBACK_HOST : "0 .0.0.0" } ) ,
122- ) . toThrow ( "Anthropic OAuth callback host must be localhost, 127.0.0.1, or ::1" ) ;
120+ expect ( ( ) => testing . resolveCallbackHost ( { OPENCLAW_OAUTH_CALLBACK_HOST : "0.0.0.0" } ) ) . toThrow (
121+ "Anthropic OAuth callback host must be localhost, 127 .0.0.1, or ::1" ,
122+ ) ;
123123 } ) ;
124124
125125 it ( "defaults the bind host to IPv4 loopback" , ( ) => {
126- expect ( __testing . resolveCallbackHost ( { } ) ) . toBe ( "127.0.0.1" ) ;
126+ expect ( testing . resolveCallbackHost ( { } ) ) . toBe ( "127.0.0.1" ) ;
127127 } ) ;
128128
129129 it . each ( [ "localhost" , "127.0.0.1" , "::1" ] ) ( "accepts loopback bind host %s" , ( host ) => {
130- expect ( __testing . resolveCallbackHost ( { OPENCLAW_OAUTH_CALLBACK_HOST : host } ) ) . toBe ( host ) ;
131- expect ( __testing . redirectUri ) . toBe ( "http://localhost:53692/callback" ) ;
130+ expect ( testing . resolveCallbackHost ( { OPENCLAW_OAUTH_CALLBACK_HOST : host } ) ) . toBe ( host ) ;
131+ expect ( testing . redirectUri ) . toBe ( "http://localhost:53692/callback" ) ;
132132 } ) ;
133133
134134 it ( "defers callback-host validation until login resolves the bind host" , ( ) => {
135135 vi . stubEnv ( "OPENCLAW_OAUTH_CALLBACK_HOST" , "0.0.0.0" ) ;
136- expect ( ( ) => __testing . resolveCallbackHost ( ) ) . toThrow (
136+ expect ( ( ) => testing . resolveCallbackHost ( ) ) . toThrow (
137137 "Anthropic OAuth callback host must be localhost, 127.0.0.1, or ::1" ,
138138 ) ;
139139 vi . unstubAllEnvs ( ) ;
140- expect ( ( ) => __testing . resolveCallbackHost ( ) ) . not . toThrow ( ) ;
140+ expect ( ( ) => testing . resolveCallbackHost ( ) ) . not . toThrow ( ) ;
141141 } ) ;
142142
143143 it ( "binds IPv4 loopback while keeping Anthropic's registered localhost redirect" , async ( ) => {
144144 vi . stubEnv ( "OPENCLAW_OAUTH_CALLBACK_HOST" , "127.0.0.1" ) ;
145145 const tokenExchange = vi . fn ( async ( _url : string | URL | Request , init ?: RequestInit ) => {
146- const body = JSON . parse ( String ( init ?. body ) ) as { redirect_uri ?: string } ;
147- expect ( body . redirect_uri ) . toBe ( __testing . redirectUri ) ;
146+ if ( typeof init ?. body !== "string" ) {
147+ throw new Error ( "token exchange did not send a JSON string body" ) ;
148+ }
149+ const body = JSON . parse ( init . body ) as { redirect_uri ?: string } ;
150+ expect ( body . redirect_uri ) . toBe ( testing . redirectUri ) ;
148151 return new Response (
149152 JSON . stringify ( {
150153 access_token : "access-token" ,
@@ -159,7 +162,7 @@ describe("Anthropic OAuth callback host", () => {
159162 const credentials = await anthropicOAuthProvider . login ( {
160163 onAuth : ( { url } ) => {
161164 const authorizationUrl = new URL ( url ) ;
162- expect ( authorizationUrl . searchParams . get ( "redirect_uri" ) ) . toBe ( __testing . redirectUri ) ;
165+ expect ( authorizationUrl . searchParams . get ( "redirect_uri" ) ) . toBe ( testing . redirectUri ) ;
163166 const state = authorizationUrl . searchParams . get ( "state" ) ;
164167 if ( ! state ) {
165168 throw new Error ( "authorization URL did not include OAuth state" ) ;
0 commit comments