computing.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { it, expect, beforeEach, describe } from 'vitest';
  2. import { interfaces } from 'inversify';
  3. import { EntityManager } from '@flowgram.ai/core';
  4. import {
  5. WorkflowDocument,
  6. WorkflowHoverService,
  7. WorkflowLineEntity,
  8. WorkflowLinesManager,
  9. WorkflowSelectService,
  10. } from '@flowgram.ai/free-layout-core';
  11. import { StackingComputing } from '../src/stacking-computing';
  12. import { StackingContextManager } from '../src/manager';
  13. import { createWorkflowContainer, workflowJSON } from './utils.mock';
  14. import { IStackingComputing, IStackingContextManager } from './type.mock';
  15. let container: interfaces.Container;
  16. let document: WorkflowDocument;
  17. let stackingContextManager: IStackingContextManager;
  18. let stackingComputing: IStackingComputing;
  19. beforeEach(async () => {
  20. container = createWorkflowContainer();
  21. container.bind(StackingContextManager).to(StackingContextManager);
  22. document = container.get<WorkflowDocument>(WorkflowDocument);
  23. stackingContextManager = container.get<StackingContextManager>(
  24. StackingContextManager,
  25. ) as unknown as IStackingContextManager;
  26. await document.fromJSON(workflowJSON);
  27. stackingContextManager.init();
  28. stackingComputing = new StackingComputing() as unknown as IStackingComputing;
  29. });
  30. describe('StackingComputing compute', () => {
  31. it('should create instance', () => {
  32. const computing = new StackingComputing();
  33. expect(computing).not.toBeUndefined();
  34. });
  35. it('should execute compute', () => {
  36. const { nodeLevel, lineLevel, topLevel, maxLevel } = stackingComputing.compute({
  37. root: document.root,
  38. nodes: stackingContextManager.nodes,
  39. context: stackingContextManager.context,
  40. });
  41. expect(topLevel).toBe(8);
  42. expect(maxLevel).toBe(16);
  43. expect(Object.fromEntries(nodeLevel)).toEqual({
  44. start_0: 1,
  45. condition_0: 2,
  46. end_0: 3,
  47. loop_0: 4,
  48. break_0: 6,
  49. variable_0: 7,
  50. });
  51. expect(Object.fromEntries(lineLevel)).toEqual({
  52. 'start_0_-condition_0_': 0,
  53. 'start_0_-loop_0_': 0,
  54. 'condition_0_if-end_0_': 0,
  55. 'condition_0_else-end_0_': 0,
  56. 'loop_0_-end_0_': 0,
  57. 'break_0_-variable_0_': 5,
  58. });
  59. });
  60. it('should put hovered line on max level', () => {
  61. const hoverService = container.get<WorkflowHoverService>(WorkflowHoverService);
  62. const hoveredLineId = 'start_0_-loop_0_';
  63. hoverService.updateHoveredKey(hoveredLineId);
  64. const { lineLevel, maxLevel } = stackingComputing.compute({
  65. root: document.root,
  66. nodes: stackingContextManager.nodes,
  67. context: stackingContextManager.context,
  68. });
  69. const hoveredLineLevel = lineLevel.get(hoveredLineId);
  70. expect(hoveredLineLevel).toBe(maxLevel);
  71. });
  72. it('should put selected line on max level', () => {
  73. const entityManager = container.get<EntityManager>(EntityManager);
  74. const selectService = container.get<WorkflowSelectService>(WorkflowSelectService);
  75. const selectedLineId = 'start_0_-loop_0_';
  76. const selectedLine = entityManager.getEntityById<WorkflowLineEntity>(selectedLineId)!;
  77. selectService.selection = [selectedLine];
  78. const { lineLevel, maxLevel } = stackingComputing.compute({
  79. root: document.root,
  80. nodes: stackingContextManager.nodes,
  81. context: stackingContextManager.context,
  82. });
  83. const selectedLineLevel = lineLevel.get(selectedLineId);
  84. expect(selectedLineLevel).toBe(maxLevel);
  85. });
  86. it('should put drawing line on max level', () => {
  87. const linesManager = container.get<WorkflowLinesManager>(WorkflowLinesManager);
  88. const drawingLine = linesManager.createLine({
  89. from: 'start_0',
  90. drawingTo: { x: 100, y: 100 },
  91. })!;
  92. const { lineLevel, maxLevel } = stackingComputing.compute({
  93. root: document.root,
  94. nodes: stackingContextManager.nodes,
  95. context: stackingContextManager.context,
  96. });
  97. const drawingLineLevel = lineLevel.get(drawingLine.id);
  98. expect(drawingLineLevel).toBe(maxLevel);
  99. });
  100. it('should put selected nodes on top level', () => {
  101. const selectService = container.get<WorkflowSelectService>(WorkflowSelectService);
  102. const selectedNodeId = 'start_0';
  103. const selectedNode = document.getNode(selectedNodeId)!;
  104. selectService.selectNode(selectedNode);
  105. const { nodeLevel, topLevel } = stackingComputing.compute({
  106. root: document.root,
  107. nodes: stackingContextManager.nodes,
  108. context: stackingContextManager.context,
  109. });
  110. const selectedNodeLevel = nodeLevel.get(selectedNodeId);
  111. expect(selectedNodeLevel).toBe(topLevel);
  112. });
  113. });
  114. describe('StackingComputing builtin methods', () => {
  115. it('computeNodeIndexesMap', () => {
  116. const nodeIndexes = stackingComputing.computeNodeIndexesMap(stackingContextManager.nodes);
  117. expect(Object.fromEntries(nodeIndexes)).toEqual({
  118. root: 0,
  119. start_0: 1,
  120. condition_0: 2,
  121. end_0: 3,
  122. loop_0: 4,
  123. break_0: 5,
  124. variable_0: 6,
  125. });
  126. });
  127. it('computeTopLevel', () => {
  128. const topLevel = stackingComputing.computeTopLevel(stackingContextManager.nodes);
  129. expect(topLevel).toEqual(8);
  130. });
  131. });