run-free-layout-test.ts 3.2 KB

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