/** @jest-environment jsdom */ import type { JourneyEvent } from '../../core/types'; import { JOURNEY_DEBUG_STORAGE_KEY, isJourneyDebugEnabled } from '../../global'; import { sendToConsole } from '../../sinks/console'; function sampleEvent(overrides: Partial = {}): JourneyEvent { return { journey: { name: 'Book Job', team: 'jbce', group: 'call-booking', service: 'call-screen', outcome: 'good', durationMs: 120, steps: [], ...overrides, }, }; } describe('[journey] sendToConsole / isJourneyDebugEnabled', () => { let debugSpy: jest.SpyInstance; beforeEach(() => { localStorage.clear(); sessionStorage.clear(); debugSpy = jest.spyOn(console, 'debug').mockImplementation(() => undefined); }); afterEach(() => { debugSpy.mockRestore(); localStorage.clear(); sessionStorage.clear(); }); describe('when st:journey:debug is not set', () => { test('isJourneyDebugEnabled is false', () => { expect(isJourneyDebugEnabled()).toBe(false); }); test('does not log', () => { sendToConsole(sampleEvent()); expect(debugSpy).not.toHaveBeenCalled(); }); }); describe('when st:journey:debug is a non-true value', () => { beforeEach(() => { sessionStorage.setItem(JOURNEY_DEBUG_STORAGE_KEY, '1'); }); test('isJourneyDebugEnabled is false', () => { expect(isJourneyDebugEnabled()).toBe(false); }); test('does not log', () => { sendToConsole(sampleEvent()); expect(debugSpy).not.toHaveBeenCalled(); }); }); describe('when st:journey:debug is true', () => { beforeEach(() => { sessionStorage.setItem(JOURNEY_DEBUG_STORAGE_KEY, 'true'); }); test('isJourneyDebugEnabled is true', () => { expect(isJourneyDebugEnabled()).toBe(true); }); test('logs the journey name, outcome, and payload', () => { const event = sampleEvent({ outcome: 'bad', reason: 'request-error' }); sendToConsole(event); expect(debugSpy).toHaveBeenCalledTimes(1); expect(debugSpy).toHaveBeenCalledWith( '[journey] book_job → bad', expect.objectContaining({ name: 'book_job', outcome: 'bad' }) ); }); }); describe('when sessionStorage throws', () => { beforeEach(() => { jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { throw new Error('denied'); }); }); afterEach(() => { jest.restoreAllMocks(); }); test('treats debug as disabled and does not throw', () => { expect(isJourneyDebugEnabled()).toBe(false); expect(() => sendToConsole(sampleEvent())).not.toThrow(); expect(debugSpy).not.toHaveBeenCalled(); }); }); });