From 2ad4c1ae417e866f256a362d34045c946bd69b01 Mon Sep 17 00:00:00 2001 From: SniperPigeon Date: Thu, 3 Dec 2026 22:13:14 +0800 Subject: [PATCH 1/2] fix(download): dismiss completed notifications after five seconds --- e2e/download-toast.spec.ts | 87 +++++++++++++++++++++++++++++++++ src/styles/components/toast.css | 4 +- src/utils/download.tsx | 13 ++--- 3 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 e2e/download-toast.spec.ts diff --git a/e2e/download-toast.spec.ts b/e2e/download-toast.spec.ts new file mode 100644 index 00000000..714d8e44 --- /dev/null +++ b/e2e/download-toast.spec.ts @@ -0,0 +1,87 @@ +import { expect, test } from '@playwright/test'; +import type { Page, Route } from '@playwright/test'; + +const chartIds = ['11111111-1111-4111-8111-111111111111', '22222222-2222-4222-8222-222222222222']; +const titles = ['Download without video', 'Download with video']; +const placeholderImage = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'); + +/** Hold each video's response so the test controls when all files have finished downloading. */ +async function mockDownloads(page: Page) { + const videos = new Map(); + await page.addInitScript(() => localStorage.setItem('languageChoice', 'en')); + await page.route('**/api3/**', async (route) => { + const path = new URL(route.request().url()).pathname; + const chartId = path.split('/').at(-2)!; + if (path.endsWith('/video')) { + videos.set(chartId, route); + return; + } + if (path.endsWith('/image') || path.includes('/avatar/')) { + await route.fulfill({ body: placeholderImage, contentType: 'image/png' }); + return; + } + if (path.endsWith('/track') || path.endsWith('/chart')) { + await route.fulfill({ body: 'Download fixture', contentType: 'application/octet-stream' }); + return; + } + if (path.endsWith('/songlist')) { + await route.fulfill({ json: { id: 'downloads', name: 'Downloads', createdBy: 'owner', visibility: 1, items: chartIds, count: chartIds.length } }); + return; + } + if (path.endsWith('/maichart/info')) { + await route.fulfill({ json: chartIds.map((id, index) => ({ + metadata: { id, title: titles[index], artist: 'Fixture', uploader: 'owner', designer: 'Fixture', levels: ['3'], hash: id, tags: [], publicTags: [], timestamp: '2026-09-20T00:00:00Z' }, + resources: {}, + levelHashes: [], + })) }); + return; + } + if (path.endsWith('/maichart/interactsum')) { + await route.fulfill({ json: chartIds.map((id) => ({ chartId: id, plays: 0, score: 0, upvotes: 0, downvotes: 0, comments: 0, myVote: 0 })) }); + return; + } + await route.fulfill({ json: path.endsWith('/account/profile') ? { username: 'viewer' } : [] }); + }); + return videos; +} + +for (const viewport of ['desktop', 'mobile']) { + test(`${viewport}: download notifications close five seconds after completion`, async ({ page }) => { + const videos = await mockDownloads(page); + await page.goto('/collection?id=downloads'); + + for (const [index, id] of chartIds.entries()) { + await page.locator(`[id="${id}"]`).getByRole('button', { name: 'Download', exact: true }).click(); + await page.locator('[data-format="zip"]').click(); + await expect(page.getByRole('dialog')).toHaveCount(0); + await expect(page.locator('.download-toast').filter({ hasText: titles[index] })).toContainText('Downloading'); + } + await expect.poll(() => videos.size).toBe(2); + // Keep the pointer away from notifications so pauseOnHover does not pause their timers. + await page.mouse.move(0, 0); + const firstToast = page.locator('.download-toast').filter({ hasText: titles[0] }); + const secondToast = page.locator('.download-toast').filter({ hasText: titles[1] }); + + // A slow pending file must keep the notification open beyond the completion timeout. + await page.waitForTimeout(5500); + await expect(firstToast).toContainText('Downloading'); + await expect(secondToast).toContainText('Downloading'); + + const firstDownload = page.waitForEvent('download'); + await videos.get(chartIds[0])!.fulfill({ status: 404, body: '' }); + expect((await firstDownload).suggestedFilename()).toBe(`${titles[0]}.zip`); + await expect(firstToast).toContainText('Download complete'); + await page.waitForTimeout(4000); + await expect(firstToast).toBeVisible(); + await expect(secondToast).toContainText('Downloading'); + + const secondDownload = page.waitForEvent('download'); + await videos.get(chartIds[1])!.fulfill({ body: 'Video fixture', contentType: 'video/mp4' }); + expect((await secondDownload).suggestedFilename()).toBe(`${titles[1]}.zip`); + await expect(secondToast).toContainText('Download complete'); + // Each completed download has its own five-second timer, including one with no optional video. + await expect(firstToast).toHaveCount(0, { timeout: 2500 }); + await expect(secondToast).toBeVisible(); + await expect(secondToast).toHaveCount(0, { timeout: 6000 }); + }); +} diff --git a/src/styles/components/toast.css b/src/styles/components/toast.css index e11d4bf3..c73264f9 100644 --- a/src/styles/components/toast.css +++ b/src/styles/components/toast.css @@ -74,9 +74,9 @@ padding: 12px 14px; } -/* 下载通知自带进度条和状态,不需要底部计时条 */ +/* 下载通知自带进度条和状态;隐藏计时条但保留动画,自动关闭依赖 animationend */ .Toastify .download-toast .Toastify__progress-bar--wrp { - display: none; + visibility: hidden; } /* 服务器没给文件大小时的不确定进度:一段色条来回滑动 */ diff --git a/src/utils/download.tsx b/src/utils/download.tsx index 4cf4b4d6..2ae736d0 100644 --- a/src/utils/download.tsx +++ b/src/utils/download.tsx @@ -63,12 +63,11 @@ export async function downloadSong(props: DownloadSongParams): Promise { const toastId = toast.loading(render('loading'), { icon: false, closeButton: false, className: 'download-toast' }); // 进度事件很密集,合并到下一帧再刷新通知 - let scheduled = false; + let scheduled: number | undefined; const refresh = () => { - if (scheduled) return; - scheduled = true; - requestAnimationFrame(() => { - scheduled = false; + if (scheduled !== undefined) return; + scheduled = requestAnimationFrame(() => { + scheduled = undefined; toast.update(toastId, { render: render('loading') }); }); }; @@ -98,12 +97,14 @@ export async function downloadSong(props: DownloadSongParams): Promise { const results = await Promise.all(FILES.map(fetchOne)); const finish = (status: DownloadStatus) => { + // 最后一帧进度不能在完成后把通知改回下载中、覆盖自动关闭设置。 + if (scheduled !== undefined) cancelAnimationFrame(scheduled); toast.update(toastId, { render: render(status), type: status === 'done' ? 'success' : 'error', isLoading: false, icon: false, - autoClose: status === 'done' ? 3000 : 5000, + autoClose: 5000, }); }; From e9f7c6b960ba09b6d02e20af1fed93325ca8942a Mon Sep 17 00:00:00 2001 From: SniperPigeon Date: Thu, 3 Dec 2026 22:32:53 +0800 Subject: [PATCH 2/2] test(download): remove toast auto-close regression tests --- e2e/download-toast.spec.ts | 87 -------------------------------------- 1 file changed, 87 deletions(-) delete mode 100644 e2e/download-toast.spec.ts diff --git a/e2e/download-toast.spec.ts b/e2e/download-toast.spec.ts deleted file mode 100644 index 714d8e44..00000000 --- a/e2e/download-toast.spec.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { expect, test } from '@playwright/test'; -import type { Page, Route } from '@playwright/test'; - -const chartIds = ['11111111-1111-4111-8111-111111111111', '22222222-2222-4222-8222-222222222222']; -const titles = ['Download without video', 'Download with video']; -const placeholderImage = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'); - -/** Hold each video's response so the test controls when all files have finished downloading. */ -async function mockDownloads(page: Page) { - const videos = new Map(); - await page.addInitScript(() => localStorage.setItem('languageChoice', 'en')); - await page.route('**/api3/**', async (route) => { - const path = new URL(route.request().url()).pathname; - const chartId = path.split('/').at(-2)!; - if (path.endsWith('/video')) { - videos.set(chartId, route); - return; - } - if (path.endsWith('/image') || path.includes('/avatar/')) { - await route.fulfill({ body: placeholderImage, contentType: 'image/png' }); - return; - } - if (path.endsWith('/track') || path.endsWith('/chart')) { - await route.fulfill({ body: 'Download fixture', contentType: 'application/octet-stream' }); - return; - } - if (path.endsWith('/songlist')) { - await route.fulfill({ json: { id: 'downloads', name: 'Downloads', createdBy: 'owner', visibility: 1, items: chartIds, count: chartIds.length } }); - return; - } - if (path.endsWith('/maichart/info')) { - await route.fulfill({ json: chartIds.map((id, index) => ({ - metadata: { id, title: titles[index], artist: 'Fixture', uploader: 'owner', designer: 'Fixture', levels: ['3'], hash: id, tags: [], publicTags: [], timestamp: '2026-09-20T00:00:00Z' }, - resources: {}, - levelHashes: [], - })) }); - return; - } - if (path.endsWith('/maichart/interactsum')) { - await route.fulfill({ json: chartIds.map((id) => ({ chartId: id, plays: 0, score: 0, upvotes: 0, downvotes: 0, comments: 0, myVote: 0 })) }); - return; - } - await route.fulfill({ json: path.endsWith('/account/profile') ? { username: 'viewer' } : [] }); - }); - return videos; -} - -for (const viewport of ['desktop', 'mobile']) { - test(`${viewport}: download notifications close five seconds after completion`, async ({ page }) => { - const videos = await mockDownloads(page); - await page.goto('/collection?id=downloads'); - - for (const [index, id] of chartIds.entries()) { - await page.locator(`[id="${id}"]`).getByRole('button', { name: 'Download', exact: true }).click(); - await page.locator('[data-format="zip"]').click(); - await expect(page.getByRole('dialog')).toHaveCount(0); - await expect(page.locator('.download-toast').filter({ hasText: titles[index] })).toContainText('Downloading'); - } - await expect.poll(() => videos.size).toBe(2); - // Keep the pointer away from notifications so pauseOnHover does not pause their timers. - await page.mouse.move(0, 0); - const firstToast = page.locator('.download-toast').filter({ hasText: titles[0] }); - const secondToast = page.locator('.download-toast').filter({ hasText: titles[1] }); - - // A slow pending file must keep the notification open beyond the completion timeout. - await page.waitForTimeout(5500); - await expect(firstToast).toContainText('Downloading'); - await expect(secondToast).toContainText('Downloading'); - - const firstDownload = page.waitForEvent('download'); - await videos.get(chartIds[0])!.fulfill({ status: 404, body: '' }); - expect((await firstDownload).suggestedFilename()).toBe(`${titles[0]}.zip`); - await expect(firstToast).toContainText('Download complete'); - await page.waitForTimeout(4000); - await expect(firstToast).toBeVisible(); - await expect(secondToast).toContainText('Downloading'); - - const secondDownload = page.waitForEvent('download'); - await videos.get(chartIds[1])!.fulfill({ body: 'Video fixture', contentType: 'video/mp4' }); - expect((await secondDownload).suggestedFilename()).toBe(`${titles[1]}.zip`); - await expect(secondToast).toContainText('Download complete'); - // Each completed download has its own five-second timer, including one with no optional video. - await expect(firstToast).toHaveCount(0, { timeout: 2500 }); - await expect(secondToast).toBeVisible(); - await expect(secondToast).toHaveCount(0, { timeout: 6000 }); - }); -}