Files
2026-11-03 15:50:13 +08:00

484 lines
27 KiB
TypeScript

import { expect, test, type Page, type Route } from '@playwright/test';
import { readFile } from 'node:fs/promises';
import type { ManagedProjectView, StepUploadBatch } from '../src/api/types';
import { checkStepFile, MAX_STEP_FILE_SIZE } from '../src/components/project/stepFiles';
const content = Buffer.from('step-test-content');
const oldUrl = 'https://assets.example.com/projects/step-test/step/old/model.step';
function objectKey(batchCode: string, name: string) {
return `projects/step-test/step/${batchCode}/model${name.slice(name.lastIndexOf('.'))}`;
}
function disposition(name: string) {
return `attachment; filename*=UTF-8''${encodeURIComponent(name)}`;
}
interface Options {
bound?: boolean;
legacy?: boolean;
notReady?: boolean;
signatureFailure?: boolean;
invalidSignature?: boolean;
uploadFailure?: boolean;
confirmFailure?: 'server' | 'network' | 'missing' | 'size-mismatch' | 'invalid-response';
signatureExpiresAt?: string;
deleteFailure?: boolean;
signatureGate?: Promise<void>;
uploadGate?: Promise<void>;
confirmGate?: Promise<void>;
}
async function ok(route: Route, data: unknown) {
await route.fulfill({ json: { code: 'OK', message: '成功', data, requestId: null } });
}
async function failure(route: Route, code: string, message: string, status = 409) {
await route.fulfill({ status, json: { code, message, data: null, requestId: 'step-request-id' } });
}
async function prepare(page: Page, options: Options = {}) {
const project: ManagedProjectView = {
project: {
projectCode: 'step-test', name: 'STEP 测试机器人', description: '项目独立模型资源管理', coverUrl: null,
urdfMainUrl: 'https://assets.example.com/robot.urdf', urdfBaseUrl: 'https://assets.example.com/',
stepMainUrl: options.bound ? oldUrl : null,
stepFileName: options.bound ? 'old.step' : null,
stepSizeBytes: options.bound ? 1024 : null,
projectType: 'WHEELED', owner: { userCode: null, nickname: '社区官方' },
repoStatus: options.notReady ? 'CREATING' : 'READY', publishStatus: 'UNPUBLISHED',
publishedCommitSha: null, publishedAt: null, sourceProjectCode: null, sourceCommitSha: null,
likeCount: 0, commentCount: 0, liked: false, sourceRepositoryUrl: null, sourceLicense: null,
createdAt: '2026-11-02T00:00:00Z', updatedAt: '2026-11-02T00:00:00Z',
},
moderationStatus: 'NORMAL', moderationReason: null, deleted: false,
};
if (options.legacy) {
delete project.project.stepMainUrl;
delete project.project.stepFileName;
delete project.project.stepSizeBytes;
}
const state = {
project,
signatures: [] as { files: StepUploadBatch['files'] }[],
uploads: [] as { body: string; headers: Record<string, string> }[],
confirmations: [] as StepUploadBatch[],
deletes: 0,
gets: 0,
requests: [] as { path: string; method: string; headers: Record<string, string>; body: string | null }[],
};
const bind = (body: StepUploadBatch) => {
project.project.stepMainUrl = `https://assets.example.com/${objectKey(body.batchCode, body.files[0].relativePath)}`;
project.project.stepFileName = body.files[0].relativePath;
project.project.stepSizeBytes = body.files[0].sizeBytes;
};
await page.addInitScript(() => localStorage.setItem('vibemake_admin_token', 'test-token'));
await page.route('https://fonts.googleapis.com/**', (route) => route.abort());
await page.route('**/mock-step-oss', async (route) => {
state.uploads.push({ body: route.request().postDataBuffer()?.toString('utf8') ?? '', headers: route.request().headers() });
await options.uploadGate;
if (options.uploadFailure && state.uploads.length === 1) return route.fulfill({ status: 503 });
return route.fulfill({ status: 204 });
});
// 全部管理请求均被拦截;测试不访问真实后端或 OSS。
await page.route('**/admin-api/**', async (route) => {
const path = new URL(route.request().url()).pathname.replace('/admin-api', '');
const method = route.request().method();
state.requests.push({ path, method, headers: route.request().headers(), body: route.request().postData() });
if (path === '/auth/me') return ok(route, { userCode: 'admin', nickname: '测试管理员', role: 'ADMIN' });
if (path === '/projects/types') return ok(route, [{ code: 'WHEELED', name: '轮式机器人' }]);
if (path === '/projects/step-test' && method === 'GET') {
state.gets++;
return ok(route, project);
}
if (path.endsWith('/tree')) return ok(route, { commitSha: 'main-sha', path: '', entries: [] });
if (path === '/projects' || path === '/project-imports' || path.endsWith('/commits')) {
return ok(route, { items: [], page: 1, size: 20, total: 0 });
}
if (path.endsWith('/step/signature') && method === 'POST') {
const body = route.request().postDataJSON() as { files: StepUploadBatch['files'] };
state.signatures.push(body);
await options.signatureGate;
if (options.signatureFailure && state.signatures.length === 1) {
return failure(route, 'OSS_UNAVAILABLE', '签名服务暂不可用', 503);
}
const name = options.invalidSignature ? 'different.step' : body.files[0].relativePath;
const batchCode = `step-batch-${state.signatures.length}`;
const key = objectKey(batchCode, name);
return ok(route, {
batchCode, mainRelativePath: name,
expiresAt: options.signatureExpiresAt ?? '2099-01-01T00:00:00Z', uploadUrl: 'http://127.0.0.1:5190/mock-step-oss',
files: [{ relativePath: name, objectKey: key, formFields: {
key, policy: 'step-policy', OSSAccessKeyId: 'step-access-key', signature: 'step-signature',
success_action_status: '204', 'Content-Disposition': disposition(name), 'x-oss-forbid-overwrite': 'true',
} }],
});
}
if (path.endsWith('/step') && method === 'PUT') {
const body = route.request().postDataJSON() as StepUploadBatch;
state.confirmations.push(body);
await options.confirmGate;
if (state.confirmations.length === 1 && options.confirmFailure) {
if (options.confirmFailure === 'server') return failure(route, 'OSS_ERROR', '绑定服务暂不可用', 503);
if (options.confirmFailure === 'missing') return failure(route, 'STEP_FILE_MISSING', 'STEP 对象不存在');
if (options.confirmFailure === 'size-mismatch') return failure(route, 'STEP_FILE_SIZE_MISMATCH', 'STEP 对象大小不匹配');
if (options.confirmFailure === 'invalid-response') return ok(route, { projectCode: 'step-test' });
// 模拟服务端已经提交,但成功响应丢失。
bind(body);
return route.abort('failed');
}
bind(body);
return ok(route, project.project);
}
if (path.endsWith('/step') && method === 'DELETE') {
state.deletes++;
if (options.deleteFailure && state.deletes === 1) {
return failure(route, 'OSS_UNAVAILABLE', '移除服务暂不可用', 503);
}
project.project.stepMainUrl = null;
project.project.stepFileName = null;
project.project.stepSizeBytes = null;
return ok(route, null);
}
return failure(route, 'UNEXPECTED_REQUEST', `${method} ${path}`, 404);
});
await page.goto('/projects/step-test');
if (!options.notReady) await page.getByRole('tab', { name: 'STEP', exact: true }).click();
return state;
}
function panel(page: Page) {
return page.getByRole('tabpanel', { name: 'STEP', exact: true });
}
async function chooseFile(page: Page, name = 'robot.step', buffer = content) {
const chooserPromise = page.waitForEvent('filechooser');
await panel(page).getByRole('button', { name: /^(选择 STEP 文件|重新选择文件)$/ }).click();
const chooser = await chooserPromise;
expect(chooser.isMultiple()).toBe(false);
await chooser.setFiles({ name, mimeType: '', buffer });
await expect(panel(page).locator('.file-list-main')).toHaveText(name);
}
function gate() {
let release!: () => void;
const promise = new Promise<void>((resolve) => { release = resolve; });
return { promise, release };
}
test('STEP 紧邻 URDF,兼容尚未返回 STEP 字段的后端', async ({ page }, testInfo) => {
const state = await prepare(page, { legacy: true });
const tabs = await page.getByRole('tab').allTextContents();
expect(tabs.indexOf('STEP')).toBe(tabs.indexOf('URDF') + 1);
await expect(panel(page).getByText('未绑定', { exact: true })).toBeVisible();
await expect(panel(page).getByText(/文件大小不超过 500 MiB/)).toBeVisible();
await expect(panel(page).getByRole('button', { name: '上传并绑定' })).toHaveCount(0);
await expect(panel(page).getByRole('button', { name: '移除绑定' })).toHaveCount(0);
await expect(panel(page).getByRole('link', { name: '下载 STEP' })).toHaveCount(0);
expect(state.signatures).toHaveLength(0);
await page.screenshot({ path: testInfo.outputPath('step-empty.png'), fullPage: true, animations: 'disabled' });
});
test('仓库未就绪时不开放 STEP 入口', async ({ page }) => {
await prepare(page, { notReady: true });
await expect(page.getByText('仓库准备中,暂不开放文件与发布操作')).toBeVisible();
await expect(page.getByRole('tab', { name: 'STEP', exact: true })).toHaveCount(0);
});
test('文件选择可清除,保留已有绑定并沿用当前面板样式', async ({ page }, testInfo) => {
await prepare(page, { bound: true });
await chooseFile(page, '机械臂 装配.STP');
await expect(panel(page).getByRole('link', { name: 'old.step' })).toHaveAttribute('href', oldUrl);
await page.screenshot({ path: testInfo.outputPath('step-selected.png'), fullPage: true, animations: 'disabled' });
await panel(page).getByRole('button', { name: /清\s*除/, exact: true }).click();
await expect(panel(page).locator('.file-list-row')).toHaveCount(0);
await expect(panel(page).getByRole('button', { name: '上传并绑定' })).toHaveCount(0);
await expect(panel(page).getByRole('link', { name: 'old.step' })).toBeVisible();
});
for (const name of ['robot.step', 'robot.stp', '机械臂 装配.STEP', 'Robot.STP']) {
test(`上传 ${name} 使用正确签名和确认契约,不写入 Git`, async ({ page }) => {
const state = await prepare(page, { bound: true });
await chooseFile(page, name);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
await expect(panel(page).getByRole('link', { name, exact: true })).toBeVisible();
expect(state.signatures).toEqual([{ files: [{ relativePath: name, sizeBytes: content.length }] }]);
expect(state.confirmations).toEqual([{ batchCode: 'step-batch-1', files: state.signatures[0].files }]);
expect(state.uploads).toHaveLength(1);
for (const value of [objectKey('step-batch-1', name), 'step-policy', 'step-access-key', 'step-signature', '204', disposition(name), 'true']) {
expect(state.uploads[0].body).toContain(value);
}
expect(state.uploads[0].body).toContain(`filename="${name}"`);
expect(state.uploads[0].body.indexOf('name="file"')).toBeGreaterThan(state.uploads[0].body.indexOf('name="x-oss-forbid-overwrite"'));
expect(state.uploads[0].headers.authorization).toBeUndefined();
expect(state.uploads[0].headers['content-type']).toContain('multipart/form-data; boundary=');
const adminWrites = state.requests.filter(({ method }) => method !== 'GET');
expect(adminWrites.every(({ headers }) => headers.authorization === 'Bearer test-token')).toBe(true);
expect(adminWrites.every(({ headers }) => headers['content-type'] === 'application/json')).toBe(true);
expect(state.requests.filter(({ method }) => method !== 'GET').map(({ path }) => path)).toEqual([
'/projects/step-test/step/signature', '/projects/step-test/step',
]);
expect(state.project.project.urdfMainUrl).toBe('https://assets.example.com/robot.urdf');
expect(state.project.project.publishStatus).toBe('UNPUBLISHED');
await expect(panel(page).getByRole('link', { name: '下载 STEP' })).toHaveAttribute('href', state.project.project.stepMainUrl!);
await expect(panel(page).getByRole('link', { name: '下载 STEP' })).toHaveAttribute('download', name);
await expect(panel(page).locator('.file-list-row')).toHaveCount(0);
});
}
test('格式与空文件校验在请求前生效,重新选择后恢复', async ({ page }) => {
const state = await prepare(page);
await chooseFile(page, 'robot.step.exe');
await expect(panel(page).getByText('仅支持 .step、.stp 文件(扩展名大小写不敏感)')).toBeVisible();
await expect(panel(page).getByRole('button', { name: '上传并绑定' })).toBeDisabled();
await chooseFile(page, 'empty.step', Buffer.alloc(0));
await expect(panel(page).getByText('不能上传空文件')).toBeVisible();
await expect(panel(page).getByRole('button', { name: '上传并绑定' })).toBeDisabled();
await chooseFile(page);
await expect(panel(page).getByRole('button', { name: '上传并绑定' })).toBeEnabled();
expect(state.signatures).toHaveLength(0);
});
test('签名、上传与确认分阶段反馈,确认前保留旧绑定并阻止重复操作', async ({ page }) => {
const signing = gate();
const uploading = gate();
const confirming = gate();
try {
const state = await prepare(page, {
bound: true, signatureGate: signing.promise, uploadGate: uploading.promise, confirmGate: confirming.promise,
});
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('status')).toHaveText('正在获取 STEP 上传签名');
await expect(panel(page).getByRole('button', { name: '重新选择文件' })).toBeDisabled();
await expect(panel(page).getByRole('button', { name: '移除绑定' })).toBeDisabled();
signing.release();
await expect(panel(page).getByRole('status')).toHaveText('正在上传 STEP 文件');
expect(state.confirmations).toHaveLength(0);
uploading.release();
await expect(panel(page).getByRole('status')).toHaveText('文件已上传,正在确认 STEP 绑定');
await expect(panel(page).getByRole('link', { name: 'old.step' })).toBeVisible();
await expect(page.getByText('STEP 已绑定', { exact: true })).toHaveCount(0);
await expect(panel(page).getByRole('button', { name: '重试绑定' })).toBeDisabled();
confirming.release();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.signatures).toHaveLength(1);
expect(state.confirmations).toHaveLength(1);
} finally {
signing.release(); uploading.release(); confirming.release();
}
});
for (const stage of ['signature', 'upload'] as const) {
test(`${stage} 失败保留文件和旧绑定,重试可成功`, async ({ page }) => {
const state = await prepare(page, { bound: true, signatureFailure: stage === 'signature', uploadFailure: stage === 'upload' });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByText(/可保留所选文件重新上传/)).toBeVisible();
await expect(panel(page).getByRole('link', { name: 'old.step' })).toBeVisible();
expect(state.confirmations).toHaveLength(0);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.signatures).toHaveLength(2);
});
}
for (const reason of ['server', 'network', 'invalid-response'] as const) {
test(`确认 ${reason} 异常保留批次,重试不重复上传`, async ({ page }) => {
const state = await prepare(page, { bound: true, confirmFailure: reason });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByText(/绑定结果尚未确认/)).toBeVisible();
await expect(page.getByText('STEP 已绑定', { exact: true })).toHaveCount(0);
if (reason === 'network') {
await expect(panel(page).getByRole('link', { name: 'robot.step' })).toBeVisible();
} else {
await expect(panel(page).getByRole('link', { name: 'old.step' })).toBeVisible();
}
await panel(page).getByRole('button', { name: '重试绑定' }).click();
await expect(page.getByText('重试将使用本次上传文件重新设置 STEP 绑定,可能覆盖其他操作的结果。')).toBeVisible();
expect(state.confirmations).toHaveLength(1);
await page.getByRole('button', { name: '确认重试', exact: true }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.signatures).toHaveLength(1);
expect(state.uploads).toHaveLength(1);
expect(state.confirmations).toHaveLength(2);
expect(state.confirmations[1]).toEqual(state.confirmations[0]);
});
}
for (const reason of ['missing', 'size-mismatch'] as const) {
test(`对象 ${reason} 时展示请求编号,重新签名上传`, async ({ page }) => {
const state = await prepare(page, { confirmFailure: reason });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByText(/已上传文件不可用,请重新上传/)).toContainText('step-request-id');
await expect(panel(page).getByRole('button', { name: '重试绑定' })).toHaveCount(0);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.signatures).toHaveLength(2);
expect(state.uploads).toHaveLength(2);
expect(state.confirmations[1].batchCode).toBe('step-batch-2');
});
}
test('已上传对象在签名过期后仍可手动重试确认,取消不发请求', async ({ page }) => {
await page.clock.install({ time: new Date('2026-11-02T08:00:00Z') });
const state = await prepare(page, { confirmFailure: 'server', signatureExpiresAt: '2026-11-02T08:15:00Z' });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByText(/绑定结果尚未确认/)).toBeVisible();
await page.clock.setFixedTime(new Date('2026-11-02T08:16:00Z'));
await panel(page).getByRole('button', { name: '重试绑定' }).click();
await expect(page.getByText('确定重试 STEP 绑定?')).toBeVisible();
await page.getByRole('button', { name: /取\s*消/, exact: true }).click();
await expect(page.getByText('确定重试 STEP 绑定?')).not.toBeVisible();
expect(state.confirmations).toHaveLength(1);
await panel(page).getByRole('button', { name: '重试绑定' }).click();
await page.getByRole('button', { name: '确认重试', exact: true }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.signatures).toHaveLength(1);
expect(state.uploads).toHaveLength(1);
expect(state.confirmations).toEqual([state.confirmations[0], state.confirmations[0]]);
});
test('重新选择文件会放弃待确认批次', async ({ page }) => {
const state = await prepare(page, { confirmFailure: 'server' });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('button', { name: '重试绑定' })).toBeEnabled();
await chooseFile(page, 'replacement.stp');
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('link', { name: 'replacement.stp' })).toBeVisible();
expect(state.signatures).toHaveLength(2);
expect(state.confirmations[1].files[0].relativePath).toBe('replacement.stp');
});
test('签名与文件不匹配时不上传或确认', async ({ page }) => {
const state = await prepare(page, { invalidSignature: true });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByText(/STEP 上传签名与所选文件不匹配/)).toBeVisible();
expect(state.uploads).toHaveLength(0);
expect(state.confirmations).toHaveLength(0);
});
test('移除绑定二次确认,仅调用 DELETE 并保留 URDF', async ({ page }) => {
const state = await prepare(page, { bound: true });
await panel(page).getByRole('button', { name: '移除绑定' }).click();
await expect(page.getByText('确定移除 STEP 绑定?(不会删除已上传对象)')).toBeVisible();
expect(state.deletes).toBe(0);
await page.getByRole('button', { name: /确\s*定/, exact: true }).click();
await expect(page.getByText('已移除 STEP 绑定', { exact: true })).toBeVisible();
await expect(panel(page).getByText('未绑定', { exact: true })).toBeVisible();
await expect(panel(page).getByRole('link', { name: '下载 STEP' })).toHaveCount(0);
expect(state.deletes).toBe(1);
expect(state.requests.find(({ method }) => method === 'DELETE')?.body).toBeNull();
expect(state.uploads).toHaveLength(0);
expect(state.project.project.urdfMainUrl).toBe('https://assets.example.com/robot.urdf');
});
test('STEP 通过跨域资源地址直接下载,原始文件名由 OSS 响应保留,不请求管理下载接口', async ({ page, context }) => {
const downloads: { url: string; headers: Record<string, string>; method: string }[] = [];
await context.route('https://assets.example.com/**', async (route) => {
downloads.push({ url: route.request().url(), headers: route.request().headers(), method: route.request().method() });
// 不返回 CORS 头,验证原生下载不依赖 fetch/Blob 或管理端鉴权。
return route.fulfill({
body: content,
headers: { 'Content-Type': 'application/octet-stream', 'Content-Disposition': disposition('机械臂 装配.STEP') },
});
});
const state = await prepare(page, { bound: true });
await chooseFile(page, '机械臂 装配.STEP');
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('link', { name: '机械臂 装配.STEP', exact: true })).toBeVisible();
const expectedUrl = `https://assets.example.com/${objectKey('step-batch-1', '机械臂 装配.STEP')}`;
const button = panel(page).getByRole('link', { name: '下载 STEP' });
await expect(button).toHaveAttribute('href', expectedUrl);
await expect(button).toHaveAttribute('download', '机械臂 装配.STEP');
const downloadPromise = page.waitForEvent('download');
await button.click();
const downloaded = await downloadPromise;
expect(downloaded.suggestedFilename()).toBe('机械臂 装配.STEP');
const downloadedPath = await downloaded.path();
expect(downloadedPath).not.toBeNull();
expect(await readFile(downloadedPath!)).toEqual(content);
expect(downloads).toHaveLength(1);
expect(downloads[0].url).toBe(expectedUrl);
expect(downloads[0].method).toBe('GET');
expect(downloads[0].headers.authorization).toBeUndefined();
expect(state.requests.some(({ path }) => path.includes('/download'))).toBe(false);
await expect(page).toHaveURL('/projects/step-test');
});
test('移除失败刷新当前状态后允许重试', async ({ page }) => {
const state = await prepare(page, { bound: true, deleteFailure: true });
await panel(page).getByRole('button', { name: '移除绑定' }).click();
await page.getByRole('button', { name: /确\s*定/, exact: true }).click();
await expect(panel(page).getByText(/请刷新确认当前绑定状态后再操作/)).toBeVisible();
await expect(panel(page).getByRole('link', { name: 'old.step' })).toBeVisible();
await expect.poll(() => state.gets).toBeGreaterThan(1);
await panel(page).getByRole('button', { name: '移除绑定' }).click();
await page.getByRole('button', { name: /确\s*定/, exact: true }).click();
await expect(panel(page).getByText('未绑定', { exact: true })).toBeVisible();
expect(state.deletes).toBe(2);
});
test('移除绑定会放弃待确认的旧请求,再上传使用新签名', async ({ page }) => {
const state = await prepare(page, { bound: true, confirmFailure: 'server' });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('button', { name: '重试绑定' })).toBeEnabled();
await panel(page).getByRole('button', { name: '移除绑定' }).click();
await page.getByRole('button', { name: /确\s*定/, exact: true }).click();
await expect(panel(page).getByText('未绑定', { exact: true })).toBeVisible();
await expect(panel(page).getByRole('button', { name: '重试绑定' })).toHaveCount(0);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(page.getByText('STEP 已绑定', { exact: true })).toBeVisible();
expect(state.deletes).toBe(1);
expect(state.signatures).toHaveLength(2);
expect(state.uploads).toHaveLength(2);
expect(state.confirmations[1].batchCode).toBe('step-batch-2');
});
test('登录失效沿用现有 401 跳转', async ({ page }) => {
await prepare(page);
await page.route('**/step/signature', (route) => failure(route, 'UNAUTHORIZED', '登录已过期', 401));
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(page).toHaveURL(/\/login\?redirect=/);
expect(await page.evaluate(() => localStorage.getItem('vibemake_admin_token'))).toBeNull();
});
test('离开项目后,未完成的 OSS 上传不会继续触发绑定', async ({ page }) => {
const uploading = gate();
try {
const state = await prepare(page, { uploadGate: uploading.promise });
await chooseFile(page);
await panel(page).getByRole('button', { name: '上传并绑定' }).click();
await expect(panel(page).getByRole('status')).toHaveText('正在上传 STEP 文件');
await page.getByRole('link', { name: '项目管理', exact: true }).first().click();
await expect(page).toHaveURL('/projects');
uploading.release();
await expect(page.getByRole('heading', { name: 'STEP 测试机器人' })).toHaveCount(0);
expect(state.confirmations).toHaveLength(0);
} finally {
uploading.release();
}
});
test('STEP 校验接受大小边界与合法文件名,拒绝空文件、目录和非法路径', () => {
const file = (name: string, size = 1) => ({ name, size } as File);
expect(MAX_STEP_FILE_SIZE).toBe(524288000);
expect(checkStepFile(null)).toEqual([]);
expect(checkStepFile(file('large.step', 51 * 1024 * 1024))).toEqual([]);
expect(checkStepFile(file('机械臂 装配.STEP', MAX_STEP_FILE_SIZE))).toEqual([]);
expect(checkStepFile(file('robot.StP'))).toEqual([]);
expect(checkStepFile(file('robot.step', MAX_STEP_FILE_SIZE + 1))).toContain('文件大小不能超过 500 MiB');
expect(checkStepFile(file('robot.step', 0))).toContain('不能上传空文件');
for (const name of ['robot.step.exe', '.step', 'robot', 'dir/robot.step', '..\\robot.stp', 'a:robot.step', 'a\u0000.step', `${'a'.repeat(1024)}.step`]) {
expect(checkStepFile(file(name)).length, name).toBeGreaterThan(0);
}
});