-
Notifications
You must be signed in to change notification settings - Fork 139
feat(vscode): import SVG to calm json file #3003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
byrash
wants to merge
8
commits into
finos:main
Choose a base branch
from
fidelity-contributions:feat/import-svg-to-calm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
249b2f1
feat(vscode): import SVG to calm json file
byrash 6d4c200
Merge branch 'main' into feat/import-svg-to-calm
byrash d49e1c4
feat(vscode plugin): Address Rveview Comments
byrash d2fb12a
Merge branch 'main' into feat/import-svg-to-calm
byrash e9e1257
fix(vscode): address PR #3003 review feedback for SVG import
byrash 71ae705
fix(vscode): address PR #3003 review feedback for SVG import
byrash c65ebd3
fix(vscode): resolve merge conflict in drawio-parser lenient decode
byrash 54d9c95
fix(vscode): address PR #3003 review feedback for SVG import
byrash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...plugins/vscode/src/extension/services/svg-import/__fixtures__/drawio-nested.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions
9
...plugins/vscode/src/extension/services/svg-import/__fixtures__/drawio-simple.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions
22
...lugins/vscode/src/extension/services/svg-import/__fixtures__/generic-simple.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
262 changes: 262 additions & 0 deletions
262
calm-plugins/vscode/src/extension/services/svg-import/calm-builder.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { buildCalmJson } from './calm-builder'; | ||
| import type { ParsedSvgGraph } from './types'; | ||
|
|
||
| describe('buildCalmJson', () => { | ||
| it('produces valid CALM JSON for a simple graph', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'API Gateway', shapeHint: 'rounded-rectangle', geometry: { x: 100, y: 50, width: 200, height: 60 }, styleProps: {} }, | ||
| { id: 'v2', label: 'User DB', shapeHint: 'cylinder', geometry: { x: 400, y: 50, width: 120, height: 80 }, styleProps: {} }, | ||
| ], | ||
| edges: [ | ||
| { id: 'e1', sourceId: 'v1', targetId: 'v2', label: 'JDBC' }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
|
|
||
| expect(doc.$schema).toBe('https://calm.finos.org/release/1.2/meta/calm.json'); | ||
| expect(doc.nodes).toHaveLength(2); | ||
| expect(doc.relationships).toHaveLength(1); | ||
| expect(result.nodeCount).toBe(2); | ||
| expect(result.relationshipCount).toBe(1); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('generates correct node types from shape hints', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Admin', shapeHint: 'person', geometry: { x: 0, y: 0, width: 40, height: 60 }, styleProps: {} }, | ||
| { id: 'v2', label: 'Orders', shapeHint: 'cylinder', geometry: { x: 200, y: 0, width: 80, height: 80 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
|
|
||
| expect(doc.nodes[0]['node-type']).toBe('actor'); | ||
| expect(doc.nodes[1]['node-type']).toBe('database'); | ||
| }); | ||
|
|
||
| it('generates connects relationships from edges', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'a', label: 'Source', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| { id: 'b', label: 'Target', shapeHint: 'rectangle', geometry: { x: 200, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [ | ||
| { id: 'e1', sourceId: 'a', targetId: 'b', label: 'HTTP' }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| const rel = doc.relationships[0]; | ||
|
|
||
| expect(rel['relationship-type'].connects.source.node).toBe('system-source'); | ||
| expect(rel['relationship-type'].connects.destination.node).toBe('system-target'); | ||
| expect(rel.description).toBe('HTTP'); | ||
| }); | ||
|
|
||
| it('generates deployed-in relationships from containment', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'container', label: 'VPC', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 500, height: 400 }, styleProps: {} }, | ||
| { id: 'child1', label: 'Service A', shapeHint: 'rounded-rectangle', geometry: { x: 50, y: 50, width: 150, height: 60 }, parentId: 'container', styleProps: {} }, | ||
| { id: 'child2', label: 'Service B', shapeHint: 'rounded-rectangle', geometry: { x: 250, y: 50, width: 150, height: 60 }, parentId: 'container', styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| const deployedIn = doc.relationships.find((r: any) => r['relationship-type']['deployed-in']); | ||
|
|
||
| expect(deployedIn).toBeDefined(); | ||
| expect(deployedIn['relationship-type']['deployed-in'].container).toBe('network-vpc'); | ||
| expect(deployedIn['relationship-type']['deployed-in'].nodes).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('preserves layout in metadata._layout', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Node', shapeHint: 'rectangle', geometry: { x: 123.5, y: 456.7, width: 200, height: 60 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
|
|
||
| expect(doc.metadata._layout['system-node']).toEqual({ x: 124, y: 457, w: 200, h: 60 }); | ||
| }); | ||
|
|
||
| it('warns about unresolved edges', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Only Node', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [ | ||
| { id: 'e1', sourceId: 'v1', targetId: 'missing' }, | ||
| ], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| expect(result.warnings).toHaveLength(1); | ||
| expect(result.warnings[0]).toContain('could not resolve'); | ||
| }); | ||
|
|
||
| it('generates unique IDs from labels', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'My Great Service!', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0]['unique-id']).toBe('service-my-great-service'); | ||
| }); | ||
|
|
||
| it('handles nodes with empty labels', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: '', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0]['unique-id']).toBe('system-1'); | ||
| expect(doc.nodes[0].name).toBe('Unnamed system 1'); | ||
| }); | ||
|
|
||
| it('preserves fill and font colors as fidelity-style metadata', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Styled', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: { fillColor: '#1c4587', fontColor: '#ffffff' } }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0].metadata).toEqual({ | ||
| 'fidelity-style': { background: '#1c4587', text: '#ffffff' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('omits fidelity-style when no colors are set', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Plain', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0].metadata).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('detects containers by geometry when no explicit parentId', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'c1', label: 'ECTA Zone', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 600, height: 400 }, styleProps: { dashed: '1', fillColor: 'none' } }, | ||
| { id: 'n1', label: 'API', shapeHint: 'rounded-rectangle', geometry: { x: 50, y: 50, width: 150, height: 60 }, styleProps: { fillColor: '#38761d' } }, | ||
| { id: 'n2', label: 'DB', shapeHint: 'cylinder', geometry: { x: 300, y: 50, width: 100, height: 80 }, styleProps: { fillColor: '#1c4587' } }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| const deployedIn = doc.relationships.find((r: any) => r['relationship-type']['deployed-in']); | ||
|
|
||
| expect(deployedIn).toBeDefined(); | ||
| expect(deployedIn['relationship-type']['deployed-in'].container).toBe('network-ecta-zone'); | ||
| expect(deployedIn['relationship-type']['deployed-in'].nodes).toContain('service-api'); | ||
| expect(deployedIn['relationship-type']['deployed-in'].nodes).toContain('database-db'); | ||
| }); | ||
|
|
||
| it('deduplicates IDs for nodes with the same label', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Service', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| { id: 'v2', label: 'Service', shapeHint: 'rounded-rectangle', geometry: { x: 200, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0]['unique-id']).toBe('service-service'); | ||
| expect(doc.nodes[1]['unique-id']).toBe('service-service-2'); | ||
| }); | ||
|
|
||
| it('deduplicates IDs for multiple collisions', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'Service', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| { id: 'v2', label: 'Service', shapeHint: 'rounded-rectangle', geometry: { x: 200, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| { id: 'v3', label: 'Service', shapeHint: 'rounded-rectangle', geometry: { x: 400, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0]['unique-id']).toBe('service-service'); | ||
| expect(doc.nodes[1]['unique-id']).toBe('service-service-2'); | ||
| expect(doc.nodes[2]['unique-id']).toBe('service-service-3'); | ||
| }); | ||
|
|
||
| it('types nodes with children as network even without style props', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'generic', | ||
| nodes: [ | ||
| { id: 'c1', label: 'Container', shapeHint: 'rectangle', geometry: { x: 0, y: 0, width: 500, height: 400 }, styleProps: {} }, | ||
| { id: 'n1', label: 'Inner Service', shapeHint: 'rounded-rectangle', geometry: { x: 50, y: 50, width: 150, height: 60 }, parentId: 'c1', styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0]['node-type']).toBe('network'); | ||
| }); | ||
|
|
||
| it('splits multi-line labels into name and description', () => { | ||
| const graph: ParsedSvgGraph = { | ||
| sourceFormat: 'drawio', | ||
| nodes: [ | ||
| { id: 'v1', label: 'ECTA\nEnterprise Click to Agree [AP167757]', shapeHint: 'rounded-rectangle', geometry: { x: 0, y: 0, width: 100, height: 50 }, styleProps: {} }, | ||
| ], | ||
| edges: [], | ||
| }; | ||
|
|
||
| const result = buildCalmJson(graph); | ||
| const doc = JSON.parse(result.json); | ||
| expect(doc.nodes[0].name).toBe('ECTA'); | ||
| expect(doc.nodes[0].description).toBe('Enterprise Click to Agree [AP167757]'); | ||
| expect(doc.nodes[0]['unique-id']).toBe('service-ecta'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.