code: def test_add_or_replace(tags):
      """Test replacing existing figures in a report."""
      # Note that tags don't matter, only titles do!
      r = Report()
      fig1, fig2 = _get_example_figures()
      r.add_figure(
          fig=fig1, title="duplicate", tags=("foo",) if tags else ()
      )
      r_state = r.__getstate__()
      html = r.html
      r_state_after = r.__getstate__()
      assert_object_equal(r_state, r_state_after)
      html_2 = r.html
      assert html == html_2  # stays the same
      r_state_after = r.__getstate__()
      assert_object_equal(r_state, r_state_after)
      assert ' id="global' not in "\n".join(html)
      assert ' id="duplicate" ' in html[0]
      assert ' id="duplicate-' not in "\n".join(html)
      r.add_figure(
          fig=fig2, title="duplicate", tags=("foo",) if tags else ()
      )
      html = r.html
      assert ' id="duplicate" ' in html[0]
      assert ' id="duplicate-1" ' in html[1]
      assert ' id="duplicate-2" ' not in "\n".join(html)
      r.add_figure(
          fig=fig1, title="duplicate", tags=("bar",) if tags else ()
      )
      html = r.html
      assert ' id="duplicate" ' in html[0]
      assert ' id="duplicate-1" ' in html[1]
      assert ' id="duplicate-2" ' in html[2]
      assert ' id="duplicate-3" ' not in "\n".join(html)
      r.add_figure(
          fig=fig2, title="nonduplicate", tags=("foo",) if tags else ()
      )
      html = r.html
      assert ' id="nonduplicate" ' in html[3]
      # By default, replace=False, so all figures should be there
      assert len(r.html) == 4
      assert len(r._content) == 4
  
      old_r = copy.deepcopy(r)
  
      # Replace our last occurrence of title='duplicate'
      r.add_figure(
          fig=fig2,
          title="duplicate",
          tags=("bar",) if tags else (),
          replace=True,
      )
      assert len(r._content) == len(r.html) == 4
      # This figure should have changed
      assert r.html[2] != old_r.html[2]
      # All other figures should be the same
      assert r.html[0] == old_r.html[0]
      assert r.html[1] == old_r.html[1]
      assert r.html[3] == old_r.html[3]
      # same DOM IDs
      html = r.html
      assert ' id="duplicate" ' in html[0]
      assert ' id="duplicate-1" ' in html[1]
      assert ' id="duplicate-2" ' in html[2]
      assert ' id="duplicate-3" ' not in "\n".join(html)
      assert ' id="global' not in "\n".join(html)
  
      # Now we change our max dup limit and should end up with a `global-`
      r._dup_limit = 2
      r.add_figure(
          fig=fig2,
          title="duplicate",
          replace=True,
      )
      html = r.html
      assert ' id="duplicate" ' in html[0]
      assert ' id="duplicate-1" ' in html[1]
      assert ' id="duplicate-2" ' in html[2]  # dom_id preserved
      assert ' id="global' not in "\n".join(html)
      r.add_figure(
          fig=fig2,
          title="duplicate",
      )  # append, should end up with global-1 ID
      html = r.html
      assert len(html) == 5
      assert ' id="global-1" ' in html[4]
  
      # And if we add a duplicate in a different section, it gets a different
      # DOM ID
      old_html = html
      section = "<div whatever 😀   etc."
      sec_san = "_div_whatever___etc_"
      r.add_figure(
          fig=fig2,
          title="duplicate",
          section=section,
          replace=True,  # should have no effect
      )
      html = r.html
      assert len(html) == 6
      assert html[:5] == old_html
      assert f' id="{sec_san}" ' in html[5]  # section anchor
      assert f' id="{sec_san}-duplicate" ' in html[5]
  
imports: import base64
  import copy
  import glob
  import os
  import pickle
  import re
  import shutil
  from io import BytesIO
  from pathlib import Path
  import numpy
  import pytest
  from matplotlib import pyplot
  from mne import Epochs, create_info, pick_channels_cov, read_cov, read_events, read_evokeds
  from mne._fiff.write import DATE_NONE
  from mne.datasets import testing
  from mne.epochs import make_metadata
  from mne.io import RawArray, read_info, read_raw_fif
  from mne.preprocessing import ICA
  from mne.report import Report, _ReportScraper, open_report, report
  from mne.report import report
  from mne.report.report import _ALLOWED_IMAGE_FORMATS, CONTENT_ORDER, _webp_supported
  from mne.utils import Bunch
  from mne.utils._testing import assert_object_equal
  from mne.viz import plot_alignment
label_desc: Use image node_id 19 for calling a global function with example usage: test_add_or_replace(tags) without return types