Finish: Phase 33 — Alert Quality Analytics

This commit is contained in:
voltsrage
2026-06-24 03:39:24 +08:00
parent 185dc93fa1
commit 68c397350b
26 changed files with 934 additions and 169 deletions
@@ -1,8 +1,26 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { mount } from '@vue/test-utils'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { mount, flushPromises } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import FeedbackButtons from '@/components/feedback/FeedbackButtons.vue'
const { feedbackByAlert } = vi.hoisted(() => {
const { ref } = require('vue')
return { feedbackByAlert: ref({}) }
})
vi.mock('@/stores/alertQuality', () => ({
useAlertQualityStore: () => ({
async submitFeedback(alertId, rating, comment = '') {
feedbackByAlert.value = {
...feedbackByAlert.value,
[alertId]: { rating, comment, submittedAt: '2026-06-23T00:00:00Z' },
}
return { id: 'fb-1', createdAt: '2026-06-23T00:00:00Z' }
},
getFeedback: (alertId) => feedbackByAlert.value[alertId] ?? null,
}),
}))
const defaultProps = {
alertId: 'alert-1',
alertType: 'SepsisWarning',
@@ -19,7 +37,7 @@ function ratingButton(wrapper, label) {
describe('FeedbackButtons', () => {
beforeEach(() => {
localStorage.clear()
feedbackByAlert.value = {}
setActivePinia(createPinia())
})
@@ -28,11 +46,21 @@ describe('FeedbackButtons', () => {
expect(ratingButtons(wrapper)).toHaveLength(6)
})
it('hidesRatingButtonsWhenCannotSubmit', () => {
const wrapper = mount(FeedbackButtons, {
props: { ...defaultProps, canSubmit: false },
})
expect(ratingButtons(wrapper)).toHaveLength(0)
expect(wrapper.text()).toContain('Acknowledge this alert to rate it.')
})
it('selectingRatingHighlightsButton', async () => {
const wrapper = mount(FeedbackButtons, { props: defaultProps })
const usefulBtn = ratingButton(wrapper, 'Useful')
await usefulBtn.trigger('click')
await flushPromises()
expect(usefulBtn.classes()).toContain('ring-2')
expect(usefulBtn.classes()).toContain('bg-green-100')
@@ -41,6 +69,7 @@ describe('FeedbackButtons', () => {
it('showNotesFieldOnPlusNote', async () => {
const wrapper = mount(FeedbackButtons, { props: defaultProps })
await ratingButton(wrapper, 'Useful').trigger('click')
await flushPromises()
const noteBtn = wrapper.findAll('button').find(b => b.text() === '+ Note')
await noteBtn.trigger('click')
@@ -54,6 +83,7 @@ describe('FeedbackButtons', () => {
const fpBtn = ratingButton(wrapper, 'False positive')
await usefulBtn.trigger('click')
await flushPromises()
expect(usefulBtn.attributes('aria-checked')).toBe('true')
expect(fpBtn.attributes('aria-checked')).toBe('false')
@@ -66,11 +96,13 @@ describe('FeedbackButtons', () => {
expect(buttons.every(b => b.element.tagName === 'BUTTON')).toBe(true)
await ratingButton(wrapper, 'Useful').trigger('click')
await flushPromises()
expect(ratingButton(wrapper, 'Useful').attributes('aria-checked')).toBe('true')
await ratingButton(wrapper, 'Too early').trigger('click')
expect(ratingButton(wrapper, 'Too early').attributes('aria-checked')).toBe('true')
expect(ratingButton(wrapper, 'Useful').attributes('aria-checked')).toBe('false')
await flushPromises()
expect(ratingButton(wrapper, 'Too early').attributes('aria-checked')).toBe('false')
expect(ratingButton(wrapper, 'Useful').attributes('aria-checked')).toBe('true')
const noteBtn = wrapper.findAll('button').find(b => b.text() === '+ Note')
await noteBtn.trigger('click')
@@ -78,6 +110,7 @@ describe('FeedbackButtons', () => {
const input = wrapper.get('input')
await input.setValue('Expected after metoprolol')
await input.trigger('keydown.enter')
await flushPromises()
expect(wrapper.find('input').exists()).toBe(false)
})
@@ -1,18 +1,32 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import FeedbackSummary from '@/views/FeedbackSummary.vue'
import { useFeedbackStore } from '@/stores/feedback'
const { mockSummaryStats, mockByAlertType } = vi.hoisted(() => ({
mockSummaryStats: {
totalFeedback: 3,
usefulRate: 67,
falsePositiveRate: 33,
},
mockByAlertType: {
SepsisWarning: [{ rating: 'useful' }, { rating: 'would-act' }],
WarningHeartRate: [{ rating: 'false-positive' }],
},
}))
vi.mock('@/stores/alertQuality', () => ({
useAlertQualityStore: () => ({
summaryStats: mockSummaryStats,
byAlertType: mockByAlertType,
submitFeedback: vi.fn(),
getFeedback: vi.fn(() => null),
}),
}))
describe('FeedbackSummary', () => {
beforeEach(() => {
localStorage.clear()
setActivePinia(createPinia())
const store = useFeedbackStore()
store.addFeedback('a1', 'SepsisWarning', 'Critical', 'useful')
store.addFeedback('a2', 'SepsisWarning', 'Critical', 'would-act')
store.addFeedback('a3', 'WarningHeartRate', 'Warning', 'false-positive')
})
it('showsAggregateStats', () => {
@@ -0,0 +1,37 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useAlertQualityStore } from './alertQuality'
vi.mock('@/api/alertQuality', () => ({
submitAlertFeedback: vi.fn().mockResolvedValue({ id: 'fb-1', createdAt: '2026-06-23T00:00:00Z' }),
fetchQualityMetricsSummary: vi.fn().mockResolvedValue({
totalAlerts: 10, totalFeedback: 4,
acknowledgementRate: 0.8, usefulRate: 0.75,
falsePositiveRate: 0.1, wouldActRate: 0.6,
avgSecondsToAcknowledge: 300, avgSecondsToResolution: 1200,
}),
fetchQualityMetrics: vi.fn().mockResolvedValue({
items: [{ alertType: 'News2Warning', usefulRate: 0.75, falsePositiveRate: 0.1, acknowledgementRate: 0.8, totalAlerts: 5 }],
}),
FEEDBACK_TYPE_MAP: { useful: 'Useful' },
}))
describe('alertQuality store', () => {
beforeEach(() => {
setActivePinia(createPinia())
})
it('loads dashboard summary and snapshots', async () => {
const store = useAlertQualityStore()
await store.loadDashboard()
expect(store.summaryStats.totalAlerts).toBe(10)
expect(store.summaryStats.usefulRate).toBe(75)
expect(store.snapshots).toHaveLength(1)
})
it('caches submitted feedback per alert', async () => {
const store = useAlertQualityStore()
await store.submitFeedback('alert-1', 'useful', 'test note')
expect(store.getFeedback('alert-1').rating).toBe('useful')
})
})
@@ -4,11 +4,11 @@ import { canAccessOps, filterNavLinks, isDashboardRole, roleCanAccessRoute, MAIN
describe('roleAccess', () => {
it('filtersNavLinksByRole', () => {
const nurseLinks = filterNavLinks(MAIN_NAV_LINKS, 'NURSE')
expect(nurseLinks.some((l) => l.to === '/feedback')).toBe(false)
expect(nurseLinks.some((l) => l.to === '/analytics/alerts')).toBe(true)
expect(nurseLinks.some((l) => l.to === '/alerts')).toBe(true)
const physicianLinks = filterNavLinks(MAIN_NAV_LINKS, 'PHYSICIAN')
expect(physicianLinks.some((l) => l.to === '/feedback')).toBe(true)
expect(physicianLinks.some((l) => l.to === '/analytics/alerts')).toBe(true)
})
it('rejectsIntegrationDashboardRole', () => {