1+ // SPDX-License-Identifier: MIT
2+
3+ import { describe , it , beforeEach , afterEach } from 'node:test' ;
4+ import assert from 'node:assert' ;
5+ import { mkdtempSync , rmSync } from 'node:fs' ;
6+ import { tmpdir } from 'node:os' ;
7+ import { join } from 'node:path' ;
8+ import { collectSystemMetricsFromEnv } from './client.js' ;
9+
10+ describe ( 'collectSystemMetricsFromEnv' , ( ) => {
11+ const originalEnv = process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] ;
12+
13+ afterEach ( ( ) => {
14+ if ( originalEnv === undefined ) {
15+ delete process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] ;
16+ } else {
17+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = originalEnv ;
18+ }
19+ } ) ;
20+
21+ it ( 'should return true when env is not set' , ( ) => {
22+ delete process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] ;
23+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , true ) ;
24+ } ) ;
25+
26+ it ( 'should return true when env is "true"' , ( ) => {
27+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'true' ;
28+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , true ) ;
29+ } ) ;
30+
31+ it ( 'should return true when env is "TRUE"' , ( ) => {
32+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'TRUE' ;
33+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , true ) ;
34+ } ) ;
35+
36+ it ( 'should return true when env is "1"' , ( ) => {
37+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = '1' ;
38+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , true ) ;
39+ } ) ;
40+
41+ it ( 'should return false when env is "false"' , ( ) => {
42+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'false' ;
43+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , false ) ;
44+ } ) ;
45+
46+ it ( 'should return false when env is "FALSE"' , ( ) => {
47+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'FALSE' ;
48+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , false ) ;
49+ } ) ;
50+
51+ it ( 'should return false when env is "0"' , ( ) => {
52+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = '0' ;
53+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , false ) ;
54+ } ) ;
55+
56+ it ( 'should return true when env is any other value' , ( ) => {
57+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'anything' ;
58+ assert . strictEqual ( collectSystemMetricsFromEnv ( ) , true ) ;
59+ } ) ;
60+ } ) ;
61+
62+ describe ( 'SHMClient' , ( ) => {
63+ let tmpDir : string ;
64+
65+ beforeEach ( ( ) => {
66+ tmpDir = mkdtempSync ( join ( tmpdir ( ) , 'shm-client-test-' ) ) ;
67+ } ) ;
68+
69+ afterEach ( ( ) => {
70+ rmSync ( tmpDir , { recursive : true , force : true } ) ;
71+ } ) ;
72+
73+ // Dynamic import to avoid issues with ESM
74+ async function importClient ( ) {
75+ const { SHMClient } = await import ( './client.js' ) ;
76+ return SHMClient ;
77+ }
78+
79+ describe ( 'collectSystemMetrics config' , ( ) => {
80+ it ( 'should default to true when not specified and env not set' , async ( ) => {
81+ delete process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] ;
82+ const SHMClient = await importClient ( ) ;
83+
84+ const client = new SHMClient ( {
85+ serverUrl : 'http://localhost:8080' ,
86+ appName : 'test-app' ,
87+ appVersion : '1.0.0' ,
88+ dataDir : tmpDir ,
89+ } ) ;
90+
91+ // Access private config via any cast
92+ const config = ( client as any ) . config ;
93+ assert . strictEqual ( config . collectSystemMetrics , true ) ;
94+ } ) ;
95+
96+ it ( 'should respect explicit false config' , async ( ) => {
97+ const SHMClient = await importClient ( ) ;
98+
99+ const client = new SHMClient ( {
100+ serverUrl : 'http://localhost:8080' ,
101+ appName : 'test-app' ,
102+ appVersion : '1.0.0' ,
103+ dataDir : tmpDir ,
104+ collectSystemMetrics : false ,
105+ } ) ;
106+
107+ const config = ( client as any ) . config ;
108+ assert . strictEqual ( config . collectSystemMetrics , false ) ;
109+ } ) ;
110+
111+ it ( 'should respect explicit true config' , async ( ) => {
112+ process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] = 'false' ;
113+ const SHMClient = await importClient ( ) ;
114+
115+ const client = new SHMClient ( {
116+ serverUrl : 'http://localhost:8080' ,
117+ appName : 'test-app' ,
118+ appVersion : '1.0.0' ,
119+ dataDir : tmpDir ,
120+ collectSystemMetrics : true ,
121+ } ) ;
122+
123+ const config = ( client as any ) . config ;
124+ assert . strictEqual ( config . collectSystemMetrics , true ) ;
125+
126+ delete process . env [ 'SHM_COLLECT_SYSTEM_METRICS' ] ;
127+ } ) ;
128+ } ) ;
129+
130+ describe ( 'DO_NOT_TRACK' , ( ) => {
131+ it ( 'should disable client when DO_NOT_TRACK=true' , async ( ) => {
132+ process . env [ 'DO_NOT_TRACK' ] = 'true' ;
133+ const SHMClient = await importClient ( ) ;
134+
135+ const client = new SHMClient ( {
136+ serverUrl : 'http://localhost:8080' ,
137+ appName : 'test-app' ,
138+ appVersion : '1.0.0' ,
139+ dataDir : tmpDir ,
140+ enabled : true , // explicitly enabled
141+ } ) ;
142+
143+ const config = ( client as any ) . config ;
144+ assert . strictEqual ( config . enabled , false ) ;
145+
146+ delete process . env [ 'DO_NOT_TRACK' ] ;
147+ } ) ;
148+
149+ it ( 'should disable client when DO_NOT_TRACK=1' , async ( ) => {
150+ process . env [ 'DO_NOT_TRACK' ] = '1' ;
151+ const SHMClient = await importClient ( ) ;
152+
153+ const client = new SHMClient ( {
154+ serverUrl : 'http://localhost:8080' ,
155+ appName : 'test-app' ,
156+ appVersion : '1.0.0' ,
157+ dataDir : tmpDir ,
158+ enabled : true ,
159+ } ) ;
160+
161+ const config = ( client as any ) . config ;
162+ assert . strictEqual ( config . enabled , false ) ;
163+
164+ delete process . env [ 'DO_NOT_TRACK' ] ;
165+ } ) ;
166+
167+ it ( 'should not disable client when DO_NOT_TRACK=false' , async ( ) => {
168+ process . env [ 'DO_NOT_TRACK' ] = 'false' ;
169+ const SHMClient = await importClient ( ) ;
170+
171+ const client = new SHMClient ( {
172+ serverUrl : 'http://localhost:8080' ,
173+ appName : 'test-app' ,
174+ appVersion : '1.0.0' ,
175+ dataDir : tmpDir ,
176+ enabled : true ,
177+ } ) ;
178+
179+ const config = ( client as any ) . config ;
180+ assert . strictEqual ( config . enabled , true ) ;
181+
182+ delete process . env [ 'DO_NOT_TRACK' ] ;
183+ } ) ;
184+
185+ it ( 'should not disable client when DO_NOT_TRACK is not set' , async ( ) => {
186+ delete process . env [ 'DO_NOT_TRACK' ] ;
187+ const SHMClient = await importClient ( ) ;
188+
189+ const client = new SHMClient ( {
190+ serverUrl : 'http://localhost:8080' ,
191+ appName : 'test-app' ,
192+ appVersion : '1.0.0' ,
193+ dataDir : tmpDir ,
194+ enabled : true ,
195+ } ) ;
196+
197+ const config = ( client as any ) . config ;
198+ assert . strictEqual ( config . enabled , true ) ;
199+ } ) ;
200+ } ) ;
201+ } ) ;
0 commit comments