test: assert setTemplateBaseline called with active-state and fetched-json fallback

Per CodeRabbit feedback: previously the test added a setTemplateBaseline
mock but did not assert it was called with the expected baseline, so the
new baseline-capture behavior could regress silently.

- assert active-state baseline path uses changeTracker.activeState
- assert fallback path uses the fetched JSON when no active state exists
- restructure workflowStore mock to use vi.hoisted so activeWorkflow can
  be swapped per-test
This commit is contained in:
Glary-Bot
2026-05-16 03:27:41 +00:00
parent 0647d7eba9
commit d2990331e1

View File

@@ -1,8 +1,18 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { setTemplateBaseline } from '@/platform/telemetry/utils/templateBaselineStore'
import { useTemplateWorkflows } from '@/platform/workflow/templates/composables/useTemplateWorkflows'
import { useWorkflowTemplatesStore } from '@/platform/workflow/templates/repositories/workflowTemplatesStore'
const hoisted = vi.hoisted(() => ({
mockActiveWorkflow: {
changeTracker: { activeState: { nodes: [], links: [] } as unknown }
} as
| { changeTracker?: { activeState?: unknown } | undefined }
| null
| undefined
}))
async function flushPromises() {
await new Promise((r) => setTimeout(r, 0))
}
@@ -51,7 +61,9 @@ vi.mock('@/stores/dialogStore', () => ({
vi.mock('@/platform/workflow/management/stores/workflowStore', () => ({
useWorkflowStore: () => ({
activeWorkflow: { changeTracker: { activeState: { nodes: [], links: [] } } }
get activeWorkflow() {
return hoisted.mockActiveWorkflow
}
})
}))
@@ -125,6 +137,11 @@ describe('useTemplateWorkflows', () => {
vi.mocked(fetch).mockResolvedValue({
json: vi.fn().mockResolvedValue({ workflow: 'data' })
} as Partial<Response> as Response)
hoisted.mockActiveWorkflow = {
changeTracker: { activeState: { nodes: [], links: [] } as unknown }
}
vi.mocked(setTemplateBaseline).mockClear()
})
it('should load templates from store', async () => {
@@ -295,6 +312,40 @@ describe('useTemplateWorkflows', () => {
expect(fetch).toHaveBeenCalledWith('mock-file-url/templates/template1.json')
})
it('captures the normalized active state as the template baseline', async () => {
const { loadWorkflowTemplate } = useTemplateWorkflows()
mockWorkflowTemplatesStore.isLoaded = true
const normalizedState = {
nodes: [{ id: 1, type: 'KSampler', widgets_values: [42] }],
links: []
}
hoisted.mockActiveWorkflow = {
changeTracker: { activeState: normalizedState }
}
await loadWorkflowTemplate('template1', 'default')
await flushPromises()
expect(setTemplateBaseline).toHaveBeenCalledWith(
'template1',
normalizedState
)
})
it('falls back to fetched JSON when no active state is available', async () => {
const { loadWorkflowTemplate } = useTemplateWorkflows()
mockWorkflowTemplatesStore.isLoaded = true
hoisted.mockActiveWorkflow = undefined
await loadWorkflowTemplate('template1', 'default')
await flushPromises()
expect(setTemplateBaseline).toHaveBeenCalledWith('template1', {
workflow: 'data'
})
})
it('should handle errors when loading templates', async () => {
const { loadWorkflowTemplate, loadingTemplateId } = useTemplateWorkflows()