flow-node-entity.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { beforeEach, describe, expect, it } from 'vitest';
  2. import { TransformData } from '@flowgram.ai/core';
  3. import { FlowDocument } from '../src/flow-document';
  4. import { baseMockAddNode } from './flow.mock';
  5. import { createDocumentContainer } from './flow-document-container.mock';
  6. interface BlockData {
  7. children: string[];
  8. pre?: string;
  9. next?: string;
  10. depth: number;
  11. childrenSize: number;
  12. }
  13. const blockData: { [key: string]: BlockData } = {
  14. root: {
  15. children: ['start_0', 'dynamicSplit_0', 'end_0'],
  16. pre: undefined,
  17. next: undefined,
  18. depth: 0,
  19. childrenSize: 10,
  20. },
  21. start_0: {
  22. children: [],
  23. next: 'dynamicSplit_0',
  24. pre: undefined,
  25. depth: 1,
  26. childrenSize: 0,
  27. },
  28. dynamicSplit_0: {
  29. children: ['$blockIcon$dynamicSplit_0', '$inlineBlocks$dynamicSplit_0'],
  30. next: 'end_0',
  31. pre: 'start_0',
  32. depth: 1,
  33. childrenSize: 7,
  34. },
  35. $blockIcon$dynamicSplit_0: {
  36. children: [],
  37. next: '$inlineBlocks$dynamicSplit_0',
  38. pre: undefined,
  39. depth: 2,
  40. childrenSize: 0,
  41. },
  42. $inlineBlocks$dynamicSplit_0: {
  43. children: ['block_0', 'block_1'],
  44. pre: '$blockIcon$dynamicSplit_0',
  45. next: undefined,
  46. depth: 2,
  47. childrenSize: 5,
  48. },
  49. block_0: {
  50. children: ['$blockOrderIcon$block_0'],
  51. depth: 3,
  52. pre: undefined,
  53. next: 'block_1',
  54. childrenSize: 1,
  55. },
  56. $blockOrderIcon$block_0: {
  57. children: [],
  58. depth: 4,
  59. pre: undefined,
  60. next: undefined,
  61. childrenSize: 0,
  62. },
  63. block_1: {
  64. children: ['$blockOrderIcon$block_1', 'noop_0'],
  65. depth: 3,
  66. pre: 'block_0',
  67. next: undefined,
  68. childrenSize: 2,
  69. },
  70. $blockOrderIcon$block_1: {
  71. children: [],
  72. next: 'noop_0',
  73. depth: 4,
  74. pre: undefined,
  75. childrenSize: 0,
  76. },
  77. noop_0: {
  78. children: [],
  79. pre: '$blockOrderIcon$block_1',
  80. next: undefined,
  81. depth: 4,
  82. childrenSize: 0,
  83. },
  84. end_0: {
  85. children: [],
  86. pre: 'dynamicSplit_0',
  87. next: undefined,
  88. depth: 1,
  89. childrenSize: 0,
  90. },
  91. };
  92. describe('flow-node-entity', () => {
  93. let container = createDocumentContainer();
  94. let document: FlowDocument;
  95. beforeEach(() => {
  96. container = createDocumentContainer();
  97. container.get(FlowDocument).fromJSON(baseMockAddNode);
  98. document = container.get<FlowDocument>(FlowDocument);
  99. });
  100. it('get children', () => {
  101. const currentBlockData: { [key: string]: BlockData } = {};
  102. document.traverse((node, depth) => {
  103. currentBlockData[node.id] = {
  104. children: node.children.map((b) => b.id),
  105. depth,
  106. next: node.next?.id,
  107. pre: node.pre?.id,
  108. childrenSize: node.allChildren.length,
  109. };
  110. });
  111. expect(currentBlockData).toEqual(blockData);
  112. });
  113. it('flow node delete', () => {
  114. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  115. const node = document.getNode('$blockOrderIcon$block_1')!;
  116. node.dispose();
  117. expect(document.toString()).toEqual(`root
  118. |-- start_0
  119. |-- dynamicSplit_0
  120. |---- $blockIcon$dynamicSplit_0
  121. |---- $inlineBlocks$dynamicSplit_0
  122. |------ block_0
  123. |-------- $blockOrderIcon$block_0
  124. |------ block_1
  125. |-------- noop_0
  126. |-- end_0`);
  127. // transform 数据还在
  128. expect(node.getData(TransformData)).toBeDefined();
  129. });
  130. });