Files
admin 0a3f2c5cc6 v1.3.13: 模块化注册架构 + PDS流程设计套件更名 + 依赖/圆周交互修复
- 模块化:EdcModule 契约 + registry + 模块独立目录;发布包增加 modules/registry.json
- 更名:PDS流程设计套件 / ProcessDesignSuite;同步构建、发布脚本与文档
- 时序图:依赖偏移以时间显示/编辑;紧贴依赖隐藏箭头并在接缝显示标签;动作条磁吸
- 圆周:依赖标签沿圆弧方向、环间空隙;动作旋转磁吸增强;短弧标签优先动作名
- 清理:release 移至源码同级(本地不入库);tool 合并到 tools;发布说明迁移 docs/release-notes
2026-11-22 10:02:54 +08:00

1081 lines
39 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 架构图模块测试:手绘识别(直线 / 箭头 / 矩形 / 圆 / 椭圆 / 三角形 / 菱形)、
// 数据模型 JSON 往返、示例场景完整性。
import 'dart:convert';
import 'dart:math' as math;
import 'package:flutter/material.dart' hide Action;
import 'package:flutter_test/flutter_test.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_models.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_painter.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_recognizer.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_samples.dart';
import 'package:sequence_diagram/model/models.dart';
import 'package:sequence_diagram/state/app_controller.dart';
void main() {
group('手绘识别', () {
test('直线:接近水平 → 吸附为水平线', () {
final ArchRecognition? r = recognizeArchStroke(
sketchLine(const Offset(20, 100), const Offset(220, 92), seed: 3),
);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.line);
expect(r.arrow, isFalse);
expect((r.a!.dy - r.b!.dy).abs(), lessThan(0.001));
});
test('直线:斜线保持角度', () {
final ArchRecognition? r = recognizeArchStroke(
sketchLine(const Offset(20, 20), const Offset(200, 160), seed: 4),
);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.line);
expect(r.arrow, isFalse);
expect(r.b!.dx - r.a!.dx, greaterThan(150));
});
test('箭头:箭杆 + 尖端折返 → 识别为箭头', () {
final ArchRecognition? r = recognizeArchStroke(
sketchArrow(const Offset(20, 140), const Offset(240, 60), seed: 5),
);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.line);
expect(r.arrow, isTrue);
});
test('矩形:随手画的方块 → 扶正为矩形', () {
const Rect target = Rect.fromLTWH(30, 40, 160, 110);
final List<Offset> pts = sketchPolyline(
<Offset>[
target.topLeft,
target.topRight,
target.bottomRight,
target.bottomLeft,
target.topLeft + const Offset(12, 0),
],
seed: 6,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.rect);
expect((r.rect.left - target.left).abs(), lessThan(6));
expect((r.rect.width - target.width).abs(), lessThan(8));
expect((r.rect.height - target.height).abs(), lessThan(8));
});
test('圆形:随手画的圆 → 扶正为圆(宽高一致)', () {
final List<Offset> pts = sketchEllipse(
Rect.fromCircle(center: const Offset(100, 100), radius: 70),
seed: 7,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.circle);
expect((r.rect.width - r.rect.height).abs(), lessThan(0.01));
});
test('椭圆:长宽比保留', () {
const Rect target = Rect.fromLTWH(20, 40, 200, 100);
final ArchRecognition? r = recognizeArchStroke(sketchEllipse(target, seed: 8));
expect(r, isNotNull);
expect(r!.type, ArchShapeType.ellipse);
expect(r.rect.width / r.rect.height, greaterThan(1.6));
});
test('三角形:顶点朝上', () {
const Rect target = Rect.fromLTWH(30, 30, 160, 120);
final List<Offset> pts = sketchPolyline(
<Offset>[
Offset(target.center.dx, target.top),
target.bottomRight,
target.bottomLeft,
Offset(target.center.dx + 10, target.top),
],
seed: 9,
jitter: 1.4,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.triangle);
expect(r.dir, 0);
});
test('菱形', () {
const Rect target = Rect.fromLTWH(30, 40, 160, 120);
final List<Offset> pts = sketchPolyline(
<Offset>[
Offset(target.center.dx, target.top),
Offset(target.right, target.center.dy),
Offset(target.center.dx, target.bottom),
Offset(target.left, target.center.dy),
Offset(target.center.dx + 10, target.top),
],
seed: 10,
jitter: 1.4,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.diamond);
});
test('折线:L 形 → 识别为折线并扶正水平 / 垂直段', () {
final ArchRecognition? r = recognizeArchStroke(sketchPolyline(
<Offset>[const Offset(20, 20), const Offset(180, 30), const Offset(190, 160)],
seed: 11,
));
expect(r, isNotNull);
expect(r!.type, ArchShapeType.polyline);
final List<Offset> pts = r.pts!;
expect(pts.length, 3);
expect((pts[1].dy - pts[0].dy).abs(), lessThan(0.001));
expect((pts[2].dx - pts[1].dx).abs(), lessThan(0.001));
});
test('连线轨迹简化:多段折线 + 近轴段扶正', () {
final List<Offset> pts = sketchPolyline(
<Offset>[
const Offset(20, 20),
const Offset(180, 30),
const Offset(190, 160)
],
seed: 11,
);
final List<Offset> v = simplifyArchPolyline(pts);
expect(v.length, 3);
expect((v[1].dy - v[0].dy).abs(), lessThan(0.001));
expect((v[2].dx - v[1].dx).abs(), lessThan(0.001));
});
test('曲线:平滑 S 形 → 识别为曲线', () {
final ArchRecognition? r = recognizeArchStroke(
sketchCurve(const Offset(30, 130), const Offset(200, 60), seed: 12),
);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.curve);
expect(r.pts!.length, greaterThanOrEqualTo(3));
});
test('云:多鼓包闭合曲线 → 识别为云', () {
const Rect rect = Rect.fromLTWH(40, 40, 200, 130);
final ArchShape proto =
ArchShape(id: 'p', type: ArchShapeType.cloud, rect: rect);
final List<Offset> pts = archInkOutline(
archShapePath(proto),
seed: 21,
jitter: 1.6,
gapFrac: 0.06,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.cloud);
});
test('圆柱:柱体 + 顶盖一笔画 → 识别为圆柱', () {
const Rect rect = Rect.fromLTWH(40, 40, 150, 150);
final ArchShape proto =
ArchShape(id: 'p', type: ArchShapeType.cylinder, rect: rect);
final List<Offset> pts = archInkOutline(
archShapePath(proto),
seed: 22,
jitter: 1.5,
gapFrac: 0.05,
);
final ArchRecognition? r = recognizeArchStroke(pts);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.cylinder);
});
test('两笔组合:分开两笔画的矩形 → 合并识别为矩形', () {
const Rect target = Rect.fromLTWH(40, 40, 160, 110);
final List<Offset> a = sketchPolyline(
<Offset>[target.topLeft, target.topRight, target.bottomRight],
seed: 31,
);
final List<Offset> b = sketchPolyline(
<Offset>[target.bottomRight, target.bottomLeft, target.topLeft],
seed: 32,
);
final ArchRecognition? r = recognizeArchStrokes(<List<Offset>>[a, b]);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.rect);
});
test('圆柱拼接:顶盖椭圆 + 柱体矩形 → 圆柱', () {
const ArchRecognition lid = ArchRecognition(
type: ArchShapeType.ellipse,
rect: Rect.fromLTWH(40, 40, 140, 56),
);
const ArchRecognition body = ArchRecognition(
type: ArchShapeType.rect,
rect: Rect.fromLTWH(40, 68, 140, 110),
);
final ArchRecognition? r = archCylinderFromRecognitions(body, lid);
expect(r, isNotNull);
expect(r!.type, ArchShapeType.cylinder);
expect(r.rect.top, 40);
expect(archCylinderFromRecognitions(lid, lid), isNull);
});
test('对照表样本全部识别成功', () {
for (final (String, List<Offset>) item in archRecognitionSheet()) {
final ArchRecognition? r = recognizeArchStroke(item.$2);
expect(r, isNotNull, reason: '样本「${item.$1}」应被识别');
}
});
});
group('架构图模型', () {
test('JSON 往返:图形 / 墨迹 / 连线 / 视口', () {
final ArchDoc doc = ArchDoc(
shapes: <ArchShape>[
const ArchShape(
id: 'S1',
type: ArchShapeType.roundRect,
rect: Rect.fromLTWH(10, 20, 120, 60),
radius: 12,
fill: 0x264CC9F0,
stroke: 0xFF4CC9F0,
text: 'PLC',
),
const ArchShape(
id: 'S2',
type: ArchShapeType.line,
rect: Rect.fromLTWH(0, 0, 100, 50),
a: Offset(0, 0),
b: Offset(100, 50),
arrow: true,
),
],
strokes: <ArchStroke>[
const ArchStroke(
id: 'K1',
pts: <Offset>[Offset(1, 2), Offset(3, 4)],
color: 0xFF64748B,
),
],
connectors: <ArchConnector>[
const ArchConnector(
id: 'C1',
fromId: 'S1',
from: Offset(0, 0),
toId: 'S2',
to: Offset(1, 1),
label: 'EtherCAT',
dash: true,
),
],
zoom: 0.8,
panX: 12,
panY: -5,
);
final ArchDoc back = ArchDoc.fromJson(
jsonDecode(jsonEncode(doc.toJson())) as Map<String, dynamic>,
);
expect(back.shapes.length, 2);
expect(back.shapes[0].type, ArchShapeType.roundRect);
expect(back.shapes[0].radius, 12);
expect(back.shapes[0].text, 'PLC');
expect(back.shapes[1].arrow, isTrue);
expect(back.strokes.single.pts.length, 2);
expect(back.connectors.single.label, 'EtherCAT');
expect(back.connectors.single.dash, isTrue);
expect(back.zoom, 0.8);
expect(back.panX, 12);
expect(back.panY, -5);
});
test('工程文件往返保留架构图(暂不与时序图互通)', () {
final Project p = Project.demo().copyWith(arch: archDemoDoneScene());
final Project back = Project.decode(p.encode());
expect(back.arch.shapes.length, p.arch.shapes.length);
expect(back.arch.connectors.length, p.arch.connectors.length);
expect(back.actions.length, p.actions.length);
expect(back.name, p.name);
});
test('JSON 往返:走线方式 / 网格 / 折线点', () {
final ArchDoc doc = ArchDoc(
shapes: <ArchShape>[
const ArchShape(
id: 'S1',
type: ArchShapeType.polyline,
rect: Rect.fromLTWH(0, 0, 100, 60),
fill: 0,
stroke: 0xFF4CC9F0,
pts: <Offset>[Offset(0, 0), Offset(60, 0), Offset(60, 60)],
),
],
connectors: <ArchConnector>[
const ArchConnector(
id: 'C1',
from: Offset(0, 0),
to: Offset(80, 80),
route: ArchRoute.ortho,
),
],
gridSnap: false,
gridSize: 40,
);
final ArchDoc back = ArchDoc.fromJson(
jsonDecode(jsonEncode(doc.toJson())) as Map<String, dynamic>,
);
expect(back.connectors.single.route, ArchRoute.ortho);
expect(back.gridSnap, isFalse);
expect(back.gridSize, 40);
expect(back.shapes.single.type, ArchShapeType.polyline);
expect(back.shapes.single.pts.length, 3);
});
test('连线折点:JSON 往返 + 几何经过折点', () {
final ArchDoc doc = ArchDoc(
connectors: <ArchConnector>[
const ArchConnector(
id: 'C1',
from: Offset(0, 0),
to: Offset(100, 0),
mids: <Offset>[Offset(40, 0), Offset(40, 60)],
),
],
);
final ArchDoc back = ArchDoc.fromJson(
jsonDecode(jsonEncode(doc.toJson())) as Map<String, dynamic>,
);
expect(back.connectors.single.mids.length, 2);
final ArchConnGeometry g =
archConnectorGeometry(back, back.connectors.single);
// 正交走线:路径经过两个折点(顺序保持),所有段横平竖直。
expect(g.pts.length, greaterThanOrEqualTo(4));
expect(g.pts[1], const Offset(40, 0));
expect(g.pts[2], const Offset(40, 60));
expect(g.pts.first, const Offset(0, 0));
expect(g.pts.last, const Offset(100, 0));
for (int i = 0; i < g.pts.length - 1; i++) {
final Offset d = g.pts[i + 1] - g.pts[i];
expect(d.dx.abs() < 0.01 || d.dy.abs() < 0.01, isTrue,
reason: '第 $i 段不是横/竖:$d');
}
});
test('连线吸附:开始 / 结束吸附到最近边缘点', () {
const Rect rr = Rect.fromLTWH(100, 100, 200, 100);
final ArchShape rect =
ArchShape(id: 'S', type: ArchShapeType.rect, rect: rr);
// 外部点 clamp 到最近边。
expect(archNearestBorderPoint(rect, const Offset(350, 140)),
const Offset(300, 140));
// 内部点投影到最近一条边。
final Offset inside = archNearestBorderPoint(rect, const Offset(200, 130));
final bool onBorder = (inside.dx - rr.left).abs() < 0.01 ||
(inside.dx - rr.right).abs() < 0.01 ||
(inside.dy - rr.top).abs() < 0.01 ||
(inside.dy - rr.bottom).abs() < 0.01;
expect(onBorder, isTrue);
// 椭圆按径向投影到边界。
final ArchShape ell = ArchShape(
id: 'E',
type: ArchShapeType.ellipse,
rect: const Rect.fromLTWH(0, 0, 200, 100));
expect(archNearestBorderPoint(ell, const Offset(300, 50)),
const Offset(200, 50));
});
test('移动吸附:磁力对齐线与网格回退', () {
const Rect a = Rect.fromLTWH(0, 0, 100, 60);
const Rect b = Rect.fromLTWH(200, 0, 100, 60);
final ArchDoc doc = ArchDoc(
shapes: <ArchShape>[
const ArchShape(id: 'A', type: ArchShapeType.rect, rect: a),
const ArchShape(id: 'B', type: ArchShapeType.rect, rect: b),
],
gridSnap: false,
);
// 拖 B:左边缘离 A 的右边缘 3px(tol=6)→ 磁力吸附对齐 + 导引线。
final ArchMoveSnap snap = archSnapMoveDelta(
doc: doc,
movingIds: <String>{'B'},
union: b.shift(const Offset(-97, 0)), // 左缘 103,靠近 A 右缘 100(差 3px)
rawDelta: const Offset(-97, 0),
tol: 6,
gridSnap: false,
);
expect(snap.guideX, 100);
expect(snap.delta.dx, -100);
// 网格回退:无对齐时按网格吸附主选中图形。
final ArchDoc grid = doc.copyWith(gridSnap: true, gridSize: 20);
final ArchMoveSnap g2 = archSnapMoveDelta(
doc: grid,
movingIds: <String>{'B'},
union: b.shift(const Offset(103, 7)),
rawDelta: const Offset(103, 7),
tol: 6,
gridSnap: true,
);
expect(g2.delta.dx, 100);
expect(g2.delta.dy, 0);
});
test('正交 / 极轴:位移约束', () {
expect(
archConstrainDelta(
raw: const Offset(30, 10), ortho: true, polar: false, polarDeg: 15),
const Offset(30, 0),
);
expect(
archConstrainDelta(
raw: const Offset(10, 30), ortho: true, polar: false, polarDeg: 15),
const Offset(0, 30),
);
final Offset p = archConstrainDelta(
raw: const Offset(100, 96.6), ortho: false, polar: true, polarDeg: 15);
final double ang = math.atan2(p.dy, p.dx) * 180 / math.pi;
expect((ang - 45).abs(), lessThan(0.01));
expect(
(p.distance - math.sqrt(100 * 100 + 96.6 * 96.6)).abs(),
lessThan(0.01),
);
});
test('图层:JSON 往返 / 可见性 / 内容范围', () {
final ArchDoc doc = ArchDoc(
layers: <ArchLayer>[
const ArchLayer(id: 'L0', name: '默认', color: 0xFF4CC9F0),
const ArchLayer(
id: 'LY2',
name: '电控',
color: 0xFF34D399,
visible: false,
locked: true),
],
shapes: <ArchShape>[
const ArchShape(
id: 'S1',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 60),
layerId: 'L0'),
const ArchShape(
id: 'S2',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(500, 0, 100, 60),
layerId: 'LY2'),
],
);
final ArchDoc back = ArchDoc.fromJson(
jsonDecode(jsonEncode(doc.toJson())) as Map<String, dynamic>,
);
expect(back.layers.length, 2);
expect(back.layerById('LY2').visible, isFalse);
expect(back.isLayerLocked('LY2'), isTrue);
expect(back.isLayerVisible('L0'), isTrue);
// 隐藏图层不计入内容范围。
expect(back.contentBounds().right, lessThan(200));
});
test('图层归属:新增对象自动进入活动图层', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.addArchLayer();
final String active = c.arch.activeLayer;
c.addArchShape(const ArchShape(
id: 'S9',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 10, 10),
));
expect(c.arch.shapeById('S9')!.layerId, active);
});
test('removeIds 连带删除吸附在图形上的连线', () {
final ArchDoc doc = archDemoDoneScene().removeIds(<String>{'S4'});
expect(doc.shapeById('S4'), isNull);
expect(
doc.connectors
.any((ArchConnector c) => c.fromId == 'S4' || c.toId == 'S4'),
isFalse,
);
});
test('连线端点:吸附图形时实时求边框交点', () {
final ArchDoc doc = archDemoDoneScene();
final ArchConnector c =
doc.connectors.firstWhere((ArchConnector x) => x.id == 'C4');
final (Offset a, Offset b) = archConnectorEnds(doc, c);
final ArchShape s5 = doc.shapeById('S5')!;
// 终点应落在视觉相机(S5)的边框上。
expect(s5.rect.inflate(2).contains(b), isTrue);
expect((a - b).distance, greaterThan(10));
});
});
group('示例场景', () {
test('手绘稿全部为墨迹(等待识别)', () {
final ArchDoc ink = archDemoInkScene();
expect(ink.strokes.length, greaterThan(10));
expect(ink.shapes, isEmpty);
expect(ink.connectors, isEmpty);
});
test('完成图含图形 / 连线 / 标题', () {
final ArchDoc done = archDemoDoneScene();
expect(done.shapes.length, greaterThan(8));
expect(done.connectors.length, 9);
expect(done.shapes.any((ArchShape s) => s.text.contains('系统架构')), isTrue);
});
});
group('对齐 / 均布', () {
AppController makeController() {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.addArchShape(const ArchShape(
id: 'A1',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 60)));
c.addArchShape(const ArchShape(
id: 'A2',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(220, 90, 60, 40)));
c.addArchShape(const ArchShape(
id: 'A3',
type: ArchShapeType.circle,
rect: Rect.fromLTWH(500, 300, 80, 80)));
c.selectArchIds(<String>['A1', 'A2', 'A3']);
return c;
}
test('左对齐 / 顶对齐:全部对齐到选中范围边界', () {
final AppController c = makeController();
c.alignArchSelection(ArchAlign.left);
final double left = c.arch.shapeById('A1')!.bounds.left;
for (final String id in <String>['A2', 'A3']) {
expect(c.arch.shapeById(id)!.bounds.left, closeTo(left, 0.001));
}
c.alignArchSelection(ArchAlign.top);
final double top = c.arch.shapeById('A1')!.bounds.top;
for (final String id in <String>['A2', 'A3']) {
expect(c.arch.shapeById(id)!.bounds.top, closeTo(top, 0.001));
}
});
test('横向 / 纵向均布:中心等距', () {
final AppController c = makeController();
c.alignArchSelection(ArchAlign.distributeH);
final List<double> cx = <double>[
for (final String id in <String>['A1', 'A2', 'A3'])
c.arch.shapeById(id)!.bounds.center.dx,
];
expect((cx[1] - cx[0]) - (cx[2] - cx[1]), closeTo(0, 0.001));
c.alignArchSelection(ArchAlign.distributeV);
final List<double> cy = <double>[
for (final String id in <String>['A1', 'A2', 'A3'])
c.arch.shapeById(id)!.bounds.center.dy,
];
expect((cy[1] - cy[0]) - (cy[2] - cy[1]), closeTo(0, 0.001));
});
test('数量不足:仅提示不改动', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.addArchShape(const ArchShape(
id: 'B1',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 40, 40)));
c.addArchShape(const ArchShape(
id: 'B2',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(80, 80, 40, 40)));
c.selectArchIds(<String>['B1', 'B2']);
final Rect before = c.arch.shapeById('B2')!.bounds;
c.alignArchSelection(ArchAlign.distributeH);
expect(c.status, contains('均布'));
expect(c.arch.shapeById('B2')!.bounds, before);
});
});
group('分文件保存', () {
test('时序图 JSON 不含架构图部分;架构图独立序列化往返', () {
final Project p = Project.demo().copyWith(arch: archDemoDoneScene());
final Map<String, dynamic> seq =
jsonDecode(p.encode(includeArch: false)) as Map<String, dynamic>;
final Map<String, dynamic> body = seq['project'] as Map<String, dynamic>;
expect(body.containsKey('arch'), isFalse);
// 默认(含架构图)仍写入:自动快照 / 旧文件兼容继续可用。
final Map<String, dynamic> full =
jsonDecode(p.encode()) as Map<String, dynamic>;
expect(
(full['project'] as Map<String, dynamic>).containsKey('arch'), isTrue);
// 独立架构图文档往返。
final ArchDoc back = ArchDoc.decode(p.arch.encode());
expect(back.shapes.length, p.arch.shapes.length);
expect(back.connectors.length, p.arch.connectors.length);
expect(back.layers.length, p.arch.layers.length);
});
});
group('连线端点重吸附', () {
test('端点可换绑到其它图形(吸附边框点)或脱离为自由端点', () {
final ArchDoc doc = archDemoDoneScene();
final ArchConnector c4 =
doc.connectors.firstWhere((ArchConnector x) => x.id == 'C4');
final ArchShape s5 = doc.shapeById('S5')!;
// 换绑:起点吸附到 S5 的边框。
final Offset border = archNearestBorderPoint(s5, s5.rect.center);
final ArchConnector moved = c4.copyWith(fromId: 'S5', from: border);
expect(moved.fromId, 'S5');
expect(s5.rect.inflate(2).contains(moved.from), isTrue);
// 脱离:显式置 null(哨兵语义),位置保留。
final ArchConnector free = c4.copyWith(fromId: null);
expect(free.fromId, isNull);
expect(free.from, c4.from);
});
});
group('IT 图元', () {
test('新增图元:标签 / 默认尺寸 / 路径 / JSON 往返', () {
for (final ArchShapeType type in <ArchShapeType>[
ArchShapeType.server,
ArchShapeType.client,
ArchShapeType.mobile,
ArchShapeType.firewall,
ArchShapeType.netSwitch,
ArchShapeType.queue,
ArchShapeType.symbol,
]) {
expect(archShapeTypeLabel(type).isNotEmpty, isTrue);
final Size d = archDefaultSize(type);
expect(d.width, greaterThan(40), reason: archShapeTypeLabel(type));
final ArchShape s = ArchShape(
id: 'X1',
type: type,
rect: const Rect.fromLTWH(0, 0, 160, 100),
stroke: 0xFF4CC9F0,
sw: 1.8,
);
final Path path = archShapePath(s);
expect(path.getBounds().isEmpty, isFalse,
reason: archShapeTypeLabel(type));
final ArchShape back = ArchShape.fromJson(s.toJson());
expect(back.type, type);
}
});
});
group('默认画布', () {
test('默认画布 A0:空文档内容边界 1189×841', () {
final Rect b = ArchDoc.empty.contentBounds();
expect(b.width, 1189);
expect(b.height, 841);
});
});
group('正交连线(一律横平竖直)', () {
test('orthoPolyline:斜段拆成横竖台阶且端点保持', () {
final List<Offset> out = orthoPolyline(<Offset>[
const Offset(0, 0),
const Offset(100, 50),
]);
expect(out.length, greaterThanOrEqualTo(3));
for (int i = 0; i < out.length - 1; i++) {
final Offset d = out[i + 1] - out[i];
expect(d.dx.abs() < 0.01 || d.dy.abs() < 0.01, isTrue,
reason: '第 $i 段不是横/竖:$d');
}
expect(out.first, const Offset(0, 0));
expect(out.last, const Offset(100, 50));
});
test('连线几何:无折点自动单拐角;有折点按折点走台阶', () {
const ArchShape a = ArchShape(
id: 'A',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 80, 60),
);
const ArchShape b = ArchShape(
id: 'B',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(300, 200, 80, 60),
);
final ArchDoc doc =
ArchDoc.empty.copyWith(shapes: <ArchShape>[a, b]);
final ArchConnector c1 = ArchConnector(
id: 'C1',
fromId: 'A',
from: a.rect.center,
toId: 'B',
to: b.rect.center,
);
final ArchConnGeometry g1 = archConnectorGeometry(doc, c1);
for (int i = 0; i < g1.pts.length - 1; i++) {
final Offset d = g1.pts[i + 1] - g1.pts[i];
expect(d.dx.abs() < 0.01 || d.dy.abs() < 0.01, isTrue,
reason: '无折点第 $i 段不是横/竖');
}
final ArchConnector c2 =
c1.copyWith(mids: <Offset>[const Offset(150, 90)]);
final ArchConnGeometry g2 = archConnectorGeometry(doc, c2);
for (int i = 0; i < g2.pts.length - 1; i++) {
final Offset d = g2.pts[i + 1] - g2.pts[i];
expect(d.dx.abs() < 0.01 || d.dy.abs() < 0.01, isTrue,
reason: '有折点第 $i 段不是横/竖');
}
final double dmin = g2.pts
.map((Offset p) => (p - const Offset(150, 90)).distance)
.reduce((double x, double y) => x < y ? x : y);
expect(dmin, lessThan(2), reason: '路径应经过拖拽的角点');
});
test('端点捕捉稳定:按保存的画线侧取边框点(不随中心方向乱跳)', () {
const ArchShape a = ArchShape(
id: 'A',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 100),
);
const ArchShape b = ArchShape(
id: 'B',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(300, 140, 100, 100),
);
final ArchDoc doc =
ArchDoc.empty.copyWith(shapes: <ArchShape>[a, b]);
final ArchConnector c1 = ArchConnector(
id: 'C1',
fromId: 'A',
from: const Offset(0, 50),
toId: 'B',
to: const Offset(350, 190),
);
// 从 A 的左侧画线:置左边中点 → 起点应留在左侧(目标在右下也不跳)。
final ArchConnGeometry g1 = archConnectorGeometry(doc, c1);
expect(g1.pts.first.dx, closeTo(0, 1));
// 从 A 的右侧画线:置右边中点 → 起点应留在右侧。
final ArchConnGeometry g2 =
archConnectorGeometry(doc, c1.copyWith(from: const Offset(100, 50)));
expect(g2.pts.first.dx, closeTo(100, 1));
// 保存端点为图形中心(程序化创建):退回朝向对方,且落在边框上。
final ArchConnGeometry g3 =
archConnectorGeometry(doc, c1.copyWith(from: a.rect.center));
final Offset p = g3.pts.first;
final bool onBorder = (p.dx - a.rect.left).abs() < 1 ||
(p.dx - a.rect.right).abs() < 1 ||
(p.dy - a.rect.top).abs() < 1 ||
(p.dy - a.rect.bottom).abs() < 1;
expect(onBorder, isTrue);
});
});
group('连线法向与自动避让', () {
test('水平相邻:出口水平、箭头垂直射入目标左边', () {
const ArchShape a = ArchShape(
id: 'A',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 80));
const ArchShape b = ArchShape(
id: 'B',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(300, 40, 100, 80));
final ArchDoc doc =
ArchDoc.empty.copyWith(shapes: <ArchShape>[a, b]);
final ArchConnector conn = ArchConnector(
id: 'C1',
fromId: 'A',
from: a.rect.center,
toId: 'B',
to: b.rect.center,
);
final ArchConnGeometry g = archConnectorGeometry(doc, conn);
expect(g.start.dx, closeTo(100, 0.5));
expect(g.end.dx, closeTo(300, 0.5));
expect((g.pts[1].dy - g.start.dy).abs(), lessThan(0.5),
reason: '出口应沿法向(水平)');
final Offset lastD = g.end - g.pts[g.pts.length - 2];
expect(lastD.dy.abs(), lessThan(0.5), reason: '箭头应水平射入');
expect(lastD.dx, greaterThan(0));
});
test('垂直相邻:出口垂直、箭头垂直射入目标上边', () {
const ArchShape a = ArchShape(
id: 'A',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 80));
const ArchShape b = ArchShape(
id: 'B',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(60, 300, 100, 80));
final ArchDoc doc =
ArchDoc.empty.copyWith(shapes: <ArchShape>[a, b]);
final ArchConnector conn = ArchConnector(
id: 'C1',
fromId: 'A',
from: a.rect.center,
toId: 'B',
to: b.rect.center,
);
final ArchConnGeometry g = archConnectorGeometry(doc, conn);
expect(g.start.dy, closeTo(80, 0.5));
expect(g.end.dy, closeTo(300, 0.5));
expect((g.pts[1].dx - g.start.dx).abs(), lessThan(0.5),
reason: '出口应沿法向(垂直)');
final Offset lastD = g.end - g.pts[g.pts.length - 2];
expect(lastD.dx.abs(), lessThan(0.5), reason: '箭头应垂直射入');
expect(lastD.dy, greaterThan(0));
});
test('多条连线贴同一条边:自动错开(槽位避让)', () {
const ArchShape hub = ArchShape(
id: 'H',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 240));
const ArchShape b1 = ArchShape(
id: 'B1',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(320, 0, 80, 60));
const ArchShape b2 = ArchShape(
id: 'B2',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(320, 90, 80, 60));
const ArchShape b3 = ArchShape(
id: 'B3',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(320, 180, 80, 60));
final List<ArchConnector> conns = <ArchConnector>[
ArchConnector(
id: 'C1',
fromId: 'H',
from: hub.rect.center,
toId: 'B1',
to: b1.rect.center),
ArchConnector(
id: 'C2',
fromId: 'H',
from: hub.rect.center,
toId: 'B2',
to: b2.rect.center),
ArchConnector(
id: 'C3',
fromId: 'H',
from: hub.rect.center,
toId: 'B3',
to: b3.rect.center),
];
final ArchDoc doc = ArchDoc.empty.copyWith(
shapes: <ArchShape>[hub, b1, b2, b3],
connectors: conns,
);
final List<double> ys = <double>[
for (final ArchConnector cn in conns)
archConnectorGeometry(doc, cn).start.dy,
];
// 起点互不相同、均落在 hub 右边框内,并保持自然顺序。
expect(ys[0], lessThan(ys[1]));
expect(ys[1], lessThan(ys[2]));
for (final double y in ys) {
expect(y, greaterThan(0));
expect(y, lessThan(240));
}
for (final ArchConnector cn in conns) {
final ArchConnGeometry g = archConnectorGeometry(doc, cn);
expect(g.start.dx, closeTo(100, 0.5), reason: '起点应贴 hub 右边框');
}
});
test('带折点:最后一段仍沿法向射入目标边', () {
const ArchShape a = ArchShape(
id: 'A',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 80));
const ArchShape b = ArchShape(
id: 'B',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(300, 200, 100, 80));
final ArchDoc doc =
ArchDoc.empty.copyWith(shapes: <ArchShape>[a, b]);
final ArchConnector conn = ArchConnector(
id: 'C1',
fromId: 'A',
from: a.rect.center,
toId: 'B',
to: b.rect.center,
mids: const <Offset>[Offset(150, 90)],
);
final ArchConnGeometry g = archConnectorGeometry(doc, conn);
final Offset lastD = g.end - g.pts[g.pts.length - 2];
expect(lastD.dx.abs() < 0.5 || lastD.dy.abs() < 0.5, isTrue,
reason: '最后一段应横平竖直(法向)');
const Rect br = Rect.fromLTWH(300, 200, 100, 80);
final bool onBorder = (g.end.dx - br.left).abs() < 0.5 ||
(g.end.dx - br.right).abs() < 0.5 ||
(g.end.dy - br.top).abs() < 0.5 ||
(g.end.dy - br.bottom).abs() < 0.5;
expect(onBorder, isTrue, reason: '终点应落在目标边框上');
});
});
group('橡皮擦', () {
test('连续擦除计入一次撤销', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.addArchShape(const ArchShape(
id: 'S1',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(0, 0, 100, 60),
));
c.addArchShape(const ArchShape(
id: 'S2',
type: ArchShapeType.rect,
rect: Rect.fromLTWH(200, 0, 100, 60),
));
c.beginArchErase();
expect(c.eraseArchIds(<String>{'S1'}), 1);
expect(c.eraseArchIds(<String>{'S2'}), 1);
expect(c.arch.shapes, isEmpty);
c.undo();
expect(c.arch.shapes.length, 2, reason: '一次连续擦除应整步回退');
});
});
group('手绘识别模式', () {
test('默认自动(桌面);可切换手动确认', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
expect(c.archAutoRecognize, isTrue); // Windows 测试环境
c.setArchAutoRecognize(false);
expect(c.archAutoRecognize, isFalse);
expect(c.status, contains('手动'));
});
test('识别按钮只识别当前墨迹(不载入示例)', () {
final AppController c = AppController(project: Project.demo());
addTearDown(c.dispose);
c.archClear();
final int t0 = c.archPlayToken;
c.archRecognizeInk();
expect(c.archPlayToken, t0, reason: '没有墨迹时不应触发识别');
expect(c.arch.strokes, isEmpty);
expect(c.arch.shapes, isEmpty, reason: '不得载入示例图形');
c.addArchStroke(const ArchStroke(
id: 'K1',
pts: <Offset>[Offset(0, 0), Offset(10, 0), Offset(10, 10)],
));
c.archRecognizeInk();
expect(c.archPlayToken, t0 + 1);
expect(c.arch.strokes.length, 1, reason: '识别前墨迹保持不动');
});
});
}
// ===================== 手绘样本生成(确定性抖动) =====================
double _rnd(math.Random r, double amp) => (r.nextDouble() * 2 - 1) * amp;
Offset _rot(Offset v, double deg) {
final double a = deg * math.pi / 180;
return Offset(v.dx * math.cos(a) - v.dy * math.sin(a),
v.dx * math.sin(a) + v.dy * math.cos(a));
}
/// 直线样本。
List<Offset> sketchLine(Offset a, Offset b, {int seed = 1, double jitter = 1.5}) {
final math.Random rnd = math.Random(seed);
final List<Offset> out = <Offset>[];
final Offset d = b - a;
final double len = d.distance;
final Offset u = d / len;
final Offset n = Offset(-u.dy, u.dx);
for (double t = 0; t <= len - 2; t += 6) {
out.add(a + u * t + n * _rnd(rnd, jitter) + u * _rnd(rnd, jitter * 0.5));
}
out.add(b);
return out;
}
/// 箭头样本:箭杆 + 尖端折返的「>」形头部(同一笔)。
List<Offset> sketchArrow(Offset a, Offset b, {int seed = 1, double jitter = 1.2}) {
final math.Random rnd = math.Random(seed);
final List<Offset> out = <Offset>[];
final Offset d = b - a;
final double len = d.distance;
final Offset u = d / len;
final Offset n = Offset(-u.dy, u.dx);
for (double t = 0; t <= len - 4; t += 7) {
out.add(a + u * t + n * _rnd(rnd, jitter));
}
final double hs = math.min(40, len * 0.18);
final Offset p1 = b + _rot(u, 155) * hs;
final Offset p2 = b + _rot(u, -155) * hs;
final Offset mid = (p1 + p2) / 2;
out.add(b);
for (int i = 1; i <= 4; i++) {
out.add(Offset.lerp(b, p1, i / 4)! + n * _rnd(rnd, jitter * 0.5));
}
for (int i = 1; i <= 4; i++) {
out.add(i <= 2
? Offset.lerp(p1, mid, i / 2)!
: Offset.lerp(mid, p2, (i - 2) / 2)!);
}
return out;
}
/// 折线样本(矩形 / 三角形 / 菱形轮廓)。
List<Offset> sketchPolyline(
List<Offset> pts, {
int seed = 1,
double step = 4,
double jitter = 1.6,
}) {
final math.Random rnd = math.Random(seed);
final List<Offset> out = <Offset>[];
for (int i = 0; i < pts.length - 1; i++) {
final Offset a = pts[i];
final Offset b = pts[i + 1];
final double len = (b - a).distance;
if (len < 0.001) continue;
final Offset u = (b - a) / len;
final Offset n = Offset(-u.dy, u.dx);
final int count = math.max(1, (len / step).floor());
for (int k = 0; k < count; k++) {
out.add(a + u * (len * k / count) + n * _rnd(rnd, jitter) + u * _rnd(rnd, jitter * 0.4));
}
}
out.add(pts.last + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter)));
return out;
}
/// 曲线样本:平滑的三次贝塞尔(带抖动)。
List<Offset> sketchCurve(Offset a, Offset b, {int seed = 1, double jitter = 1.6}) {
final math.Random rnd = math.Random(seed);
final Offset c1 = Offset(a.dx + 50, a.dy - 90);
final Offset c2 = Offset(b.dx - 50, b.dy + 100);
final List<Offset> out = <Offset>[];
const int n = 40;
for (int i = 0; i <= n; i++) {
final double t = i / n;
final double u = 1 - t;
final Offset p = a * (u * u * u) +
c1 * (3 * u * u * t) +
c2 * (3 * u * t * t) +
b * (t * t * t);
out.add(p + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter)));
}
return out;
}
/// 椭圆 / 圆样本(留收笔缺口)。
List<Offset> sketchEllipse(
Rect r, {
int seed = 1,
double jitter = 1.8,
double gapFrac = 0.06,
}) {
final math.Random rnd = math.Random(seed);
final List<Offset> out = <Offset>[];
final int n = 90;
final double stop = math.pi * 2 * (1 - gapFrac);
for (int i = 0; i < n; i++) {
final double a = stop * i / n;
final Offset p = Offset(
r.center.dx + r.width / 2 * math.cos(a),
r.center.dy + r.height / 2 * math.sin(a),
);
out.add(p + Offset(_rnd(rnd, jitter), _rnd(rnd, jitter)));
}
return out;
}