/** @jest-environment jsdom */ import '../config'; import { defineJourney, resetJourney } from '../core'; import { moduleRuntime } from '../core/runtime'; import { JOURNEY_DEBUG_STORAGE_KEY, JOURNEY_ENABLED_STORAGE_KEY, ST_JOURNEY_GLOBAL_KEY, ST_JOURNEY_SCHEMA_VERSION, disableJourney, enableJourney, isJourneyEnabled, setJourneyDefaultEnabled, type StJourneyGlobal, } from '../global'; import { setJourneyRum } from '../sinks/datadog'; function getStJourney(): StJourneyGlobal { return (globalThis as Record)[ST_JOURNEY_GLOBAL_KEY] as StJourneyGlobal; } describe('[journey] __stJourney global', () => { beforeEach(() => { localStorage.clear(); sessionStorage.clear(); setJourneyDefaultEnabled(false); delete getStJourney().rum; delete (globalThis as Record).DD_RUM; resetJourney({ sinks: [] }); }); afterEach(() => { localStorage.clear(); sessionStorage.clear(); setJourneyDefaultEnabled(false); delete getStJourney().rum; delete (globalThis as Record).DD_RUM; resetJourney({ sinks: [] }); }); test('installs __stJourney on package load', () => { expect(getStJourney()).toEqual( expect.objectContaining({ schemaVersion: ST_JOURNEY_SCHEMA_VERSION, enable: expect.any(Function), disable: expect.any(Function), isEnabled: expect.any(Function), enableDebugging: expect.any(Function), disableDebugging: expect.any(Function), isDebugEnabled: expect.any(Function), getDebugState: expect.any(Function), }) ); }); test('enable / disable toggle force-enable localStorage', () => { expect(isJourneyEnabled()).toBe(false); enableJourney(); expect(localStorage.getItem(JOURNEY_ENABLED_STORAGE_KEY)).toBe('true'); expect(isJourneyEnabled()).toBe(true); expect(getStJourney().isEnabled()).toBe(true); disableJourney(); expect(localStorage.getItem(JOURNEY_ENABLED_STORAGE_KEY)).toBeNull(); expect(isJourneyEnabled()).toBe(false); }); test('isJourneyEnabled is true when host default is on', () => { expect(isJourneyEnabled()).toBe(false); setJourneyDefaultEnabled(true); expect(isJourneyEnabled()).toBe(true); expect(getStJourney().isEnabled()).toBe(true); setJourneyDefaultEnabled(false); expect(isJourneyEnabled()).toBe(false); }); test('isJourneyEnabled is true when either host default or force flag is on', () => { setJourneyDefaultEnabled(true); enableJourney(); expect(isJourneyEnabled()).toBe(true); setJourneyDefaultEnabled(false); expect(isJourneyEnabled()).toBe(true); disableJourney(); expect(isJourneyEnabled()).toBe(false); setJourneyDefaultEnabled(true); expect(isJourneyEnabled()).toBe(true); }); test('host default is shared on __stJourney for cross-bundle reads', () => { setJourneyDefaultEnabled(true); expect(getStJourney().defaultEnabled).toBe(true); expect(isJourneyEnabled()).toBe(true); }); test('enableDebugging / disableDebugging toggle console-debug sessionStorage', () => { const st = getStJourney(); st.enableDebugging(); expect(sessionStorage.getItem(JOURNEY_DEBUG_STORAGE_KEY)).toBe('true'); expect(st.isDebugEnabled()).toBe(true); st.disableDebugging(); expect(sessionStorage.getItem(JOURNEY_DEBUG_STORAGE_KEY)).toBeNull(); expect(st.isDebugEnabled()).toBe(false); }); test('getDebugState omits runtimes until debugging is enabled', () => { enableJourney(); const journey = defineJourney({ name: 'Book Job', team: 'jbce', group: 'call-booking', service: 'call-screen', }); journey.start(); expect(getStJourney().getDebugState()).toEqual( expect.objectContaining({ enabled: true, debug: false, runtimes: [], }) ); getStJourney().enableDebugging(); const on = getStJourney().getDebugState(); expect(on.debug).toBe(true); expect(on.runtimes.some(r => r.id === moduleRuntime.id)).toBe(true); expect(on.runtimes.some(r => r.openJourneys.some(j => j.name === 'Book Job'))).toBe(true); getStJourney().disableDebugging(); expect(getStJourney().getDebugState().runtimes).toEqual([]); }); test('getDebugState.hasRum reflects DD_RUM or the fallback bridge', () => { getStJourney().enableDebugging(); expect(getStJourney().getDebugState().hasRum).toBe(false); (globalThis as Record).DD_RUM = { addAction: jest.fn() }; expect(getStJourney().getDebugState().hasRum).toBe(true); delete (globalThis as Record).DD_RUM; jest.spyOn(console, 'warn').mockImplementation(() => undefined); setJourneyRum({ addAction: jest.fn() }); expect(getStJourney().getDebugState().hasRum).toBe(true); }); });