Skip to content

Commit d7b40fc

Browse files
committed
patch(fix): octo model-serialization to deserialize overlays.
1 parent b46e159 commit d7b40fc

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

packages/octo/src/services/serialization/model/__snapshots__/model-serialization.service.spec.ts.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,59 @@ exports[`Model Serialization Service UT serialize() should serialize only bounda
337337
}
338338
`;
339339

340+
exports[`Model Serialization Service UT serialize() should serialize overlay with multiple anchors of same parent 1`] = `
341+
{
342+
"anchors": [
343+
{
344+
"anchorId": "anchor-1",
345+
"className": "TestAnchor",
346+
"parent": {
347+
"context": "app=test-app",
348+
},
349+
},
350+
{
351+
"anchorId": "anchor-2",
352+
"className": "TestAnchor",
353+
"parent": {
354+
"context": "app=test-app",
355+
},
356+
},
357+
],
358+
"dependencies": [],
359+
"models": {
360+
"app=test-app": {
361+
"className": "App",
362+
"model": {
363+
"name": "test-app",
364+
},
365+
},
366+
},
367+
"overlays": [
368+
{
369+
"className": "TestOverlay",
370+
"overlay": {
371+
"anchors": [
372+
{
373+
"anchorId": "anchor-1",
374+
"parent": {
375+
"context": "app=test-app",
376+
},
377+
},
378+
{
379+
"anchorId": "anchor-2",
380+
"parent": {
381+
"context": "app=test-app",
382+
},
383+
},
384+
],
385+
"overlayId": "overlay-1",
386+
"properties": {},
387+
},
388+
},
389+
],
390+
}
391+
`;
392+
340393
exports[`Model Serialization Service UT serialize() should serialize when multiple models have dependency on same model 1`] = `
341394
{
342395
"anchors": [],

packages/octo/src/services/serialization/model/model-serialization.service.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TestAnchor, TestOverlay } from '../../../../test/helpers/test-classes.js';
12
import { ModelSerializedOutput } from '../../../app.type.js';
23
import { Container } from '../../../decorators/container.js';
34
import { IDependency } from '../../../functions/dependency/dependency.js';
@@ -9,13 +10,16 @@ import { Server } from '../../../models/server/server.model.js';
910
import { Service } from '../../../models/service/service.model.js';
1011
import { Subnet } from '../../../models/subnet/subnet.model.js';
1112
import { OverlayDataRepository, OverlayDataRepositoryFactory } from '../../../overlays/overlay-data.repository.js';
13+
import { OverlayService, OverlayServiceFactory } from '../../../overlays/overlay.service.js';
1214
import { ModelSerializationService, ModelSerializationServiceFactory } from './model-serialization.service.js';
1315

1416
describe('Model Serialization Service UT', () => {
1517
beforeEach(() => {
1618
Container.registerFactory(OverlayDataRepository, OverlayDataRepositoryFactory);
1719
Container.get(OverlayDataRepository, { args: [true, [], []] });
1820

21+
Container.registerFactory(OverlayService, OverlayServiceFactory);
22+
1923
Container.registerFactory(ModelSerializationService, ModelSerializationServiceFactory);
2024
Container.get(ModelSerializationService, { args: [true] });
2125
});
@@ -97,6 +101,18 @@ describe('Model Serialization Service UT', () => {
97101
expect(app1.name).toBe('test-app');
98102
});
99103

104+
it('should deserialize a single model', async () => {
105+
const app_0 = new App('test-app');
106+
107+
const service = await Container.get(ModelSerializationService);
108+
service.registerClass('App', App);
109+
110+
const output = await service.serialize(app_0);
111+
const app_1 = (await service.deserialize(output)) as App;
112+
113+
expect(app_1.name).toBe('test-app');
114+
});
115+
100116
it('should return the serialized root on deserialization', async () => {
101117
const app0 = new App('test-app');
102118
const region0 = new Region('region-0');
@@ -142,6 +158,31 @@ describe('Model Serialization Service UT', () => {
142158
);
143159
expect(newAppDependencies).toEqual(oldAppDependencies);
144160
});
161+
162+
it('should deserialize overlay with multiple anchors of same parent', async () => {
163+
const app_0 = new App('test-app');
164+
const anchor1 = new TestAnchor('anchor-1', app_0);
165+
const anchor2 = new TestAnchor('anchor-2', app_0);
166+
app_0['anchors'].push(anchor1, anchor2);
167+
168+
const service = await Container.get(ModelSerializationService);
169+
service.registerClass('App', App);
170+
service.registerClass('TestAnchor', TestAnchor);
171+
service.registerClass('TestOverlay', TestOverlay);
172+
const overlayService = await Container.get(OverlayService);
173+
174+
const overlay1_0 = new TestOverlay('overlay-1', {}, [anchor1, anchor2]);
175+
await overlayService.addOverlay(overlay1_0);
176+
177+
const appSerialized = await service.serialize(app_0);
178+
const app_1 = (await service.deserialize(appSerialized)) as App;
179+
180+
const overlay1_1 = await overlayService.getOverlayById('overlay-1');
181+
expect(overlay1_1!.getAnchors().map((a) => a.getParent().getContext())).toEqual([
182+
app_1.getContext(),
183+
app_1.getContext(),
184+
]);
185+
});
145186
});
146187

147188
describe('serialize()', () => {
@@ -197,5 +238,20 @@ describe('Model Serialization Service UT', () => {
197238

198239
expect(await service.serialize(app)).toMatchSnapshot();
199240
});
241+
242+
it('should serialize overlay with multiple anchors of same parent', async () => {
243+
const service = await Container.get(ModelSerializationService);
244+
const overlayService = await Container.get(OverlayService);
245+
246+
const app = new App('test-app');
247+
const anchor1 = new TestAnchor('anchor-1', app);
248+
const anchor2 = new TestAnchor('anchor-2', app);
249+
app['anchors'].push(anchor1, anchor2);
250+
251+
const overlay1 = new TestOverlay('overlay-1', {}, [anchor1, anchor2]);
252+
await overlayService.addOverlay(overlay1);
253+
254+
expect(await service.serialize(app)).toMatchSnapshot();
255+
});
200256
});
201257
});

packages/octo/src/services/serialization/model/model-serialization.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export class ModelSerializationService {
9191
this.overlayDataRepository['newOverlays'] = newOverlays;
9292

9393
// If no dependencies to serialize, return the first seen model.
94-
return serializedOutput.dependencies.length > 0 ? seen[serializedOutput.dependencies[0].from] : seen[0];
94+
return serializedOutput.dependencies.length > 0
95+
? seen[serializedOutput.dependencies[0].from]
96+
: seen[Object.keys(seen)[0]];
9597
}
9698

9799
registerClass(className: string, deserializationClass: any): void {

0 commit comments

Comments
 (0)