import assert from 'node:assert/strict' import { createRequire } from 'node:module' import { mkdir } from 'node:fs/promises' import { seedDemo } from './demo.mjs' const require = createRequire(import.meta.url) const { chromium } = require(process.env.PM_PLAYWRIGHT || 'C:/Users/Administrator/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright') const demo = await seedDemo(), frontend = process.env.PM_FRONTEND_URL || 'http://127.0.0.1:8093' const unique = `workspace-ui-${Date.now()}`, spaceId = await demo.call('/spaces', 'POST', { name: unique }) let projectId, browser try { browser = await chromium.launch({ channel: 'chrome', headless: true }) const context = await browser.newContext({ viewport: { width: 1366, height: 768 } }) await context.addCookies([{ name: 'Admin-Token', value: demo.token, url: frontend }]) const page = await context.newPage(), errors = [] page.on('pageerror', error => errors.push(error.message)) await page.goto(`${frontend}/project/collaboration`, { waitUntil: 'networkidle' }) await page.locator('.header-actions .el-dropdown').hover() await page.getByRole('menuitem', { name: '项目', exact: true }).click() const newDialog = page.locator('.el-dialog').filter({ hasText: '新建项目' }) await newDialog.locator('input').first().fill(unique) assert.ok(!(await newDialog.innerText()).includes('时区')) await newDialog.locator('.el-select').click() await page.getByRole('option', { name: unique, exact: true }).click() const created = page.waitForResponse(response => response.url().endsWith('/project/manage/projects') && response.request().method() === 'POST') await newDialog.getByRole('button', { name: '保存', exact: true }).click() projectId = (await (await created).json()).data assert.ok(projectId) await newDialog.waitFor({ state: 'hidden' }) await demo.call(`/projects/${projectId}/items`, 'POST', { type: 'TASK', title: '保留数据验证', description: null, assigneeId: null, priority: 'MEDIUM', plannedStartAt: '2026-10-20T10:00:00Z', plannedEndAt: '2026-10-20T11:00:00Z' }) await page.getByRole('button', { name: '刷新', exact: true }).click() await page.locator('.work-surface .el-table__body tr').first().waitFor() assert.ok((await page.locator('.workspace-select').innerText()).includes('空间创建人:')) assert.ok((await page.locator('.workspace-select').innerText()).includes('项目创建人:')) assert.equal(await page.getByRole('button', { name: '项目设置', exact: true }).count(), 0) assert.ok(!(await page.locator('.pm-app').innerText()).includes('Asia/Shanghai')) assert.ok((await page.locator('.work-surface').innerText()).includes('2026-10-20 18:00')) /** 打开当前对象操作菜单;页面仅为服务端有维护资格的创建人提供这些动作。 */ async function action(kind, label) { await page.getByRole('button', { name: `${kind}操作`, exact: true }).hover(); await page.getByRole('menuitem', { name: label, exact: true }).click() } await action('空间', '编辑空间') let dialog = page.locator('.el-dialog').filter({ hasText: '编辑空间' }) await dialog.locator('input').first().fill(`${unique}-edited`) await dialog.getByRole('button', { name: '保存', exact: true }).click() await dialog.waitFor({ state: 'hidden' }) await page.locator('.workspace-select .el-select').first().click() await page.getByRole('option').filter({ hasText: `${unique}-edited` }).click() assert.equal((await demo.call('/spaces')).find(s => s.id === spaceId).name, `${unique}-edited`) await action('项目', '编辑项目') dialog = page.locator('.el-dialog').filter({ hasText: '编辑项目' }) assert.ok(!(await dialog.innerText()).includes('时区')) await dialog.locator('input').first().fill(`${unique}-edited`) await dialog.getByRole('button', { name: '保存', exact: true }).click() await dialog.waitFor({ state: 'hidden' }) await page.waitForFunction(name => document.querySelector('.workspace-select .el-select:nth-child(2)')?.innerText.includes(name), `${unique}-edited`) await action('空间', '删除空间') await page.getByRole('button', { name: '确定', exact: true }).click() await page.locator('.el-message').filter({ hasText: '空间仍有未删除项目' }).waitFor() assert.ok((await demo.call('/spaces')).some(s => s.id === spaceId)) await page.getByRole('button', { name: '新建工作项', exact: true }).click() dialog = page.locator('.el-dialog').filter({ hasText: '新建工作项' }) assert.equal(await dialog.locator('.el-date-editor').count(), 2) assert.ok(!(await dialog.innerText()).includes('Asia/Shanghai')) await dialog.getByRole('button', { name: '取消', exact: true }).click() await page.getByRole('tab', { name: '甘特排期', exact: true }).click() assert.ok(!(await page.locator('.gantt-note').innerText()).includes('Asia/Shanghai')) await page.getByRole('tab', { name: '工作项列表', exact: true }).click() // 新项目只产生独立协作编辑事件;不能调用旧项目服务,数值 ID 相同也可能属于无关业务项目。 let changes = [] for (let attempt = 0; attempt < 20; attempt++) { changes = (await demo.call('/notifications')).filter(n => n.projectId === projectId && n.message === '项目变更:PROJECT_EDITED'); if (changes.length >= 1) break; await new Promise(resolve => setTimeout(resolve, 500)) } assert.equal(changes.length, 1, '只有独立协作维护入口提交项目编辑事件') await page.getByRole('button', { name: '刷新', exact: true }).click() await page.locator('.work-surface .el-table__body tr').first().waitFor() await mkdir('.codex-tmp/pm-ui', { recursive: true }) await page.screenshot({ path: '.codex-tmp/pm-ui/workspace-maintenance.png', fullPage: true }) await action('项目', '删除项目') await page.getByRole('button', { name: '确定', exact: true }).click() await page.locator('.el-empty').filter({ hasText: '选择协作项目' }).waitFor() assert.ok(!(await demo.call('/projects')).some(p => p.id === projectId)) await action('空间', '删除空间') await page.getByRole('button', { name: '确定', exact: true }).click() await page.waitForFunction(() => !document.querySelector('.workspace-select')?.innerText.includes('workspace-ui-')) assert.ok(!(await demo.call('/spaces')).some(s => s.id === spaceId)) assert.deepEqual(errors, []) console.log('PASS: 独立协作项目创建、创建人展示、空间/项目编辑、非空拒绝、软删除、固定北京时间、工作项双选择器;浏览器无错误') } finally { if (browser) await browser.close() // 只软删除本脚本创建的唯一对象;已有示例及用户数据不被覆盖,测试内容按要求保留。 const project = (await demo.call('/projects')).find(p => p.id === projectId) if (project) await demo.call(`/projects/${projectId}`, 'DELETE', { version: project.version }) const space = (await demo.call('/spaces')).find(s => s.id === spaceId) if (space) await demo.call(`/spaces/${spaceId}`, 'DELETE', { version: space.version }) }