Skip to content

Commit 3d5b970

Browse files
committed
test: update CompasNSDocFileService tests
1 parent 3a365d2 commit 3d5b970

File tree

2 files changed

+308
-68
lines changed

2 files changed

+308
-68
lines changed

packages/compas-open-scd/src/compas-services/CompasNSDocFileService.ts

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,45 @@ function generateIdFromName(name: string): string {
3434
let hash = 0;
3535
for (let i = 0; i < name.length; i++) {
3636
const char = name.charCodeAt(i);
37-
hash = ((hash << 5) - hash) + char;
37+
hash = (hash << 5) - hash + char;
3838
hash = hash & hash;
3939
}
40-
40+
4141
const hashStr = Math.abs(hash).toString(16).padStart(8, '0');
42-
return `${hashStr.slice(0, 8)}-${hashStr.slice(0, 4)}-4${hashStr.slice(1, 4)}-${hashStr.slice(0, 4)}-${hashStr}${hashStr.slice(0, 4)}`;
42+
return `${hashStr.slice(0, 8)}-${hashStr.slice(0, 4)}-4${hashStr.slice(
43+
1,
44+
4
45+
)}-${hashStr.slice(0, 4)}-${hashStr}${hashStr.slice(0, 4)}`;
4346
}
4447

45-
function parseVersionNumber(version: string, revision: string, release: string): number {
48+
function parseVersionNumber(
49+
version: string,
50+
revision: string,
51+
release: string
52+
): number {
4653
const versionNum = parseInt(version) || 0;
4754
const revisionNum = revision.charCodeAt(0) - 65;
4855
const releaseNum = parseInt(release) || 0;
49-
56+
5057
return versionNum * 1000000 + revisionNum * 1000 + releaseNum;
5158
}
5259

5360
function parseNsDocFilename(filename: string): NsDocFileInfo | null {
54-
const match = filename.match(/^IEC_61850-([0-9]+-[0-9]+)_(\d{4})([A-Z])(\d+)-en\.nsdoc$/);
61+
const match = filename.match(
62+
/^IEC_61850-([0-9]+-[0-9]+)_(\d{4})([A-Z])(\d+)-en\.nsdoc$/
63+
);
5564
if (match) {
5665
const [, standardPart, version, revision, release] = match;
5766
const id = `IEC 61850-${standardPart}`;
5867
const fullVersion = `${version}${revision}${release}`;
59-
68+
6069
return {
6170
id,
6271
version,
6372
revision,
6473
release,
6574
filename,
66-
fullVersion
75+
fullVersion,
6776
};
6877
}
6978
return null;
@@ -75,110 +84,123 @@ async function isValidNsDocFile(filename: string): Promise<boolean> {
7584
if (!response.ok) {
7685
return false;
7786
}
78-
87+
7988
const content = await response.text();
8089
const doc = new DOMParser().parseFromString(content, 'text/xml');
8190
const nsElement = doc.querySelector('NSDoc');
8291
const xmlns = nsElement?.getAttribute('xmlns');
83-
92+
8493
return xmlns === 'http://www.iec.ch/61850/2016/NSD';
8594
} catch (error) {
8695
return false;
8796
}
8897
}
8998

90-
9199
// Get NSDOC files from manifest.json
92100
async function getNsDocFilesFromManifest(): Promise<string[]> {
93101
try {
94102
const manifestResponse = await fetch('/public/nsdoc/manifest.json');
95103
if (!manifestResponse.ok) {
96104
return [];
97105
}
98-
106+
99107
const manifest = await manifestResponse.json();
100108
if (!Array.isArray(manifest)) {
101109
return [];
102110
}
103-
104-
const nsdocFiles = manifest.filter((filename: unknown) =>
105-
typeof filename === 'string' && filename.endsWith('-en.nsdoc')
111+
112+
const nsdocFiles = manifest.filter(
113+
(filename: unknown) =>
114+
typeof filename === 'string' && filename.endsWith('-en.nsdoc')
106115
) as string[];
107-
116+
108117
return nsdocFiles;
109-
110118
} catch (error) {
111119
return [];
112120
}
113121
}
114122

115123
// Discover NSDOC files using pattern-based approach (fallback)
116-
async function getNsDocFilesByPattern(): Promise<string[]> {
124+
async function getNsDocFilesByPattern(): Promise<string[]> {
117125
const standards = ['7-2', '7-3', '7-4', '8-1'];
118126
const versions = ['2003A2', '2007B3', '2007B4', '2007B5'];
119-
127+
120128
const potentialFiles: string[] = [];
121129
for (const standard of standards) {
122130
for (const version of versions) {
123131
potentialFiles.push(`IEC_61850-${standard}_${version}-en.nsdoc`);
124132
}
125133
}
126-
127-
const testPromises = potentialFiles.map(async (filename) => {
134+
135+
const testPromises = potentialFiles.map(async filename => {
128136
try {
129137
const response = await fetch(`/public/nsdoc/${filename}`);
130138
return response.ok ? filename : null;
131139
} catch (e) {
132140
return null;
133141
}
134142
});
135-
143+
136144
const existingFiles = await Promise.all(testPromises);
137-
const discoveredFiles = existingFiles.filter((filename): filename is string => filename !== null);
138-
145+
const discoveredFiles = existingFiles.filter(
146+
(filename): filename is string => filename !== null
147+
);
148+
139149
return discoveredFiles;
140150
}
141151

142-
async function parseAndValidateNsDocFiles(filenames: string[]): Promise<NsDocFileInfo[]> {
152+
async function parseAndValidateNsDocFiles(
153+
filenames: string[]
154+
): Promise<NsDocFileInfo[]> {
143155
const parsedFiles: NsDocFileInfo[] = [];
144-
156+
145157
for (const filename of filenames) {
146158
const fileInfo = parseNsDocFilename(filename);
147159
if (fileInfo) {
148160
const isValid = await isValidNsDocFile(filename);
149161
if (isValid) {
150162
parsedFiles.push(fileInfo);
151163
} else {
152-
console.warn(`Skipping invalid NSDOC file: ${filename} (missing or incorrect schema)`);
164+
console.warn(
165+
`Skipping invalid NSDOC file: ${filename} (missing or incorrect schema)`
166+
);
153167
}
154168
}
155169
}
156-
170+
157171
return parsedFiles;
158172
}
159173

160174
function selectLatestVersions(parsedFiles: NsDocFileInfo[]): NsDocFile[] {
161175
const standardsMap = new Map<string, NsDocFileInfo>();
162-
176+
163177
for (const fileInfo of parsedFiles) {
164178
const currentFileInMap = standardsMap.get(fileInfo.id);
165-
179+
166180
if (!currentFileInMap) {
167181
standardsMap.set(fileInfo.id, fileInfo);
168182
} else {
169-
const currentVersionNum = parseVersionNumber(currentFileInMap.version, currentFileInMap.revision, currentFileInMap.release);
170-
const newVersionNum = parseVersionNumber(fileInfo.version, fileInfo.revision, fileInfo.release);
171-
183+
const currentVersionNum = parseVersionNumber(
184+
currentFileInMap.version,
185+
currentFileInMap.revision,
186+
currentFileInMap.release
187+
);
188+
const newVersionNum = parseVersionNumber(
189+
fileInfo.version,
190+
fileInfo.revision,
191+
fileInfo.release
192+
);
193+
172194
if (newVersionNum > currentVersionNum) {
173195
standardsMap.set(fileInfo.id, fileInfo);
174196
}
175197
}
176198
}
177-
199+
178200
return Array.from(standardsMap.values()).map(fileInfo => ({
179201
filename: fileInfo.filename,
180202
name: fileInfo.id,
181-
id: generateIdFromName(fileInfo.id + fileInfo.fullVersion)
203+
id: generateIdFromName(fileInfo.id + fileInfo.fullVersion),
182204
}));
183205
}
184206

@@ -188,12 +210,14 @@ async function getNsDocFiles(): Promise<NsDocFile[]> {
188210
if (nsdocFiles.length === 0) {
189211
nsdocFiles = await getNsDocFilesByPattern();
190212
}
191-
213+
192214
if (nsdocFiles.length === 0) {
193-
console.warn('No NSDOC files found using either manifest or pattern-based discovery');
215+
console.warn(
216+
'No NSDOC files found using either manifest or pattern-based discovery'
217+
);
194218
return [];
195219
}
196-
220+
197221
const parsedFiles = await parseAndValidateNsDocFiles(nsdocFiles);
198222

199223
return selectLatestVersions(parsedFiles);
@@ -210,31 +234,33 @@ export function CompasNSDocFileService(): {
210234
return {
211235
async listNsdocFiles(): Promise<NsDocListResponse> {
212236
const nsDocFiles = await getNsDocFiles();
213-
237+
214238
return {
215239
files: nsDocFiles.map((nsDocFile: NsDocFile) => ({
216240
id: nsDocFile.id,
217241
nsdocId: nsDocFile.name,
218242
filename: nsDocFile.filename,
219-
checksum: nsDocFile.id
220-
}))
243+
checksum: nsDocFile.id,
244+
})),
221245
};
222246
},
223247

224248
async getNsdocFile(id: string): Promise<NsdocContentResponse> {
225249
const nsDocFiles = await getNsDocFiles();
226-
const nsDocFile: NsDocFile | undefined = nsDocFiles.find((f: NsDocFile) => f.id === id);
250+
const nsDocFile: NsDocFile | undefined = nsDocFiles.find(
251+
(f: NsDocFile) => f.id === id
252+
);
227253

228254
if (!nsDocFile) {
229255
return Promise.reject(`Unable to find nsDoc file with id ${id}`);
230256
}
231-
257+
232258
const content = await fetch(`/public/nsdoc/${nsDocFile.filename}`)
233259
.catch(handleError)
234260
.then(handleResponse);
235-
261+
236262
return {
237-
content
263+
content,
238264
};
239265
},
240266
};

0 commit comments

Comments
 (0)