Files
vibemake-admin/tests/environment.spec.ts
T
2026-10-27 14:07:44 +08:00

64 lines
3.0 KiB
TypeScript

import { expect, test } from '@playwright/test';
const admin = { userCode: 'test-admin', nickname: '测试管理员', email: 'test@example.com', role: 'ADMIN' };
for (const deployment of [
{
origin: 'https://dev-vibemake-admin.robotquan.com',
api: 'https://dev-vibemake-api.robotquan.com/admin-api',
label: '测试环境',
},
{
origin: 'https://vibemake-admin.robotquan.com',
api: 'https://vibemake-api.robotquan.com/admin-api',
label: '线上环境',
},
{
origin: 'http://127.0.0.1:5190',
api: 'http://127.0.0.1:5190/admin-api',
label: '本地开发 · 测试接口',
},
]) {
test(`${deployment.origin} 自动选择对应接口及环境标记`, async ({ page }) => {
// 使用同一份本地源码模拟不同部署域名,全部业务响应均模拟,不访问真实账号。
if (deployment.origin.startsWith('https:')) {
await page.route(`${deployment.origin}/**`, async (route) => {
const url = new URL(route.request().url());
const response = await route.fetch({ url: `http://127.0.0.1:5190${url.pathname}${url.search}` });
await route.fulfill({ response });
});
}
await page.route('https://fonts.googleapis.com/**', (route) => route.abort());
const requests: { method: string; url: string }[] = [];
await page.route('**/admin-api/**', async (route) => {
const request = route.request();
requests.push({ method: request.method(), url: request.url() });
const headers = {
'Access-Control-Allow-Origin': deployment.origin,
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'content-type, authorization',
};
if (request.method() === 'OPTIONS') return route.fulfill({ status: 204, headers });
const pathname = new URL(request.url()).pathname;
const data = pathname.endsWith('/auth/login')
? { token: 'test-token', tokenType: 'Bearer', expiresIn: 3600, admin }
: pathname.endsWith('/auth/me')
? admin
: pathname.endsWith('/projects/types')
? []
: { items: [], page: 1, size: 20, total: 0 };
await route.fulfill({ status: 200, headers, json: { code: 'OK', message: '成功', data, requestId: null } });
});
await page.goto(`${deployment.origin}/login`);
await page.getByRole('textbox', { name: '邮箱', exact: true }).fill('test@example.com');
await page.getByLabel('密码', { exact: true }).fill('mock-only-not-sent-to-server');
await page.getByRole('button', { name: /登\s*录/ }).click();
await expect(page).toHaveURL(`${deployment.origin}/projects`);
await expect(page.locator('.vm-env')).toHaveText(deployment.label);
await expect(page.getByRole('button', { name: /查\s*询/, exact: true })).toBeVisible();
const posts = requests.filter((request) => request.method === 'POST');
expect(posts).toEqual([{ method: 'POST', url: `${deployment.api}/auth/login` }]);
expect(requests.every((request) => request.url.startsWith(`${deployment.api}/`))).toBe(true);
});
}