|
13 | 13 | ArchitectureModel, |
14 | 14 | CodeMapping, |
15 | 15 | Container, |
| 16 | + ContainerKind, |
16 | 17 | Context, |
17 | 18 | Layer, |
18 | 19 | ) |
@@ -637,3 +638,181 @@ def test_enricher_exact_root_match(): |
637 | 638 |
|
638 | 639 | # node3: prefix but not subdirectory - should NOT match |
639 | 640 | assert enriched.nodes[2].container is None |
| 641 | + |
| 642 | + |
| 643 | +# ---------------------------- |
| 644 | +# v2: Nested container enrichment |
| 645 | +# ---------------------------- |
| 646 | + |
| 647 | + |
| 648 | +def build_v2_test_model() -> ArchitectureModel: |
| 649 | + """Build a v2 model with nested containers for enrichment tests.""" |
| 650 | + contexts = { |
| 651 | + "billing": Context(id="billing", name="Billing Context"), |
| 652 | + } |
| 653 | + containers = { |
| 654 | + "billing-service": Container( |
| 655 | + id="billing-service", |
| 656 | + kind=ContainerKind.SERVICE, |
| 657 | + context="billing", |
| 658 | + code=CodeMapping( |
| 659 | + roots=("services/billing",), |
| 660 | + layers={ |
| 661 | + "api": Layer(id="api", patterns=("services/billing/api/**",)), |
| 662 | + "domain": Layer(id="domain", patterns=("services/billing/domain/**",)), |
| 663 | + }, |
| 664 | + ), |
| 665 | + tags=("critical",), |
| 666 | + children={ |
| 667 | + "invoice-module": Container( |
| 668 | + id="invoice-module", |
| 669 | + kind=ContainerKind.MODULE, |
| 670 | + code=CodeMapping( |
| 671 | + roots=("services/billing/domain/invoice",), |
| 672 | + layers={ |
| 673 | + "model": Layer(id="model", patterns=("services/billing/domain/invoice/model/**",)), |
| 674 | + "repo": Layer(id="repo", patterns=("services/billing/domain/invoice/repo/**",)), |
| 675 | + }, |
| 676 | + ), |
| 677 | + ), |
| 678 | + }, |
| 679 | + ), |
| 680 | + "shared-utils": Container( |
| 681 | + id="shared-utils", |
| 682 | + kind=ContainerKind.LIBRARY, |
| 683 | + code=CodeMapping( |
| 684 | + roots=("libs/shared",), |
| 685 | + layers={}, |
| 686 | + ), |
| 687 | + ), |
| 688 | + } |
| 689 | + model = ArchitectureModel( |
| 690 | + version=2, |
| 691 | + contexts=contexts, |
| 692 | + containers=containers, |
| 693 | + relations=(), |
| 694 | + metadata={}, |
| 695 | + ) |
| 696 | + return DefaultModelResolver().resolve(model) |
| 697 | + |
| 698 | + |
| 699 | +def test_enricher_v2_nested_container_deepest_match(): |
| 700 | + """Nested container with more specific root wins over parent.""" |
| 701 | + model = build_v2_test_model() |
| 702 | + |
| 703 | + node = IRNode( |
| 704 | + id=CanonicalId(language=Language.PYTHON, code_root="billing", fqname="invoice.model.entity"), |
| 705 | + kind=SymbolKind.MODULE, |
| 706 | + path="services/billing/domain/invoice/model/entity.py", |
| 707 | + ) |
| 708 | + ir = ArchitectureIR( |
| 709 | + schema_version=2, produced_by="test", repo_root="/test", |
| 710 | + nodes=(node,), edges=(), metadata={}, |
| 711 | + ) |
| 712 | + |
| 713 | + enriched = DefaultArchitectureEnricher().enrich(ir, model) |
| 714 | + n = enriched.nodes[0] |
| 715 | + |
| 716 | + # Should match the nested container (longer root) |
| 717 | + assert n.container == "billing-service.invoice-module" |
| 718 | + assert n.layer == "model" |
| 719 | + assert n.context == "billing" # inherited from parent |
| 720 | + assert n.service == "billing-service" |
| 721 | + assert n.container_kind == "module" |
| 722 | + |
| 723 | + |
| 724 | +def test_enricher_v2_parent_container_match(): |
| 725 | + """Node under parent root but not nested child root matches parent.""" |
| 726 | + model = build_v2_test_model() |
| 727 | + |
| 728 | + node = IRNode( |
| 729 | + id=CanonicalId(language=Language.PYTHON, code_root="billing", fqname="billing.api.routes"), |
| 730 | + kind=SymbolKind.MODULE, |
| 731 | + path="services/billing/api/routes.py", |
| 732 | + ) |
| 733 | + ir = ArchitectureIR( |
| 734 | + schema_version=2, produced_by="test", repo_root="/test", |
| 735 | + nodes=(node,), edges=(), metadata={}, |
| 736 | + ) |
| 737 | + |
| 738 | + enriched = DefaultArchitectureEnricher().enrich(ir, model) |
| 739 | + n = enriched.nodes[0] |
| 740 | + |
| 741 | + assert n.container == "billing-service" |
| 742 | + assert n.layer == "api" |
| 743 | + assert n.service == "billing-service" |
| 744 | + assert n.container_kind == "service" |
| 745 | + |
| 746 | + |
| 747 | +def test_enricher_v2_library_container(): |
| 748 | + """Library container sets container_kind correctly.""" |
| 749 | + model = build_v2_test_model() |
| 750 | + |
| 751 | + node = IRNode( |
| 752 | + id=CanonicalId(language=Language.PYTHON, code_root="shared", fqname="libs.shared.util"), |
| 753 | + kind=SymbolKind.MODULE, |
| 754 | + path="libs/shared/util.py", |
| 755 | + ) |
| 756 | + ir = ArchitectureIR( |
| 757 | + schema_version=2, produced_by="test", repo_root="/test", |
| 758 | + nodes=(node,), edges=(), metadata={}, |
| 759 | + ) |
| 760 | + |
| 761 | + enriched = DefaultArchitectureEnricher().enrich(ir, model) |
| 762 | + n = enriched.nodes[0] |
| 763 | + |
| 764 | + assert n.container == "shared-utils" |
| 765 | + assert n.service == "shared-utils" |
| 766 | + assert n.container_kind == "library" |
| 767 | + |
| 768 | + |
| 769 | +def test_enricher_v2_edge_service_and_kind(): |
| 770 | + """Edges get src/dst service and container_kind from enriched nodes.""" |
| 771 | + model = build_v2_test_model() |
| 772 | + |
| 773 | + src_node = IRNode( |
| 774 | + id=CanonicalId(language=Language.PYTHON, code_root="billing", fqname="billing.api.routes"), |
| 775 | + kind=SymbolKind.MODULE, |
| 776 | + path="services/billing/api/routes.py", |
| 777 | + ) |
| 778 | + dst_node = IRNode( |
| 779 | + id=CanonicalId(language=Language.PYTHON, code_root="shared", fqname="libs.shared.util"), |
| 780 | + kind=SymbolKind.MODULE, |
| 781 | + path="libs/shared/util.py", |
| 782 | + ) |
| 783 | + edge = IREdge(src=src_node.id, dst=dst_node.id, dep_type=DepType.IMPORT) |
| 784 | + |
| 785 | + ir = ArchitectureIR( |
| 786 | + schema_version=2, produced_by="test", repo_root="/test", |
| 787 | + nodes=(src_node, dst_node), edges=(edge,), metadata={}, |
| 788 | + ) |
| 789 | + |
| 790 | + enriched = DefaultArchitectureEnricher().enrich(ir, model) |
| 791 | + e = enriched.edges[0] |
| 792 | + |
| 793 | + assert e.src_service == "billing-service" |
| 794 | + assert e.src_container_kind == "service" |
| 795 | + assert e.dst_service == "shared-utils" |
| 796 | + assert e.dst_container_kind == "library" |
| 797 | + |
| 798 | + |
| 799 | +def test_enricher_v2_unmatched_node_has_no_service(): |
| 800 | + """Nodes that don't match any container have None for service/container_kind.""" |
| 801 | + model = build_v2_test_model() |
| 802 | + |
| 803 | + node = IRNode( |
| 804 | + id=CanonicalId(language=Language.PYTHON, code_root="other", fqname="lib.utils"), |
| 805 | + kind=SymbolKind.MODULE, |
| 806 | + path="other/utils.py", |
| 807 | + ) |
| 808 | + ir = ArchitectureIR( |
| 809 | + schema_version=2, produced_by="test", repo_root="/test", |
| 810 | + nodes=(node,), edges=(), metadata={}, |
| 811 | + ) |
| 812 | + |
| 813 | + enriched = DefaultArchitectureEnricher().enrich(ir, model) |
| 814 | + n = enriched.nodes[0] |
| 815 | + |
| 816 | + assert n.container is None |
| 817 | + assert n.service is None |
| 818 | + assert n.container_kind is None |
0 commit comments