mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-11 14:00:29 +00:00
Handle empty chat session payloads in AI settings (#1149)
This commit is contained in:
@@ -12,6 +12,7 @@ import { AIAPI } from '@/api/ai';
|
||||
import { AIChatAPI, type ChatSession, type FileChange } from '@/api/aiChat';
|
||||
import { hasFeature, loadLicenseStatus } from '@/stores/license';
|
||||
import type { AISettings as AISettingsType, AIProvider, AuthMethod } from '@/types/ai';
|
||||
import { normalizeChatSessions } from '@/components/Settings/aiSettingsChatSessions';
|
||||
|
||||
// Providers are now configured via accordion sections, not a single-provider selector
|
||||
|
||||
@@ -284,10 +285,10 @@ export const AISettings: Component = () => {
|
||||
setChatSessionsLoading(true);
|
||||
setChatSessionsError('');
|
||||
try {
|
||||
const sessions = await AIChatAPI.listSessions();
|
||||
const sessions = normalizeChatSessions(await AIChatAPI.listSessions());
|
||||
setChatSessions(sessions);
|
||||
const current = selectedSessionId();
|
||||
if (!Array.isArray(sessions) || sessions.length === 0) {
|
||||
if (sessions.length === 0) {
|
||||
setSelectedSessionId('');
|
||||
} else if (!current || !sessions.some((session) => session.id === current)) {
|
||||
setSelectedSessionId(sessions[0].id);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { normalizeChatSessions } from '@/components/Settings/aiSettingsChatSessions';
|
||||
|
||||
describe('normalizeChatSessions', () => {
|
||||
it('returns the original array when the API payload is valid', () => {
|
||||
const sessions = [
|
||||
{ id: 'session-1', title: 'First', message_count: 1, updated_at: '2026-03-25T10:00:00Z' },
|
||||
];
|
||||
|
||||
expect(normalizeChatSessions(sessions)).toEqual(sessions);
|
||||
});
|
||||
|
||||
it('returns an empty array when the API payload is null', () => {
|
||||
expect(normalizeChatSessions(null)).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns an empty array when the API payload is not an array', () => {
|
||||
expect(normalizeChatSessions({ id: 'session-1' })).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { ChatSession } from '@/api/aiChat';
|
||||
|
||||
export function normalizeChatSessions(value: unknown): ChatSession[] {
|
||||
return Array.isArray(value) ? value : [];
|
||||
}
|
||||
Reference in New Issue
Block a user