-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathglobal-data-collector.test.ts
More file actions
136 lines (102 loc) · 5.1 KB
/
global-data-collector.test.ts
File metadata and controls
136 lines (102 loc) · 5.1 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { GlobalDataCollector } from '../../profile-logic/global-data-collector';
import { StringTable } from '../../utils/string-table';
describe('GlobalDataCollector', function () {
describe('source table management', function () {
it('should create an empty source table', function () {
const collector = new GlobalDataCollector();
const { shared } = collector.finish();
expect(shared.sources.filename).toEqual([]);
expect(shared.sources.id).toEqual([]);
expect(shared.sources.length).toBe(0);
});
it('should add unique sources and return correct indexes', function () {
const collector = new GlobalDataCollector();
// Add different sources
const sourceIndex1 = collector.indexForSource(null, 'file1.js');
const sourceIndex2 = collector.indexForSource('uuid2', 'file2.js');
// Duplicate filename but different UUID
const sourceIndex3 = collector.indexForSource('uuid3', 'file1.js');
expect(sourceIndex1).toBe(0);
expect(sourceIndex2).toBe(1);
expect(sourceIndex3).toBe(2);
const { shared } = collector.finish();
const stringTable = StringTable.withBackingArray(shared.stringArray);
const file1Index = stringTable.indexForString('file1.js');
const file2Index = stringTable.indexForString('file2.js');
expect(shared.sources.filename).toEqual([
file1Index,
file2Index,
file1Index,
]);
expect(shared.sources.id).toEqual([null, 'uuid2', 'uuid3']);
expect(shared.sources.length).toBe(3);
});
it('should return existing index for duplicate filename/uuid combinations', function () {
const collector = new GlobalDataCollector();
// Add the same source twice
const sourceIndex1 = collector.indexForSource('same-uuid', 'file1.js');
const sourceIndex2 = collector.indexForSource('same-uuid', 'file1.js');
expect(sourceIndex1).toBe(sourceIndex2);
expect(sourceIndex1).toBe(0);
const { shared } = collector.finish();
expect(shared.sources.id).toEqual(['same-uuid']);
expect(shared.sources.length).toBe(1);
});
it('should handle null UUIDs correctly', function () {
const collector = new GlobalDataCollector();
// Add the same filename with null UUID twice
const sourceIndex1 = collector.indexForSource(null, 'file1.js');
const sourceIndex2 = collector.indexForSource(null, 'file1.js');
expect(sourceIndex1).toBe(sourceIndex2);
expect(sourceIndex1).toBe(0);
const { shared } = collector.finish();
expect(shared.sources.id).toEqual([null]);
expect(shared.sources.length).toBe(1);
});
it('should handle different UUIDs for the same filename', function () {
const collector = new GlobalDataCollector();
// Add same filename with different UUIDs
const sourceIndex1 = collector.indexForSource('uuid1', 'file1.js');
const sourceIndex2 = collector.indexForSource('uuid2', 'file1.js');
const sourceIndex3 = collector.indexForSource(null, 'file1.js');
expect(sourceIndex1).toBe(0);
expect(sourceIndex2).toBe(1);
expect(sourceIndex3).toBe(2);
const { shared } = collector.finish();
expect(shared.sources.id).toEqual(['uuid1', 'uuid2', null]);
expect(shared.sources.length).toBe(3);
});
});
describe('string table integration', function () {
it('should maintain correct string table state', function () {
const collector = new GlobalDataCollector();
// Add sources
collector.indexForSource(null, 'first.js');
collector.indexForSource('uuid', 'second.js');
const { shared } = collector.finish();
expect(shared.stringArray).toEqual(['first.js', 'second.js']);
const stringTable = StringTable.withBackingArray(shared.stringArray);
const firstIndex = stringTable.indexForString('first.js');
const secondIndex = stringTable.indexForString('second.js');
expect(shared.sources.filename).toEqual([firstIndex, secondIndex]);
});
it('should handle string deduplication correctly', function () {
const collector = new GlobalDataCollector();
// Add a source, then add another source with the same filename
const sourceIndex1 = collector.indexForSource('uuid1', 'same.js');
const sourceIndex2 = collector.indexForSource('uuid2', 'same.js');
// Should be different source indexes since UUIDs are different
expect(sourceIndex1).not.toBe(sourceIndex2);
const { shared } = collector.finish();
// Should have both files with the same filename string but different source entries
const stringTable = StringTable.withBackingArray(shared.stringArray);
const filenameIndex = stringTable.indexForString('same.js');
expect(shared.sources.filename).toEqual([filenameIndex, filenameIndex]);
expect(shared.sources.id).toEqual(['uuid1', 'uuid2']);
expect(shared.sources.length).toBe(2);
});
});
});