Files
management/Management/tests/query-contract.test.ts

98 lines
2.9 KiB
TypeScript

import { afterEach, expect, it, vi } from 'vitest'
import { componentGateway } from '@/application/component-gateway'
import { activityGateway } from '@/application/activity-gateway'
import { userGateway } from '@/application/user-gateway'
afterEach(() => vi.unstubAllGlobals())
it('forwards every documented log filter, drops unsupported ones and preserves server results', async () => {
const items = [{ id: 1, module: 'FUTURE', action: 'CUSTOM', targetId: 'external-id' }]
const fetcher = vi.fn<typeof fetch>(async () =>
Response.json({ code: 'OK', data: { items, total: 91 } }),
)
vi.stubGlobal('fetch', fetcher)
const query = {
page: 2,
size: 15,
module: 'OTHER',
action: 'PUBLISH',
success: false,
operatorId: 0,
keyword: '名称',
targetId: '123',
targetType: 'OTHER',
targetCode: 'CODE',
from: '2026-10-01T00:00:00',
to: '2026-10-12T23:59:59',
operatorName: 'unsupported',
sort: 'unsupported',
}
const result = await activityGateway.list(query)
const url = new URL(String(fetcher.mock.calls[0]?.[0]), 'http://local')
expect(Object.fromEntries(url.searchParams)).toEqual({
page: '2',
size: '15',
module: 'OTHER',
action: 'PUBLISH',
success: 'false',
operatorId: '0',
keyword: '名称',
targetId: '123',
targetType: 'OTHER',
targetCode: 'CODE',
from: query.from,
to: query.to,
})
expect(result).toEqual({ items, total: 91 })
})
it('does not send component status, tags, description or sorting as search conditions', async () => {
const items = [{ id: 1, componentName: '后端返回的名称', category: '自由分类' }]
const fetcher = vi.fn<typeof fetch>(async () =>
Response.json({ code: 'OK', data: { items, total: 31 } }),
)
vi.stubGlobal('fetch', fetcher)
const query = {
page: 1,
size: 10,
keyword: '其他',
category: '安装板',
status: 'complete',
tags: 'tag',
description: 'text',
sort: 'name',
}
expect(await componentGateway.list(query)).toEqual({ items, total: 31 })
const url = new URL(String(fetcher.mock.calls[0]?.[0]), 'http://local')
expect(Object.fromEntries(url.searchParams)).toEqual({
page: '1',
size: '10',
keyword: '其他',
category: '安装板',
})
})
it('maps user filters to the documented userType and status and sends no extra conditions', async () => {
const fetcher = vi.fn<typeof fetch>(async () =>
Response.json({ code: 'OK', data: { items: [], total: 0 } }),
)
vi.stubGlobal('fetch', fetcher)
const query = {
page: 3,
size: 10,
keyword: '昵称',
role: 'admin',
status: 'disabled',
lastLogin: 'yesterday',
}
await userGateway.listUsers(query)
const url = new URL(String(fetcher.mock.calls[0]?.[0]), 'http://local')
expect(Object.fromEntries(url.searchParams)).toEqual({
page: '3',
size: '10',
keyword: '昵称',
userType: 'ADMIN',
status: 'DISABLED',
})
})