/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import test from 'node:test'; import assert from 'node:assert/strict'; import { bindConversationIdToPayload, prependCreatedSession, removeSessionAndResolveNext, shouldCreateDefaultSession, shouldRequireExplicitSession, } from './sessionFlow.js'; test('shouldRequireExplicitSession blocks sends only when session mode is enabled without a selection', () => { assert.equal( shouldRequireExplicitSession({ sessionEnabled: true, activeSessionId: null, }), true, ); assert.equal( shouldRequireExplicitSession({ sessionEnabled: true, activeSessionId: 42 }), false, ); assert.equal( shouldRequireExplicitSession({ sessionEnabled: false, activeSessionId: null, }), false, ); }); test('shouldCreateDefaultSession only creates after a successful empty list response', () => { assert.equal(shouldCreateDefaultSession([]), true); assert.equal(shouldCreateDefaultSession([{ id: 1 }]), false); assert.equal(shouldCreateDefaultSession(null), false); assert.equal(shouldCreateDefaultSession(undefined), false); }); test('prependCreatedSession keeps the new session at the top without duplicates', () => { const created = { id: 3, title: 'New' }; const sessions = [ { id: 1, title: 'First' }, { id: 3, title: 'Stale copy' }, { id: 2, title: 'Second' }, ]; assert.deepEqual(prependCreatedSession(sessions, created), [ { id: 3, title: 'New' }, { id: 1, title: 'First' }, { id: 2, title: 'Second' }, ]); }); test('removeSessionAndResolveNext clears the active selection instead of creating a replacement session', () => { const result = removeSessionAndResolveNext({ sessions: [{ id: 9, title: 'Only one' }], deletedSessionId: 9, activeSessionId: 9, }); assert.deepEqual(result.remainingSessions, []); assert.equal(result.deletedActiveSession, true); assert.equal(result.nextActiveSessionId, null); }); test('removeSessionAndResolveNext switches to the first remaining session after deleting the active one', () => { const result = removeSessionAndResolveNext({ sessions: [ { id: 4, title: 'Active' }, { id: 5, title: 'Next' }, { id: 6, title: 'Later' }, ], deletedSessionId: 4, activeSessionId: 4, }); assert.deepEqual(result.remainingSessions, [ { id: 5, title: 'Next' }, { id: 6, title: 'Later' }, ]); assert.equal(result.nextActiveSessionId, 5); }); test('bindConversationIdToPayload always follows the active session in explicit-session mode when forced', () => { const payload = bindConversationIdToPayload( { stream: true, conversation_id: 7 }, { sessionEnabled: true, activeSessionId: 12, force: true }, ); assert.deepEqual(payload, { stream: true, conversation_id: 12, }); }); test('bindConversationIdToPayload preserves an existing conversation_id when force is not requested', () => { const payload = bindConversationIdToPayload( { stream: true, conversation_id: 7 }, { sessionEnabled: true, activeSessionId: 12 }, ); assert.deepEqual(payload, { stream: true, conversation_id: 7, }); });