@@ -11,6 +11,14 @@ import type { MCDocument, MCProject, MCUser } from '@mermaidchart/sdk/dist/types
11
11
12
12
/** Config file with auth_key setup */
13
13
const CONFIG_AUTHED = 'test/fixtures/config-authed.toml' ;
14
+ /** Markdown file that has every Mermaid diagrams already linked */
15
+ const LINKED_MARKDOWN_FILE = 'test/fixtures/linked-markdown-file.md' ;
16
+ /** Markdown file that has some linked and some unlinked Mermaid diagrams */
17
+ const PARTIALLY_LINKED_MARKDOWN_FILE = 'test/fixtures/partially-linked-markdown-file.md' ;
18
+ /** Markdown file that has unlinked Mermaid diagrams */
19
+ const UNLINKED_MARKDOWN_FILE = 'test/fixtures/unlinked-markdown-file.md' ;
20
+ /** Markdown file that has non-standard Markdown features like YAML frontmatter */
21
+ const UNUSUAL_MARKDOWN_FILE = 'test/fixtures/unusual-markdown-file.md' ;
14
22
15
23
type Optional < T > = T | undefined ;
16
24
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -269,16 +277,97 @@ describe('link', () => {
269
277
) ;
270
278
} ) ;
271
279
}
280
+
281
+ it ( 'should link diagrams in a markdown file' , async ( ) => {
282
+ const unlinkedMarkdownFile = 'test/output/markdown-file.md' ;
283
+ await copyFile ( UNLINKED_MARKDOWN_FILE , unlinkedMarkdownFile ) ;
284
+
285
+ const { program } = mockedProgram ( ) ;
286
+
287
+ vi . mock ( '@inquirer/confirm' ) ;
288
+ vi . mock ( '@inquirer/select' ) ;
289
+ vi . mocked ( confirm ) . mockResolvedValue ( true ) ;
290
+ vi . mocked ( select ) . mockResolvedValueOnce ( mockedProjects [ 0 ] . id ) ;
291
+
292
+ vi . mocked ( MermaidChart . prototype . createDocument )
293
+ . mockResolvedValueOnce ( mockedEmptyDiagram )
294
+ . mockResolvedValueOnce ( { ...mockedEmptyDiagram , documentID : 'second-id' } ) ;
295
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'link' , unlinkedMarkdownFile ] , {
296
+ from : 'user' ,
297
+ } ) ;
298
+
299
+ const file = await readFile ( unlinkedMarkdownFile , { encoding : 'utf8' } ) ;
300
+
301
+ expect ( file ) . toMatch ( `id: ${ mockedEmptyDiagram . documentID } \n` ) ;
302
+ expect ( file ) . toMatch ( `id: second-id\n` ) ;
303
+ } ) ;
304
+
305
+ it ( 'should link diagrams in partially linked markdown file' , async ( ) => {
306
+ const partiallyLinkedMarkdownFile = 'test/output/partially-linked-markdown-file.md' ;
307
+ await copyFile ( PARTIALLY_LINKED_MARKDOWN_FILE , partiallyLinkedMarkdownFile ) ;
308
+
309
+ const { program } = mockedProgram ( ) ;
310
+
311
+ vi . mock ( '@inquirer/confirm' ) ;
312
+ vi . mock ( '@inquirer/select' ) ;
313
+ vi . mocked ( confirm ) . mockResolvedValue ( true ) ;
314
+ vi . mocked ( select ) . mockResolvedValueOnce ( mockedProjects [ 0 ] . id ) ;
315
+
316
+ vi . mocked ( MermaidChart . prototype . createDocument ) . mockResolvedValueOnce ( {
317
+ ...mockedEmptyDiagram ,
318
+ documentID : 'second-id' ,
319
+ } ) ;
320
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'link' , partiallyLinkedMarkdownFile ] , {
321
+ from : 'user' ,
322
+ } ) ;
323
+
324
+ const file = await readFile ( partiallyLinkedMarkdownFile , { encoding : 'utf8' } ) ;
325
+
326
+ expect ( file ) . toMatch ( `id: second-id\n` ) ;
327
+ } ) ;
328
+
329
+ it ( 'should handle unusual markdown formatting' , async ( ) => {
330
+ const unusualMarkdownFile = 'test/output/unusual-markdown-file.md' ;
331
+ await copyFile ( UNUSUAL_MARKDOWN_FILE , unusualMarkdownFile ) ;
332
+
333
+ const { program } = mockedProgram ( ) ;
334
+
335
+ vi . mock ( '@inquirer/confirm' ) ;
336
+ vi . mock ( '@inquirer/select' ) ;
337
+ vi . mocked ( confirm ) . mockResolvedValue ( true ) ;
338
+ vi . mocked ( select ) . mockResolvedValueOnce ( mockedProjects [ 0 ] . id ) ;
339
+
340
+ vi . mocked ( MermaidChart . prototype . createDocument ) . mockResolvedValueOnce ( {
341
+ ...mockedEmptyDiagram ,
342
+ documentID : 'my-mocked-diagram-id' ,
343
+ } ) ;
344
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'link' , unusualMarkdownFile ] , {
345
+ from : 'user' ,
346
+ } ) ;
347
+
348
+ const file = await readFile ( unusualMarkdownFile , { encoding : 'utf8' } ) ;
349
+
350
+ const idLineRegex = / ^ .* i d : m y - m o c k e d - d i a g r a m - i d \n / gm;
351
+
352
+ expect ( file ) . toMatch ( idLineRegex ) ;
353
+ // other than the added `id: xxxx` field, everything else should be identical,
354
+ // although in practice, we'd expect some formatting changes
355
+ expect ( file . replace ( idLineRegex , '' ) ) . toStrictEqual (
356
+ await readFile ( UNUSUAL_MARKDOWN_FILE , { encoding : 'utf8' } ) ,
357
+ ) ;
358
+ } ) ;
272
359
} ) ;
273
360
274
361
describe ( 'pull' , ( ) => {
275
362
const diagram = 'test/output/connected-diagram.mmd' ;
276
363
const diagram2 = 'test/output/connected-diagram-2.mmd' ;
364
+ const linkedMarkdownFile = 'test/output/linked-markdown-file.md' ;
277
365
278
366
beforeEach ( async ( ) => {
279
367
await Promise . all ( [
280
368
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram ) ,
281
369
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram2 ) ,
370
+ copyFile ( LINKED_MARKDOWN_FILE , linkedMarkdownFile ) ,
282
371
] ) ;
283
372
} ) ;
284
373
@@ -327,16 +416,41 @@ title: My cool flowchart
327
416
expect ( diagramContents ) . toContain ( "flowchart TD\n A[I've been updated!]" ) ;
328
417
}
329
418
} ) ;
419
+
420
+ it ( 'should pull documents from within markdown file' , async ( ) => {
421
+ const { program } = mockedProgram ( ) ;
422
+
423
+ vi . mocked ( MermaidChart . prototype . getDocument )
424
+ . mockResolvedValueOnce ( {
425
+ ...mockedEmptyDiagram ,
426
+ code : "flowchart TD\n A[I've been updated!]" ,
427
+ } )
428
+ . mockResolvedValueOnce ( {
429
+ ...mockedEmptyDiagram ,
430
+ code : 'pie\n "Flowchart" : 2' ,
431
+ } ) ;
432
+
433
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'pull' , linkedMarkdownFile ] , {
434
+ from : 'user' ,
435
+ } ) ;
436
+
437
+ const file = await readFile ( linkedMarkdownFile , { encoding : 'utf8' } ) ;
438
+
439
+ expect ( file ) . toMatch ( "flowchart TD\n A[I've been updated!]" ) ;
440
+ expect ( file ) . toMatch ( 'pie\n "Flowchart" : 2' ) ;
441
+ } ) ;
330
442
} ) ;
331
443
332
444
describe ( 'push' , ( ) => {
333
445
const diagram = 'test/output/connected-diagram.mmd' ;
334
446
const diagram2 = 'test/output/connected-diagram-2.mmd' ;
447
+ const linkedMarkdownFile = 'test/output/linked-markdown-file.md' ;
335
448
336
449
beforeEach ( async ( ) => {
337
450
await Promise . all ( [
338
451
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram ) ,
339
452
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram2 ) ,
453
+ copyFile ( LINKED_MARKDOWN_FILE , linkedMarkdownFile ) ,
340
454
] ) ;
341
455
} ) ;
342
456
@@ -368,4 +482,16 @@ describe('push', () => {
368
482
} ) ,
369
483
) ;
370
484
} ) ;
485
+
486
+ it ( 'should push documents from within markdown file' , async ( ) => {
487
+ const { program } = mockedProgram ( ) ;
488
+
489
+ vi . mocked ( MermaidChart . prototype . getDocument ) . mockResolvedValue ( mockedEmptyDiagram ) ;
490
+
491
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'push' , linkedMarkdownFile ] , {
492
+ from : 'user' ,
493
+ } ) ;
494
+
495
+ expect ( vi . mocked ( MermaidChart . prototype . setDocument ) ) . toHaveBeenCalledTimes ( 2 ) ;
496
+ } ) ;
371
497
} ) ;
0 commit comments