import {
  getCategoricalTermcollectionTw,
  getCategoryGroupsetting,
  getGeneVariantTw,
  getGenesetMutTw
} from "./chunk-7FACFCZK.js";
import {
  termjson
} from "./chunk-DHPLHIVP.js";
import {
  Locator,
  detectGte,
  detectLst,
  detectOne
} from "./chunk-DJQTUDJM.js";
import {
  getRunPp,
  rideInit
} from "./chunk-W55XSG6K.js";
import {
  require_tape
} from "./chunk-QWOE5YTB.js";
import "./chunk-74ZU4BT6.js";
import "./chunk-IW57W7EQ.js";
import "./chunk-ZTYNN6G5.js";
import "./chunk-DKVUOBIE.js";
import "./chunk-PRZWSBMA.js";
import "./chunk-LTOBEUM2.js";
import "./chunk-MKAF2BHB.js";
import "./chunk-WKNI3HRQ.js";
import "./chunk-VGYTAYFK.js";
import "./chunk-67DBIM6H.js";
import {
  getFilterItemByTag,
  hideCategory
} from "./chunk-SA7APTJR.js";
import "./chunk-HJ6L54YS.js";
import "./chunk-LSEFWW72.js";
import "./chunk-SKREEF3H.js";
import "./chunk-HYOEWQ5P.js";
import "./chunk-HBW42TDT.js";
import "./chunk-G6O3URDN.js";
import "./chunk-FN5XPUPH.js";
import "./chunk-IIT367QZ.js";
import "./chunk-RZGEKL77.js";
import "./chunk-UI7OCM2A.js";
import "./chunk-YZK37YCC.js";
import "./chunk-7EMWHCVW.js";
import "./chunk-7IYJZZQI.js";
import "./chunk-M3J4MINX.js";
import "./chunk-PF4DSFDR.js";
import "./chunk-QH6W3NVF.js";
import "./chunk-NOEAT6CX.js";
import "./chunk-4UWS5Y3N.js";
import "./chunk-DD4R5P6W.js";
import "./chunk-JNITUVXP.js";
import "./chunk-KSGA62R2.js";
import "./chunk-LOZEKOES.js";
import "./chunk-TOU7EVFQ.js";
import "./chunk-OAWQ6LOO.js";
import "./chunk-TLT4YIG3.js";
import "./chunk-KYBIQBXE.js";
import "./chunk-I6Y4O3RR.js";
import "./chunk-OMR2DT66.js";
import "./chunk-DQC5FFGV.js";
import {
  __toESM
} from "./chunk-HFNDKYVF.js";

// plots/test/barchart.integration.spec.js
var import_tape = __toESM(require_tape(), 1);

// termdb/test/vocabData.js
var terms = [
  {
    id: "a",
    name: "AAA",
    parent_id: null,
    included_types: ["categorical", "integer", "float", "condition", "survival"],
    child_types: ["categorical", "integer", "float", "condition", "survival"]
  },
  {
    id: "b",
    name: "BBB",
    parent_id: null,
    included_types: ["categorical", "integer", "float", "condition", "survival"],
    child_types: ["categorical", "integer", "float", "condition", "survival"]
  },
  {
    type: "categorical",
    id: "c",
    name: "CCC",
    parent_id: "a",
    isleaf: true,
    groupsetting: {
      disabled: true
    },
    values: {
      1: { label: "Yes" },
      0: { label: "No" }
    },
    included_types: ["categorical"],
    child_types: []
  },
  {
    type: "float",
    id: "d",
    name: "DDD",
    parent_id: "a",
    isleaf: true,
    bins: {
      default: {
        type: "regular-bin",
        bin_size: 0.2,
        stopinclusive: true,
        first_bin: { startunbounded: true, stop: 0.2, stopinclusive: true }
      },
      less: {
        type: "regular-bin",
        bin_size: 0.4,
        stopinclusive: true,
        first_bin: { startunbounded: true, stop: 0.2, stopinclusive: true }
      }
    },
    included_types: ["float"],
    child_types: []
  },
  {
    type: "condition",
    id: "e",
    name: "EEE",
    parent_id: "a",
    isleaf: true,
    groupsetting: {
      disabled: true
    },
    included_types: ["condition"],
    child_types: []
  },
  {
    type: "categorical",
    id: "f",
    name: "FFF",
    parent_id: "b",
    isleaf: true,
    groupsetting: {
      disabled: true
    },
    values: {
      1: { label: "Yes" },
      0: { label: "No" }
    },
    included_types: ["categorical"],
    child_types: []
  },
  {
    type: "categorical",
    id: "g",
    name: "CCC",
    parent_id: "ab",
    isleaf: true,
    groupsetting: {
      disabled: true
    },
    values: {
      1: { label: "Yes" },
      0: { label: "No" }
    },
    included_types: ["categorical"],
    child_types: []
  },
  {
    type: "survival",
    id: "s",
    name: "Survival term",
    isleaf: true,
    values: {
      1: { label: "Deceased" },
      0: { label: "Alive" }
    }
  },
  {
    type: "termCollection",
    name: "age percentages",
    termlst: [{ id: "agedx" }, { id: "a_death" }, { id: "a_ndi" }, { id: "agelastvisit" }],
    numerators: ["agedx"]
  }
];
var vocab = JSON.stringify({
  route: null,
  terms,
  sampleannotation: {
    1: {
      c: 1,
      d: 0.1
    },
    2: {
      c: 0,
      d: 0.5
    },
    3: {
      c: 1,
      d: 0.8
    },
    4: {
      c: 1,
      d: 0.2
    },
    5: {
      c: 0,
      d: 0.4
    },
    6: {
      c: 1,
      d: 0.3
    },
    7: {
      c: 0,
      d: 0.5
    },
    8: {
      c: 1,
      d: 0.9
    },
    9: {
      c: 0,
      d: 0.05
    },
    10: {
      c: 1,
      d: 1.1
    }
  }
});

// plots/test/barchart.integration.spec.js
(0, import_tape.default)("\n", function(test) {
  test.comment("-***- plots/barchart -***-");
  test.end();
});
(0, import_tape.default)("term1=categorical", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "diaggrp"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    testBarCount(barchart);
    testAxisDimension(barchart);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  let barDiv;
  function testBarCount(barchart) {
    barDiv = barchart.Inner.dom.barDiv;
    const minBars = 5;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} Diagnosis Group bars`);
    test.equal(numBars, numOverlays, "should have equal numbers of bars and overlays");
  }
  function testAxisDimension(barchart) {
    const xAxis = barDiv.select(".sjpcb-bar-chart-x-axis").node();
    const seriesG = barDiv.select(".bars-series").node();
    test.true(xAxis.getBBox().width >= seriesG.getBBox().width, "x-axis width should be >= series width");
  }
});
(0, import_tape.default)("term1=categorical (no values)", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "diaggrp_novalues"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    testTw(barchart);
    testBarCount(barchart);
    testAxisDimension(barchart);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testTw(barchart) {
    const tw = barchart.Inner.config.term;
    test.ok(tw.term.values && typeof tw.term.values == "object", "term.values{} should be an object");
    test.ok(Array.isArray(tw.term.categories.lst), "term.categories.lst[] should be an array");
    test.equal(tw.term.categories.lst.length, 7, "term.categories.lst[] should have 7 sample counts");
  }
  let barDiv;
  function testBarCount(barchart) {
    barDiv = barchart.Inner.dom.barDiv;
    const minBars = 5;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} Diagnosis Group bars`);
    test.equal(numBars, numOverlays, "should have equal numbers of bars and overlays");
  }
  function testAxisDimension(barchart) {
    const xAxis = barDiv.select(".sjpcb-bar-chart-x-axis").node();
    const seriesG = barDiv.select(".bars-series").node();
    test.true(xAxis.getBBox().width >= seriesG.getBBox().width, "x-axis width should be >= series width");
  }
});
(0, import_tape.default)("term1=termCollection categorical", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [{ chartType: "summary", childType: "barchart", term: getCategoricalTermcollectionTw() }]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    const barDiv = barchart.Inner.dom.barDiv;
    testBarCount(barDiv);
    testBarOrder(barDiv);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount(barDiv) {
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    test.equal(numBars, 3, "should render 3 bars, one per member term");
  }
  function testBarOrder(barDiv) {
    const labelGs = barDiv.selectAll('[data-testid="sjpcb-bars-rowlabels"] g').nodes();
    test.equal(labelGs.length, 3, "should have 3 row label elements");
    if (labelGs.length < 2) return;
    const yValues = labelGs.map((g) => {
      const t = g.getAttribute("transform") || "";
      const m = t.match(/translate\([^,]+,([^)]+)\)/);
      return m ? parseFloat(m[1]) : 0;
    });
    test.ok(
      yValues[0] < yValues[1] && yValues[1] < yValues[2],
      "row label y positions should increase in termIds order (CNV < Fusion < Germline)"
    );
  }
});
(0, import_tape.default)("term1=categorical, term2=defaultbins", function(test) {
  test.timeoutAfter(5e3);
  test.plan(4);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "diaggrp" },
          term2: { id: "agedx" }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    testOverlayOrder();
    await triggerUncomputableOverlay(barchart);
    clickLegendToHideOverlay(barchart);
    await testHiddenOverlayData(barchart);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 5;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} Diagnosis Group bars`);
    test.true(numOverlays > numBars, "number of overlays should be greater than bars");
  }
  function testOverlayOrder() {
    const bars_grp = barDiv.selectAll(".bars-cell-grp");
    const legend_rows = barDiv.selectAll(".legend-row");
    let overlay_ordered = true;
    const legend_ids = [];
    legend_rows.each((d) => legend_ids.push(d.dataId));
    bars_grp.each((d) => {
      if (!overlay_ordered) return;
      const bar_ids = d.visibleData.map((d2) => d2.dataId);
      overlay_ordered = legend_ids.filter((id) => bar_ids.includes(id)).reduce((bool, id, i) => bool && bar_ids[i] === id, overlay_ordered);
    });
    test.true(overlay_ordered, "overlay order is same as legend");
  }
  async function triggerUncomputableOverlay(barchart) {
    await detectLst({
      target: barchart.Inner.dom.legendDiv.node(),
      // avoid detecting legend entries for descriptive stats,
      // term legend items have a corresponding icon as the first div,
      // making the second div (even-numbered child) the relevant target/selector
      selector: ".sjpp-htmlLegend:nth-child(even)",
      count: 6,
      matchAs: ">=",
      trigger: () => {
        barchart.Inner.app.dispatch({
          type: "plot_edit",
          id: "diaggrp",
          config: {
            term2: {
              id: "aaclassic_5",
              term: termjson["aaclassic_5"],
              q: termjson["aaclassic_5"].bins.default
            }
          }
        });
      }
    });
  }
  const legendDataId = "not exposed";
  async function clickLegendToHideOverlay(barchart) {
    const legendDiv = barchart.Inner.dom.legendDiv;
    const item = legendDiv.selectAll(".sjpp-htmlLegend").filter((d) => d.dataId == legendDataId).node();
    hideCategory(item.__data__, barchart.Inner, true);
  }
  async function testHiddenOverlayData(barchart) {
    const legendDiv = barchart.Inner.dom.legendDiv;
    const item = await detectLst({ elem: legendDiv.node(), selector: ".sjpp-hidden-legend-item", matchAs: ">=" });
    test.equal(item.length, 1, "should hide a clicked uncomputable overlay legend");
  }
});
(0, import_tape.default)("term0=defaultbins, term1=categorical", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "diaggrp" },
          term0: { id: "agedx" }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts > 2, "should have more than 2 charts by Age at Cancer Diagnosis");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=categorical, term2=categorical (patient-level)", function(test) {
  test.timeoutAfter(5e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "diaggrp" },
          term2: { id: "sex" }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 5;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} Diagnosis Group bars`);
    test.true(numOverlays > numBars, "number of overlays should be greater than bars");
  }
});
(0, import_tape.default)("term1=geneVariant with groups", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [{ chartType: "summary", childType: "barchart", term: getGeneVariantTw() }]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant term");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneVariant, term2=geneVariant using region but not gene", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: getGeneVariantTw(),
          term2: getGeneVariantTw(true)
          // gives region but not gene
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant term");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneVariant geneset with groups", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: getGenesetMutTw()
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant gene set");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=categorical, term2=geneVariant", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "diaggrp" },
          term2: { term: { type: "geneVariant", gene: "TP53" }, q: { type: "predefined-groupset" } }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant term");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=categorical (patient-level), term2=geneVariant", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "genetic_race" },
          term2: { term: { type: "geneVariant", gene: "TP53" }, q: { type: "predefined-groupset" } }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 2;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} bars`);
    test.true(numOverlays > numBars, "number of overlays should be greater than bars");
  }
});
(0, import_tape.default)("term1=geneExp, term2=geneVariant SKIPPED", function(test) {
  test.timeoutAfter(1e4);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          term2: { term: { type: "geneVariant", gene: "TP53" }, q: { type: "predefined-groupset" } },
          // must set geneExp q.mode=discrete to show barchart, otherwise it will become violin and not trigger provied postRender for barchart
          term: { term: { type: "geneExpression", gene: "TP53" }, q: { mode: "discrete" } }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant term");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneVariant, term2=geneExp", function(test) {
  test.timeoutAfter(1e4);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: { term: { type: "geneVariant", gene: "TP53" }, q: { type: "predefined-groupset" } },
          term2: { term: { type: "geneExpression", gene: "TP53" } }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testNumCharts
      }
    }
  });
  function testNumCharts(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numCharts = barDiv.selectAll(".pp-sbar-div").size();
    test.true(numCharts == 1, "Should have 1 chart from gene variant term");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneExp", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            term: { type: "geneExpression", gene: "TP53", name: "TP53" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBars = barDiv.selectAll("rect").size();
    test.ok(
      barDiv.selectAll(".pp-sbar-div").size() == 1 && barchart.Inner.config.term.bins.length && numBars,
      "Should correctly render a bar chart for a gene expression term = TP53"
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=numeric term2=geneExp with default bins", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: { id: "agedx" },
          term2: {
            term: { type: "geneExpression", gene: "TP53" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBarCalls = barDiv.selectAll(".bars-cell").size();
    const tableRows = barDiv.selectAll("tr").size();
    test.equal(
      numBarCalls,
      tableRows - 1,
      "Should display the correct number of bars and table rows when overlaid by gene expression term."
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneExp, term2=categorical", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            term: { gene: "TP53", type: "geneExpression" },
            q: { mode: "discrete" }
          },
          term2: {
            id: "diaggrp"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  async function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBarCells = await detectGte({ elem: barDiv.node(), selector: ".bars-cell", count: 22 });
    const tableRows = await detectGte({ elem: barDiv.node(), selector: "tr", count: 22 });
    test.ok(
      // table has one row for header
      numBarCells.length >= tableRows.length - 1,
      "Should display the correct number of cells and table rows when gene expression term is overlaid by a categorical term."
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=geneExp, term2=categorical (patient-level)", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            term: { gene: "TP53", type: "geneExpression" },
            q: { mode: "discrete" }
          },
          term2: {
            id: "sex"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 5;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} bars`);
    test.true(numOverlays > numBars, "number of overlays should be greater than bars");
  }
});
(0, import_tape.default)("term1=condition, term2=gene exp with default bins", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            id: "Hearing loss"
          },
          term2: {
            term: { gene: "TP53", type: "geneExpression" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  async function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBarCells = await detectGte({ elem: barDiv.node(), selector: ".bars-cell", count: 14 });
    const tableRows = await detectGte({ elem: barDiv.node(), selector: "tr", count: 14 });
    test.ok(
      // table has one row for header
      numBarCells.length >= tableRows.length - 1,
      "Should display the correct number of cells and table rows when gene expression term is overlaid by a conditional term."
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=TP53 gene exp, term2=BCR gene exp, both terms with default bins", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            term: { gene: "TP53", type: "geneExpression" },
            q: { mode: "discrete" }
          },
          term2: {
            term: { gene: "BCR", type: "geneExpression" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  async function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBarCells = await detectGte({ elem: barDiv.node(), selector: ".bars-cell", count: 27 });
    const tableRows = await detectGte({ elem: barDiv.node(), selector: "tr", count: 28 });
    test.ok(
      numBarCells.length == tableRows.length - 1,
      "Should display the correct number of cells and table rows when gene expression term is overlaid by a gene expression term."
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("term1=categorical, term0=gene exp with default bins", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            id: "diaggrp"
          },
          term0: {
            term: { gene: "TP53", name: "TP53", type: "geneExpression" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  async function runTests(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBarchart = await detectGte({ elem: barDiv.node(), selector: ".pp-bars-svg", count: 8 });
    test.equal(
      numBarchart.length,
      8,
      "Should display the correct number of barcharts when a categorical term is overlaid by a gene expression term."
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("series visibility - q.hiddenValues", function(test) {
  test.timeoutAfter(5e3);
  test.plan(2);
  const hiddenValues = { 1: true };
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "sex",
            q: {
              hiddenValues
            }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testHiddenValues
      }
    }
  });
  function testHiddenValues(barchart) {
    const bar = barchart.Inner;
    test.deepEqual(
      bar.settings.exclude.cols.sort(),
      Object.keys(hiddenValues).sort(),
      "should have the correct number of hidden bars by q.hiddenValues"
    );
    test.equal(
      bar.dom.holder.selectAll(".bars-cell").size(),
      bar.settings.cols.length - bar.settings.exclude.cols.length,
      "should render the correct number of visible bars"
    );
    if (test._ok) bar.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("series visibility - numeric", function(test) {
  test.timeoutAfter(5e3);
  runpp({
    state: {
      nav: { activeTab: 1 },
      plots: [
        {
          chartType: "barchart",
          term: { id: "aaclassic_5" }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runNumericExcludedTests
      }
    }
  });
  function runNumericExcludedTests(barchart) {
    rideInit({ arg: barchart, bus: barchart, eventType: "postRender.test" }).run(testHiddenByValuesAndOrder).use(triggerHiddenLegendClick, { wait: 800 }).to(testRevealedBar, { wait: 100 }).use(triggerMenuClickToHide, { wait: 100 }).to(testHiddenLegendDisplay, { wait: 600 }).done(test);
  }
  function testHiddenByValuesAndOrder(barchart) {
    const bar = barchart.Inner;
    const excluded = bar.settings.exclude.cols;
    test.true(
      excluded.length > 1 && excluded.length == Object.keys(bar.config.term.q.hiddenValues).length,
      "should have the correct number of excluded numeric series by q.hiddenValues"
    );
    const foundHiddenLabels = bar.dom.legendDiv.selectAll(".sjpp-htmlLegend").filter((d) => d?.isHidden == true).nodes();
    test.equal(
      foundHiddenLabels.length + 1,
      excluded.length,
      "should display the correct number of hidden legend labels"
    );
    const barOrder = [...bar.dom.holder.node().querySelectorAll(".bars-cell-grp")].sort(
      (a, b) => a.__data__.data[0].y - b.__data__.data[0].y
    );
    test.deepEqual(
      barOrder.map((d) => d.__data__.seriesId),
      ["<5000", "5000 to <10000", "10000 to <15000", "15000 to <20000", "20000 to <25000", "\u226525000"],
      "should render the bars in the expected order"
    );
  }
  let numHiddenLegendBeforeClick;
  function triggerHiddenLegendClick(barchart) {
    numHiddenLegendBeforeClick = barchart.Inner.settings.exclude.cols.length;
    const node = barchart.Inner.dom.legendDiv.selectAll(".sjpp-htmlLegend").filter((d) => d?.isHidden == true).node();
    hideCategory(node.__data__, barchart.Inner, false);
  }
  function testRevealedBar(barchart) {
    const bar = barchart.Inner;
    const excluded = bar.settings.exclude.cols;
    test.equal(excluded.length, numHiddenLegendBeforeClick - 1, "should adjust the number of excluded series data");
    const foundHiddenLabels = bar.dom.legendDiv.selectAll(".legend-row").filter((d) => d?.isHidden == true).nodes();
    test.equal(
      foundHiddenLabels.length + 1,
      excluded.length,
      "should adjust the number of hidden legend labels after clicking to reveal one"
    );
  }
  function triggerMenuClickToHide(barchart) {
    const node = barchart.Inner.dom.holder.selectAll(".bars-cell-grp").filter((d) => d.seriesId == "not exposed").node().dispatchEvent(new Event("click", { bubbles: true }));
    barchart.Inner.app.tip.d.selectAll(".sja_menuoption").filter((d) => d.label.includes("Hide")).node().click();
  }
  function testHiddenLegendDisplay(barchart) {
    test.equal(
      barchart.Inner.dom.legendDiv.selectAll(".sjpp-htmlLegend").filter(function() {
        return this.innerHTML.includes("not exposed");
      }).size(),
      1,
      "should hide a special numeric value by menu click"
    );
  }
});
(0, import_tape.default)("series visibility and order - condition", function(test) {
  test.timeoutAfter(5e3);
  const conditionHiddenValues = { "1: Mild": 1 };
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "Arrhythmias",
            q: {
              hiddenValues: conditionHiddenValues
            }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": testConditionHiddenValues
      }
    }
  });
  function testConditionHiddenValues(barchart) {
    const bar = barchart.Inner;
    const excluded = bar.settings.exclude.cols;
    test.equal(excluded.length, 1, "should have the correct number of hidden condition bars by q.hiddenValues");
    const barOrder = [...bar.dom.holder.node().querySelectorAll(".bars-cell-grp")].sort(
      (a, b) => a.__data__.data[0].y - b.__data__.data[0].y
    );
    test.deepEqual(
      barOrder.map((d) => d.__data__.seriesId),
      ["0: No condition", "2: Moderate", "3: Severe", "4: Life-threatening"],
      "should render the bars in the expected order"
    );
    if (test._ok) bar.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("single barchart, categorical filter", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          in: 1,
          join: "",
          lst: [
            {
              type: "tvs",
              tvs: { term: { id: "diaggrp" }, values: [{ key: "Acute lymphoblastic leukemia" }] }
            }
          ]
        }
      },
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "agedx"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 3;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} bars`);
    test.true(numOverlays == numBars, "number of overlays should equal number of bars");
  }
});
(0, import_tape.default)("single barchart, categorical filter (patient-level)", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          in: 1,
          join: "",
          lst: [
            {
              type: "tvs",
              tvs: { term: { id: "sex" }, values: [{ key: "2" }] }
            }
          ]
        }
      },
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "agedx"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 3;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} bars`);
    test.true(numOverlays == numBars, "number of overlays should equal number of bars");
  }
});
(0, import_tape.default)("single geneExp barchart, categorical filter (patient-level)", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          in: 1,
          join: "",
          lst: [
            {
              type: "tvs",
              tvs: { term: { id: "sex" }, values: [{ key: "2" }] }
            }
          ]
        }
      },
      plots: [
        {
          chartType: "summary",
          childType: "barchart",
          term: {
            term: { type: "geneExpression", gene: "TP53" },
            q: { mode: "discrete" }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    await detectOne({ elem: barDiv.node(), selector: ".pp-bars-svg" });
    testBarCount();
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount() {
    const minBars = 3;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    const numOverlays = barDiv.selectAll(".bars-cell").size();
    test.true(numBars > minBars, `should have more than ${minBars} bars`);
    test.true(numOverlays == numBars, "number of overlays should equal number of bars");
  }
});
(0, import_tape.default)("single barchart (patient-level), compound filter (patient-level + sample-level)", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          in: 1,
          join: "and",
          lst: [
            {
              type: "tvs",
              tvs: { term: { id: "diaggrp" }, values: [{ key: "Wilms tumor" }] }
            },
            {
              type: "tvs",
              tvs: { term: { id: "sex" }, values: [{ key: "1" }] }
            }
          ]
        }
      },
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "sex"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    test.equal(
      barchart.Inner.dom.holder.node().querySelectorAll(".bars-cell-grp").length,
      1,
      "should show one bar series"
    );
    test.equal(
      barchart.Inner.dom.holder.node().querySelector(".bars-cell-grp").__data__.seriesId,
      "1",
      "should show one bar series that matches filter value"
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
import_tape.default.skip("genevariant barchart, compound filter", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          join: "or",
          in: true,
          lst: [
            { type: "tvs", tvs: { term: { id: "sex" }, values: [{ key: "1" }] } },
            {
              type: "tvs",
              tvs: { term: { gene: "TP53", name: "TP53", type: "geneExpression" }, ranges: [{ start: 1, stop: 10 }] }
            }
          ]
        }
      },
      plots: [{ chartType: "summary", childType: "barchart", term: getGeneVariantTw() }]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    test.equal(
      barchart.Inner.dom.holder.node().querySelectorAll(".bars-cell-grp").length,
      2,
      "should show two bar series"
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("single barchart, TP53 mutation dtTerm filter", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      termfilter: { filter: tp53dtTermFilter },
      plots: [
        {
          chartType: "barchart",
          term: { id: "agedx" }
          // since p53 mutation filter is applied at sample level, barchart term must be sample-level (agedx)
          // patient-level term (sex) won't show data
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    test.equal(
      barchart.Inner.dom.holder.node().querySelectorAll(".bars-cell-grp").length,
      2,
      "should show two bar series"
    );
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("click non-group bar to add filter", function(test) {
  test.timeoutAfter(8e3);
  test.plan(3);
  const termfilter = { filter: [] };
  runpp({
    state: {
      nav: {
        activeCohort: 0
      },
      termfilter,
      plots: [
        {
          chartType: "barchart",
          term: { id: "agedx", term: termjson["agedx"], q: termjson["agedx"].bins.less },
          term2: {
            id: "Arrhythmias",
            term: termjson["Arrhythmias"],
            q: {
              hiddenValues: {
                "Unknown status": 1
              }
            }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    if (barDiv) return;
    barchart.Inner.bus.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    triggerBarClick(barchart);
    await detectLst({ elem: barchart.Inner.app.tip.d.node(), selector: ".sja_menuoption", count: 1, matchAs: ">=" });
    triggerMenuClick(barchart);
    await detectLst({
      elem: barchart.Inner.app.Inner.dom.holder.node(),
      selector: ".sja_filter_item",
      count: 1,
      matchAs: ">="
    });
    testTermValues(barchart);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  let clickedData, currData;
  function triggerBarClick(barchart) {
    const elem = barDiv.node().querySelector(".bars-cell").querySelector("rect");
    clickedData = elem.__data__;
    currData = barchart.Inner.currServerData;
    elem.dispatchEvent(new Event("click", { bubbles: true }));
  }
  function triggerMenuClick(barchart) {
    barchart.Inner.app.tip.d.selectAll(".sja_menuoption").filter((d) => d.label.includes("filter")).node().dispatchEvent(new Event("click", { bubbles: true }));
  }
  function testTermValues(barchart) {
    const config = barchart.Inner.state.config;
    const termfilter2 = barchart.Inner.app.Inner.state.termfilter;
    const filter = getFilterItemByTag(termfilter2.filter, "filterUiRoot");
    test.equal(
      filter && filter.lst.length,
      2,
      "should create two tvslst filters when a numeric term overlay is clicked"
    );
    test.deepEqual(
      filter.lst[0],
      {
        type: "tvs",
        tvs: {
          term: config.term.term,
          ranges: [currData.refs.bins[1].find((d) => d.label == clickedData.seriesId)]
        }
      },
      "should create a numeric term-value filter with a ranges key"
    );
    const q = JSON.parse(JSON.stringify(config.term2.q));
    const t2ValKey = config.term2 && config.term2.term.values && Object.keys(config.term2.term.values).filter((key) => config.term2.term.values[key].label == clickedData.dataId)[0];
    test.deepEqual(
      filter.lst[1],
      {
        type: "tvs",
        tvs: Object.assign(
          {
            term: config.term2.term,
            values: [
              {
                key: t2ValKey !== void 0 ? t2ValKey : clickedData.dataId,
                label: clickedData.dataId in config.term2.term.values ? config.term2.term.values[clickedData.dataId].label : clickedData.dataId
              }
            ]
          },
          q
        )
      },
      "should create a condition term-value filter with bar_by_*, value_by_*, and other expected keys"
    );
  }
});
(0, import_tape.default)("click custom categorical group bar to add filter", function(test) {
  test.timeoutAfter(3e3);
  const termfilter = { filter: [] };
  const tw = getCategoryGroupsetting();
  runpp({
    state: {
      termfilter,
      plots: [{ chartType: "barchart", term: tw }]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  function runTests(barchart) {
    barchart.on("postRender.test", null);
    if (barDiv) return;
    barchart.Inner.bus.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    rideInit({ arg: barchart, bus: barchart, eventType: "postRender.test" }).run(triggerBarClick, { wait: 600 }).use(triggerMenuClick, { wait: 500 }).to(testTermValues, { wait: 100 }).done(test);
  }
  let clickedData;
  function triggerBarClick(barchart) {
    const elem = barDiv.node().querySelector(".bars-cell").querySelector("rect");
    clickedData = elem.__data__;
    elem.dispatchEvent(new Event("click", { bubbles: true }));
  }
  function triggerMenuClick(barchart) {
    barchart.Inner.app.tip.d.selectAll(".sja_menuoption").filter((d) => d.label.includes("filter")).node().click();
  }
  function testTermValues(barchart) {
    const config = barchart.Inner.state.config;
    const currData = barchart.Inner.currServerData;
    const termfilter2 = barchart.Inner.app.Inner.state.termfilter;
    const filter = getFilterItemByTag(termfilter2.filter, "filterUiRoot");
    test.equal(
      filter && filter.lst.length,
      1,
      "should create one tvslst filters when a numeric term overlay is clicked"
    );
    test.deepEqual(
      filter.lst[0],
      {
        type: "tvs",
        tvs: {
          term: {
            name: "group",
            type: "samplelst",
            values: {
              Group: {
                key: "Group",
                label: "Group",
                list: [
                  { sampleId: "41" },
                  { sampleId: "42" },
                  { sampleId: "43" },
                  { sampleId: "44" },
                  { sampleId: "45" },
                  { sampleId: "46" },
                  { sampleId: "47" },
                  { sampleId: "48" },
                  { sampleId: "49" },
                  { sampleId: "50" },
                  { sampleId: "51" },
                  { sampleId: "52" },
                  { sampleId: "53" },
                  { sampleId: "54" },
                  { sampleId: "55" },
                  { sampleId: "56" },
                  { sampleId: "57" },
                  { sampleId: "58" },
                  { sampleId: "59" },
                  { sampleId: "60" },
                  { sampleId: "61" },
                  { sampleId: "63" },
                  { sampleId: "64" },
                  { sampleId: "65" },
                  { sampleId: "66" },
                  { sampleId: "67" },
                  { sampleId: "68" },
                  { sampleId: "69" },
                  { sampleId: "70" },
                  { sampleId: "71" },
                  { sampleId: "72" },
                  { sampleId: "73" },
                  { sampleId: "74" },
                  { sampleId: "75" },
                  { sampleId: "76" },
                  { sampleId: "77" },
                  { sampleId: "78" },
                  { sampleId: "80" },
                  { sampleId: "93" }
                ]
              },
              "Not in Group": {
                key: "Not in Group",
                label: "Not in Group",
                color: "#aaa",
                list: [
                  { sampleId: "41" },
                  { sampleId: "42" },
                  { sampleId: "43" },
                  { sampleId: "44" },
                  { sampleId: "45" },
                  { sampleId: "46" },
                  { sampleId: "47" },
                  { sampleId: "48" },
                  { sampleId: "49" },
                  { sampleId: "50" },
                  { sampleId: "51" },
                  { sampleId: "52" },
                  { sampleId: "53" },
                  { sampleId: "54" },
                  { sampleId: "55" },
                  { sampleId: "56" },
                  { sampleId: "57" },
                  { sampleId: "58" },
                  { sampleId: "59" },
                  { sampleId: "60" },
                  { sampleId: "61" },
                  { sampleId: "63" },
                  { sampleId: "64" },
                  { sampleId: "65" },
                  { sampleId: "66" },
                  { sampleId: "67" },
                  { sampleId: "68" },
                  { sampleId: "69" },
                  { sampleId: "70" },
                  { sampleId: "71" },
                  { sampleId: "72" },
                  { sampleId: "73" },
                  { sampleId: "74" },
                  { sampleId: "75" },
                  { sampleId: "76" },
                  { sampleId: "77" },
                  { sampleId: "78" },
                  { sampleId: "80" },
                  { sampleId: "93" }
                ],
                in: false
              }
            }
          }
        },
        noEdit: true
      },
      "should create a customset filter with the clicked group.values array"
    );
  }
});
(0, import_tape.default)("numeric exclude range", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: { id: "aaclassic_5", term: termjson["aaclassic_5"] },
          term2: { id: "sex" }
        }
      ],
      termfilter: {
        filter: {
          type: "tvslst",
          join: "and",
          in: true,
          lst: [
            {
              type: "tvs",
              tag: "cohortFilter",
              tvs: {
                term: { id: "subcohort", type: "multivalue" },
                values: [
                  { key: "ABC", label: "ABC" }
                  //{key:'XYZ',label:'XYZ'},
                ]
              }
            },
            {
              type: "tvslst",
              tag: "filterUiRoot",
              join: "",
              in: true,
              lst: [
                {
                  type: "tvs",
                  tvs: {
                    term: termjson["aaclassic_5"],
                    ranges: [{ start: 1e4, stopunbounded: true, startinclusive: false, stopinclusive: true }],
                    isnot: true
                  }
                }
              ]
            }
          ]
        }
      },
      nav: {
        header_mode: "with_tabs"
      },
      activeCohort: -1
    },
    barchart: {
      callbacks: {
        "postRender.test": testBarCount
      }
    }
  });
  function testBarCount(barchart) {
    const barDiv = barchart.Inner.dom.barDiv;
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    test.equal(numBars, 8, "should have 8 bars");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("max number of bins: exceeded", (test) => {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      plots: [
        {
          chartType: "barchart",
          term: {
            term: termjson["aaclassic_5"],
            q: {
              type: "regular-bin",
              bin_size: 1e3,
              stopinclusive: true,
              first_bin: { startunbounded: true, stop: 1, stopinclusive: true, bin: "first" },
              numDecimals: 1,
              last_bin: { start: 3e4, bin: "last", stopunbounded: true },
              startinclusive: false
            }
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  let barDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    barDiv = barchart.Inner.dom.barDiv;
    testBarCount(barchart);
    triggerExceedMaxBin(barchart);
    await testExceedMaxBin(barchart);
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
  function testBarCount(barchart) {
    const numBars = barDiv.selectAll(".bars-cell-grp").size();
    test.equal(numBars, 22, "should have 22 age bars");
  }
  function triggerExceedMaxBin(barchart) {
    barchart.Inner.app.dispatch({
      type: "plot_edit",
      id: "aaclassic_5",
      config: {
        term: {
          id: "aaclassic_5",
          term: barchart.Inner.config.term.term,
          q: {
            type: "regular-bin",
            bin_size: 100,
            stopinclusive: true,
            first_bin: { startunbounded: true, stop: 1, stopinclusive: true, bin: "first" },
            numDecimals: 1,
            last_bin: { start: 3e4, bin: "last", stopunbounded: true },
            startinclusive: false
          }
        }
      }
    });
  }
  async function testExceedMaxBin(barchart) {
    const barLoc = Locator.init(barchart.Inner.dom.holder.node());
    const numBars = await barLoc.shows(".bars-cell-grp", { visibility: false, count: 22 }).get();
    test.equal(numBars.length, 22, "should hide the previously rendered 22 age bars on error and not re-render");
    const errorbar = await barLoc.shows(".sja_errorbar").get(0);
    test.true(errorbar?.innerText.includes("max_num_bins_reached"), "should show a max number of bins error");
  }
});
(0, import_tape.default)("minimum sample size", (test) => {
  test.timeoutAfter(3e3);
  const runpp2 = getRunPp("mass", {
    state: {
      vocab: {
        dslabel: "ProtectedTest",
        genome: "hg38-test"
      }
    },
    debug: 1
  });
  runpp2({
    state: {
      termfilter: {
        filter: {
          type: "tvslst",
          join: "and",
          lst: [
            {
              type: "tvs",
              tvs: {
                term: termjson.agedx,
                ranges: [{ start: 1, startinclusive: true, stop: 3, stopinclusive: true }]
              }
            },
            {
              type: "tvs",
              tvs: {
                term: termjson.sex,
                values: [{ key: "1" }]
              }
            }
          ]
        }
      },
      nav: {
        activeTab: 1
      },
      plots: [
        {
          chartType: "barchart",
          term: {
            term: termjson["diaggrp"]
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests,
        error: runTests
      }
    },
    debug: 1
  });
  let barDiv, errDiv;
  async function runTests(barchart) {
    barchart.on("postRender.test", null).on("error", null);
    barDiv = barchart.Inner.dom.barDiv;
    errDiv = barchart.Inner.dom.errdiv;
    await testBarCount(0, "with stricter filter");
    test.true(errDiv.text().includes("has less than 10 samples"), "should display the expected error message");
    test.notEqual(errDiv.style("display"), "none", "should have a visible red error div");
    await triggerClearedError(barchart);
  }
  async function testBarCount(expected, testcase) {
    if (expected > 0) {
      const numBars2 = await Locator.init(barDiv.node()).shows(".bars-cell-grp", { count: expected }).length();
      test.equal(numBars2, expected, `should have ${expected} bars ${testcase}`);
      return;
    }
    const numBars = await Locator.init(barDiv.node()).find(".bars-cell-grp").length();
    test.equal(numBars, expected, `should have ${expected} bars ${testcase}`);
  }
  async function triggerClearedError(barchart) {
    barchart.on("postRender.test", testClearedError).on("error", testClearedError);
    await barchart.Inner.app.dispatch({
      type: "filter_replace",
      filter: {
        type: "tvslst",
        join: "",
        in: true,
        lst: [
          {
            type: "tvs",
            tvs: {
              term: termjson.agedx,
              ranges: [{ start: 1, startinclusive: true, stop: 3, stopinclusive: true }]
            }
          }
        ]
      }
    });
  }
  async function testClearedError(barchart) {
    barchart.on("postRender.test", null).on("error", null);
    await testBarCount(3, "with looser filter");
    test.equal(errDiv.text(), "", "should have an empty error message");
    test.equal(errDiv.style("display"), "none", "should have a hidden red error div");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
(0, import_tape.default)("no visible series data, no overlay", function(test) {
  test.timeoutAfter(3e3);
  runpp({
    state: {
      nav: {
        header_mode: "search_only"
      },
      plots: [
        {
          chartType: "barchart",
          term: {
            id: "cisplateq_5"
          }
        }
      ]
    },
    barchart: {
      callbacks: {
        "postRender.test": runTests
      }
    }
  });
  async function runTests(barchart) {
    barchart.on("postRender.test", null);
    const barLoc = Locator.init(barchart.Inner.dom.holder);
    const banner = await barLoc.shows('[data-testid="sjpp-barchart-banner"]').get();
    test.equal(banner.length, 1, "should show the banner");
    test.true(
      banner[0]?.innerText.includes("No visible barchart data "),
      "should show a banner for no visible data to render"
    );
    test.equal(await barLoc.shows(".sjpp-hidden-legend-item").length(), 1, "should show a legend entry");
    if (test._ok) barchart.Inner.app.destroy();
    test.end();
  }
});
var tp53dtTermFilter = {
  type: "tvslst",
  in: true,
  join: "",
  lst: [
    {
      type: "tvs",
      tvs: {
        term: {
          id: "snvindel_somatic",
          query: "snvindel",
          name: "SNV/indel (somatic)",
          parent_id: null,
          isleaf: true,
          type: "dtsnvindel",
          dt: 1,
          values: {
            M: { label: "MISSENSE" },
            F: { label: "FRAMESHIFT" },
            WT: { label: "Wildtype" }
          },
          name_noOrigin: "SNV/indel",
          origin: "somatic",
          parentTerm: {
            name: "TP53",
            genes: [
              {
                kind: "gene",
                id: "TP53",
                gene: "TP53",
                name: "TP53",
                type: "geneVariant"
              }
            ],
            type: "geneVariant"
          }
        },
        values: [
          {
            key: "M",
            label: "MISSENSE",
            value: "M",
            bar_width_frac: null
          }
        ],
        genotype: "variant",
        mcount: "any"
      }
    }
  ],
  tag: "filterUiRoot"
};
var runpp = getRunPp("mass", {
  state: {
    nav: {
      activeTab: 1
    },
    vocab: {
      dslabel: "TermdbTest",
      genome: "hg38-test"
    }
  },
  debug: 1
});
//# sourceMappingURL=barchart.integration.spec-COVZPXMH.js.map
