Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions frontend/src/stores/product-expert.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,11 @@ export const useProductExpertStore = defineStore('product-expert', {
},
hydrateMessages (messages) {
if (!messages) return
const isAiMessage = (message) => message.answer && Array.isArray(message.answer)
const isUserMessage = (message) => Object.prototype.hasOwnProperty.call(message, 'query') && message.query
// both predicates must return real booleans: the switch(true)
// below matches with strict equality, so a truthy string (e.g.
// the query text) would silently never match
const isAiMessage = (message) => Boolean(message.answer && Array.isArray(message.answer))
const isUserMessage = (message) => Boolean(Object.prototype.hasOwnProperty.call(message, 'query') && message.query)

messages.forEach((message) => {
switch (true) {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/frontend/stores/product-expert-hydrate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createPinia, setActivePinia } from 'pinia'
import { describe, expect, test, vi } from 'vitest'

vi.mock('@/composables/services/MqttExpertTopicHelper.ts', () => ({ default: {}, getEntityTopicPaths: () => ({}) }))

// imported after mocks so vi.mock hoisting resolves correctly
import { useProductExpertStore } from '../../../../frontend/src/stores/product-expert.js'

// The shape hydrateMessages consumes: `{ answer: [...] }` for AI turns,
// `{ query: '...' }` for the user's.
const MIXED_TRANSCRIPT = [
{ answer: [{ kind: 'questions', questions: [{ question: 'Where is the data coming from?', options: [] }] }] },
{ query: 'Where is the data coming from? Sensors' },
{ answer: [{ content: 'Great, noted.' }] },
{ query: 'What else do you need to know?' },
{ answer: [{ content: 'Nothing, we are done.' }] }
]

describe('product-expert hydrateMessages', () => {
// Regression: the switch(true) dispatch matches with strict equality, so
// a predicate returning a truthy string instead of a boolean silently
// dropped every user message from rehydrated transcripts.
test('hydrates ai and human messages in order', () => {
setActivePinia(createPinia())
const store = useProductExpertStore()
store.hydrateMessages(MIXED_TRANSCRIPT)
expect(store.messages.map(m => m._type)).toEqual(['ai', 'human', 'ai', 'human', 'ai'])
expect(store.messages[1].content).toBe('Where is the data coming from? Sensors')
})
})
Loading