code: def test_nested_toc(app):
      app.build()
  
      # toc.ncx
      toc = EPUBElementTree.fromstring(
          (app.outdir / "toc.ncx").read_bytes()
      )
      assert toc.find("./ncx:docTitle/ncx:text").text == "Python"
  
      # toc.ncx / navPoint
      def navinfo(elem):
          label = elem.find("./ncx:navLabel/ncx:text")
          content = elem.find("./ncx:content")
          return (
              elem.get("id"),
              elem.get("playOrder"),
              content.get("src"),
              label.text,
          )
  
      navpoints = toc.findall("./ncx:navMap/ncx:navPoint")
      assert len(navpoints) == 4
      assert navinfo(navpoints[0]) == (
          "navPoint1",
          "1",
          "index.xhtml",
          "Welcome to Sphinx Tests’s documentation!",
      )
      assert navpoints[0].findall("./ncx:navPoint") == []
  
      # toc.ncx / nested navPoints
      assert navinfo(navpoints[1]) == (
          "navPoint2",
          "2",
          "foo.xhtml",
          "foo",
      )
      navchildren = navpoints[1].findall("./ncx:navPoint")
      assert len(navchildren) == 4
      assert navinfo(navchildren[0]) == (
          "navPoint3",
          "2",
          "foo.xhtml",
          "foo",
      )
      assert navinfo(navchildren[1]) == (
          "navPoint4",
          "3",
          "quux.xhtml",
          "quux",
      )
      assert navinfo(navchildren[2]) == (
          "navPoint5",
          "4",
          "foo.xhtml#foo-1",
          "foo.1",
      )
      assert navinfo(navchildren[3]) == (
          "navPoint8",
          "6",
          "foo.xhtml#foo-2",
          "foo.2",
      )
  
      # nav.xhtml / nav
      def navinfo(elem):
          anchor = elem.find("./xhtml:a")
          return (anchor.get("href"), anchor.text)
  
      nav = EPUBElementTree.fromstring(
          (app.outdir / "nav.xhtml").read_bytes()
      )
      toc = nav.findall("./xhtml:body/xhtml:nav/xhtml:ol/xhtml:li")
      assert len(toc) == 4
      assert navinfo(toc[0]) == (
          "index.xhtml",
          "Welcome to Sphinx Tests’s documentation!",
      )
      assert toc[0].findall("./xhtml:ol") == []
  
      # nav.xhtml / nested toc
      assert navinfo(toc[1]) == ("foo.xhtml", "foo")
      tocchildren = toc[1].findall("./xhtml:ol/xhtml:li")
      assert len(tocchildren) == 3
      assert navinfo(tocchildren[0]) == ("quux.xhtml", "quux")
      assert navinfo(tocchildren[1]) == ("foo.xhtml#foo-1", "foo.1")
      assert navinfo(tocchildren[2]) == ("foo.xhtml#foo-2", "foo.2")
  
      grandchild = tocchildren[1].findall("./xhtml:ol/xhtml:li")
      assert len(grandchild) == 1
      assert navinfo(grandchild[0]) == ("foo.xhtml#foo-1-1", "foo.1-1")
  
imports: import os
  import subprocess
  from pathlib import Path
  from subprocess import CalledProcessError
  from xml.etree import ElementTree
  import pytest
  from sphinx.builders.epub3 import _XML_NAME_PATTERN
label_desc: Use image node_id 4 for calling a global function with example usage: test_nested_toc(app) and returns: elem,  elem,  content,  label, anchor,  anchor