|
| 1 | +import { expect } from 'chai' |
| 2 | +import { SessionManager } from '@/utils/ai/SessionManager' |
| 3 | + |
| 4 | +describe('SessionManager', () => { |
| 5 | + let sessionManager: SessionManager |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + // Create a new SessionManager instance before each test |
| 9 | + sessionManager = new SessionManager() |
| 10 | + }) |
| 11 | + |
| 12 | + describe('getState', () => { |
| 13 | + it('should return a copy of the state', () => { |
| 14 | + const state = sessionManager.getState() |
| 15 | + |
| 16 | + // Verify that it returns an object |
| 17 | + expect(state).to.be.an('object') |
| 18 | + |
| 19 | + // Verify that the returned state contains expected properties |
| 20 | + expect(state).to.have.property('systemPrompt') |
| 21 | + expect(state).to.have.property('isNewSession') |
| 22 | + expect(state).to.have.property('presetPrompt') |
| 23 | + |
| 24 | + // Modifying the returned state should not affect the original state |
| 25 | + state.isNewSession = false |
| 26 | + expect(sessionManager.getState().isNewSession).to.be.true |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + describe('resetSession', () => { |
| 31 | + it('should reset the session to initial values', () => { |
| 32 | + // First modify the session state |
| 33 | + sessionManager.startSession() |
| 34 | + sessionManager.updatePreset('test-preset') |
| 35 | + |
| 36 | + // Then reset |
| 37 | + sessionManager.resetSession() |
| 38 | + |
| 39 | + // Verify the state has been reset |
| 40 | + const state = sessionManager.getState() |
| 41 | + expect(state.isNewSession).to.be.true |
| 42 | + expect(state.systemPrompt).to.equal('') |
| 43 | + expect(state.presetPrompt).to.equal('') |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + describe('resetSessionKeepPreset', () => { |
| 48 | + it('should reset the session while keeping preset prompt', () => { |
| 49 | + // First modify the session state |
| 50 | + sessionManager.startSession() |
| 51 | + sessionManager.updatePreset('test-preset') |
| 52 | + |
| 53 | + // Then reset, but keep the preset prompt |
| 54 | + sessionManager.resetSessionKeepPreset() |
| 55 | + |
| 56 | + // Verify the state has been reset, but preset prompt is retained |
| 57 | + const state = sessionManager.getState() |
| 58 | + expect(state.isNewSession).to.be.true |
| 59 | + expect(state.systemPrompt).to.equal('') |
| 60 | + expect(state.presetPrompt).to.equal('test-preset') |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('startSession', () => { |
| 65 | + it('should mark the session as not new', () => { |
| 66 | + // Initially isNewSession should be true |
| 67 | + expect(sessionManager.getState().isNewSession).to.be.true |
| 68 | + |
| 69 | + // Start the session |
| 70 | + sessionManager.startSession() |
| 71 | + |
| 72 | + // Verify isNewSession becomes false |
| 73 | + expect(sessionManager.getState().isNewSession).to.be.false |
| 74 | + }) |
| 75 | + |
| 76 | + it('should not affect other state properties', () => { |
| 77 | + // Set some initial state |
| 78 | + sessionManager.updatePreset('test-preset') |
| 79 | + |
| 80 | + // Record the current system prompt state |
| 81 | + const initialSystemPrompt = sessionManager.getState().systemPrompt |
| 82 | + |
| 83 | + // Start the session |
| 84 | + sessionManager.startSession() |
| 85 | + |
| 86 | + // Verify only the isNewSession property was modified |
| 87 | + const state = sessionManager.getState() |
| 88 | + expect(state.systemPrompt).to.equal(initialSystemPrompt) |
| 89 | + expect(state.presetPrompt).to.equal('test-preset') |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + describe('updatePreset', () => { |
| 94 | + it('should update the preset prompt', () => { |
| 95 | + sessionManager.updatePreset('new-preset') |
| 96 | + |
| 97 | + expect(sessionManager.getState().presetPrompt).to.equal('new-preset') |
| 98 | + }) |
| 99 | + |
| 100 | + it('should mark the session as new', () => { |
| 101 | + // First mark the session as not new |
| 102 | + sessionManager.startSession() |
| 103 | + expect(sessionManager.getState().isNewSession).to.be.false |
| 104 | + |
| 105 | + // Update the preset prompt |
| 106 | + sessionManager.updatePreset('new-preset') |
| 107 | + |
| 108 | + // Verify the session is marked as new |
| 109 | + expect(sessionManager.getState().isNewSession).to.be.true |
| 110 | + }) |
| 111 | + |
| 112 | + it('should clear the system prompt', () => { |
| 113 | + // Simulate having an existing system prompt |
| 114 | + sessionManager.getSystemPrompt('en') |
| 115 | + expect(sessionManager.getState().systemPrompt).to.not.equal('') |
| 116 | + |
| 117 | + // Update the preset prompt |
| 118 | + sessionManager.updatePreset('new-preset') |
| 119 | + |
| 120 | + // Verify the system prompt is cleared |
| 121 | + expect(sessionManager.getState().systemPrompt).to.equal('') |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + describe('getSystemPrompt', () => { |
| 126 | + it('should load system prompt if session is new', () => { |
| 127 | + // Using sinon would encounter import issues, so we simply test behavior here |
| 128 | + const result = sessionManager.getSystemPrompt('en') |
| 129 | + |
| 130 | + // Verify it returned a non-empty string |
| 131 | + expect(result).to.be.a('string') |
| 132 | + expect(result).to.not.equal('') |
| 133 | + |
| 134 | + // Verify the state was updated |
| 135 | + expect(sessionManager.getState().systemPrompt).to.equal(result) |
| 136 | + }) |
| 137 | + |
| 138 | + it('should return cached system prompt if session is not new', () => { |
| 139 | + // Get the system prompt for the first time |
| 140 | + const firstPrompt = sessionManager.getSystemPrompt('en') |
| 141 | + |
| 142 | + // Mark the session as not new |
| 143 | + sessionManager.startSession() |
| 144 | + |
| 145 | + // Get the system prompt again |
| 146 | + const secondPrompt = sessionManager.getSystemPrompt('en') |
| 147 | + |
| 148 | + // Verify the same prompt is returned |
| 149 | + expect(secondPrompt).to.equal(firstPrompt) |
| 150 | + }) |
| 151 | + |
| 152 | + it('should reload system prompt if empty even if session is not new', () => { |
| 153 | + // Get the system prompt for the first time |
| 154 | + sessionManager.getSystemPrompt('en') |
| 155 | + |
| 156 | + // Mark the session as not new |
| 157 | + sessionManager.startSession() |
| 158 | + |
| 159 | + // Manually clear the system prompt |
| 160 | + sessionManager['state'].systemPrompt = '' |
| 161 | + |
| 162 | + // Get the system prompt again |
| 163 | + const reloadedPrompt = sessionManager.getSystemPrompt('en') |
| 164 | + |
| 165 | + // Verify it returned a non-empty prompt |
| 166 | + expect(reloadedPrompt).to.be.a('string') |
| 167 | + expect(reloadedPrompt).to.not.equal('') |
| 168 | + }) |
| 169 | + }) |
| 170 | +}) |
0 commit comments