# frozen_string_literal: true require_relative "../test_helper" require_relative "../snapshot_utils" require_relative "../../lib/herb/engine" require_relative "../../lib/herb/engine/visitors/instrumentation_visitor" module Engine class DebugModeTest < Minitest::Spec include SnapshotUtils LINK_SOURCE = %(
  • <%= link_to "Abc", "" %>
  • ) STATIC_SOURCE = %(
    Abc
    ) FILENAME = "app/views/posts/_card.html.erb" class ZeroLocationInjector < Herb::Visitor def visit_document_node(node) super node.children << Herb::AST::ERBContentNode.build( tag_opening: Herb::Token.from("TOKEN_ERB_START", "<%="), content: Herb::Token.from("TOKEN_ERB_CONTENT", " generated "), tag_closing: Herb::Token.from("TOKEN_ERB_END", "%>"), valid: true ) end def inspect "#" end end test "debug mode disabled by default" do template = "

    Hello <%= @name %>!

    " assert_compiled_snapshot(template) end test "debug mode enabled" do template = "

    Hello <%= @name %>!

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "debug mode options" do template = "
    Test
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/test.html.erb") end test "visible erb expression gets debug span" do template = "

    Welcome <%= @user.name %>!

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "multiple visible erb expressions get debug spans" do template = "

    Hello <%= @name %> on <%= Date.today %>!

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "attribute erb expressions do NOT get debug spans" do template = '
    Content
    ' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "script content erb expressions do NOT get debug spans" do template = <<~ERB ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "style content erb expressions do NOT get debug spans" do template = <<~ERB ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "render calls get outline boundaries" do template = '<%= render "shared/header" %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "partial render calls get partial outline boundaries" do template = '<%= render partial: "user_card", locals: { user: @user } %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "top-level element with only ERB output as child" do template = "

    <%= hello %>

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "top-level element with only ERB output as child for partial" do template = "

    <%= hello %>

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "_test.html.erb") end test "collection render calls get outline boundaries" do template = "<%= render @posts %>" assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "erb control flow does NOT get debug markup" do template = <<~ERB <% if @user.present? %>

    User found

    <% else %>

    No user

    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "erb comments do NOT get debug markup" do template = <<~ERB

    Content

    <%# This is a comment %>

    More content

    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "block expressions get debug spans" do template = <<~ERB <%= content_for :sidebar do %>
    Sidebar content
    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "render block expressions get outline boundaries" do template = <<~ERB <%= render "layout" do %>
    Block content
    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "mixed content and attributes" do template = <<~ERB

    Hello <%= @name %>!

    Today is <%= Date.today.strftime('%A') %>

    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "nested erb expressions with render calls" do template = <<~ERB

    Welcome <%= @user.name %>!

    <%= render "shared/navigation" %>
    <%= render partial: "content", locals: { data: @data } %>
    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/welcome.html.erb") end test "debug mode with escape enabled" do template = "

    User input: <%= user_content %>

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], escape: true, filename: "test.html.erb") end test "debug mode with escape disabled" do template = "

    Safe content: <%= safe_html %>

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], escape: false, filename: "test.html.erb") end test "erb yield expressions get debug spans" do template = "
    <%= yield :sidebar %>
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "layout.erb") end test "complex nested template with all features" do template = <<~ERB <%= @page_title %> <%= render "shared/header" %>
    <% if @user.present? %>

    Welcome back, <%= @user.name %>!

    <%= render partial: "user_dashboard", locals: { user: @user } %> <% else %>

    Welcome, guest!

    <%= render "auth/login_form" %> <% end %>
    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/layouts/application.html.erb") end test "zero overhead when debug disabled" do template = "

    Hello <%= @name %>!

    " engine_without_debug = Herb::Engine.new(template) engine_with_debug = Herb::Engine.new(template, debug: false) assert_equal engine_without_debug.src, engine_with_debug.src end test "turbo_frame_tag does NOT get erb-output outline type" do template = '<%= turbo_frame_tag "posts" do %>

    Content

    <% end %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "content_for with block does NOT get erb-output outline type" do template = "<%= content_for :sidebar do %>
    Sidebar content
    <% end %>" assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "content_tag with block does NOT get erb-output outline type" do template = '<%= content_tag :div, class: "wrapper" do %>Content<% end %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "link_to with block does NOT get erb-output outline type" do template = '<%= link_to "/users" do %>View Users<% end %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "tag helper with block does NOT get erb-output outline type" do template = '<%= tag.div class: "container" do %>Content<% end %>' assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "form_with block does NOT get erb-output outline type" do template = "<%= form_with model: @user do |f| %>Form content<% end %>" assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "yield expressions get NOT erb-output outline type" do template = "

    <%= yield :title %>

    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "layout.html.erb") end test "if with elements" do template = <<~HTML
    <% if event.static_metadata && event.static_metadata.location != "Earth" %> <%= event.static_metadata.location %> <% end %> <%= event.formatted_dates %> <% if event.static_metadata.last_edition? %>
    <%= fa "box-archive", size: :xs, style: :regular, class: "fill-gray-400" %> Final Edition
    <% end %>
    HTML assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "layout.html.erb") end test "gets view and erb output view type for just output tag" do assert_compiled_snapshot("<%= hello %>", visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "puts debug span on parent if HTMLTextContent is only spaces" do assert_compiled_snapshot("

    <%= hello %>

    ", { visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb" }) end test "puts debug span on parent if HTMLTextContent is only whitespace" do assert_compiled_snapshot(<<~HTML, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb")

    <%= hello %>

    HTML end test "mulitple top-level elements should be wrapped in type=view div" do assert_compiled_snapshot(<<~HTML, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb")

    Hello

    World

    HTML end test "non HTML-element top-level node should be wrapped in type=view div" do assert_compiled_snapshot(<<~HTML, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") <%= content_tag :div do %> Content <% end %> HTML end test "head content erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new]) <%= @page_title %> ERB end test "textarea content erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new]) ERB end test "pre content erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new])
              Code block:
              <%= @code_sample %>
    
              More code: <%= @another_sample %>
            
    ERB end test "nested excluded contexts do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new])
    ERB end test "html comment erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new]) ERB end test "html doctype erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new]) "> > ERB end test "complex erb with control flow and fa helper calls" do template = <<~ERB
    <% speakers_count = talk.speakers.size %> <% if speakers_count > 1 %> <%= fa("users", size: :sm, style: :light, class: "shrink-0 grow-0 my-1") %> <% elsif speakers_count == 1 %> <%= fa("user", size: :sm, style: :regular, class: "shrink-0 grow-0 my-1") %> <% end %>
    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "regular div content still gets debug spans after excluded context tests" do assert_compiled_snapshot("
    <%= @content %>
    ", visitors: [Herb::Engine::DebugVisitor.new]) end test "javascript_tag content erb expressions do NOT get debug spans" do template = <<~ERB <%= javascript_tag do %> var userId = <%= @user.id %>; var name = "<%= @user.name %>"; <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "svg content erb expressions do NOT get debug spans" do template = <<~ERB <%= @label %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "svg with defs and style erb expressions do NOT get debug spans" do template = <<~ERB ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "nested svg inside div erb expressions do NOT get debug spans inside svg" do template = <<~ERB

    <%= @chart_title %>

    <%= @value %>

    <%= @description %>

    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "math content erb expressions do NOT get debug spans" do template = <<~ERB <%= @variable %> = <%= @value %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "nested math inside div erb expressions do NOT get debug spans inside math" do template = <<~ERB

    <%= @equation_name %>

    <%= @base %> <%= @exponent %>
    ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "test.html.erb") end test "sidecar component displays component name instead of component.html.erb" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/example/component.html.erb") end test "sidecar component with snake_case name converts to PascalCase" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/user_card/component.html.erb") end test "sidecar component with nested namespace" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/admin/user_card/component.html.erb") end test "sidecar component with deeply nested namespace" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/admin/settings/user_card/component.html.erb") end test "regular component file converts to PascalCase" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/button_component.html.erb") end test "sidecar component with erb extension" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/example/component.erb") end test "sidecar component with herb extension" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/example/component.herb") end test "sidecar component with html.herb extension" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/example/component.html.herb") end test "namespaced regular component includes namespace" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/components/ui/avatar_component.html.erb") end test "regular view keeps original basename" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/users/show.html.erb") end test "partial keeps original basename with underscore" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/users/_card.html.erb") end test "partial in components subfolder is not treated as component" do template = "
    Hello
    " assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new], filename: "app/views/page/components/_dropdowns.html.erb") end test "block without debug disable comment content erb expressions get debug spans" do template = <<~ERB <%= content_for :sidebar do %>
    <%= "Sidebar content" %>
    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "block with debug disable comment content erb expressions do NOT get debug spans" do template = <<~ERB <%= content_for :sidebar do # herb:debug disable %>
    <%= "Sidebar content" %>
    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "block with debug disable comment suppresses debug spans for multiple expressions" do template = <<~ERB <%= content_for :title do # herb:debug disable %> <%= [t('app_title.text', app_name: AppSettings.app_name), breadcrumb_trail&.first&.name].compact.join(" | ") %> <%= :subtitle %> <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "block with debug disable comment suppresses debug spans in nested html" do template = <<~ERB <%= content_for :sidebar do # herb:debug disable %>
    <%= "First" %>

    <%= "Second" %>

    <%= "Third" %> <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "debug disable comment works on non-content_for blocks" do template = <<~ERB <%= some_helper do # herb:debug disable %>
    <%= "Hello" %>
    <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "debug disable comment with extra whitespace" do template = <<~ERB <%= content_for :title do # herb:debug disable %> <%= "Title" %> <% end %> ERB assert_compiled_snapshot(template, visitors: [Herb::Engine::DebugVisitor.new]) end test "standalone title content erb expressions do NOT get debug spans" do assert_compiled_snapshot(<<~ERB, visitors: [Herb::Engine::DebugVisitor.new]) <%= @page_title %> ERB end test "annotates a generated node with the first line rather than line zero" do compiled = Herb::Engine.new( "
    Hello
    ", filename: "app/views/test.html.erb", visitors: [ZeroLocationInjector.new, Herb::Engine::DebugVisitor.new] ).src assert_snapshot_matches(compiled, "debug_mode_test-0") end describe "marking which render a tag came from" do def visitors_for(node:, instrument: true) visitors = [Herb::Engine::DebugVisitor.new(node: node)] visitors << Herb::Engine::InstrumentationVisitor.new if instrument visitors end def compiled(node:, instrument: true, source: LINK_SOURCE) Herb::Engine.new(source, filename: FILENAME, visitors: visitors_for(node: node, instrument: instrument)).src end def linking_object Object.new.tap do |object| object.define_singleton_method(:link_to) { |text, href| %(#{text}) } end end def assert_rendered_snapshot(name, renders: 1, **options) source = compiled(**options) object = linking_object html = [] #: Array[String] Herb::Engine::Runtime::Session.capture do renders.times { html << object.instance_eval(source) } end assert_snapshot_matches(html.join("\n"), name, options) end test "says nothing about the render unless asked" do assert_compiled_snapshot(LINK_SOURCE, filename: FILENAME, visitors: visitors_for(node: false)) end test "compiles the marker as a tag the session fills in" do assert_compiled_snapshot(LINK_SOURCE, filename: FILENAME, visitors: visitors_for(node: true)) end test "keeps the marker on the element the tag produced" do assert_rendered_snapshot("one render", node: true) end test "tells two renders of one tag apart" do assert_rendered_snapshot("two renders", renders: 2, node: true) end test "still says which render markup that came from no tag belongs to" do assert_rendered_snapshot("static markup", renders: 2, node: true, source: STATIC_SOURCE) end test "leaves the marker empty when nothing is instrumenting the render" do assert_rendered_snapshot("no instrumentation", node: true, instrument: false) end end end end