-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphNormalizer.test.ts
More file actions
121 lines (101 loc) · 3.51 KB
/
Copy pathGraphNormalizer.test.ts
File metadata and controls
121 lines (101 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { GraphNormalizer, GraphTree } from '@shared/visualServer/GraphNormalizer';
describe('GraphNormalizer', () => {
let definitions: Record<string, any>;
let normalizer: GraphNormalizer;
beforeEach(() => {
definitions = {};
normalizer = new GraphNormalizer(definitions);
});
it('should not modify short return structures', () => {
const tree: GraphTree[] = [
{
node: { containerName: 'TestContainer', returnStructure: 'short' },
dependencies: [],
},
];
const result = normalizer.normalize(tree);
expect(result[0].node.returnStructure).toBe('short');
expect(Object.keys(definitions)).toHaveLength(0);
});
it('should extract long return structures into definitions', () => {
const longStructure = 'x'.repeat(51);
const tree: GraphTree[] = [
{
node: { containerName: 'TestContainer', returnStructure: longStructure },
dependencies: [],
},
];
const result = normalizer.normalize(tree);
expect(result[0].node.returnStructure).toMatch(/^REF:/);
const refId = result[0].node.returnStructure;
expect(definitions[refId]).toBe(longStructure);
});
it('should handle return structures that are objects', () => {
const longObject = { data: 'x'.repeat(50) }; // JSON string will be > 50 chars
const tree: GraphTree[] = [
{
node: { containerName: 'TestContainer', returnStructure: longObject },
dependencies: [],
},
];
const result = normalizer.normalize(tree);
expect(result[0].node.returnStructure).toMatch(/^REF:/);
const refId = result[0].node.returnStructure;
expect(definitions[refId]).toEqual(longObject);
});
it('should reuse existing definitions for identical structures', () => {
const longStructure = 'y'.repeat(60);
const tree1: GraphTree[] = [
{
node: { containerName: 'C1', returnStructure: longStructure },
dependencies: [],
},
];
const tree2: GraphTree[] = [
{
node: { containerName: 'C2', returnStructure: longStructure },
dependencies: [],
},
];
const result1 = normalizer.normalize(tree1);
const result2 = normalizer.normalize(tree2);
expect(result1[0].node.returnStructure).toBe(result2[0].node.returnStructure);
expect(Object.keys(definitions)).toHaveLength(1);
const refId = result1[0].node.returnStructure;
expect(definitions[refId]).toBe(longStructure);
});
it('should traverse nested dependencies', () => {
const longStructure = 'z'.repeat(55);
const tree: GraphTree[] = [
{
node: { containerName: 'Root' },
dependencies: [
{
node: { containerName: 'Child', returnStructure: longStructure },
dependencies: [],
},
],
},
];
const result = normalizer.normalize(tree);
const childNode = result[0].dependencies[0].node;
expect(childNode.returnStructure).toMatch(/^REF:/);
expect(definitions[childNode.returnStructure]).toBe(longStructure);
});
it('should traverse methods', () => {
const longStructure = 'm'.repeat(55);
const tree: GraphTree[] = [
{
node: {
containerName: 'Root',
methods: [{ name: 'method1', returnStructure: longStructure }],
},
dependencies: [],
},
];
const result = normalizer.normalize(tree);
const method = result[0].node.methods[0];
expect(method.returnStructure).toMatch(/^REF:/);
expect(definitions[method.returnStructure]).toBe(longStructure);
});
});