run-fixed-layout-test.ts 3.1 KB

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