248 lines
14 KiB
TypeScript
248 lines
14 KiB
TypeScript
import { expect, test, type Page, type Route } from '@playwright/test';
|
|
import type { ManagedProjectView, ProjectUpdateInput } from '../src/api/types';
|
|
|
|
test.use({ timezoneId: 'Asia/Shanghai' });
|
|
|
|
async function ok(route: Route, data: unknown) {
|
|
await route.fulfill({ json: { code: 'OK', message: '成功', data, requestId: null } });
|
|
}
|
|
|
|
async function prepare(page: Page, options: { publishedAt?: string | null; ignoreDate?: boolean; failSave?: boolean } = {}) {
|
|
const project: ManagedProjectView = {
|
|
project: {
|
|
projectCode: 'date-test', name: '日期测试项目', description: '项目描述', coverUrl: null,
|
|
urdfMainUrl: null, urdfBaseUrl: null, projectType: 'GENERAL',
|
|
owner: { userCode: null, nickname: '社区官方' }, repoStatus: 'READY', publishStatus: 'UNPUBLISHED',
|
|
publishedCommitSha: null, publishedAt: options.publishedAt === undefined ? '2026-11-01T10:03:04.000' : options.publishedAt,
|
|
sourceProjectCode: null, sourceCommitSha: null,
|
|
likeCount: 0, commentCount: 0, liked: false, sourceRepositoryUrl: null, sourceLicense: null,
|
|
createdAt: '2026-10-27T08:00:00', updatedAt: '2026-11-02T09:00:00',
|
|
},
|
|
moderationStatus: 'NORMAL', moderationReason: null, deleted: false,
|
|
};
|
|
const other: ManagedProjectView = {
|
|
...project,
|
|
project: { ...project.project, projectCode: 'other-date', name: '另一日期项目', publishedAt: '2026-10-30T10:03:04' },
|
|
};
|
|
const state = { project, updates: [] as ProjectUpdateInput[], lists: [] as Record<string, string>[] };
|
|
await page.addInitScript(() => localStorage.setItem('vibemake_admin_token', 'test-token'));
|
|
await page.route('https://fonts.googleapis.com/**', (route) => route.abort());
|
|
// 所有管理 API 都拦截,不读写真实项目。
|
|
await page.route('**/admin-api/**', async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const path = url.pathname.replace('/admin-api', '');
|
|
if (path === '/auth/me') return ok(route, { userCode: 'admin', nickname: '测试管理员', role: 'ADMIN' });
|
|
if (path === '/projects/types') return ok(route, [{ code: 'GENERAL', name: '通用' }]);
|
|
if (path === '/projects/date-test') {
|
|
if (route.request().method() === 'PATCH') {
|
|
const input = route.request().postDataJSON() as ProjectUpdateInput;
|
|
state.updates.push(input);
|
|
expect(input).not.toHaveProperty('updatedAt');
|
|
if (options.failSave && state.updates.length === 1) {
|
|
return route.fulfill({ status: 400, json: { code: 'INVALID_DATE', message: '发布时间校验失败', data: null } });
|
|
}
|
|
const { publishedAt, ...base } = input;
|
|
Object.assign(project.project, base);
|
|
if (publishedAt !== undefined && !options.ignoreDate) project.project.publishedAt = publishedAt;
|
|
return ok(route, project.project);
|
|
}
|
|
return ok(route, project);
|
|
}
|
|
if (path === '/projects') {
|
|
const query = Object.fromEntries(url.searchParams);
|
|
expect(query).not.toHaveProperty('sortBy');
|
|
expect(query).not.toHaveProperty('sortDirection');
|
|
expect(query.sort).toBe('PUBLISHED_AT_DESC');
|
|
state.lists.push(query);
|
|
// 故意给出不同于客户端时间排序的顺序,验证表格原样展示服务端分页结果。
|
|
const items = query.sort === 'PUBLISHED_AT_DESC' ? [other, project] : [project, other];
|
|
return ok(route, { items, page: Number(query.page ?? 1), size: Number(query.size ?? 20), total: 42 });
|
|
}
|
|
if (path.endsWith('/tree')) return ok(route, { commitSha: 'main-sha', path: '', entries: [] });
|
|
if (path.endsWith('/commits') || path === '/project-imports') return ok(route, { items: [], page: 1, size: 20, total: 0 });
|
|
throw new Error(`未预期的管理请求:${route.request().method()} ${path}`);
|
|
});
|
|
return state;
|
|
}
|
|
|
|
async function openEditor(page: Page) {
|
|
await page.goto('/projects/date-test');
|
|
await page.getByRole('button', { name: '编辑信息', exact: true }).click();
|
|
const dialog = page.getByRole('dialog', { name: '编辑项目信息' });
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toBeVisible();
|
|
await expect(dialog.getByLabel('更新时间', { exact: true })).toHaveCount(0);
|
|
return dialog;
|
|
}
|
|
|
|
async function setDate(page: Page, value: string | null) {
|
|
const dialog = page.getByRole('dialog', { name: '编辑项目信息' });
|
|
const input = dialog.getByLabel('发布时间', { exact: true });
|
|
if (value === null) {
|
|
await input.hover();
|
|
await dialog.locator('.ant-picker-clear').click();
|
|
} else {
|
|
await input.fill(value);
|
|
await input.press('Enter');
|
|
}
|
|
}
|
|
|
|
for (const publishedAt of ['2026-11-01T10:03:04', '2026-11-01T10:03:04.123', '2026-11-01T02:03:04Z', '2026-11-01T10:03:04+08:00']) {
|
|
test(`回填发布时间 ${publishedAt},未修改日期时省略字段`, async ({ page }) => {
|
|
const state = await prepare(page, { publishedAt });
|
|
const dialog = await openEditor(page);
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-11-01 10:03:04');
|
|
await dialog.getByLabel('项目名称', { exact: true }).fill('修改后的项目');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates).toEqual([{ name: '修改后的项目', description: '项目描述', projectType: 'GENERAL', coverUrl: '' }]);
|
|
});
|
|
}
|
|
|
|
test('修改发布时间提交上海时间,刷新回显,保留发布状态且不提交系统更新时间', async ({ page }, testInfo) => {
|
|
const state = await prepare(page);
|
|
const dialog = await openEditor(page);
|
|
await setDate(page, '2026-10-28 12:34:56');
|
|
await dialog.getByText('编辑项目信息', { exact: true }).click();
|
|
await dialog.screenshot({ path: testInfo.outputPath('project-edit-published-date.png') });
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates[0].publishedAt).toBe('2026-10-28T12:34:56.000');
|
|
expect(state.project.project.updatedAt).toBe('2026-11-02T09:00:00');
|
|
expect(state.project.project.publishStatus).toBe('UNPUBLISHED');
|
|
expect(state.project.project.publishedCommitSha).toBeNull();
|
|
await expect(page.getByText('2026-10-28 12:34:56', { exact: true })).toBeVisible();
|
|
await expect(page.getByText('2026-11-02 09:00:00', { exact: true })).toBeVisible();
|
|
await page.getByRole('button', { name: '编辑信息', exact: true }).click();
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-10-28 12:34:56');
|
|
await setDate(page, '2026-10-29 12:34:56');
|
|
await dialog.getByRole('button', { name: /取\s*消/ }).click();
|
|
expect(state.updates).toHaveLength(1);
|
|
await page.getByRole('button', { name: '编辑信息', exact: true }).click();
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-10-28 12:34:56');
|
|
});
|
|
|
|
test('未设置发布时间可留空保存,不自动填入更新时间或当前时间', async ({ page }) => {
|
|
const state = await prepare(page, { publishedAt: null });
|
|
const dialog = await openEditor(page);
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates[0]).not.toHaveProperty('publishedAt');
|
|
expect(state.project.project.publishedAt).toBeNull();
|
|
});
|
|
|
|
test('首次设置发布时间与清空均支持,清空必须显式提交 null', async ({ page }) => {
|
|
const state = await prepare(page, { publishedAt: null });
|
|
const dialog = await openEditor(page);
|
|
await setDate(page, '2026-10-28 12:34:56');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates[0].publishedAt).toBe('2026-10-28T12:34:56.000');
|
|
await page.getByRole('button', { name: '编辑信息', exact: true }).click();
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-10-28 12:34:56');
|
|
await setDate(page, null);
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates[1].publishedAt).toBeNull();
|
|
expect(state.project.project.publishedAt).toBeNull();
|
|
});
|
|
|
|
for (const value of ['2026-10-28 12:34:56', null]) {
|
|
test(`后端忽略发布时间${value === null ? '清空' : '修改'}时保留表单,不误报成功`, async ({ page }) => {
|
|
const state = await prepare(page, { ignoreDate: true });
|
|
const dialog = await openEditor(page);
|
|
await setDate(page, value);
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(page.getByText('服务端未保存所选发布时间,请刷新确认结果后重试')).toBeVisible();
|
|
await expect(dialog).toBeVisible();
|
|
await expect(page.getByText('已保存', { exact: true })).toHaveCount(0);
|
|
expect(state.updates).toHaveLength(1);
|
|
});
|
|
}
|
|
|
|
test('保存失败保留发布时间与基础信息,允许重试', async ({ page }) => {
|
|
const state = await prepare(page, { failSave: true });
|
|
const dialog = await openEditor(page);
|
|
await setDate(page, '2026-10-28 12:34:56');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(page.getByText('发布时间校验失败', { exact: true })).toBeVisible();
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-10-28 12:34:56');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates).toHaveLength(2);
|
|
expect(state.updates[1]).toEqual(state.updates[0]);
|
|
});
|
|
|
|
test('发布时间倒序使用 sort 枚举,保留筛选分页,不发送未支持的升序参数', async ({ page }, testInfo) => {
|
|
const state = await prepare(page);
|
|
await page.goto('/projects');
|
|
const header = page.getByRole('columnheader', { name: '发布时间' });
|
|
await expect(header).toBeVisible();
|
|
await expect(header).toHaveAttribute('aria-sort', 'descending');
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ sort: 'PUBLISHED_AT_DESC', page: '1', size: '20' });
|
|
await expect(page.getByRole('columnheader', { name: '更新时间' })).toHaveCount(0);
|
|
await page.getByPlaceholder('名称 / 描述').fill('日期');
|
|
await page.getByRole('button', { name: /查\s*询/, exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)?.keyword).toBe('日期');
|
|
await page.getByTitle('2', { exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)?.page).toBe('2');
|
|
await header.click();
|
|
await expect(header).toHaveAttribute('aria-sort', 'descending');
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ keyword: '日期', sort: 'PUBLISHED_AT_DESC', page: '1', size: '20' });
|
|
await expect(page.locator('.ant-table-tbody .pt-name')).toHaveText(['另一日期项目', '日期测试项目']);
|
|
await page.screenshot({ path: testInfo.outputPath('project-published-sort.png'), fullPage: true });
|
|
await page.getByTitle('2', { exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)).toMatchObject({ sort: 'PUBLISHED_AT_DESC', page: '2', keyword: '日期' });
|
|
await page.getByRole('button', { name: /查\s*询/, exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)).toMatchObject({ sort: 'PUBLISHED_AT_DESC', page: '1', keyword: '日期' });
|
|
await page.getByTitle('20 条/页', { exact: true }).click();
|
|
await page.getByRole('option', { name: '50 条/页', exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)).toMatchObject({ sort: 'PUBLISHED_AT_DESC', page: '1', size: '50', keyword: '日期' });
|
|
await header.click();
|
|
await expect(header).toHaveAttribute('aria-sort', 'descending');
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ keyword: '日期', sort: 'PUBLISHED_AT_DESC', page: '1', size: '50' });
|
|
});
|
|
|
|
test('仅提供发布时间倒序,创建时间只展示,重置后仍显式请求发布时间排序', async ({ page }) => {
|
|
const state = await prepare(page);
|
|
await page.goto('/projects?ownerUserCode=owner-1');
|
|
const published = page.getByRole('columnheader', { name: '发布时间' });
|
|
const created = page.getByRole('columnheader', { name: '创建时间' });
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ ownerUserCode: 'owner-1', sort: 'PUBLISHED_AT_DESC', page: '1', size: '20' });
|
|
await expect(published).toHaveAttribute('aria-sort', 'descending');
|
|
await expect(created).not.toHaveAttribute('aria-sort');
|
|
await expect(created.locator('.ant-table-column-sorters')).toHaveCount(0);
|
|
await expect(page.getByRole('combobox', { name: '排序方式' })).toHaveCount(0);
|
|
await expect(page.getByText('创建时间倒序', { exact: true })).toHaveCount(0);
|
|
await expect(page.getByText('资源齐全优先', { exact: true })).toHaveCount(0);
|
|
await expect(page.locator('.ant-table-tbody .nowrap').filter({ hasText: '2026-10-27 08:00:00' })).toHaveCount(2);
|
|
const requests = state.lists.length;
|
|
await created.click();
|
|
expect(state.lists).toHaveLength(requests);
|
|
await expect(published).toHaveAttribute('aria-sort', 'descending');
|
|
await page.getByTitle('2', { exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)?.page).toBe('2');
|
|
await page.getByRole('button', { name: /重\s*置/, exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ sort: 'PUBLISHED_AT_DESC', page: '1', size: '20' });
|
|
await expect(page.getByPlaceholder('所有者编码')).toHaveValue('');
|
|
await expect(published).toHaveAttribute('aria-sort', 'descending');
|
|
await expect(created).not.toHaveAttribute('aria-sort');
|
|
await page.getByTitle('2', { exact: true }).click();
|
|
await expect.poll(() => state.lists.at(-1)).toEqual({ sort: 'PUBLISHED_AT_DESC', page: '2', size: '20' });
|
|
});
|
|
|
|
test.describe('其他浏览器时区', () => {
|
|
test.use({ timezoneId: 'America/New_York' });
|
|
test('发布时间按用户本地时间回填,跨日转换后仍提交上海时间且无时区后缀', async ({ page }) => {
|
|
const state = await prepare(page, { publishedAt: '2026-01-02T02:30:00.000' });
|
|
const dialog = await openEditor(page);
|
|
await expect(dialog.getByLabel('发布时间', { exact: true })).toHaveValue('2026-01-01 13:30:00');
|
|
await setDate(page, '2026-01-01 23:45:00');
|
|
await dialog.getByRole('button', { name: /确\s*定/ }).click();
|
|
await expect(dialog).toBeHidden();
|
|
expect(state.updates[0].publishedAt).toBe('2026-01-02T12:45:00.000');
|
|
await expect(page.getByText('2026-01-01 23:45:00', { exact: true })).toBeVisible();
|
|
});
|
|
});
|