run-free-layout-test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, test } from 'vitest';
  2. import { VariableEngine } from '@flowgram.ai/variable-core';
  3. import { ASTKind } from '@flowgram.ai/variable-core';
  4. import {
  5. FlowNodeVariableData
  6. } from '../src';
  7. import { TestConfig, getContainer } from './container';
  8. import { WorkflowDocument, WorkflowJSON } from '@flowgram.ai/free-layout-core';
  9. export const runFreeLayoutTest = (testName: string, spec: WorkflowJSON, config?: TestConfig) => {
  10. describe(testName, async () => {
  11. const container = getContainer('free', config);
  12. const flowDocument = container.get(WorkflowDocument);
  13. const variableEngine = container.get(VariableEngine);
  14. variableEngine.onScopeChange(action => {
  15. const { type, scope } = action;
  16. // 作用域默认创建一个默认变量
  17. if (type === 'add') {
  18. scope.ast.set('/', {
  19. kind: ASTKind.VariableDeclaration,
  20. type: ASTKind.String,
  21. key: String(scope.id),
  22. });
  23. }
  24. });
  25. // 创建一个全局作用域
  26. variableEngine.createScope('testScope');
  27. const traverseVariableDatas = () =>
  28. flowDocument
  29. .getAllNodes()
  30. // TODO 包含 $ 的节点不注册 variableData
  31. .filter(_node => !_node.id.startsWith('$'))
  32. .map(_node => _node.getData(FlowNodeVariableData))
  33. .filter(Boolean);
  34. // 初始化所有节点的私有作用域
  35. const initAllNodePrivate = () => {
  36. traverseVariableDatas().forEach(_data => {
  37. _data.initPrivate();
  38. });
  39. };
  40. // 初始化所有节点的可用变量
  41. const printAllNodeAvailableMapping = (_scopeType: 'public' | 'private' = 'public') =>
  42. traverseVariableDatas().reduce((acm, _data) => {
  43. const scope = _data[_scopeType]!;
  44. acm.set(String(scope.id), scope.available.variableKeys);
  45. return acm;
  46. }, new Map<string, string[]>());
  47. // 初始化所有节点的覆盖作用域
  48. const printAllCovers = (_scopeType: 'public' | 'private' = 'public') =>
  49. traverseVariableDatas().reduce((acm, _data) => {
  50. const scope = _data[_scopeType]!;
  51. acm.set(
  52. String(scope.id),
  53. scope.coverScopes.map(_scope => String(_scope.id)),
  54. );
  55. return acm;
  56. }, new Map<string, string[]>());
  57. await flowDocument.fromJSON(spec);
  58. test('test get Deps', () => {
  59. expect(printAllNodeAvailableMapping()).toMatchSnapshot();
  60. });
  61. test('test get Covers', () => {
  62. expect(printAllCovers()).toMatchSnapshot();
  63. });
  64. test('test get Deps After Init Private', () => {
  65. initAllNodePrivate();
  66. expect(printAllNodeAvailableMapping()).toMatchSnapshot();
  67. });
  68. test('test get private scope Deps', () => {
  69. expect(printAllNodeAvailableMapping('private')).toMatchSnapshot();
  70. });
  71. test('test get Covers After Init Private', () => {
  72. expect(printAllCovers()).toMatchSnapshot();
  73. });
  74. test('test get private scope Covers', () => {
  75. expect(printAllCovers('private')).toMatchSnapshot();
  76. });
  77. test('test sort', () => {
  78. expect(variableEngine.getAllScopes({ sort: true }).map(_scope => _scope.id)).toMatchSnapshot();
  79. });
  80. });
  81. }