diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveList.vue b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveList.vue index db0f4434a..fb0993368 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveList.vue +++ b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveList.vue @@ -8,6 +8,7 @@ import { NButton, NDropdown, NEmpty, NPopover, NTooltip } from 'naive-ui' import { computed, h } from 'vue' import { useBuddyI18n } from '@/i18n/buddyI18n' import DesktopIcon from '@/shared/ui/icon/DesktopIcon.vue' +import DesktopRingSpinner from '@/shared/ui/loading/DesktopRingSpinner.vue' import DesktopSpaceIcon from '../space/DesktopSpaceIcon.vue' const props = defineProps<{ @@ -15,6 +16,7 @@ const props = defineProps<{ spaces: readonly LocalSpace[] results: readonly ArchiveDeleteResult[] disabled: boolean + spaceActionBusy: { spaceId: string | null } | null language: BuddyLocale }>() const emit = defineEmits<{ @@ -73,8 +75,9 @@ const groups = computed(() => { {{ t('desktop.archive.spaceActions', { space: group.name }) }} diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveManager.vue b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveManager.vue index 94364db7b..d111264a1 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveManager.vue +++ b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskArchiveManager.vue @@ -21,7 +21,14 @@ const { query, spaceId, items, results, cursor, busy, loading, error, load, snap errorMessage: cause => resolveLocalChatErrorMessage(cause, props.language), notDeletedMessage: () => t('desktop.archive.notCompleted'), }) -const confirmation = shallowRef<{ items: readonly ConversationArchiveItem[], space?: string } | null>(null) +const confirmation = shallowRef<{ items: readonly ConversationArchiveItem[], space?: { id: string | null, name: string } } | null>(null) +const spaceActionBusy = shallowRef<{ spaceId: string | null } | null>(null) +const spaceActionProgressVisible = computed(() => { + const action = spaceActionBusy.value + return action !== null + && items.value.some(item => item.spaceId === action.spaceId) + && (action.spaceId === null || props.spaces.some(space => space.id === action.spaceId)) +}) const notice = shallowRef(null) const detachedFailures = computed(() => results.value.filter(result => result.status === 'failed' && !items.value.some(item => item.id === result.item.id))) const disabled = computed(() => busy.value || !props.actions.ready.value) @@ -31,18 +38,31 @@ function requestDelete(targets: readonly ConversationArchiveItem[]) { } async function requestSpaceDelete(spaceId: string | null, name: string) { notice.value = null - const targets = await snapshotSpace(spaceId) - if (targets?.length) - confirmation.value = { items: targets, space: name } - else if (targets) - notice.value = t('desktop.archive.emptySpace') + spaceActionBusy.value = { spaceId } + try { + const targets = await snapshotSpace(spaceId) + if (targets?.length) + confirmation.value = { items: targets, space: { id: spaceId, name } } + else if (targets) + notice.value = t('desktop.archive.emptySpace') + } + finally { + spaceActionBusy.value = null + } } async function confirmDelete() { - const batch = confirmation.value?.items + const pendingConfirmation = confirmation.value + const batch = pendingConfirmation?.items if (!batch || disabled.value) return confirmation.value = null - await deleteBatch(batch) + spaceActionBusy.value = pendingConfirmation.space ? { spaceId: pendingConfirmation.space.id } : null + try { + await deleteBatch(batch) + } + finally { + spaceActionBusy.value = null + } } @@ -51,7 +71,7 @@ async function confirmDelete() {
- +
{{ t('desktop.archive.unavailable') }} @@ -65,7 +85,7 @@ async function confirmDelete() {
{{ t('desktop.archive.loading') }}
- +
{{ t('desktop.archive.more') }} @@ -76,7 +96,7 @@ async function confirmDelete() {

- {{ t('desktop.archive.deleteSpaceConfirm', { space: confirmation.space, count: confirmation.items.length }) }} + {{ t('desktop.archive.deleteSpaceConfirm', { space: confirmation.space.name, count: confirmation.items.length }) }}

{{ t('desktop.archive.deleteConfirm', { count: confirmation?.items.length ?? 0 }) }}

    diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts index 3ab5d5b56..799a5edd0 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts +++ b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts @@ -22,6 +22,7 @@ function space(id: string, name: string, revokedAt: string | null = null): Local } async function mount(items = [item()], spaces: readonly LocalSpace[] = []) { const ready = shallowRef(true) + const currentSpaces = shallowRef(spaces) const list = vi.fn().mockResolvedValue({ items, nextCursor: null }) const remove = vi.fn().mockResolvedValue(true) const restore = vi.fn().mockResolvedValue(true) @@ -43,12 +44,12 @@ async function mount(items = [item()], spaces: readonly LocalSpace[] = []) { } const root = document.createElement('div') document.body.append(root) - const app = createApp({ render: () => h(DesktopTaskArchiveManager, { actions, spaces, language: 'zh-CN', onView }) }) + const app = createApp({ render: () => h(DesktopTaskArchiveManager, { actions, spaces: currentSpaces.value, language: 'zh-CN', onView }) }) app.mount(root) cleanups.push(() => app.unmount()) await nextTick() await nextTick() - return { root, ready, list, remove, restore, onView, notify: () => listeners.forEach(listener => listener()) } + return { root, ready, spaces: currentSpaces, list, remove, restore, onView, notify: () => listeners.forEach(listener => listener()) } } function button(label: string, root: ParentNode = document) { const match = [...root.querySelectorAll('button')].find(button => (button.getAttribute('aria-label') || button.textContent?.trim()) === label) @@ -56,12 +57,16 @@ function button(label: string, root: ParentNode = document) { return match! } -async function openSpaceCleanup(name: string, root: ParentNode) { +async function selectSpaceCleanup(name: string, root: ParentNode) { button(`空间操作:${name}`, root).click() await vi.waitFor(() => expect(document.querySelector('.n-dropdown-option')).not.toBeNull()) const option = [...document.querySelectorAll('.n-dropdown-option')].find(option => option.textContent?.includes('清理归档任务')) expect(option).toBeDefined() option!.querySelector('.n-dropdown-option-body')!.click() +} + +async function openSpaceCleanup(name: string, root: ParentNode) { + await selectSpaceCleanup(name, root) await vi.waitFor(() => expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')).not.toBeNull()) } @@ -116,6 +121,122 @@ it('keeps confirmed IDs and revisions fixed across refresh and reports each dele expect(f.root.querySelector('[data-delete-result="failed"]')?.textContent).toContain('历史任务 two') }) +it.each([ + { spaceId: null, name: '未绑定空间', otherName: '设计空间' }, + { spaceId: 'space-a', name: '设计空间', otherName: '未绑定空间' }, +])('shows snapshot progress only on $name and clears it before confirmation', async ({ spaceId, name, otherName }) => { + const target = item('target', { spaceId }) + const other = item('other', { spaceId: spaceId === null ? 'space-a' : null }) + const f = await mount([target, other], [space('space-a', '设计空间')]) + const snapshot = Promise.withResolvers<{ items: ConversationArchiveItem[], nextCursor: string | null }>() + f.list.mockReturnValueOnce(snapshot.promise) + + const actions = button(`空间操作:${name}`, f.root) + const otherActions = button(`空间操作:${otherName}`, f.root) + await selectSpaceCleanup(name, f.root) + await vi.waitFor(() => expect(actions.getAttribute('aria-busy')).toBe('true')) + expect(f.list).toHaveBeenLastCalledWith({ kind: 'managed', spaceId, limit: 100 }) + expect(actions.querySelector('.desktop-ring-spinner')).not.toBeNull() + expect(otherActions.getAttribute('aria-busy')).toBeNull() + expect(otherActions.querySelector('.desktop-ring-spinner')).toBeNull() + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() + + snapshot.resolve({ items: [target], nextCursor: null }) + await vi.waitFor(() => expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')?.textContent).toContain('历史任务 target')) + const confirmation = document.querySelector('[data-testid="task-archive-delete-confirm"]')! + expect(actions.getAttribute('aria-busy')).toBeNull() + expect(actions.querySelector('.desktop-ring-spinner')).toBeNull() + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() + + button('取消', confirmation).click() + await nextTick() + expect(f.root.querySelector('[data-testid="task-archive-manager"]')?.getAttribute('aria-busy')).toBe('false') + expect(actions.querySelector('.desktop-ring-spinner')).toBeNull() + expect(f.remove).not.toHaveBeenCalled() +}) + +it('keeps space progress through every deletion and the final refresh, including partial failure', async () => { + const first = item('first', { spaceId: 'space-a' }) + const second = item('second', { spaceId: 'space-a', metadataRevision: 7 }) + const f = await mount([first, second], [space('space-a', '设计空间')]) + await openSpaceCleanup('设计空间', f.root) + const actions = button('空间操作:设计空间', f.root) + const firstDeletion = Promise.withResolvers() + const secondDeletion = Promise.withResolvers() + const refresh = Promise.withResolvers<{ items: ConversationArchiveItem[], nextCursor: string | null }>() + f.remove.mockReturnValueOnce(firstDeletion.promise).mockReturnValueOnce(secondDeletion.promise) + f.list.mockReturnValueOnce(refresh.promise) + + button('删除', document.querySelector('[data-testid="task-archive-delete-confirm"]')!).click() + await vi.waitFor(() => expect(actions.getAttribute('aria-busy')).toBe('true')) + expect(f.remove.mock.calls).toEqual([['first', 3]]) + expect(actions.querySelector('.desktop-ring-spinner')).not.toBeNull() + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() + + firstDeletion.resolve(true) + await vi.waitFor(() => expect(f.remove.mock.calls).toEqual([['first', 3], ['second', 7]])) + expect(actions.getAttribute('aria-busy')).toBe('true') + expect(actions.querySelector('.desktop-ring-spinner')).not.toBeNull() + + secondDeletion.resolve(false) + await vi.waitFor(() => expect(f.root.querySelector('[data-delete-result="failed"]')).not.toBeNull()) + expect(actions.getAttribute('aria-busy')).toBe('true') + expect(actions.querySelector('.desktop-ring-spinner')).not.toBeNull() + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() + + refresh.resolve({ items: [second], nextCursor: null }) + await vi.waitFor(() => expect(actions.getAttribute('aria-busy')).toBeNull()) + expect(actions.querySelector('.desktop-ring-spinner')).toBeNull() + expect(actions.disabled).toBe(false) + expect(f.root.querySelector('[data-archive-id="first"]')).toBeNull() + expect(f.root.querySelector('[data-archive-id="second"] [data-delete-result="failed"]')).not.toBeNull() +}) + +it.each(['group removed', 'list empty', 'space unavailable'])('falls back to toolbar progress when confirmation outlives its action button: %s', async (change) => { + const target = item('target', { spaceId: 'space-a' }) + const other = item('other') + const f = await mount([target, other], [space('space-a', '设计空间')]) + f.list.mockResolvedValueOnce({ items: [target], nextCursor: null }) + await openSpaceCleanup('设计空间', f.root) + + if (change === 'space unavailable') { + f.spaces.value = [] + await nextTick() + } + else { + f.list.mockResolvedValue({ items: change === 'list empty' ? [] : [other], nextCursor: null }) + f.notify() + await vi.waitFor(() => expect(f.root.querySelector('[data-archive-space="space-a"]')).toBeNull()) + } + expect(f.root.querySelector('[data-archive-space="space-a"] [data-testid="task-archive-space-actions"]')).toBeNull() + + const deletion = Promise.withResolvers() + f.remove.mockReturnValueOnce(deletion.promise) + button('删除', document.querySelector('[data-testid="task-archive-delete-confirm"]')!).click() + await vi.waitFor(() => expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).not.toBeNull()) + expect(f.root.querySelector('[data-testid="task-archive-manager"]')?.getAttribute('aria-busy')).toBe('true') + expect(f.root.querySelector('.desktop-ring-spinner')).toBeNull() + expect(f.remove.mock.calls).toEqual([['target', 3]]) + + deletion.resolve(true) + await vi.waitFor(() => expect(f.root.querySelector('[data-testid="task-archive-manager"]')?.getAttribute('aria-busy')).toBe('false')) + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() +}) + +it('clears snapshot progress after failure and allows retry', async () => { + const f = await mount() + f.list.mockRejectedValueOnce(new Error('snapshot failed')) + await selectSpaceCleanup('未绑定空间', f.root) + await vi.waitFor(() => expect(f.root.textContent).toContain('本地任务操作失败')) + expect(f.root.querySelector('.desktop-ring-spinner')).toBeNull() + expect(f.root.querySelector('[data-testid="task-archive-toolbar"] .n-spin')).toBeNull() + expect(button('空间操作:未绑定空间', f.root).disabled).toBe(false) + expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')).toBeNull() + + await openSpaceCleanup('未绑定空间', f.root) + expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')?.textContent).toContain('历史任务 task') +}) + it('previews every archive page in a space and only submits the confirmed immutable targets', async () => { const first = item('visible', { spaceId: 'space-a' }) const second = item('beyond-first-page', { spaceId: 'space-a', metadataRevision: 8 }) diff --git a/apps/buddy/src/shared/ui/loading/DesktopRingSpinner.vue b/apps/buddy/src/shared/ui/loading/DesktopRingSpinner.vue new file mode 100644 index 000000000..52fcf1ffa --- /dev/null +++ b/apps/buddy/src/shared/ui/loading/DesktopRingSpinner.vue @@ -0,0 +1,23 @@ + + +