forked from salesforcecli/plugin-metadata-enrichment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenrichmentRecords.test.ts
More file actions
204 lines (190 loc) · 8.48 KB
/
enrichmentRecords.test.ts
File metadata and controls
204 lines (190 loc) · 8.48 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect } from 'chai';
import { EnrichmentRecords, EnrichmentStatus } from '@salesforce/metadata-enrichment';
import type { SourceComponent } from '@salesforce/source-deploy-retrieve';
function createSourceComponent(name: string, typeName: string): SourceComponent {
return {
fullName: name,
name,
type: { name: typeName },
} as SourceComponent;
}
describe('EnrichmentRecords', () => {
describe('constructor', () => {
it('should create NOT_PROCESSED records for each source component with type and name', () => {
const source = [
createSourceComponent('Cmp1', 'LightningComponentBundle'),
createSourceComponent('Cmp2', 'LightningComponentBundle'),
];
const records = new EnrichmentRecords(source);
expect(records.recordSet.size).to.equal(2);
const arr = Array.from(records.recordSet);
expect(arr.every((r) => r.status === EnrichmentStatus.NOT_PROCESSED)).to.be.true;
expect(arr.map((r) => r.componentName).sort()).to.deep.equal(['Cmp1', 'Cmp2']);
expect(arr.every((r) => r.requestBody?.contentBundles?.length === 0)).to.be.true;
});
it('should set placeholder requestBody for each component (library sets real metadataType for processed components)', () => {
const source = [createSourceComponent('MyLwc', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
const record = Array.from(records.recordSet)[0];
expect(record.requestBody).to.not.be.null;
expect(record.requestBody!.contentBundles).to.deep.equal([]);
expect(record.requestBody!.metadataType).to.equal('Generic');
});
it('should not create record when component has no name', () => {
const source = [{ fullName: undefined, name: undefined, type: { name: 'LWC' } }] as unknown as SourceComponent[];
const records = new EnrichmentRecords(source);
expect(records.recordSet.size).to.equal(0);
});
it('should not create record when component has no type', () => {
const source = [{ fullName: 'NoType', name: 'NoType', type: undefined }] as unknown as SourceComponent[];
const records = new EnrichmentRecords(source);
expect(records.recordSet.size).to.equal(0);
});
});
describe('addRecords', () => {
it('should add SKIPPED record for component not in recordSet', () => {
const records = new EnrichmentRecords([]);
records.addRecords(new Set([{
componentName: 'SkippedCmp',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: { contentBundles: [], metadataType: 'Generic' },
response: null,
message: null,
status: EnrichmentStatus.SKIPPED,
}]));
expect(records.recordSet.size).to.equal(1);
const record = Array.from(records.recordSet)[0];
expect(record.componentName).to.equal('SkippedCmp');
expect(record.status).to.equal(EnrichmentStatus.SKIPPED);
expect(record.requestBody).to.not.be.null;
expect(record.requestBody!.contentBundles).to.deep.equal([]);
expect(record.requestBody!.metadataType).to.equal('Generic');
});
it('should not duplicate when record already exists', () => {
const source = [createSourceComponent('Existing', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
records.addRecords(new Set([{
componentName: 'Existing',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: { contentBundles: [], metadataType: 'Generic' },
response: null,
message: null,
status: EnrichmentStatus.SKIPPED,
}]));
expect(records.recordSet.size).to.equal(1);
});
});
describe('updateWithStatus', () => {
it('should set status for matching records', () => {
const source = [createSourceComponent('Cmp1', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
records.updateWithStatus(
new Set([{ typeName: 'LightningComponentBundle', componentName: 'Cmp1' }]),
EnrichmentStatus.SKIPPED
);
const record = Array.from(records.recordSet)[0];
expect(record.status).to.equal(EnrichmentStatus.SKIPPED);
});
});
describe('updateWithResults', () => {
it('should replace record requestBody with result requestBody (library-supplied metadataType/maxTokens)', () => {
const source = [createSourceComponent('Cmp1', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
const libraryRequestBody = {
contentBundles: [{ resourceName: 'Cmp1', files: {} }],
metadataType: 'Lwc' as const,
maxTokens: 50,
};
records.updateWithResults([
{
componentName: 'Cmp1',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: libraryRequestBody,
response: { metadata: { durationMs: 0, failureCount: 0, successCount: 1, timestamp: '' }, results: [] },
message: null,
status: EnrichmentStatus.SUCCESS,
},
]);
const record = Array.from(records.recordSet)[0];
expect(record.requestBody).to.equal(libraryRequestBody);
expect(record.requestBody).to.not.be.null;
expect(record.requestBody!.metadataType).to.equal('Lwc');
expect(record.requestBody!.maxTokens).to.equal(50);
});
it('should update record to SUCCESS when response is present', () => {
const source = [createSourceComponent('Cmp1', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
records.updateWithResults([
{
componentName: 'Cmp1',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: { contentBundles: [], metadataType: 'Generic', maxTokens: 50 },
response: {
metadata: { durationMs: 0, failureCount: 0, successCount: 1, timestamp: '' },
results: [],
},
message: null,
status: EnrichmentStatus.SUCCESS,
},
]);
const record = Array.from(records.recordSet)[0];
expect(record.status).to.equal(EnrichmentStatus.SUCCESS);
expect(record.response).to.not.be.null;
});
it('should update record to FAIL when response is null', () => {
const source = [createSourceComponent('Cmp1', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
records.updateWithResults([
{
componentName: 'Cmp1',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: { contentBundles: [], metadataType: 'Generic', maxTokens: 50 },
response: null,
message: 'error',
status: EnrichmentStatus.FAIL,
},
]);
const record = Array.from(records.recordSet)[0];
expect(record.status).to.equal(EnrichmentStatus.FAIL);
expect(record.message).to.equal('error');
});
it('should not override status when record is SKIPPED', () => {
const source = [createSourceComponent('Cmp1', 'LightningComponentBundle')];
const records = new EnrichmentRecords(source);
records.updateWithStatus(
new Set([{ typeName: 'LightningComponentBundle', componentName: 'Cmp1' }]),
EnrichmentStatus.SKIPPED
);
records.updateWithResults([
{
componentName: 'Cmp1',
componentType: { name: 'LightningComponentBundle' } as SourceComponent['type'],
requestBody: { contentBundles: [], metadataType: 'Generic', maxTokens: 50 },
response: {
metadata: { durationMs: 0, failureCount: 0, successCount: 1, timestamp: '' },
results: [],
},
message: null,
status: EnrichmentStatus.SUCCESS,
},
]);
const record = Array.from(records.recordSet)[0];
expect(record.status).to.equal(EnrichmentStatus.SKIPPED);
});
});
});