@@ -216,6 +216,56 @@ describe("buildCliContext", () => {
216216 } )
217217 } )
218218
219+ test ( "Config.get() throws: the opt-out of the project the command acts on is read, not the cwd's" , async ( ) => {
220+ delete process . env . ALTIMATE_TELEMETRY_DISABLED
221+ cfg . mockImplementation ( ( async ( ) => {
222+ throw new Error ( "InstanceRef not provided" )
223+ } ) as never )
224+ await using dir = await tmpdir ( )
225+ await using project = await tmpdir ( )
226+ fs . mkdirSync ( path . join ( project . path , ".altimate-code" ) )
227+ fs . writeFileSync (
228+ path . join ( project . path , ".altimate-code" , "altimate-code.json" ) ,
229+ JSON . stringify ( { telemetry : { disabled : true } } ) ,
230+ )
231+ const idPath = path . join ( dir . path , "machine-id" )
232+ fs . writeFileSync ( idPath , VALID_UUID , "utf8" )
233+ const saved = {
234+ cs : process . env . APPLICATIONINSIGHTS_CONNECTION_STRING ,
235+ opencodeOptOut : process . env . OPENCODE_DISABLE_TELEMETRY ,
236+ content : process . env . OPENCODE_CONFIG_CONTENT ,
237+ }
238+ delete process . env . OPENCODE_DISABLE_TELEMETRY
239+ delete process . env . OPENCODE_CONFIG_CONTENT
240+ process . env . APPLICATIONINSIGHTS_CONNECTION_STRING =
241+ "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/"
242+ const home = spyOn ( os , "homedir" ) . mockImplementation ( ( ) => dir . path )
243+ const net = spyOn ( globalThis , "fetch" ) . mockImplementation ( ( async ( ) => new Response ( "" , { status : 200 } ) ) as any )
244+ const decode = ( encoded : string ) =>
245+ JSON . parse ( Buffer . from ( encoded , "base64url" ) . toString ( "utf8" ) ) as Record < string , unknown >
246+ try {
247+ await Telemetry . shutdown ( )
248+ await Telemetry . init ( )
249+ expect ( Telemetry . isEnabled ( ) ) . toBe ( true )
250+ // The opted-out project: no id.
251+ expect ( "machine_id" in decode ( await buildCliContext ( idPath , project . path ) ) ) . toBe ( false )
252+ // The same process from a directory with no opt-out: the id is sent.
253+ expect ( decode ( await buildCliContext ( idPath , dir . path ) ) [ "machine_id" ] ) . toBe ( VALID_UUID )
254+ } finally {
255+ await Telemetry . shutdown ( )
256+ net . mockRestore ( )
257+ home . mockRestore ( )
258+ for ( const [ key , value ] of [
259+ [ "APPLICATIONINSIGHTS_CONNECTION_STRING" , saved . cs ] ,
260+ [ "OPENCODE_DISABLE_TELEMETRY" , saved . opencodeOptOut ] ,
261+ [ "OPENCODE_CONFIG_CONTENT" , saved . content ] ,
262+ ] as const ) {
263+ if ( value === undefined ) delete process . env [ key ]
264+ else process . env [ key ] = value
265+ }
266+ }
267+ } )
268+
219269 test ( "includes machine_id when telemetry is enabled" , async ( ) => {
220270 delete process . env . ALTIMATE_TELEMETRY_DISABLED
221271 await using dir = await tmpdir ( )
0 commit comments