Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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<{
items: readonly ConversationArchiveItem[]
spaces: readonly LocalSpace[]
results: readonly ArchiveDeleteResult[]
disabled: boolean
spaceActionBusy: { spaceId: string | null } | null
language: BuddyLocale
}>()
const emit = defineEmits<{
Expand Down Expand Up @@ -73,8 +75,9 @@ const groups = computed(() => {
<NDropdown v-if="!group.spaceId || group.space" trigger="click" placement="bottom-end" :options="spaceActions" @select="!disabled && emit('deleteSpace', group.spaceId, group.name)">
<NTooltip>
<template #trigger>
<NButton class="buddy-icon-button shrink-0" size="small" quaternary :disabled="disabled" :aria-label="t('desktop.archive.spaceActions', { space: group.name })" data-testid="task-archive-space-actions">
<DesktopIcon :component="MoreHorizontal20Regular" :size="18" />
<NButton class="buddy-icon-button shrink-0" size="small" quaternary :disabled="disabled" :aria-busy="spaceActionBusy?.spaceId === group.spaceId ? true : undefined" :aria-label="t('desktop.archive.spaceActions', { space: group.name })" data-testid="task-archive-space-actions">
<DesktopRingSpinner v-if="spaceActionBusy?.spaceId === group.spaceId" />
<DesktopIcon v-else :component="MoreHorizontal20Regular" :size="18" />
</NButton>
</template>
{{ t('desktop.archive.spaceActions', { space: group.name }) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null>(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)
Expand All @@ -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
}
}
</script>

Expand All @@ -51,7 +71,7 @@ async function confirmDelete() {
<div class="relative flex flex-wrap items-start justify-between gap-x-6 gap-y-3" data-testid="task-archive-toolbar">
<DesktopTaskArchiveFilters v-model:query="query" v-model:space-id="spaceId" class="w-full max-w-[28rem]" :spaces="spaces" :disabled="disabled" :language="language" />
<DesktopTaskArchivePolicy class="ml-auto w-56 max-w-full" :actions="actions" :language="language" :disabled="busy" />
<NSpin v-if="loading || busy" class="absolute right-0 top-full mt-1" :size="16" />
<NSpin v-if="!spaceActionProgressVisible && (loading || busy)" class="absolute right-0 top-full mt-1" :size="16" />
</div>
<NAlert v-if="!actions.ready.value" type="warning" :show-icon="false">
{{ t('desktop.archive.unavailable') }}
Expand All @@ -65,7 +85,7 @@ async function confirmDelete() {
<div v-if="loading && !items.length" class="flex min-h-40 items-center justify-center text-sm text-muted" role="status">
{{ t('desktop.archive.loading') }}
</div>
<DesktopTaskArchiveList v-else-if="items.length || (actions.ready.value && !error)" :items="items" :spaces="spaces" :results="results" :disabled="disabled" :language="language" @view="emit('view', $event)" @restore="restore" @delete="requestDelete([$event])" @delete-space="requestSpaceDelete" />
<DesktopTaskArchiveList v-else-if="items.length || (actions.ready.value && !error)" :items="items" :spaces="spaces" :results="results" :disabled="disabled" :space-action-busy="spaceActionBusy" :language="language" @view="emit('view', $event)" @restore="restore" @delete="requestDelete([$event])" @delete-space="requestSpaceDelete" />
<div v-if="cursor" class="flex justify-center">
<NButton quaternary :loading="loading" :disabled="disabled || loading" @click="load(true)">
{{ t('desktop.archive.more') }}
Expand All @@ -76,7 +96,7 @@ async function confirmDelete() {
</NAlert>
<NModal :show="confirmation !== null" data-testid="task-archive-delete-confirm" preset="dialog" type="warning" :title="t('desktop.tasks.deleteTaskConfirmTitle')" @update:show="!$event && (confirmation = null)">
<p v-if="confirmation?.space">
{{ t('desktop.archive.deleteSpaceConfirm', { space: confirmation.space, count: confirmation.items.length }) }}
{{ t('desktop.archive.deleteSpaceConfirm', { space: confirmation.space.name, count: confirmation.items.length }) }}
</p>
<p>{{ t('desktop.archive.deleteConfirm', { count: confirmation?.items.length ?? 0 }) }}</p>
<ul class="max-h-40 overflow-auto">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaskArchiveActions['list']>().mockResolvedValue({ items, nextCursor: null })
const remove = vi.fn<TaskArchiveActions['delete']>().mockResolvedValue(true)
const restore = vi.fn<TaskArchiveActions['restore']>().mockResolvedValue(true)
Expand All @@ -43,25 +44,29 @@ 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<HTMLButtonElement>('button')].find(button => (button.getAttribute('aria-label') || button.textContent?.trim()) === label)
expect(match, `button: ${label}`).toBeDefined()
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<HTMLElement>('.n-dropdown-option')].find(option => option.textContent?.includes('清理归档任务'))
expect(option).toBeDefined()
option!.querySelector<HTMLElement>('.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())
}

Expand Down Expand Up @@ -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<boolean>()
const secondDeletion = Promise.withResolvers<boolean>()
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<boolean>()
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 })
Expand Down
23 changes: 23 additions & 0 deletions apps/buddy/src/shared/ui/loading/DesktopRingSpinner.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<template>
<span class="desktop-ring-spinner block h-3 w-3 flex-none rounded-full" aria-hidden="true" />
</template>

<style scoped lang="scss">
.desktop-ring-spinner {
border: 1.5px solid var(--buddy-border-strong);
border-top-color: var(--buddy-text-secondary);
animation: desktop-ring-spin 900ms linear infinite;
}

@keyframes desktop-ring-spin {
to {
transform: rotate(360deg);
}
}

@media (prefers-reduced-motion: reduce) {
.desktop-ring-spinner {
animation: none;
}
}
</style>