Files

143 lines
6.4 KiB
Dart
Raw Permalink Normal View History

import 'dart:ui' show Rect, Size;
import 'package:flutter_test/flutter_test.dart';
import 'package:sequence_diagram/modules/sequence/layout/flow_layout.dart';
import 'package:sequence_diagram/modules/sequence/layout/layout.dart';
import 'package:sequence_diagram/model/models.dart';
import 'package:sequence_diagram/state/app_controller.dart';
void main() {
group('station management', () {
test('setStations reassigns actions of a removed station', () {
final AppController c = AppController(project: Project.demo());
// 演示工程至少包含两个工位;移除第二个工位后其动作应被重新分配。
expect(c.project.stations.length, greaterThan(1));
final String removed = c.project.stations[1].id;
expect(c.project.actions.any((Action a) => a.stationId == removed), isTrue);
c.setStations(<Station>[c.project.stations.first]);
expect(c.project.stations.length, 1);
expect(c.project.actions.every((Action a) => a.stationId == c.project.stations.first.id), isTrue);
expect(c.canUndo, isTrue);
});
test('setStations ignores an empty list (keep at least one station)', () {
final AppController c = AppController(project: Project.demo());
final int before = c.project.stations.length;
c.setStations(<Station>[]);
expect(c.project.stations.length, before);
});
test('setStations can add and rename stations', () {
final AppController c = AppController(project: Project.demo());
final int before = c.project.stations.length;
final List<Station> next = <Station>[
...c.project.stations,
const Station(id: 'ST9', name: '工位 9', order: 9, color: 0xFF4ADE80),
];
c.setStations(next);
expect(c.project.stations.length, before + 1);
expect(c.project.stationById('ST9')?.name, '工位 9');
});
});
group('空泳道策略', () {
test('保留空泳道:动作移走后泳道仍在,关闭后自动移除', () {
final AppController c = AppController(project: Project.demo());
// 演示工程中 A4 独占 L3(layer 2):移走后该泳道变为空泳道。
c.moveActionLane('A4', 1);
expect(c.project.renderLanes, <int>[0, 1, 2]);
expect(c.project.renderLaneCount, 3);
// 三种视图的布局都包含保留的空泳道。
expect(BarLayout(c.project, const Size(1280, 800)).lanes, <int>[0, 1, 2]);
expect(CircularLayout(c.project, const Size(1280, 800)).lanes, <int>[0, 1, 2]);
// 关闭策略:空泳道移除、序号顺延。
c.setKeepEmptyLanes(false);
expect(c.project.renderLanes, <int>[0, 1]);
// 重新开启:以当前使用中的泳道为保底(不会把已收缩的泳道补回来)。
c.setKeepEmptyLanes(true);
expect(c.project.renderLanes, <int>[0, 1]);
// 策略字段随 JSON 往返保存。
final Project decoded = Project.decode(c.project.encode());
expect(decoded.keepEmptyLanes, isTrue);
expect(decoded.minLaneCount, 2);
});
test('关闭策略时动作移走立即收缩', () {
final AppController c = AppController(project: Project.demo());
c.setKeepEmptyLanes(false);
c.moveActionLane('A4', 1);
expect(c.project.renderLanes, <int>[0, 1]);
});
});
group('泳道槽位(加宽)', () {
test('加宽后动作按槽位分行,几何与反查一致,配置随 JSON 往返', () {
final AppController c = AppController(project: Project.demo());
// L1(layer 0)加宽为 2 槽:加宽本身不散乱动作(仍全部在槽 1),
// 用户再把 A2 拖到槽 2。
c.setLaneSlots(0, 2);
expect(c.project.slotsOf(0), 2);
c.moveActionTo('A2', 0, 1);
final BarLayout layout = BarLayout(c.project, const Size(1280, 800));
final Action a1 = c.project.actionById('A1')!;
final Action a2 = c.project.actionById('A2')!;
expect(layout.slotOf(a1), 0);
expect(layout.slotOf(a2), 1);
final Rect r1 = layout.rectFor(a1);
final Rect r2 = layout.rectFor(a2);
expect((r1.top - r2.top).abs(), greaterThan(10), reason: '不同槽位应位于不同子行');
// 纵坐标反查(泳道, 槽位)。
expect(layout.laneSlotAt(r1.center.dy), (0, 0));
expect(layout.laneSlotAt(r2.center.dy), (0, 1));
// 圆周视图:同一轨道的不同槽位对应不同子环半径。
final CircularLayout cl = CircularLayout(c.project, const Size(1280, 800));
expect(cl.subRingCount, greaterThan(0));
expect(cl.radiusFor(0, 1) - cl.radiusFor(0, 0), closeTo(cl.pitch, 0.001));
expect(cl.laneSlotAtRadius(cl.radiusFor(0, 1)), (0, 1));
// 流程图:同一泳道的不同槽位对应不同列中心。
final FlowLayout fl = FlowLayout(c.project);
expect((fl.nodeById('A1')!.rect.center.dx - fl.nodeById('A2')!.rect.center.dx).abs(),
greaterThan(50));
expect(fl.laneWidth(0), greaterThan(FlowLayout.nodeW));
// JSON 往返保留槽位配置。
final Project decoded = Project.decode(c.project.encode());
expect(decoded.slotsOf(0), 2);
});
test('跨泳道移动可指定槽位;缩窄时槽位被钳制且动作不丢失', () {
final AppController c = AppController(project: Project.demo());
// 把 A4 移到 L2(layer 1)槽 1。
c.moveActionTo('A4', 1, 0);
expect(c.project.actionById('A4')!.layer, 1);
expect(c.project.actionById('A4')!.row, 0);
// 加宽 L2 为 2 槽 → 3 槽,再缩回 2 槽:A4 的槽位在布局中钳制到末槽。
c.setLaneSlots(1, 3);
c.moveActionTo('A4', 1, 2);
c.setLaneSlots(1, 2);
final BarLayout layout = BarLayout(c.project, const Size(1280, 800));
expect(layout.slotOf(c.project.actionById('A4')!), 1);
expect(c.project.actionById('A4')!.row, 2, reason: '数据保留原值,仅布局钳制');
});
});
group('自动保存设置', () {
test('默认启用且 10 分钟一次;启停与间隔随 JSON 往返', () {
final AppController c = AppController(project: Project.demo());
expect(c.project.autoSaveEnabled, isTrue);
expect(c.project.autoSaveMinutes, 10);
c.setAutoSave(minutes: 5);
expect(c.project.autoSaveMinutes, 5);
c.setAutoSave(enabled: false);
expect(c.project.autoSaveEnabled, isFalse);
final Project decoded = Project.decode(c.project.encode());
expect(decoded.autoSaveEnabled, isFalse);
expect(decoded.autoSaveMinutes, 5);
});
});
}