Skip to content

Commit e69ff85

Browse files
authored
fix(cicero-core): add missing await on updateExternalModels in fromAr… (#878)
1 parent 2f9178c commit e69ff85

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/cicero-core/src/templateloader.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ class TemplateLoader {
9090
// add model files
9191
Logger.debug(method, 'Adding model files to model manager');
9292
const mm = template.getModelManager();
93-
ctoModelFiles.forEach( (mf,index) => {
93+
ctoModelFiles.forEach((mf, index) => {
9494
mm.addCTOModel(mf, ctoModelFileNames[index], true);
9595
});
9696

97-
if(options && options.offline) {
97+
if (options && options.offline) {
9898
mm.validateModelFiles();
9999
}
100100
else {
101-
mm.updateExternalModels();
101+
await mm.updateExternalModels();
102102
}
103103

104104
Logger.debug(method, 'Setting grammar');
@@ -193,7 +193,7 @@ class TemplateLoader {
193193
const mm = template.getModelManager();
194194
mm.addModelFiles(modelFiles, modelFileNames, true);
195195

196-
if(options && options.offline) {
196+
if (options && options.offline) {
197197
mm.validateModelFiles();
198198
}
199199
else {
@@ -202,7 +202,7 @@ class TemplateLoader {
202202

203203
if (!options || !options.offline) {
204204
mm.getModelFiles().forEach(mf => {
205-
if(mf.isExternal()) {
205+
if (mf.isExternal()) {
206206
fs.writeFileSync(path + '/model/' + mf.getName(), mf.getDefinitions());
207207
}
208208
});
@@ -223,7 +223,7 @@ class TemplateLoader {
223223
tsFiles.forEach((file) => {
224224
const resolvedPath = slash(fsPath.resolve(path));
225225
const resolvedFilePath = slash(fsPath.resolve(file.name));
226-
const truncatedPath = resolvedFilePath.replace(resolvedPath+'/', '');
226+
const truncatedPath = resolvedFilePath.replace(resolvedPath + '/', '');
227227
template.getLogicManager().addLogicFile(file.contents, truncatedPath);
228228
Logger.debug(method, `Loaded ${truncatedPath}`, file.contents);
229229
});

packages/cicero-core/test/template.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,36 @@ describe('Template', () => {
368368
const template2 = await Template.fromArchive(buffer);
369369
template2.getMetadata().getLogo().should.be.an.instanceof(Buffer);
370370
});
371+
372+
it('should await updateExternalModels before returning from fromArchive', async () => {
373+
const { ModelManager } = require('@accordproject/concerto-core');
374+
375+
// Build a buffer offline so no network call during setup
376+
const source = await Template.fromDirectory('./test/data/latedeliveryandpenalty', { offline: true });
377+
const buffer = await source.toArchive('es6');
378+
379+
// Patch updateExternalModels with an async spy that sets a flag when resolved
380+
let externalModelsResolved = false;
381+
const original = ModelManager.prototype.updateExternalModels;
382+
ModelManager.prototype.updateExternalModels = async function () {
383+
// Simulate a small async delay (e.g. network call)
384+
await new Promise(resolve => setTimeout(resolve, 50));
385+
externalModelsResolved = true;
386+
};
387+
388+
try {
389+
// Call fromArchive without offline flag — should hit updateExternalModels path
390+
await Template.fromArchive(buffer);
391+
} finally {
392+
ModelManager.prototype.updateExternalModels = original;
393+
}
394+
395+
// If fromArchive correctly awaits updateExternalModels, the flag must be true here
396+
assert.isTrue(
397+
externalModelsResolved,
398+
'fromArchive() must await updateExternalModels() before returning'
399+
);
400+
});
371401
});
372402

373403
describe('#fromCompiledArchive', () => {

0 commit comments

Comments
 (0)