1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15import { AllocationLogic } from '../../../src/js-heap/logic/Allocation';
16import { AllocationFunction } from '../../../src/js-heap/model/UiStruct';
17
18jest.mock('../../../src/js-heap/model/UiStruct', () => {
19  return {
20    AllocationFunction: {
21      fileId: 0,
22      functionIndex: 0,
23      parentsId: [1, 2, 3],
24      parents: [],
25      combineId: new Set([]),
26      status: true,
27      id: '12',
28      name: 'name1',
29      scriptName: 'scriptName1',
30      scriptId: 1,
31      line: 1,
32      column: 1,
33      count: 1,
34      size: 1,
35      liveCount: 1,
36      liveSize: 1,
37      hasParent: true,
38      clone: () => {
39        return {
40          fileId: 1,
41          functionIndex: 324,
42          parentsId: [10, 56, 3],
43          parents: [],
44          combineId: new Set([]),
45          status: true,
46          id: '21',
47          name: 'name2',
48          scriptName: 'scriptName2',
49          scriptId: 21,
50          line: 321,
51          column: 3,
52          count: 43,
53          size: 45,
54          liveCount: 11,
55          liveSize: 43,
56          hasParent: true,
57        };
58      },
59    },
60    clone: () => {
61      return {
62        fileId: 42,
63        functionIndex: 44,
64        parentsId: [1, 24],
65        parents: [],
66        combineId: new Set([]),
67        status: true,
68        id: '90',
69        name: 'name3',
70        scriptName: 'scriptName3',
71        scriptId: 65,
72        line: 6,
73        column: 87,
74        count: 23,
75        size: 54,
76        liveCount: 56,
77        liveSize: 44,
78        hasParent: true,
79      };
80    },
81  };
82});
83
84describe('Allocation Test', () => {
85  let set: Set<number> = new Set([]);
86  let data = {
87    end_ts: 342211,
88    id: 3,
89    isParseSuccess: true,
90    name: 'Test',
91    path: '',
92    pid: 34,
93    tart_ts: 8847320334,
94    type: 1,
95    heapLoader: {
96      rootNode: {
97        detachedness: 43,
98        displayName: '',
99        distance: 230000000,
100        edgeCount: 432,
101        fileId: 46,
102        firstEdgeIndex: 54,
103        flag: 5,
104        id: 54,
105        name: 'Test',
106        nodeIndex: 0,
107        nodeOldIndex: 0,
108        retainedSize: 5411,
109        retainsCount: 0,
110        retainsEdgeIdx: [0, 1],
111        retainsNodeIdx: [320, 49],
112        selfSize: 0,
113        traceNodeId: 0,
114        type: 34,
115        edges: [
116          {
117            edgeIndex: 5,
118            edgeOldIndex: 31,
119            fromNodeId: 1,
120            nameOrIndex: '-test-',
121            nodeId: 425,
122            retainEdge: [],
123            retainsNode: [],
124            toNodeId: 1134,
125            type: 25,
126          },
127          {
128            edgeIndex: 54,
129            edgeOldIndex: 35,
130            fromNodeId: 3,
131            nameOrIndex: '-test-',
132            nodeId: 9800,
133            retainEdge: [],
134            retainsNode: [],
135            toNodeId: 2311,
136            type: 5,
137          },
138        ],
139      },
140    },
141    snapshotStruct: {
142      traceNodes: [
143        {
144          fileId: 0,
145          functionIndex: 0,
146          parentsId: [1, 2, 3],
147          parents: [],
148          combineId: set,
149          status: true,
150          id: '12',
151          name: 'name1',
152          scriptName: 'scriptName1',
153          scriptId: 1,
154          line: 1,
155          column: 1,
156          count: 1,
157          size: 1,
158          liveCount: 1,
159          liveSize: 1,
160          hasParent: true,
161          clone: () => {
162            return {
163              fileId: 0,
164              functionIndex: 0,
165              parentsId: [1, 2, 3],
166              parents: [],
167              combineId: set,
168              status: true,
169              id: '12',
170              name: 'name1',
171              scriptName: 'scriptName1',
172              scriptId: 1,
173              line: 1,
174              column: 1,
175              count: 1,
176              size: 1,
177              liveCount: 1,
178              liveSize: 1,
179              hasParent: true,
180            };
181          },
182        },
183        {
184          fileId: 65,
185          functionIndex: 6,
186          parentsId: [1, 65, 3],
187          parents: [],
188          combineId: set,
189          status: true,
190          id: '45',
191          name: 'name6',
192          scriptName: 'scriptName6',
193          scriptId: 1,
194          line: 64,
195          column: 65,
196          count: 66,
197          size: 6,
198          liveCount: 1,
199          liveSize: 120,
200          hasParent: true,
201          clone: () => {
202            return {
203              fileId: 21,
204              functionIndex: 33,
205              parentsId: [54, 23],
206              parents: [],
207              combineId: set,
208              status: true,
209              id: '12',
210              name: 'name7',
211              scriptName: 'scriptName7',
212              scriptId: 321,
213              line: 341,
214              column: 123,
215              count: 21,
216              size: 134,
217              liveCount: 16,
218              liveSize: 109,
219              hasParent: true,
220            };
221          },
222        },
223        {
224          fileId: 23,
225          functionIndex: 1,
226          parentsId: [111, 24, 3],
227          parents: [],
228          combineId: set,
229          status: true,
230          id: '4345',
231          name: 'name7',
232          scriptName: 'scriptName7',
233          scriptId: 421,
234          line: 13,
235          column: 51,
236          count: 61,
237          size: 165,
238          liveCount: 10,
239          liveSize: 651,
240          hasParent: true,
241          clone: () => {
242            return {
243              fileId: 3,
244              functionIndex: 57,
245              parentsId: [1, 87, 31],
246              parents: [],
247              combineId: set,
248              status: true,
249              id: '251',
250              name: 'name8',
251              scriptName: 'scriptName8',
252              scriptId: 91,
253              line: 189,
254              column: 891,
255              count: 81,
256              size: 14,
257              liveCount: 431,
258              liveSize: 21,
259              hasParent: true,
260            };
261          },
262        },
263      ],
264      nodeMap: new Map(),
265      nodeCount: 1,
266      edges: [
267        {
268          edgeIndex: 6,
269          edgeOldIndex: 0,
270          fromNodeId: 1,
271          nameOrIndex: '-test-',
272          nodeId: 67,
273          retainEdge: [],
274          retainsNode: [],
275          toNodeId: 77,
276          type: 61,
277        },
278        {
279          edgeIndex: 16,
280          edgeOldIndex: 37,
281          fromNodeId: 16,
282          nameOrIndex: '-test-',
283          nodeId: 67,
284          retainEdge: [],
285          retainsNode: [],
286          toNodeId: 876,
287          type: 8,
288        },
289      ],
290      samples: [],
291      functionInfos: [1],
292    },
293  };
294  it('AllocationTest01', () => {
295    let allocationLogic = new AllocationLogic(data);
296    let nodeById = allocationLogic.getNodeById(12);
297    expect(nodeById).not.toBe();
298  });
299
300  it('AllocationTest02', () => {
301    let allocationLogic = new AllocationLogic(data);
302    let nodeById = allocationLogic.getNodeById(11);
303    expect(nodeById).toBe(null);
304  });
305  it('AllocationTest03', () => {
306    let allocationLogic = new AllocationLogic(data);
307    let nodeStack = allocationLogic.getNodeStack(12);
308    expect(nodeStack.length).toBe(0);
309  });
310  it('AllocationTest04', () => {
311    let allocationLogic = new AllocationLogic(data);
312    let nodeStack = allocationLogic.getFunctionNodeIds(12);
313    expect(nodeStack).not.toBe([]);
314  });
315  it('AllocationTest05', () => {
316    let allocationLogic = new AllocationLogic(data);
317    let parentData = data.snapshotStruct.traceNodes[0];
318    let nodeStack = allocationLogic.getParent(parentData);
319    expect(nodeStack).toBeUndefined();
320  });
321  it('AllocationTest06', () => {
322    let allocationLogic = new AllocationLogic(data);
323    let parentData = {
324      fileId: 23,
325      functionIndex: 35,
326      parentsId: [1],
327      parents: [],
328      combineId: set,
329      status: true,
330      id: '2450',
331      name: 'name9',
332      scriptName: 'scriptName9',
333      scriptId: 981,
334      line: 8341,
335      column: 21,
336      count: 211,
337      size: 134,
338      liveCount: 541,
339      liveSize: 176,
340      hasParent: true,
341    };
342    let nodeStack = allocationLogic.getParent(parentData);
343    expect(nodeStack).toBeUndefined();
344  });
345
346  it('AllocationTest07', () => {
347    let allocationLogic = new AllocationLogic(data);
348    let nodeStack = allocationLogic.getFunctionList();
349    expect(nodeStack.length).not.toEqual(0);
350  });
351
352  it('AllocationTest08', () => {
353    let allocationLogic = new AllocationLogic(data);
354    let parentData = data.snapshotStruct.traceNodes[0];
355    let nodeStack = allocationLogic.getFunctionStack(parentData, []);
356    expect(nodeStack).toBeUndefined();
357  });
358});
359