@@ -34,16 +34,19 @@ const DISCOVERY_MODELS_BODY = JSON.stringify({
3434
3535async function startCopilotServer ( handle : {
3636 models : { status : number ; body : string } | ( ( response : ServerResponse ) => void ) ;
37- embeddings ?: { status : number ; body : string } ;
37+ embeddings ?: { status : number ; body : string } | ( ( response : ServerResponse ) => void ) ;
38+ observeRequest ?: ( request : IncomingMessage , body : string ) => void ;
3839} ) : Promise < CopilotServer > {
3940 const requests : CopilotServer [ "requests" ] = [ ] ;
4041 const server = createServer ( ( req : IncomingMessage , res : ServerResponse ) => {
4142 void ( async ( ) => {
4243 // Drain the request body so keep-alive sockets close cleanly.
43- await new Promise < void > ( ( resolve ) => {
44- req . resume ( ) ;
45- req . on ( "end" , resolve ) ;
44+ const body = await new Promise < string > ( ( resolve ) => {
45+ const chunks : Buffer [ ] = [ ] ;
46+ req . on ( "data" , ( chunk : Buffer ) => chunks . push ( chunk ) ) ;
47+ req . on ( "end" , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ) ;
4648 } ) ;
49+ handle . observeRequest ?.( req , body ) ;
4750 requests . push ( { method : req . method , url : req . url } ) ;
4851 const isEmbeddings = req . method === "POST" && req . url === "/embeddings" ;
4952 const route = isEmbeddings && handle . embeddings ? handle . embeddings : handle . models ;
@@ -241,25 +244,75 @@ describe("githubCopilotMemoryEmbeddingProviderAdapter real transport", () => {
241244 }
242245 } ) ;
243246
244- it ( "returns embedding vectors on a successful response over real transport" , async ( ) => {
245- const server = await startCopilotServer ( {
246- models : { status : 200 , body : DISCOVERY_MODELS_BODY } ,
247- embeddings : {
248- status : 200 ,
249- body : JSON . stringify ( { data : [ { index : 0 , embedding : [ 0.1 , 0.2 , 0.3 ] } ] } ) ,
250- } ,
251- } ) ;
252- pointTokenAt ( server . baseUrl ) ;
253-
254- const result = await githubCopilotMemoryEmbeddingProviderAdapter . create ( defaultCreateOptions ( ) ) ;
255- const vector = await result . provider ?. embed ( "hello" , { inputType : "query" } ) ;
247+ it . each ( [ "profile" , "custom" ] ) (
248+ "preserves %s authentication and embedding wire requests" ,
249+ async ( auth ) => {
250+ const observed : Array < {
251+ authorization : string | undefined ;
252+ integration : unknown ;
253+ custom : unknown ;
254+ body : string ;
255+ } > = [ ] ;
256+ const server = await startCopilotServer ( {
257+ models : { status : 200 , body : DISCOVERY_MODELS_BODY } ,
258+ observeRequest : ( request , body ) => {
259+ observed . push ( {
260+ authorization : request . headers . authorization ,
261+ integration : request . headers [ "copilot-integration-id" ] ,
262+ custom : request . headers [ "x-proof" ] ,
263+ body,
264+ } ) ;
265+ } ,
266+ embeddings : ( response ) => {
267+ response . writeHead ( 200 , { "content-type" : "application/json" } ) ;
268+ response . end (
269+ JSON . stringify ( {
270+ data :
271+ server . requests . length === 2
272+ ? [ { index : 0 , embedding : [ 3 , 4 ] } ]
273+ : [
274+ { index : 1 , embedding : [ 0 , 2 ] } ,
275+ { index : 0 , embedding : [ 3 , 0 ] } ,
276+ ] ,
277+ } ) ,
278+ ) ;
279+ } ,
280+ } ) ;
281+ pointTokenAt ( server . baseUrl ) ;
256282
257- expect ( server . requests ) . toEqual ( [
258- { method : "GET" , url : "/models" } ,
259- { method : "POST" , url : "/embeddings" } ,
260- ] ) ;
261- expect ( Array . isArray ( vector ) ) . toBe ( true ) ;
262- expect ( vector ) . toHaveLength ( 3 ) ;
263- expect ( vector ?. every ( ( value ) => typeof value === "number" ) ) . toBe ( true ) ;
264- } ) ;
283+ const result = await githubCopilotMemoryEmbeddingProviderAdapter . create ( {
284+ ...defaultCreateOptions ( ) ,
285+ remote : {
286+ ...( auth === "custom" ? { baseUrl : server . baseUrl , apiKey : "explicit-test-token" } : { } ) ,
287+ headers : { Authorization : "replaced-test-token" , "X-Proof" : "forwarded" } ,
288+ } ,
289+ } ) ;
290+ expect ( await result . provider ?. embed ( "hello" , { inputType : "query" } ) ) . toEqual ( [ 0.6 , 0.8 ] ) ;
291+ expect ( await result . provider ?. embedBatch ( [ "first" , { text : "second" } ] ) ) . toEqual ( [
292+ [ 1 , 0 ] ,
293+ [ 0 , 1 ] ,
294+ ] ) ;
295+
296+ expect ( server . requests ) . toEqual ( [
297+ { method : "GET" , url : "/models" } ,
298+ { method : "POST" , url : "/embeddings" } ,
299+ { method : "POST" , url : "/embeddings" } ,
300+ ] ) ;
301+ const authorization = `Bearer ${ auth === "custom" ? "explicit-test-token" : "copilot_test_token_abc" } ` ;
302+ expect ( observed ) . toEqual (
303+ [
304+ "" ,
305+ JSON . stringify ( { model : "text-embedding-3-small" , input : [ "hello" ] } ) ,
306+ JSON . stringify ( { model : "text-embedding-3-small" , input : [ "first" , "second" ] } ) ,
307+ ] . map ( ( body ) => ( {
308+ authorization,
309+ integration : "copilot-developer-cli" ,
310+ custom : "forwarded" ,
311+ body,
312+ } ) ) ,
313+ ) ;
314+ expect ( resolveFirstGithubTokenMock ) . toHaveBeenCalledTimes ( auth === "custom" ? 0 : 1 ) ;
315+ expect ( resolveCopilotRuntimeAuthMock ) . toHaveBeenCalledTimes ( auth === "custom" ? 0 : 1 ) ;
316+ } ,
317+ ) ;
265318} ) ;
0 commit comments