Setting the file. One moment.
Telemetry Test · Media Use · heygen-com/hyperframes · Skills Docs
ContentsBack to the top of the page Resolve
(opens in a new tab)
scripts/lib/ telemetry.test.mjs
JavaScript · 98 lines · 3 KB
9 // dir so the run never touches the real config, and pin the env opt-outs to a
10 // neutral state; restore both afterwards.
11 const ENV_KEYS = [
12 "HOME" ,
13 "USERPROFILE" ,
14 "HYPERFRAMES_CONFIG_DIR" ,
15 "HYPERFRAMES_NO_TELEMETRY" ,
16 "DO_NOT_TRACK" ,
17 "CI" ,
18 "NODE_ENV" ,
19 ];
20 const savedEnv = Object. fromEntries ( ENV_KEYS . map (( k ) => [k, process.env[k]]));
21 const home = mkdtempSync ( join ( tmpdir (), "mu-telemetry-" ));
22 const configPath = join (home, ".hyperframes" , "config.json" );
23
24 function writeConfig ( contents ) {
25 if (contents === null ) {
26 rmSync (configPath, { force: true });
27 } else {
28 mkdirSync ( dirname (configPath), { recursive: true });
29 writeFileSync (configPath, contents);
30 }
31 }
32
33 test. before (() => {
34 for ( const key of ENV_KEYS ) delete process.env[key];
35 process.env. HOME = process.env. USERPROFILE = home;
36 });
37
38 test. after (() => {
39 for ( const key of ENV_KEYS ) {
40 if (savedEnv[key] === undefined ) delete process.env[key];
41 else process.env[key] = savedEnv[key];
42 }
43 rmSync (home, { recursive: true , force: true });
44 });
45
46 test ( "optedOut is false with no config and no opt-out env" , () => {
47 writeConfig ( null );
48 assert. equal ( optedOut (), false );
49 });
50
51 test ( "optedOut honors telemetryEnabled: false from the shared config" , () => {
52 writeConfig ( JSON . stringify ({ telemetryEnabled: false }));
53 assert. equal ( optedOut (), true );
54 });
55
56 test ( "optedOut is false when the shared config enables telemetry" , () => {
57 writeConfig ( JSON . stringify ({ telemetryEnabled: true }));
58 assert. equal ( optedOut (), false );
59 });
60
61 test ( "optedOut treats an unreadable shared config as no opt-out" , () => {
62 writeConfig ( "{not json" );
63 assert. equal ( optedOut (), false );
64 });
65
66 test ( "optedOut still honors the env opt-outs" , () => {
67 writeConfig ( JSON . stringify ({ telemetryEnabled: true }));
68 process.env. DO_NOT_TRACK = "1" ;
69 try {
70 assert. equal ( optedOut (), true );
71 } finally {
72 delete process.env. DO_NOT_TRACK ;
73 }
74 });
75
76 test ( "optedOut reads the shared config from HYPERFRAMES_CONFIG_DIR when it is set" , () => {
77 writeConfig ( null );
78 const configDir = join (home, "elsewhere" );
79 mkdirSync (configDir, { recursive: true });
80 writeFileSync ( join (configDir, "config.json" ), JSON . stringify ({ telemetryEnabled: false }));
81 process.env. HYPERFRAMES_CONFIG_DIR = configDir;
82 try {
83 assert. equal ( optedOut (), true );
84 } finally {
85 delete process.env. HYPERFRAMES_CONFIG_DIR ;
86 }
87 });
88
89 test ( "an opt-out in ~/.hyperframes holds until HYPERFRAMES_CONFIG_DIR has its own config" , () => {
90 writeConfig ( JSON . stringify ({ telemetryEnabled: false }));
91 process.env. HYPERFRAMES_CONFIG_DIR = join (home, "moved" );
92 try {
93 assert. equal ( optedOut (), true );
94 } finally {
95 delete process.env. HYPERFRAMES_CONFIG_DIR ;
96 writeConfig ( null );
97 }
98 });