basant307/AI_Governance_Project
048
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { afterEach, describe, expect, it } from 'vitest';8import { ROOT_CONTEXT, createContextKey } from '@opentelemetry/api';9import {10 getSessionContext,11 setSessionContext,12 getCurrentSessionId,13} from './session-context.js';14 15describe('session-context', () => {16 afterEach(() => {17 setSessionContext(undefined);18 });19 20 it('returns the context that was set', () => {21 const key = createContextKey('session-test-key');22 const ctx = ROOT_CONTEXT.setValue(key, 'session-value');23 24 setSessionContext(ctx);25 26 expect(getSessionContext()?.getValue(key)).toBe('session-value');27 });28 29 it('replaces the stored context', () => {30 const key = createContextKey('session-replace-key');31 const firstContext = ROOT_CONTEXT.setValue(key, 'first');32 const secondContext = ROOT_CONTEXT.setValue(key, 'second');33 34 setSessionContext(firstContext);35 setSessionContext(secondContext);36 37 expect(getSessionContext()?.getValue(key)).toBe('second');38 });39 40 it('clears the stored context', () => {41 const key = createContextKey('session-clear-key');42 setSessionContext(ROOT_CONTEXT.setValue(key, 'session-value'));43 44 setSessionContext(undefined);45 46 expect(getSessionContext()).toBeUndefined();47 });48});49 50describe('getCurrentSessionId', () => {51 afterEach(() => {52 setSessionContext(undefined);53 });54 55 it('returns undefined when no session has been set', () => {56 expect(getCurrentSessionId()).toBeUndefined();57 });58 59 it('returns the session ID passed to setSessionContext', () => {60 const key = createContextKey('sid-test-key');61 setSessionContext(ROOT_CONTEXT.setValue(key, 'ctx'), 'session-abc');62 63 expect(getCurrentSessionId()).toBe('session-abc');64 });65 66 it('updates when setSessionContext is called with a new session ID', () => {67 const key = createContextKey('sid-update-key');68 setSessionContext(ROOT_CONTEXT.setValue(key, 'first'), 'session-1');69 setSessionContext(ROOT_CONTEXT.setValue(key, 'second'), 'session-2');70 71 expect(getCurrentSessionId()).toBe('session-2');72 });73 74 it('clears when setSessionContext is called without a session ID', () => {75 const key = createContextKey('sid-clear-key');76 setSessionContext(ROOT_CONTEXT.setValue(key, 'ctx'), 'session-xyz');77 setSessionContext(undefined);78 79 expect(getCurrentSessionId()).toBeUndefined();80 });81});82 