66
77import { describe , it , expect , afterEach , vi } from 'vitest' ;
88import { ApiKeyAuthProvider } from './api-key-provider.js' ;
9+ import * as resolver from './value-resolver.js' ;
10+
11+ vi . mock ( './value-resolver.js' , async ( importOriginal ) => {
12+ const actual = await importOriginal < typeof import ( './value-resolver.js' ) > ( ) ;
13+ return {
14+ ...actual ,
15+ resolveAuthValue : vi . fn ( ) ,
16+ } ;
17+ } ) ;
918
1019describe ( 'ApiKeyAuthProvider' , ( ) => {
1120 afterEach ( ( ) => {
1221 vi . unstubAllEnvs ( ) ;
22+ vi . restoreAllMocks ( ) ;
1323 } ) ;
1424
1525 describe ( 'initialization' , ( ) => {
@@ -26,6 +36,7 @@ describe('ApiKeyAuthProvider', () => {
2636
2737 it ( 'should resolve API key from environment variable' , async ( ) => {
2838 vi . stubEnv ( 'TEST_API_KEY' , 'env-api-key' ) ;
39+ vi . mocked ( resolver . resolveAuthValue ) . mockResolvedValue ( 'env-api-key' ) ;
2940
3041 const provider = new ApiKeyAuthProvider ( {
3142 type : 'apiKey' ,
@@ -38,6 +49,10 @@ describe('ApiKeyAuthProvider', () => {
3849 } ) ;
3950
4051 it ( 'should throw if environment variable is not set' , async ( ) => {
52+ vi . mocked ( resolver . resolveAuthValue ) . mockRejectedValue (
53+ new Error ( "Environment variable 'MISSING_KEY_12345' is not set" ) ,
54+ ) ;
55+
4156 const provider = new ApiKeyAuthProvider ( {
4257 type : 'apiKey' ,
4358 key : '$MISSING_KEY_12345' ,
@@ -114,6 +129,8 @@ describe('ApiKeyAuthProvider', () => {
114129
115130 it ( 'should return undefined for env-var keys on 403' , async ( ) => {
116131 vi . stubEnv ( 'RETRY_TEST_KEY' , 'some-key' ) ;
132+ vi . mocked ( resolver . resolveAuthValue ) . mockResolvedValue ( 'some-key' ) ;
133+
117134 const provider = new ApiKeyAuthProvider ( {
118135 type : 'apiKey' ,
119136 key : '$RETRY_TEST_KEY' ,
@@ -128,9 +145,13 @@ describe('ApiKeyAuthProvider', () => {
128145 } ) ;
129146
130147 it ( 'should re-resolve and return headers for command keys on 401' , async ( ) => {
148+ vi . mocked ( resolver . resolveAuthValue )
149+ . mockResolvedValueOnce ( 'initial-key' )
150+ . mockResolvedValueOnce ( 'refreshed-key' ) ;
151+
131152 const provider = new ApiKeyAuthProvider ( {
132153 type : 'apiKey' ,
133- key : '!echo refreshed-key ' ,
154+ key : '!some command ' ,
134155 } ) ;
135156 await provider . initialize ( ) ;
136157
@@ -142,9 +163,11 @@ describe('ApiKeyAuthProvider', () => {
142163 } ) ;
143164
144165 it ( 'should stop retrying after MAX_AUTH_RETRIES' , async ( ) => {
166+ vi . mocked ( resolver . resolveAuthValue ) . mockResolvedValue ( 'rotating-key' ) ;
167+
145168 const provider = new ApiKeyAuthProvider ( {
146169 type : 'apiKey' ,
147- key : '!echo rotating-key ' ,
170+ key : '!some command ' ,
148171 } ) ;
149172 await provider . initialize ( ) ;
150173
0 commit comments