import fs from 'node:fs'; import { expect, test } from '@playwright/test'; import { strFromU8, unzipSync } from 'fflate'; test.describe('workbench editing shell', () => { test.beforeEach(async ({ page }) => { await page.goto('/'); await expect(page.getByText('道路编译工作台', { exact: true })).toBeVisible(); }); test('starts in browse mode and persists the edit switch', async ({ page }) => { const mode = page.getByRole('switch', { name: '编辑模式' }); await expect(mode).not.toBeChecked(); await expect(page.getByRole('button', { name: '放弃' })).toHaveCount(0); await mode.check(); await expect(mode).toBeChecked(); await expect(page.getByRole('button', { name: '放弃' })).toBeVisible(); await page.reload(); await expect(page.getByRole('switch', { name: '编辑模式' })).toBeChecked(); }); test('keeps the map mounted while compiling the current edits', async ({ page }) => { const map = page.getByLabel('道路地图'); await expect(map).toBeVisible(); const canvasCount = await map.locator('canvas').count(); expect(canvasCount).toBeGreaterThan(0); await page.getByRole('button', { name: '保存并重新生成' }).click(); await expect(page.getByText('已保存并重新生成')).toBeVisible({ timeout: 30_000 }); await expect(map).toBeVisible(); await expect(map.locator('canvas')).toHaveCount(canvasCount); }); test('supports layer visibility and inspector accordion state', async ({ page }) => { const roads = page.getByLabel('自研道路与路口面'); await expect(roads).toBeChecked(); await roads.uncheck(); await expect(roads).not.toBeChecked(); await roads.check(); const issues = page.getByText(/待检查问题(/).locator('..'); await expect(issues).not.toHaveAttribute('open', ''); await issues.locator('summary').click(); await expect(issues).toHaveAttribute('open', ''); await issues.locator('summary').click(); await expect(issues).not.toHaveAttribute('open', ''); const overview = page.getByText('当前编译概览').locator('..'); await overview.locator('summary').click(); await expect(overview).toHaveAttribute('open', ''); }); test('edits a signal field and can discard the draft', async ({ page }) => { await page.getByRole('switch', { name: '编辑模式' }).check(); await page.getByText('元素', { exact: true }).click(); const signalSelect = page.getByLabel('检查信号灯'); await expect(signalSelect.locator('option')).not.toHaveCount(1); await signalSelect.selectOption({ index: 1 }); const heading = page.getByLabel('横杆方向(度)'); const original = await heading.inputValue(); await heading.fill(String((Number(original) + 15) % 360)); await expect(page.getByText('未保存修改 0 项')).toHaveCount(0); await expect(page.getByRole('button', { name: '放弃' })).toBeEnabled(); await page.getByRole('button', { name: '放弃' }).click(); await expect(page.getByRole('button', { name: '放弃' })).toBeDisabled(); await expect(heading).toHaveValue(original); }); test('sends the compile request through the browser workflow', async ({ page }) => { const compile = page.waitForRequest( (request) => request.method() === 'POST' && request.url().endsWith('/api/compile'), ); await page.getByRole('button', { name: '保存并重新生成' }).click(); await compile; await expect(page.getByText('已保存并重新生成')).toBeVisible({ timeout: 30_000 }); }); test('exports a package that satisfies the native-road contract', async ({ page }) => { const downloadPromise = page.waitForEvent('download'); await page.getByRole('link', { name: '导出道路包' }).click(); const download = await downloadPromise; expect(download.suggestedFilename()).toBe('e2e-fengshu-er-road.native-road.zip'); const archivePath = await download.path(); expect(archivePath).toBeTruthy(); const entries = unzipSync(fs.readFileSync(archivePath!)); const names = Object.keys(entries); const manifest = JSON.parse(strFromU8(entries['manifest.json'])); expect(manifest).toMatchObject({ areaId: 'e2e-fengshu-er-road', contract: 'native-road-package/v1.1' }); expect(names).toContain('compiled.json'); expect(names).toContain('traffic-signal-assemblies.json'); expect(names).toContain('traffic-signals.json'); expect(names.some((name) => name.includes('override') || name.endsWith('.osm'))).toBe(false); expect(JSON.parse(strFromU8(entries['compiled.json']))).not.toHaveProperty('source'); }); test('exposes the edit-only history controls without changing browse controls', async ({ page }) => { await expect(page.getByRole('button', { name: '撤销' })).toHaveCount(0); await page.getByRole('switch', { name: '编辑模式' }).check(); await expect(page.getByRole('button', { name: '撤销' })).toBeVisible(); await expect(page.getByRole('button', { name: '重做' })).toBeVisible(); await expect(page.getByRole('button', { name: '放弃' })).toBeVisible(); await page.getByRole('switch', { name: '编辑模式' }).uncheck(); await expect(page.getByRole('button', { name: '撤销' })).toHaveCount(0); }); test('opens an ordinary junction from a deep link with its workspace context', async ({ page }) => { const state = await page.request.get('/api/state').then((response) => response.json()); const nodeCounts = new Map(); for (const endpoint of state.compiled.model.endpoints) { const nodeId = String(endpoint.nodeId); nodeCounts.set(nodeId, (nodeCounts.get(nodeId) || 0) + 1); } const nodeId = [...nodeCounts].find(([, count]) => count >= 2)?.[0]; expect(nodeId).toBeTruthy(); await page.goto(`/junction/node/${encodeURIComponent(nodeId!)}`); await expect(page.getByLabel('路口编辑工作区')).toBeVisible(); await expect(page.getByText(`路口节点 ${nodeId}`)).toBeVisible(); await expect(page.getByText('设施与验证', { exact: true })).toBeVisible(); await page.getByRole('switch', { name: '编辑模式' }).check(); await expect(page.getByLabel('路口编辑工作区').getByText('道路参数', { exact: true })).toBeVisible(); }); });