import { afterEach, expect, it, vi } from 'vitest' import { nextTick, createSSRApp, h, defineComponent } from 'vue' import { renderToString } from 'vue/server-renderer' import { createPinia } from 'pinia' import BatchHistorySummary from './BatchHistorySummary.vue' import { setupHarness, settle } from '../utils/setupHarness' import { api } from '../api' import { useWorkflowsStore } from '../stores/workflows' import { setCsrfToken } from '../api/client' const deletionProps = vi.hoisted(() => [] as any[]) vi.mock('./DeleteRecordButton.vue', async () => { const { defineComponent, h } = await import('vue') return { default: defineComponent({ props: ['kind', 'record'], setup(props) { deletionProps.push({ kind: props.kind, record: props.record }) return () => h('button', { 'data-kind': props.kind }, '删除记录') }, }), } }) const mounted: any[] = [] const batch = (id = 'a', version = 1) => ({ batch: { id, version, name: '历史批次' + id, state: 'paused', desired_state: 'pause', created_at: '', }, items: [ { item_index: 0, target_kind: 'run', target_id: 'r', state: 'running' }, { item_index: 1, target_kind: 'job', target_id: 'j', state: 'queued' }, ], }) function view() { const v = setupHarness(BatchHistorySummary, { batchId: 'a' }) mounted.push(v) return v } afterEach(() => { mounted.splice(0).forEach((v) => v.unmount()) vi.restoreAllMocks() }) it('loads historical grouping and keeps child run/job destinations usable', async () => { vi.spyOn(api, 'batch').mockResolvedValue(batch()) const v = view() await settle() expect(v.bindings.snapshot.value.batch.id).toBe('a') expect(v.bindings.childLink(batch().items[0])).toEqual({ path: '/tasks', query: { batch: 'a', run: 'r' }, }) expect(v.bindings.childLink(batch().items[1])).toEqual({ path: '/tasks', query: { batch: 'a', job: 'j' }, }) }) it('ignores late results after switching groups', async () => { let resolve: any vi.spyOn(api, 'batch').mockImplementation((id) => id === 'a' ? new Promise((r) => (resolve = r)) : Promise.resolve(batch('b')), ) const v = view() v.setProps({ batchId: 'b' }) await nextTick() await settle() resolve(batch('a')) await settle() expect(v.bindings.snapshot.value.batch.id).toBe('b') }) it('protects another login and displays permission failures without old group data', async () => { let resolve: any const request = vi.spyOn(api, 'batch').mockImplementation(() => new Promise((r) => (resolve = r))) const v = view() setCsrfToken('new-session') resolve(batch()) await settle() expect(v.bindings.snapshot.value).toBeUndefined() request.mockRejectedValue(new Error('无权限访问该批次')) await v.bindings.load() expect(v.bindings.snapshot.value).toBeUndefined() expect(v.bindings.notice.value).toContain('无权限') }) it('forwards pause/resume/cancel with current versions and clears tombstoned grouping', async () => { vi.spyOn(api, 'batch').mockResolvedValue(batch()) const control = vi.spyOn(api, 'controlBatch').mockResolvedValue(batch('a', 2)) const v = view() await settle() await v.bindings.control('run') expect(control).toHaveBeenCalledWith(expect.objectContaining({ id: 'a', version: 1 }), 'run') expect(v.bindings.snapshot.value.batch.version).toBe(2) useWorkflowsStore(v.pinia).forgetBatch('a') await nextTick() expect(v.bindings.snapshot.value).toBeUndefined() expect(v.events).toContainEqual(['dismiss']) }) it('removes an already deleted group link instead of leaving a permanent loading state', async () => { const v = setupHarness(BatchHistorySummary, { batchId: 'gone' }, () => useWorkflowsStore().forgetBatch('gone'), ) mounted.push(v) await settle() expect(v.bindings.busy.value).toBe(false) expect(v.events).toContainEqual(['dismiss']) }) async function renderedSummary(items: any[]) { const data = { ...batch('a', 5), batch: { ...batch('a', 5).batch, state: 'succeeded' }, items } vi.spyOn(api, 'batch').mockResolvedValue(data) const links: any[] = [] deletionProps.length = 0 const component = { ...BatchHistorySummary, setup(props: any, ctx: any) { const bindings = (BatchHistorySummary as any).setup(props, ctx) bindings.snapshot.value = data bindings.busy.value = false return bindings }, } const app = createSSRApp(component, { batchId: 'a' }) app.use(createPinia()) app.component( 'ElButton', defineComponent({ setup(_, ctx) { return () => h('button', ctx.slots.default?.()) }, }), ) app.component( 'RouterLink', defineComponent({ props: ['to'], setup(props, ctx) { links.push(props.to) return () => h('a', ctx.slots.default?.()) }, }), ) return { html: await renderToString(app), links, data } } it('renders candidate links to known stories or the story list and no unknown job links', async () => { const rendered = await renderedSummary([ { item_index: 0, target_kind: 'candidate', target_id: 'candidate-a', state: 'succeeded', spec_json: { candidate_id: 'candidate-a' }, }, { item_index: 1, target_kind: 'candidate', target_id: 'candidate-b', state: 'succeeded', spec_json: { story_id: 'story-b' }, }, { item_index: 2, target_kind: 'candidate', target_id: 'candidate-c', state: 'succeeded', spec_json: { draft_id: 'story-c' }, }, { item_index: 3, target_kind: 'future-type', target_id: 'opaque', state: 'succeeded' }, ]) expect(rendered.links).toEqual([ { path: '/studio' }, { path: '/studio/story-b' }, { path: '/studio/story-c' }, ]) expect(rendered.html).toContain('候选记录') expect(rendered.html).toContain('未记录所属故事') expect(rendered.html).toContain('未知类型记录') }) it('connects the real summary template to the shared batch deletion control', async () => { const rendered = await renderedSummary([]) expect(deletionProps).toEqual([{ kind: 'batch', record: rendered.data.batch }]) expect(rendered.html).toContain('data-kind="batch"') })