test(site): cover site list flows (#560)

This commit is contained in:
InfinityPacer
2026-07-19 08:57:44 +08:00
committed by GitHub
parent 126a204b2a
commit 374a0866be
7 changed files with 823 additions and 24 deletions

View File

@@ -0,0 +1,39 @@
import type { Site, SiteStatistic, SiteUserData } from '@/api/types'
let siteSeed = 0
export function createSite(overrides: Partial<Site> = {}): Site {
siteSeed += 1
return {
domain: `site-${siteSeed}.example.com`,
downloader: '',
id: siteSeed,
is_active: true,
name: `测试站点 ${siteSeed}`,
pri: siteSeed,
url: `https://site-${siteSeed}.example.com/`,
...overrides,
}
}
export function createSiteStatistic(overrides: Partial<SiteStatistic> = {}): SiteStatistic {
return {
fail: 0,
lst_state: 0,
seconds: 1,
success: 10,
...overrides,
}
}
export function createSiteUserData(overrides: Partial<SiteUserData> = {}): SiteUserData {
return {
bonus: 100,
download: 1024,
ratio: 2,
seeding: 3,
upload: 2048,
username: 'site-user',
...overrides,
}
}

View File

@@ -0,0 +1,73 @@
import type { Site, SiteStatistic, SiteUserData } from '@/api/types'
import { HttpResponse, http, type JsonBodyType } from 'msw'
const API_BASE_URL = 'http://localhost/api/v1/'
export const siteApiUrls = {
list: new URL('site/', API_BASE_URL).href,
priorities: new URL('site/priorities', API_BASE_URL).href,
statistic: (domain: string) => new URL(`site/statistic/${domain}`, API_BASE_URL).href,
statistics: new URL('site/statistic', API_BASE_URL).href,
userDataLatest: new URL('site/userdata/latest', API_BASE_URL).href,
}
export function siteListHandler(
sites: Site[],
status: number | (() => number) = 200,
onRequest: () => void | Promise<void> = () => {},
) {
return http.get(siteApiUrls.list, async () => {
await onRequest()
return HttpResponse.json(sites as unknown as JsonBodyType, {
status: typeof status === 'function' ? status() : status,
})
})
}
export function siteStatisticsHandler(
statistics: SiteStatistic[],
status = 200,
onRequest: () => void | Promise<void> = () => {},
) {
return http.get(siteApiUrls.statistics, async () => {
await onRequest()
return HttpResponse.json(statistics as unknown as JsonBodyType, { status })
})
}
export function siteStatisticHandler(
domain: string,
statistic: SiteStatistic,
status = 200,
onRequest: () => void | Promise<void> = () => {},
) {
return http.get(siteApiUrls.statistic(domain), async () => {
await onRequest()
return HttpResponse.json(statistic as unknown as JsonBodyType, { status })
})
}
export function siteUserDataLatestHandler(
userData: SiteUserData[],
status = 200,
onRequest: () => void | Promise<void> = () => {},
) {
return http.get(siteApiUrls.userDataLatest, async () => {
await onRequest()
return HttpResponse.json(userData as unknown as JsonBodyType, { status })
})
}
export function saveSitePrioritiesHandler(
onSave: (priorities: Array<{ id: number; pri: number }>) => void | Promise<void> = () => {},
options: { status?: number; success?: boolean } = {},
) {
return http.post(siteApiUrls.priorities, async ({ request }) => {
const priorities = (await request.json()) as Array<{ id: number; pri: number }>
await onSave(priorities)
return HttpResponse.json(
{ success: options.success ?? (options.status ?? 200) < 400 },
{ status: options.status ?? 200 },
)
})
}