|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import { MarkdownTreeParser, createParser, extractSection } from '../index.js'; |
| 8 | +import { MarkdownCLI } from '../bin/md-tree.js'; // Added for checkLinks test |
8 | 9 | import fs from 'node:fs/promises'; |
9 | 10 | import path from 'node:path'; |
10 | 11 | import { fileURLToPath } from 'node:url'; |
@@ -583,6 +584,101 @@ Plain text with email@domain.com should not be a link. |
583 | 584 | ); |
584 | 585 | }); |
585 | 586 |
|
| 587 | + // Test suite for checkLinks |
| 588 | + await test('Check Links Functionality', async () => { |
| 589 | + const cli = new MarkdownCLI(); |
| 590 | + const testReferenceLinksFile = path.join( |
| 591 | + __dirname, |
| 592 | + 'test-reference-links.md' |
| 593 | + ); |
| 594 | + const testDummySampleFilePath = path.join( |
| 595 | + __dirname, |
| 596 | + 'test-dummy-sample.md' |
| 597 | + ); |
| 598 | + |
| 599 | + // Create a unique dummy file for local link checking to pass |
| 600 | + await fs.writeFile( |
| 601 | + testDummySampleFilePath, |
| 602 | + '# Test Dummy Sample File\n\nThis is a test dummy file for testing links.' |
| 603 | + ); |
| 604 | + |
| 605 | + await test('Check reference-style links', async () => { |
| 606 | + const markdownContent = await fs.readFile( |
| 607 | + testReferenceLinksFile, |
| 608 | + 'utf-8' |
| 609 | + ); |
| 610 | + const tree = await cli.parser.parse(markdownContent); |
| 611 | + const links = cli.parser.selectAll(tree, 'link'); |
| 612 | + const definitions = cli.parser.selectAll(tree, 'definition'); |
| 613 | + |
| 614 | + const collectedUrls = new Set(); |
| 615 | + for (const link of links) { |
| 616 | + if (link.url) collectedUrls.add(link.url); |
| 617 | + } |
| 618 | + for (const def of definitions) { |
| 619 | + if (def.url) collectedUrls.add(def.url); |
| 620 | + } |
| 621 | + |
| 622 | + assert( |
| 623 | + collectedUrls.has('https://www.google.com'), |
| 624 | + 'Should find Google link from definition' |
| 625 | + ); |
| 626 | + assert( |
| 627 | + collectedUrls.has('./test-dummy-sample.md'), |
| 628 | + 'Should find local-doc link from definition' |
| 629 | + ); |
| 630 | + assert( |
| 631 | + !collectedUrls.has('[undefined]'), |
| 632 | + 'Should not treat undefined reference as a URL' |
| 633 | + ); |
| 634 | + // The following assertions were removed because selectAll(tree, 'link') for reference-style links |
| 635 | + // correctly returns linkReference nodes which do not have the 'url' property populated directly. |
| 636 | + // The url is resolved by checkLinks by looking at 'definition' nodes, |
| 637 | + // which is already tested by the collectedUrls assertions and the checkLinks output assertions. |
| 638 | + // assert(links.some(link => link.reference === 'google' && link.url === 'https://www.google.com'), 'Link object for google should have url'); |
| 639 | + // assert(links.some(link => link.reference === 'local-doc' && link.url === './sample.md'), 'Link object for local-doc should have url'); |
| 640 | + |
| 641 | + // Test the checkLinks output |
| 642 | + const originalConsoleLog = console.log; |
| 643 | + const logs = []; |
| 644 | + console.log = (...args) => { |
| 645 | + logs.push(args.join(' ')); |
| 646 | + }; |
| 647 | + |
| 648 | + try { |
| 649 | + await cli.checkLinks(testReferenceLinksFile); |
| 650 | + } finally { |
| 651 | + console.log = originalConsoleLog; |
| 652 | + // Clean up test dummy file |
| 653 | + try { |
| 654 | + await fs.unlink(testDummySampleFilePath); |
| 655 | + } catch (error) { |
| 656 | + // Ignore cleanup errors - file might not exist |
| 657 | + console.warn( |
| 658 | + `Warning: Could not clean up ${testDummySampleFilePath}:`, |
| 659 | + error.message |
| 660 | + ); |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + const output = logs.join('\n'); |
| 665 | + assert( |
| 666 | + output.includes('🔗 Checking 2 unique URLs'), |
| 667 | + 'Should check 2 unique URLs' |
| 668 | + ); |
| 669 | + assert( |
| 670 | + output.includes('✅ https://www.google.com'), |
| 671 | + 'Should successfully check Google link' |
| 672 | + ); |
| 673 | + // Note: The actual local file path in output might be resolved. |
| 674 | + // We check for the original URL './test-dummy-sample.md' and the "✅" status. |
| 675 | + assert( |
| 676 | + output.includes('✅ ./test-dummy-sample.md'), |
| 677 | + 'Should successfully check local test dummy sample file link' |
| 678 | + ); |
| 679 | + }); |
| 680 | + }); |
| 681 | + |
586 | 682 | // Summary |
587 | 683 | console.log(`\n📊 Test Results: ${passedTests}/${testCount} passed`); |
588 | 684 |
|
|
0 commit comments