<html>
  <head>
    <style>
      <%= escape_code_stylesheet %>
    </style>
  </head>
  <body style="font-family:sans-serif;line-height:1.25;font-size:14px">
    <h1><%= example.full_description %></h1>

    <code><b>bin/rspec <%= RerunArgument.for(example) %></b></code>

    <h2>Failure/Error <button onclick="maximizeElementHeight('failure-errors')">Maximize</button></h2>
    <pre id="failure-errors" style="overflow: auto; resize: vertical;"><%= escape_code formatted_exception %></pre>

    <% if summary.js_errors.present? %>
      <h2>JS Errors</h2>
      <ul>
        <% summary.js_errors.each do |error| %>
          <li><%= error["errorMessage"] %> (<%= error["sourceName"] %>:<%= error["lineNumber"] %>)
        <% end %>
      </ul>
    <% end %>

    <h2>Rails Log <button onclick="maximizeElementHeight('rails-log')">Maximize</button></h2>
    <pre id="rails-log" style="overflow: auto; resize: vertical;"><%= escape_code summary.log_messages.join("\n") %></pre>

    <h2>Preceding Specs</h2>
    <div style="overflow: auto; max-height: 300px;"><%= recent_spec_runs %></div>

    <% if summary.screenshot_name %>
      <h2>Screenshot</h2>
      <img style="border:1px solid #000" src="./<%= summary.screenshot_name %>" />
    <% end %>

    <% if summary.page_html %>
      <h2>HTML</h2>
      <code>
        <pre><%= summary.page_html %></pre>
      </code>
    <% end %>
    <script>
      // CSS height: max(fit-content, 300px) doesn't seem to work, so use this instead to ensure the given element
      // is either the size of its contents, or the max-height provided on initial load.
      function clampElementHeight(id, maxHeight) {
        if (document.getElementById(id).clientHeight >= maxHeight) {
          document.getElementById(id).style.height = maxHeight + 'px'
        }
      }

      function maximizeElementHeight(id) {
        document.getElementById(id).style.height = 'fit-content'
      }

      clampElementHeight('failure-errors', 300)
      clampElementHeight('rails-log', 300)
    </script>
  </body>
</html>
