11
22import { KubernetesClient } from 'kubernetesjs' ;
33import * as fs from 'fs' ;
4+ import * as path from 'path' ;
5+ require ( 'dotenv' ) . config ( { path : path . join ( __dirname , '../../../.env' ) } ) ;
46import { createJobTeardown } from '../../test-utils' ;
57
68describe ( 'LLM External Function (Integration)' , ( ) => {
@@ -10,9 +12,9 @@ describe('LLM External Function (Integration)', () => {
1012
1113 beforeAll ( async ( ) => {
1214 const { spawn } = require ( 'child_process' ) ;
13- proxyProcess = spawn ( 'kubectl' , [ 'proxy' , '--port=8005 ' ] ) ;
15+ proxyProcess = spawn ( 'kubectl' , [ 'proxy' , '--port=8001 ' ] ) ;
1416 await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
15- k8s = new KubernetesClient ( { restEndpoint : 'http://127.0.0.1:8005 ' } as any ) ;
17+ k8s = new KubernetesClient ( { restEndpoint : 'http://127.0.0.1:8001 ' } as any ) ;
1618 } ) ;
1719
1820 afterAll ( async ( ) => {
@@ -40,7 +42,10 @@ describe('LLM External Function (Integration)', () => {
4042 image : 'constructive/function-test-runner:v2' ,
4143 imagePullPolicy : "IfNotPresent" ,
4244 command : [ "npx" , "ts-node" , "functions/_runtimes/node/runner.js" , "functions/llm-external/src/index.ts" ] ,
43- env : [ { name : "OPENAI_API_KEY" , value : "sk-mock-key" } , { name : "PORT" , value : "8080" } ]
45+ env : [
46+ { name : "OPENAI_API_KEY" , value : process . env . OPENAI_API_KEY } ,
47+ { name : "PORT" , value : "8080" }
48+ ]
4449 } ]
4550 }
4651 }
@@ -61,35 +66,48 @@ describe('LLM External Function (Integration)', () => {
6166 if ( pods . items && pods . items . length > 0 ) podName = pods . items [ 0 ] . metadata . name ;
6267 }
6368 if ( podName ) {
64- // Check logs for startup
65- let logs = '' ;
6669 try {
67- const res = await fetch ( `http://127.0.0.1:8005/api/v1/namespaces/${ NAMESPACE } /pods/${ podName } /log?tailLines=50` ) ;
68- logs = await res . text ( ) ;
69- } catch ( e ) { }
70- logsResponse = logs ;
70+ const res = await fetch ( `http://127.0.0.1:8001/api/v1/namespaces/${ NAMESPACE } /pods/${ podName } /log?tailLines=50` ) ;
71+ const logs = await res . text ( ) ;
72+ logsResponse = logs ;
7173
72- if ( logs . includes ( 'listening on port' ) ) {
73- // Once listening, trigger the function via Proxy
74- if ( triggers < 5 ) { // Retry trigger a few times
75- try {
76- await fetch ( `http://127.0.0.1:8005/api/v1/namespaces/${ NAMESPACE } /pods/${ podName } /proxy/` , { method : 'POST' , body : JSON . stringify ( { } ) , headers : { 'Content-Type' : 'application/json' } } ) ;
77- triggers ++ ;
78- } catch ( e ) { }
79- }
74+ if ( logs . includes ( 'listening on port' ) ) {
75+ // Trigger with OpenAI payload
76+ // Trigger the function
77+ console . log ( '[Test] Triggering function...' ) ;
78+ const triggerRes = await fetch ( `http://127.0.0.1:8001/api/v1/namespaces/${ NAMESPACE } /pods/${ podName } :8080/proxy/` , {
79+ method : 'POST' ,
80+ body : JSON . stringify ( { provider : 'test' , prompt : 'Can you explain the quantum field theory in simple terms?' } ) ,
81+ headers : { 'Content-Type' : 'application/json' }
82+ } ) ;
8083
81- // Verify KNS activity (either success or DNS error proving intent)
82- if ( logs . includes ( 'GetUsers' ) || logs . includes ( 'constructive-server' ) || logs . includes ( 'ENOTFOUND' ) || logs . includes ( 'ECONNREFUSED' ) || logs . includes ( 'runner' ) ) {
83- success = true ;
84- break ;
84+ if ( triggerRes . ok ) {
85+ const body = await triggerRes . json ( ) ;
86+ console . log ( '[Test] Response:' , body ) ;
87+ if ( body . works ) {
88+ success = true ;
89+ logsResponse = logs ;
90+ break ;
91+ }
92+ }
8593 }
86- }
94+ // logsResponse = logs; // update logsResponse in loop
95+ } catch ( e ) { }
8796 }
8897 } catch ( e ) { }
8998 await new Promise ( r => setTimeout ( r , 2000 ) ) ;
9099 }
91100
92- if ( ! success ) throw new Error ( `LLM External Service Failed (No KNS Activity detected): ${ logsResponse } ` ) ;
101+ // Fetch Logs
102+ if ( podName ) {
103+ try {
104+ const res = await fetch ( `http://127.0.0.1:8001/api/v1/namespaces/${ NAMESPACE } /pods/${ podName } /log` ) ;
105+ const logs = await res . text ( ) ;
106+ console . log ( '\n[Evidence] Function Pod Logs:\n' + logs + '\n' ) ;
107+ } catch ( e ) { }
108+ }
109+
110+ if ( ! success ) throw new Error ( `LLM External Service Failed: Did not receive success response.` ) ;
93111 expect ( success ) . toBe ( true ) ;
94112
95113 await teardown ( ) ;
0 commit comments