File size: 1,965 Bytes
28ad89a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { describe, expect, it } from 'vitest'

import type { ChatMessage } from '@/lib/chat-messages'

import { lastVisibleMessageIsUser, routedSessionIsLoading, threadLoadingState } from './thread-loading'

function message(id: string, role: ChatMessage['role'], hidden = false): ChatMessage {
  return {
    id,
    role,
    parts: [{ type: 'text', text: `${role}:${id}` }],
    hidden
  }
}

describe('thread loading state', () => {
  it('returns session when routed session is still hydrating', () => {
    expect(threadLoadingState(true, true, true, false)).toBe('session')
  })

  it('returns response while awaiting an assistant reply to the last visible user message', () => {
    const messages = [message('u1', 'user'), message('a1', 'assistant', true)]

    expect(lastVisibleMessageIsUser(messages)).toBe(true)
    expect(threadLoadingState(false, true, true, lastVisibleMessageIsUser(messages))).toBe('response')
  })

  it('does not show response loading when the last visible message is not user-authored', () => {
    const messages = [message('u1', 'user'), message('a1', 'assistant')]

    expect(lastVisibleMessageIsUser(messages)).toBe(false)
    expect(threadLoadingState(false, true, true, lastVisibleMessageIsUser(messages))).toBeUndefined()
  })
})

describe('routedSessionIsLoading', () => {
  const base = {
    activeSessionId: 'runtime-1' as string | null,
    knownHistory: false,
    messagesEmpty: false,
    resumeExhausted: false,
    routeSessionMismatch: false,
    routedSessionView: true
  }

  it('keeps the session loader up when known history is held off the view', () => {
    expect(
      routedSessionIsLoading({
        ...base,
        knownHistory: true,
        messagesEmpty: true
      })
    ).toBe(true)
  })

  it('does not treat a brand-new empty routed draft as still loading', () => {
    expect(
      routedSessionIsLoading({
        ...base,
        messagesEmpty: true
      })
    ).toBe(false)
  })
})