Files
road-compiler/test/e2e/workbench.spec.ts
T

258 lines
13 KiB
TypeScript

import fs from 'node:fs';
import { expect, test } from '@playwright/test';
import { strFromU8, unzipSync } from 'fflate';
import type OLMap from 'ol/Map';
import type VectorLayer from 'ol/layer/Vector';
import type VectorSource from 'ol/source/Vector';
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<string, number>();
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.getByLabel('道路地图')).toBeVisible();
expect((await page.getByLabel('道路地图').boundingBox())?.width).toBeGreaterThan(0);
await expect(page.getByText('JunctionTools', { exact: true })).toBeVisible();
await expect(page.getByText(nodeId, { exact: true })).toHaveCount(0);
await expect(page.getByText('设施与验证', { exact: true })).toBeVisible();
await expect(page.getByText('当前为浏览模式,开启编辑后可调整路口几何参数')).toBeVisible();
await page.getByRole('button', { name: '开启编辑模式' }).click();
await expect(page.getByLabel('路口编辑工作区').getByText('路口参数', { exact: true })).toBeVisible();
await expect(page.getByLabel('路口编辑工作区').getByText('展开单独定制', { exact: true }).first()).toBeVisible();
await expect(page.getByLabel('路口编辑工作区').getByText('进口收口距离', { exact: true })).toBeVisible();
await expect(
page
.getByLabel('路口编辑工作区')
.getByText(/进口宽度/)
.first(),
).toBeVisible();
});
test('discards junction drafts and lists each approach road once', async ({ page }) => {
const state = await page.request.get('/api/state').then((response) => response.json());
const nodeCounts = new Map<string, number>();
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 page.getByRole('button', { name: '开启编辑模式' }).click();
const roadsText = await page.locator('.junction-workspace__body dd').nth(1).innerText();
const roads = roadsText
.split('、')
.map((road) => road.trim())
.filter(Boolean);
expect(new Set(roads).size).toBe(roads.length);
const input = page.getByLabel('路口编辑工作区').locator('input').first();
const original = Number(await input.inputValue());
await input.fill(String(original + 0.1));
await expect(page.getByRole('button', { name: '放弃草稿' })).toBeEnabled();
await page.getByRole('button', { name: '放弃草稿' }).click();
await expect(input).toHaveValue(String(original));
await page.getByRole('button', { name: '展开单独定制' }).first().click();
const detailRows = page.locator('.parameter-group__details .parameter');
await expect(detailRows.first()).toBeVisible();
await expect(detailRows.first().locator('.parameter-label')).toContainText(/进口/);
});
test('opens a junction signal from the local facility list without exposing delete', async ({ page }) => {
const state = await page.request.get('/api/state').then((response) => response.json());
const nodeCounts = new Map<string, number>();
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 page.getByRole('button', { name: '开启编辑模式' }).click();
const signalItem = page
.getByLabel('已有设施')
.getByRole('button', { name: /信号灯/ })
.first();
if ((await signalItem.count()) === 0) return;
await signalItem.click();
const signalSelect = page.getByLabel('检查信号灯');
await expect(signalSelect).toHaveValue(/.+/);
await expect(page.getByRole('button', { name: '删除信号灯' })).toHaveCount(0);
await expect(page.getByText('当前选中:信号灯。可在下方参数中调整。')).toBeVisible();
const heading = page.getByLabel('横杆方向(度)');
const original = await heading.inputValue();
await heading.fill(String((Number(original) + 15) % 360));
await expect(page.getByRole('button', { name: '放弃草稿' })).toBeEnabled();
await page.getByRole('button', { name: '放弃草稿' }).click();
await expect(heading).toHaveValue(original);
});
test('keeps complex junction structure and map selection linked across reload', async ({ page }) => {
const initial = await page.request.get('/api/state').then((response) => response.json());
const candidate = initial.debug.junctionCandidates.find((item: { nodeIds: string[] }) => item.nodeIds.length >= 3);
expect(candidate).toBeTruthy();
const acceptedResponse = await page.request.post('/api/junction-clusters', {
data: { candidateId: candidate.id, nodeIds: candidate.nodeIds },
});
expect(acceptedResponse.ok()).toBe(true);
const accepted = await acceptedResponse.json();
const id = accepted.added.id;
const structure = accepted.junctionStructures[`cluster:${id}`];
expect(structure.arms.length).toBeGreaterThanOrEqual(3);
await page.goto(`/junction/cluster/${encodeURIComponent(id)}`);
const workspace = page.getByLabel('路口编辑工作区');
await expect(workspace).toBeVisible();
const armButtons = workspace.locator('.junction-workspace__object-list').first().getByRole('button');
await expect(armButtons).toHaveCount(structure.arms.length);
const map = await page
.getByLabel('道路地图')
.evaluateHandle((element) => (element as HTMLDivElement & { __roadWorkbenchMap: OLMap }).__roadWorkbenchMap);
await armButtons.first().click();
await expect(armButtons.first()).toHaveAttribute('aria-pressed', 'true');
await expect
.poll(async () =>
page.getByLabel('道路地图').evaluate((element) => {
const map = (element as HTMLDivElement & { __roadWorkbenchMap: OLMap }).__roadWorkbenchMap;
const layer = map
.getLayers()
.getArray()
.find((item) => item.getZIndex() === 89) as VectorLayer<VectorSource> | undefined;
return layer?.getSource()?.getFeatures().length || 0;
}),
)
.toBeGreaterThan(0);
const sameMap = await page
.getByLabel('道路地图')
.evaluate(
(element, original) =>
(element as HTMLDivElement & { __roadWorkbenchMap: OLMap }).__roadWorkbenchMap === original,
map,
);
expect(sameMap).toBe(true);
await expect(workspace.getByText('路口参数', { exact: true })).toHaveCount(0);
const scoped = await page.request
.get(`/api/edit-state?cluster=${encodeURIComponent(id)}`)
.then((response) => response.json());
expect(scoped.handles.handles).toEqual([]);
expect(scoped.junctionStructure.topologyFingerprint).toBe(structure.topologyFingerprint);
await page.reload();
await expect(armButtons).toHaveCount(structure.arms.length);
await expect(armButtons.first()).toHaveAttribute('aria-pressed', 'false');
const refreshed = await page.request.get('/api/state').then((response) => response.json());
expect(refreshed.junctionStructures[`cluster:${id}`].arms.map((arm: { key: string }) => arm.key)).toEqual(
structure.arms.map((arm: { key: string }) => arm.key),
);
});
});