11import { describe , it , expect , beforeEach , afterEach } from "vitest"
2- import { applyEnvOverrides , PROVIDER_ENV_VAR , PROVIDER_OVERRIDE_PREFIX } from "../env-overrides.js"
2+ import { applyEnvOverrides , PROVIDER_ENV_VAR , KILOCODE_PREFIX , KILO_PREFIX } from "../env-overrides.js"
33import type { CLIConfig } from "../types.js"
44
55describe ( "env-overrides" , ( ) => {
@@ -10,6 +10,13 @@ describe("env-overrides", () => {
1010 // Reset environment variables before each test
1111 process . env = { ...originalEnv }
1212
13+ // Clear any KILOCODE_* or KILO_* environment variables to ensure clean test state
14+ for ( const key of Object . keys ( process . env ) ) {
15+ if ( key . startsWith ( KILOCODE_PREFIX ) || key . startsWith ( KILO_PREFIX ) ) {
16+ delete process . env [ key ]
17+ }
18+ }
19+
1320 // Create a test config
1421 testConfig = {
1522 version : "1.0.0" ,
@@ -70,24 +77,97 @@ describe("env-overrides", () => {
7077 } )
7178 } )
7279
73- describe ( "KILO_PROVIDER_OVERRIDE_* overrides" , ( ) => {
74- it ( "should override any field in current provider" , ( ) => {
75- process . env [ `${ PROVIDER_OVERRIDE_PREFIX } kilocodeModel` ] = "anthropic/claude-opus-4.0"
76- process . env [ `${ PROVIDER_OVERRIDE_PREFIX } kilocodeOrganizationId` ] = "new-org-id"
80+ describe ( "KILOCODE_* overrides for kilocode provider" , ( ) => {
81+ it ( "should transform KILOCODE_MODEL to kilocodeModel" , ( ) => {
82+ process . env [ `${ KILOCODE_PREFIX } MODEL` ] = "anthropic/claude-opus-4.0"
83+
84+ const result = applyEnvOverrides ( testConfig )
85+
86+ const provider = result . providers . find ( ( p ) => p . id === "default" )
87+ expect ( provider ?. kilocodeModel ) . toBe ( "anthropic/claude-opus-4.0" )
88+ } )
89+
90+ it ( "should transform KILOCODE_ORGANIZATION_ID to kilocodeOrganizationId" , ( ) => {
91+ process . env [ `${ KILOCODE_PREFIX } ORGANIZATION_ID` ] = "new-org-id"
92+
93+ const result = applyEnvOverrides ( testConfig )
94+
95+ const provider = result . providers . find ( ( p ) => p . id === "default" )
96+ expect ( provider ?. kilocodeOrganizationId ) . toBe ( "new-org-id" )
97+ } )
98+
99+ it ( "should handle multiple KILOCODE_* overrides" , ( ) => {
100+ process . env [ `${ KILOCODE_PREFIX } MODEL` ] = "anthropic/claude-opus-4.0"
101+ process . env [ `${ KILOCODE_PREFIX } ORGANIZATION_ID` ] = "new-org-id"
102+ process . env [ `${ KILOCODE_PREFIX } TOKEN` ] = "new-token"
77103
78104 const result = applyEnvOverrides ( testConfig )
79105
80106 const provider = result . providers . find ( ( p ) => p . id === "default" )
81107 expect ( provider ?. kilocodeModel ) . toBe ( "anthropic/claude-opus-4.0" )
82108 expect ( provider ?. kilocodeOrganizationId ) . toBe ( "new-org-id" )
109+ expect ( provider ?. kilocodeToken ) . toBe ( "new-token" )
110+ } )
111+ } )
112+
113+ describe ( "KILO_* overrides for non-kilocode providers" , ( ) => {
114+ it ( "should transform KILO_API_KEY to apiKey for non-kilocode provider" , ( ) => {
115+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
116+ process . env [ `${ KILO_PREFIX } API_KEY` ] = "new-key"
117+
118+ const result = applyEnvOverrides ( testConfig )
119+
120+ expect ( result . provider ) . toBe ( "anthropic-provider" )
121+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
122+ expect ( provider ?. apiKey ) . toBe ( "new-key" )
123+ } )
124+
125+ it ( "should transform KILO_API_MODEL_ID to apiModelId" , ( ) => {
126+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
127+ process . env [ `${ KILO_PREFIX } API_MODEL_ID` ] = "claude-3-opus-20240229"
128+
129+ const result = applyEnvOverrides ( testConfig )
130+
131+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
132+ expect ( provider ?. apiModelId ) . toBe ( "claude-3-opus-20240229" )
133+ } )
134+
135+ it ( "should transform KILO_BASE_URL to baseUrl" , ( ) => {
136+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
137+ process . env [ `${ KILO_PREFIX } BASE_URL` ] = "https://api.example.com"
138+
139+ const result = applyEnvOverrides ( testConfig )
140+
141+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
142+ expect ( provider ?. baseUrl ) . toBe ( "https://api.example.com" )
143+ } )
144+
145+ it ( "should not apply KILO_* overrides to kilocode provider" , ( ) => {
146+ process . env [ PROVIDER_ENV_VAR ] = "kilocode"
147+ process . env [ `${ KILO_PREFIX } API_KEY` ] = "should-not-apply"
148+
149+ const result = applyEnvOverrides ( testConfig )
150+
151+ const provider = result . providers . find ( ( p ) => p . id === "default" )
152+ expect ( provider ?. apiKey ) . toBeUndefined ( )
153+ } )
154+
155+ it ( "should not apply KILOCODE_* overrides to non-kilocode provider" , ( ) => {
156+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
157+ process . env [ `${ KILOCODE_PREFIX } MODEL` ] = "should-not-apply"
158+
159+ const result = applyEnvOverrides ( testConfig )
160+
161+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
162+ expect ( provider ?. kilocodeModel ) . toBeUndefined ( )
83163 } )
84164 } )
85165
86166 describe ( "Combined overrides" , ( ) => {
87- it ( "should apply both provider and field overrides together" , ( ) => {
167+ it ( "should apply both provider and field overrides together for non-kilocode provider " , ( ) => {
88168 process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
89- process . env [ `${ PROVIDER_OVERRIDE_PREFIX } apiModelId ` ] = "claude-3-opus-20240229"
90- process . env [ `${ PROVIDER_OVERRIDE_PREFIX } apiKey ` ] = "new-key"
169+ process . env [ `${ KILO_PREFIX } API_MODEL_ID ` ] = "claude-3-opus-20240229"
170+ process . env [ `${ KILO_PREFIX } API_KEY ` ] = "new-key"
91171
92172 const result = applyEnvOverrides ( testConfig )
93173
@@ -96,6 +176,19 @@ describe("env-overrides", () => {
96176 expect ( provider ?. apiModelId ) . toBe ( "claude-3-opus-20240229" )
97177 expect ( provider ?. apiKey ) . toBe ( "new-key" )
98178 } )
179+
180+ it ( "should apply both provider and field overrides together for kilocode provider" , ( ) => {
181+ process . env [ PROVIDER_ENV_VAR ] = "default"
182+ process . env [ `${ KILOCODE_PREFIX } MODEL` ] = "anthropic/claude-opus-4.0"
183+ process . env [ `${ KILOCODE_PREFIX } ORGANIZATION_ID` ] = "new-org-id"
184+
185+ const result = applyEnvOverrides ( testConfig )
186+
187+ expect ( result . provider ) . toBe ( "default" )
188+ const provider = result . providers . find ( ( p ) => p . id === "default" )
189+ expect ( provider ?. kilocodeModel ) . toBe ( "anthropic/claude-opus-4.0" )
190+ expect ( provider ?. kilocodeOrganizationId ) . toBe ( "new-org-id" )
191+ } )
99192 } )
100193
101194 describe ( "Edge cases" , ( ) => {
@@ -115,8 +208,8 @@ describe("env-overrides", () => {
115208 expect ( result ) . toEqual ( testConfig )
116209 } )
117210
118- it ( "should handle empty string override values" , ( ) => {
119- process . env [ `${ PROVIDER_OVERRIDE_PREFIX } apiModelId ` ] = ""
211+ it ( "should handle empty string override values for KILOCODE_* " , ( ) => {
212+ process . env [ `${ KILOCODE_PREFIX } MODEL ` ] = ""
120213
121214 const result = applyEnvOverrides ( testConfig )
122215
@@ -125,13 +218,36 @@ describe("env-overrides", () => {
125218 expect ( provider ?. kilocodeModel ) . toBe ( "anthropic/claude-sonnet-4.5" )
126219 } )
127220
128- it ( "should ignore KILO_PROVIDER_OVERRIDE_ with no field name" , ( ) => {
129- process . env [ PROVIDER_OVERRIDE_PREFIX ] = "value"
221+ it ( "should handle empty string override values for KILO_*" , ( ) => {
222+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
223+ process . env [ `${ KILO_PREFIX } API_KEY` ] = ""
224+
225+ const result = applyEnvOverrides ( testConfig )
226+
227+ // Empty strings should not trigger overrides
228+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
229+ expect ( provider ?. apiKey ) . toBe ( "test-key" )
230+ } )
231+
232+ it ( "should ignore KILOCODE_ with no field name" , ( ) => {
233+ process . env [ KILOCODE_PREFIX . slice ( 0 , - 1 ) ] = "value"
130234
131235 const result = applyEnvOverrides ( testConfig )
132236
133237 // Should not modify anything
134238 expect ( result ) . toEqual ( testConfig )
135239 } )
240+
241+ it ( "should ignore KILO_PROVIDER since it's handled separately" , ( ) => {
242+ process . env [ PROVIDER_ENV_VAR ] = "anthropic-provider"
243+
244+ const result = applyEnvOverrides ( testConfig )
245+
246+ // KILO_PROVIDER should change the provider but not add a 'provider' field
247+ expect ( result . provider ) . toBe ( "anthropic-provider" )
248+
249+ const provider = result . providers . find ( ( p ) => p . id === "anthropic-provider" )
250+ expect ( provider ?. provider ) . toBe ( "anthropic" ) // Original value
251+ } )
136252 } )
137253} )
0 commit comments