2026-08-27 03:00:49 +08:00
import { describe , expect , it } from 'vitest'
import { Context } from '@deepseek-ai/cordis'
import type { CredentialInfo } from '@deepseek-ai/dsh-credentials/types'
2026-08-28 17:17:27 +08:00
import { remoteErrorOf , remoteMethods } from '@deepseek-ai/dsh-typert-protocol'
2026-08-27 03:00:49 +08:00
import CredentialsController from '../src/credentials.ts'
import { MemoryCredentials } from '../../../credentials/credentials/tests/memory.ts'
/** A store whose `describe` carries more than the view declares, as a foreign provider might. */
class LeakyCredentials extends MemoryCredentials {
override describe ( ) : Promise < CredentialInfo > {
return Promise . resolve (
{ configured : true , source : 'memory' , writable : true , value : 'sk-leaked' } as CredentialInfo ,
)
}
}
/** A store whose write rejects with a bare string, the way some client libraries do. */
class LiteralRejectingCredentials extends MemoryCredentials {
override async set ( ) : Promise < void > {
throw 'the store refused'
}
}
/** A store whose provider-owned policy rejects an otherwise valid write. */
class RejectingCredentials extends MemoryCredentials {
override set ( ) : Promise < void > {
return Promise . reject ( new Error ( 'a read-only source shadows this reference' ) )
}
}
async function boot (
seed : Record < string , string > = { } ,
provider : typeof MemoryCredentials = MemoryCredentials ,
) : Promise < CredentialsController > {
const ctx = new Context ( )
await ctx . plugin ( provider , seed )
await ctx . plugin ( CredentialsController )
return ctx . credentialsController
}
describe ( 'the credentials Remote namespace a configuration surface calls' , ( ) = > {
it ( 'publishes the credentials namespace from its own service key' , async ( ) = > {
const controller = await boot ( )
const binding = controller . typertRemote
expect ( binding . serviceKey ) . toBe ( 'credentialsController' )
expect ( binding . namespace ) . toBe ( 'credentials' )
expect ( remoteMethods ( controller ) ) . toEqual ( [
{ method : 'describe' , invocation : { kind : 'direct' } } ,
{ method : 'set' , invocation : { kind : 'direct' } } ,
{ method : 'unset' , invocation : { kind : 'direct' } } ,
] )
} )
it ( 'reports the actionable configuration error while no credential provider is mounted' , async ( ) = > {
const ctx = new Context ( )
await ctx . plugin ( CredentialsController )
for ( const call of [
( ) = > ctx . credentialsController . describe ( [ 'DEEPSEEK_API_KEY' ] ) ,
( ) = > ctx . credentialsController . set ( 'DEEPSEEK_API_KEY' , 'sk-live' ) ,
( ) = > ctx . credentialsController . unset ( 'DEEPSEEK_API_KEY' ) ,
] ) {
const failure = await call ( ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
expect ( remoteErrorOf ( failure ) ) . toMatchObject ( {
code : 'gateway/internal' ,
2026-08-27 03:00:49 +08:00
message : 'credentials service is absent: this deployment does not mount a credential provider (e.g. @deepseek-ai/dsh-credentials-local) in its composition' ,
details : { } ,
} )
}
} )
it ( 'describes a batch of references as one map, values excluded' , async ( ) = > {
const controller = await boot ( { DEEPSEEK_API_KEY : 'sk-seeded' } )
const described = await controller . describe ( [ 'DEEPSEEK_API_KEY' , 'OPENAI_API_KEY' ] )
expect ( described ) . toEqual ( {
DEEPSEEK_API_KEY : { configured : true , source : 'memory' , writable : true } ,
OPENAI_API_KEY : { configured : false , writable : true } ,
} )
expect ( JSON . stringify ( described ) ) . not . toContain ( 'sk-seeded' )
} )
it ( 'reports an invalid reference as bad-request' , async ( ) = > {
const controller = await boot ( )
for ( const call of [
( ) = > controller . describe ( [ 'DEEPSEEK_API_KEY' , 'not a var' ] ) ,
( ) = > controller . set ( 'not a var' , 'sk-live' ) ,
( ) = > controller . unset ( 'not a var' ) ,
] ) {
const failure = await call ( ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
expect ( remoteErrorOf ( failure ) ) . toMatchObject ( { code : 'gateway/bad-request' } )
2026-08-27 03:00:49 +08:00
}
} )
it ( 'answers the largest batch it accepts and reports one reference more as bad-request' , async ( ) = > {
const controller = await boot ( )
const accepted = Array . from ( { length : 64 } , ( _unused , index ) = > ` REF_ ${ String ( index ) } ` )
expect ( Object . keys ( await controller . describe ( accepted ) ) ) . toHaveLength ( 64 )
const failure = await controller . describe ( [ . . . accepted , 'REF_64' ] ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
expect ( remoteErrorOf ( failure ) ) . toMatchObject ( { code : 'gateway/bad-request' } )
2026-08-27 03:00:49 +08:00
} )
it ( 'answers only the fields the view declares, whatever a provider returns' , async ( ) = > {
const controller = await boot ( { } , LeakyCredentials )
const described = await controller . describe ( [ 'DEEPSEEK_API_KEY' ] )
expect ( described . DEEPSEEK_API_KEY ) . toEqual ( { configured : true , source : 'memory' , writable : true } )
expect ( JSON . stringify ( described ) ) . not . toContain ( 'sk-leaked' )
} )
it ( 'stores and removes through the same references the batch describes' , async ( ) = > {
const controller = await boot ( )
await controller . set ( 'DEEPSEEK_API_KEY' , 'sk-live' )
expect ( await controller . describe ( [ 'DEEPSEEK_API_KEY' ] ) )
. toEqual ( { DEEPSEEK_API_KEY : { configured : true , source : 'memory' , writable : true } } )
await controller . unset ( 'DEEPSEEK_API_KEY' )
expect ( await controller . describe ( [ 'DEEPSEEK_API_KEY' ] ) )
. toEqual ( { DEEPSEEK_API_KEY : { configured : false , writable : true } } )
} )
2026-08-28 17:17:27 +08:00
it ( 'reports a refused write as credential/rejected naming only the reference' , async ( ) = > {
2026-08-27 03:00:49 +08:00
const controller = await boot ( { } , RejectingCredentials )
const failure = await controller . set ( 'DEEPSEEK_API_KEY' , 'sk-live' ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
const { code , message , details } = remoteErrorOf ( failure ) ? ? { }
expect ( code ) . toBe ( 'credential/rejected' )
2026-08-27 03:00:49 +08:00
expect ( message ) . toContain ( 'read-only source' )
expect ( details ) . toEqual ( { ref : 'DEEPSEEK_API_KEY' } )
} )
it ( 'reports an empty value as bad-request' , async ( ) = > {
const controller = await boot ( )
const failure = await controller . set ( 'DEEPSEEK_API_KEY' , '' ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
expect ( remoteErrorOf ( failure ) ) . toMatchObject ( { code : 'gateway/bad-request' } )
2026-08-27 03:00:49 +08:00
} )
it ( 'stringifies a refusal that is not an Error' , async ( ) = > {
const controller = await boot ( { } , LiteralRejectingCredentials )
const failure = await controller . set ( 'DEEPSEEK_API_KEY' , 'sk-live' ) . catch ( ( error : unknown ) = > error )
2026-08-28 17:17:27 +08:00
expect ( remoteErrorOf ( failure ) ? . message ) . toBe ( 'the store refused' )
2026-08-27 03:00:49 +08:00
} )
} )