22
33import { describe , expect , it , vi , beforeEach } from "vitest" ;
44
5- // `shouldTrack()` reads `POSTHOG_API_KEY` from module-level const that's
6- // evaluated at module load time, so changing `import.meta.env` after import
7- // has no effect on the key. Each test resets module cache and re-imports.
5+ // `shouldTrack()` reads module-level constants evaluated at module load time,
6+ // so changing env after import has no effect. Each test resets module cache.
87
98const OPT_OUT_KEY = "hyperframes-studio:telemetryDisabled" ;
109
11- function setKey ( value : string | undefined ) : void {
12- if ( value === undefined ) {
13- delete ( import . meta. env as Record < string , unknown > ) . VITE_HYPERFRAMES_POSTHOG_KEY ;
14- } else {
15- ( import . meta. env as Record < string , unknown > ) . VITE_HYPERFRAMES_POSTHOG_KEY = value ;
16- }
17- }
18-
1910function setNoTelemetry ( value : string | undefined ) : void {
2011 if ( value === undefined ) {
2112 delete ( import . meta. env as Record < string , unknown > ) . VITE_HYPERFRAMES_NO_TELEMETRY ;
@@ -37,29 +28,16 @@ async function loadShouldTrack(): Promise<() => boolean> {
3728describe ( "studio client shouldTrack" , ( ) => {
3829 beforeEach ( ( ) => {
3930 setDev ( false ) ;
40- setKey ( "phc_test_key" ) ;
4131 setNoTelemetry ( undefined ) ;
4232 localStorage . clear ( ) ;
4333 vi . unstubAllGlobals ( ) ;
4434 } ) ;
4535
46- it ( "returns true when key is configured, not in dev mode, and no opt-outs" , async ( ) => {
36+ it ( "returns true when not in dev mode and no opt-outs" , async ( ) => {
4737 const shouldTrack = await loadShouldTrack ( ) ;
4838 expect ( shouldTrack ( ) ) . toBe ( true ) ;
4939 } ) ;
5040
51- it ( "returns false when API key does not start with phc_" , async ( ) => {
52- setKey ( "not_a_real_key" ) ;
53- const shouldTrack = await loadShouldTrack ( ) ;
54- expect ( shouldTrack ( ) ) . toBe ( false ) ;
55- } ) ;
56-
57- it ( "returns false when API key is empty string" , async ( ) => {
58- setKey ( "" ) ;
59- const shouldTrack = await loadShouldTrack ( ) ;
60- expect ( shouldTrack ( ) ) . toBe ( false ) ;
61- } ) ;
62-
6341 it ( "returns false when user has opted out via localStorage" , async ( ) => {
6442 localStorage . setItem ( OPT_OUT_KEY , "1" ) ;
6543 const shouldTrack = await loadShouldTrack ( ) ;
@@ -93,7 +71,6 @@ describe("studio client shouldTrack", () => {
9371 it ( "memoizes its decision after the first call" , async ( ) => {
9472 const shouldTrack = await loadShouldTrack ( ) ;
9573 const first = shouldTrack ( ) ;
96- // Flip an underlying input — memoized return must not change.
9774 localStorage . setItem ( OPT_OUT_KEY , "1" ) ;
9875 expect ( shouldTrack ( ) ) . toBe ( first ) ;
9976 } ) ;
0 commit comments