"""Figure Engine (Component 8) unit tests — no DB, no binary deps.""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from app.figure.engine import ( bar_chart_panel, domain_blocks_panel, esc, wrap_text, ) from app.services.experiment_figures import build_experiment_figure P53_CONTEXT = { "blast": { "count": 12, "top_hit": {"accession": "P04637", "description": "Cellular tumor antigen p53", "identity_pct": 100.0}, "hits": [ {"accession": "P04637", "description": "Cellular tumor antigen p53", "identity_pct": 100.0}, {"accession": "Q9TTA1", "description": "Cellular tumor antigen p53 tupaia", "identity_pct": 99.7}, {"accession": "P02340", "description": "Cellular tumor antigen p53 mouse", "identity_pct": 87.2}, ], }, "uniprot": {"accession": "P04637", "full_name": "Cellular tumor antigen p53", "gene_names": ["TP53"], "organism": "Homo sapiens"}, "msa": {"sequence_count": 6, "method": "clustalo", "alignment_mode": "global"}, "phylo": {"phylotree_newick": "(P04637:0.001,(P02340:0.02,Q9TTA1:0.03):0.005);"}, "domains": {"sequence_length": 393, "domains": [ {"accession": "IPR012345", "name": "p53_DNA_binding", "start": 92, "end": 292}, {"accession": "IPR045493", "name": "p53_tetramerization", "start": 323, "end": 356}, ]}, "interpret": {"interpretation": "The query is homo sapiens p53. Strong homology to P04637."}, } def test_esc_escapes_xml(): assert "<" in esc(" & confusion") def test_wrap_text_merges_short_phrases(): lines = wrap_text("a b c", 5) assert lines and "a b c" in lines def test_bar_chart_renders_bars_and_labels(): svg = bar_chart_panel([("A", 100.0), ("B", 50.0)], x=0, y=0, w=300, h=120) assert svg.count("") assert svg.count("") for letter in "ABCDEF": assert letter in svg assert "BioNexus experiment analysis" in svg assert "Generated by BioNexus Figure Engine" in svg assert "P04637" in svg assert "InterPro" in svg.lower() or "interpro" in svg.lower() def test_build_experiment_figure_empty_context_still_valid(): svg = build_experiment_figure(None, "exp-empty") assert svg.count("") assert "No data" in svg or "No domain data" in svg def test_build_experiment_figure_panel_count(): svg = build_experiment_figure(P53_CONTEXT) assert svg.count("= 6 # one label badge per panel