<%= render "posts/search", term: @query %>))
end
test "leaves out a template the host does not compile slots for" do
write("_plain.html.erb", "
<%= card %>
")
entry = write("index.html.erb", %(
<%= @name %>
<%= render "posts/plain", card: @name %>))
compile = lambda { |source, file|
next nil if File.basename(file).start_with?("_")
visitor = Herb::Engine::Slots::Visitor.new(mark: false)
Herb::Engine.new(source, visitors: [visitor], filename: file)
visitor
}
reached = Herb::Engine::Slots::Dependencies.new(@project_path, compile: compile).across(entry)["@name"]
files = reached.map { |slot| File.basename(slot[:file]) }.uniq
assert_equal ["index.html.erb"], files
end
test "says nothing at all when the host compiles no slots anywhere" do
entry = write("index.html.erb", "
<%= @name %>
")
subject = Herb::Engine::Slots::Dependencies.new(@project_path, compile: ->(_source, _file) {})
assert_empty subject.across(entry)
assert_empty subject.slots_for(entry)
assert_nil subject.version_for(entry)
end
test "finds the same state when a visitor rewrote the tree first" do
template = %(
<%= @name.presence || "x" %>
<%= @name.length %>
)
path = File.join(@view_root, "index.html.erb")
File.write(path, template)
compile = lambda { |source, file|
visitor = Herb::Engine::Slots::Visitor.new(mark: false)
Herb::Engine.new(source, visitors: [Herb::Engine::DebugVisitor.new, visitor], filename: file)
visitor
}
rewritten = Herb::Engine::Slots::Dependencies.new(@project_path, compile: compile).slots_for(path)
plain = Herb::Engine::Slots::Dependencies.new(@project_path).slots_for(path)
assert_equal plain.keys.sort, rewritten.keys.sort
assert_equal(plain.values.map { |slot| slot[:state] }, rewritten.values.map { |slot| slot[:state] })
refute_empty(rewritten.values.flat_map { |slot| slot[:state] })
end
test "builds the map the way the host compiles, so the versions agree" do
template = "
<%= @name %>
"
path = File.join(@view_root, "index.html.erb")
File.write(path, template)
marker = Herb::Engine::Slots::Visitor.new
Herb::Engine.new(template, visitors: [Herb::Engine::DebugVisitor.new, marker], filename: path)
compile = lambda { |source, file|
visitor = Herb::Engine::Slots::Visitor.new
Herb::Engine.new(source, visitors: [Herb::Engine::DebugVisitor.new, visitor], filename: file)
visitor
}
matching = Herb::Engine::Slots::Dependencies.new(@project_path, compile: compile)
bare = Herb::Engine::Slots::Dependencies.new(@project_path)
assert_equal marker.schema[:version], matching.version_for(path)
refute_equal marker.schema[:version], bare.version_for(path)
end
test "names the state a slot reads" do
dependencies = dependencies_for("
<%= @name %>
")
assert_equal ["@name"], dependencies[0][:state]
end
test "calls a slot that is the state itself an identity" do
dependencies = dependencies_for("
<%= @name %>
")
assert_equal :identity, dependencies[0][:mode]
end
test "calls a slot that computes from the state derived" do
dependencies = dependencies_for("
<%= @post.title %>
")
assert_equal ["@post"], dependencies[0][:state]
assert_equal :derived, dependencies[0][:mode]
end
test "reads an attribute a slot writes whole as an identity" do
dependencies = dependencies_for(%())
assert_equal ["@name"], dependencies[0][:state]
assert_equal :identity, dependencies[0][:mode]
end
test "refuses an attribute the template only partly wrote" do
dependencies = dependencies_for(%())
assert_equal ["@state"], dependencies[0][:state]
assert_equal :derived, dependencies[0][:mode]
end
test "calls a conditional structural and reports only what its condition reads" do
dependencies = dependencies_for("
<% if @admin %><%= @name %><% end %>
")
assert_equal ["@admin"], dependencies[0][:state]
assert_equal :structural, dependencies[0][:mode]
end
test "gives a collapsed conditional the state of every branch it stands for" do
dependencies = dependencies_for("<% if @admin %>
<%= @name %>
<% else %>
<%= @guest %>
<% end %>")
assert_equal ["@admin", "@guest", "@name"], dependencies[0][:state]
end
test "never calls a collapsed conditional an identity, since the condition picks the value" do
dependencies = dependencies_for("<% if @admin %>
<%= @name %>
<% else %>
<%= @guest %>
<% end %>")
assert_equal :derived, dependencies[0][:mode]
end
test "gives a slot inside a branch the state that branch's body reads" do
dependencies = dependencies_for("
<% if @admin %><%= @name %><% end %>
")
assert_equal ["@name"], dependencies[1][:state]
assert_equal :identity, dependencies[1][:mode]
end
test "follows a collection into the slots its body renders" do
dependencies = dependencies_for("
<% @items.each do |item| %>
<%= item.name %>
<% end %>
")
assert_equal ["@items"], dependencies[0][:state]
assert_equal :structural, dependencies[0][:mode]
assert_equal ["@items"], dependencies[1][:state]
assert_equal :derived, dependencies[1][:mode]
end
test "gives every slot of a template an entry" do
dependencies = dependencies_for(%(<%= @label %>))
assert_equal [0, 1], dependencies.keys.sort
assert_equal ["@url"], dependencies[0][:state]
assert_equal ["@label"], dependencies[1][:state]
end
test "reports a constant a slot reads, and never as an identity" do
dependencies = dependencies_for("
<%= Time.now %>
")
assert_equal ["Time.now"], dependencies[0][:state]
assert_equal :derived, dependencies[0][:mode]
end
test "leaves a slot that reads no state without any" do
dependencies = dependencies_for(%(
<%= "hello" %>
))
assert_empty dependencies[0][:state]
assert_equal :derived, dependencies[0][:mode]
end
test "follows state into a partial that was passed it under another name" do
entry = page_with_partial
reached = subject.across(entry)["@query"].map { |slot| [File.basename(slot[:file]), slot[:index], slot[:mode]] }
assert_equal [
["index.html.erb", 0, :identity],
["index.html.erb", 1, :derived],
["_search.html.erb", 0, :identity],
["_search.html.erb", 1, :derived]
], reached
end
test "keys what it reaches by the name the page knows, not the partial's" do
entry = page_with_partial
across = subject.across(entry)
assert_equal ["@query"], across.keys
end
test "gives every slot the version of the template it came from" do
entry = page_with_partial
versions = subject.across(entry)["@query"].group_by { |slot| File.basename(slot[:file]) }.transform_values { |slots| slots.map { |slot| slot[:version] }.uniq }
assert_equal 1, versions["index.html.erb"].size
assert_equal 1, versions["_search.html.erb"].size
refute_equal versions["index.html.erb"], versions["_search.html.erb"]
end
def named_by(strategy)
Herb::Engine::Slots::Dependencies.new(
@project_path,
compile: lambda { |source, path|
visitor = Herb::Engine::Slots::Visitor.new(mark: false, identifier: strategy)
Herb::Engine.new(source, visitors: [visitor], filename: path, project_path: @project_path)
visitor
}
)
end
test "names a template the way the markers do when they carry a digest" do
entry = write("index.html.erb", %())
files = named_by(:digest).payload(entry)["state"]["@query"].map { |slot| slot["file"] }
assert_equal [Digest::SHA256.hexdigest("app/views/posts/index.html.erb").slice(0, 12)], files
end
test "names a template the way the markers do when a caller names them" do
entry = write("index.html.erb", %())
files = named_by(->(path) { "v1/#{path}" }).payload(entry)["state"]["@query"].map { |slot| slot["file"] }
assert_equal ["v1/app/views/posts/index.html.erb"], files
end
test "names a template the way the page does" do
entry = page_with_partial
files = subject.payload(entry)["state"]["@query"].map { |slot| slot["file"] }.uniq
assert_equal ["app/views/posts/index.html.erb", "app/views/posts/_search.html.erb"], files
end
test "sends only the slots a page may write itself" do
entry = page_with_partial
indices = subject.payload(entry)["state"]["@query"]
assert_equal subject.across(entry)["@query"].count { |slot| slot[:mode] == :identity }, indices.size
assert(indices.none? { |slot| slot.key?("mode") })
end
test "says what a request calls a template's state" do
entry = write("index.html.erb", %())
assert_equal({ "query" => "@query" }, subject.payload(entry)["params"])
end
test "takes the name a caller declares for state a request spells differently" do
entry = write("index.html.erb", "
<% @posts.each do |post| %>
<%= post %>
<% end %>
")
params = subject.payload(entry, params: { "p" => "@posts" })["params"]
assert_equal({ "p" => "@posts" }, params)
end
test "leaves out state a request cannot set" do
entry = write("index.html.erb", "
<%= Time.now %>
")
assert_empty subject.payload(entry)["params"]
end
test "carries the declared names into the parked map" do
entry = write("index.html.erb", "
<% @posts.each do |post| %>
<%= post %>
<% end %>
")
tag = subject.dependencies_tag(entry, params: { "p" => "@posts" })
json = tag.delete_prefix("").delete_suffix("")
assert_equal({ "p" => "@posts" }, JSON.parse(json)["params"])
end
test "parks the map the way statics are parked" do
entry = page_with_partial
tag = subject.dependencies_tag(entry)
assert_match(/\A/, tag)
assert_match(%r{\z}, tag)
json = tag.delete_prefix("").delete_suffix("")
assert_equal subject.payload(entry), JSON.parse(json)
end
test "follows state into a partial rendered inside an each block" do
write("_card.html.erb", "
<%= card.title %>
")
entry = write("index.html.erb", %(<% @posts.each do |post| %><%= render "posts/card", card: post %><% end %>))
files = subject.across(entry)["@posts"].map { |slot| File.basename(slot[:file]) }
assert_equal ["index.html.erb", "index.html.erb", "_card.html.erb"], files
end
test "leaves an item template reached through an each block to the server" do
write("_card.html.erb", "
<%= card %>
")
entry = write("index.html.erb", %(<% @posts.each do |post| %><%= render "posts/card", card: post %><% end %>))
card = subject.across(entry)["@posts"].find { |slot| File.basename(slot[:file]) == "_card.html.erb" }
assert_equal :derived, card[:mode]
end
test "leaves everything an each block's item template renders to the server" do
write("_name.html.erb", "<%= card %>")
write("_card.html.erb", %(
<%= render "posts/name", card: card %>
))
entry = write("index.html.erb", %(<% @posts.each do |post| %><%= render "posts/card", card: post %><% end %>))
modes = subject.across(entry)["@posts"].reject { |slot| File.basename(slot[:file]) == "index.html.erb" }.map { |slot| slot[:mode] }
refute_empty modes
assert_equal [:derived], modes.uniq
end
test "keeps writing a partial rendered inside a block that runs once" do
write("_field.html.erb", "
<%= card %>
")
entry = write("index.html.erb", %(<% form_with model: @post do |f| %><%= render "posts/field", card: @post %><% end %>))
field = subject.across(entry)["@post"].find { |slot| File.basename(slot[:file]) == "_field.html.erb" }
assert_equal :identity, field[:mode]
end
test "leaves a collection's item template to the server" do
write("_card.html.erb", "
<%= card %>
")
entry = write("index.html.erb", %(<%= render partial: "posts/card", collection: @posts %>))
card = subject.across(entry)["@posts"].find { |slot| File.basename(slot[:file]) == "_card.html.erb" }
assert_equal :derived, card[:mode]
end
test "keeps writing a partial that was rendered once" do
write("_card.html.erb", "
<%= card %>
")
entry = write("index.html.erb", %(<%= render "posts/card", card: @post %>))
card = subject.across(entry)["@post"].find { |slot| File.basename(slot[:file]) == "_card.html.erb" }
assert_equal :identity, card[:mode]
end
test "leaves everything below a collection to the server" do
write("_name.html.erb", "<%= card %>")
write("_card.html.erb", %(
<%= render "posts/name", card: card %>
))
entry = write("index.html.erb", %(<%= render partial: "posts/card", collection: @posts %>))
modes = subject.across(entry)["@posts"].reject { |slot| File.basename(slot[:file]) == "index.html.erb" }.map { |slot| slot[:mode] }
refute_empty modes
assert_equal [:derived], modes.uniq
end
test "asks for markup only where values cannot answer" do
entry = write("index.html.erb", "
<% if @admin %><%= @name %><% end %>
")
assert_equal([0], subject.subtree_slots(entry, ["@admin"]).map { |slot| slot[:index] })
end
test "leaves a value change to the values" do
entry = write("index.html.erb", "
<% if @admin %><%= @name %><% end %>
")
assert_empty subject.subtree_slots(entry, ["@name"])
end
test "asks for a collection that changed" do
entry = write("index.html.erb", "
<% @items.each do |item| %>
<%= item %>
<% end %>
")
slots = subject.subtree_slots(entry, ["@items"])
assert_equal [:structural], slots.map { |slot| slot[:mode] }.uniq
end
test "says nothing about state the page does not read" do
entry = write("index.html.erb", "
<% if @admin %>x<% end %>
")
assert_empty subject.subtree_slots(entry, ["@nothing"])
end
test "never calls a constant an identity, so a page is never sent one to write" do
entry = write("index.html.erb", "
<%= Post.count %>
")
assert_equal([:derived], subject.across(entry)["Post.count"].map { |slot| slot[:mode] })
assert_empty subject.payload(entry)["state"]["Post.count"]
end
test "leaves a constant out of the names a request can set" do
entry = write("index.html.erb", "
<%= Post.count %>
")
assert_empty subject.payload(entry)["params"]
end
test "leaves a template that reaches nothing out of the map" do
entry = write("index.html.erb", "
static
")
assert_empty subject.across(entry)
end
test "carries the presence a boolean attribute compares" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (draft: "", sending: false) %>
<%= draft %>
ERB
manifest = states_of(path)
assert_equal [["draft", { "value" => "" }], ["sending", nil]], manifest["presence"].values
assert_equal manifest["presence"].keys.map(&:to_i).sort, (manifest["reads"]["draft"] + manifest["reads"]["sending"]).sort - manifest["reads"]["draft"].take(1)
end
test "carries a combo presence and registers every state it reads" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (pending: false, failed: false) %>
<% if pending? && failed? %>Stuck<% else %>Fine<% end %>
ERB
manifest = states_of(path)
assert_equal [{ "any" => [["pending", nil], ["failed", nil]] }], manifest["presence"].values
index = manifest["presence"].keys.first.to_i
assert_equal({ "pending" => [index], "failed" => [index] }, manifest["reads"])
conditional = manifest["conditionals"].values.first
assert_equal [{ "branch" => 0, "condition" => { "all" => [["pending", nil], ["failed", nil]] } }], conditional["arms"]
end
test "carries a derived state with its condition" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (pending: false, failed: false, busy: pending || failed) %>
<% if busy %>Busy<% else %>Idle<% end %>
ERB
manifest = states_of(path)
declaration = manifest["declarations"].find { |declared| declared["name"] == "busy" }
assert_equal "boolean", declaration["kind"].to_s
assert_equal({ "any" => [["pending", nil], ["failed", nil]] }, declaration["derived"])
end
test "carries a counted state with its fold" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (pending_count: 0) %>
<% @messages.each do |message| %>
<%# herb:key message.id %>
<%# herb:state (pending: false) %>
<% if pending? %><% pending_count += 1 %><% end %>
<%= message.body %>
<% end %>
<%= pending_count %>
ERB
manifest = states_of(path)
declaration = manifest["declarations"].find { |declared| declared["name"] == "pending_count" }
assert_equal({ "collection" => 0, "when" => ["pending", nil], "by" => 1 }, declaration["count"])
assert_equal({ "pending_count" => [3] }, manifest["reads"])
end
test "reaches a state read through a tag helper's attributes" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (draft: "", agreed: false) %>
<%= tag.input value: draft %>
<%= tag.input type: "checkbox", checked: agreed %>
ERB
manifest = states_of(path)
assert_equal [0], manifest["reads"]["draft"]
assert_equal [["agreed", nil]], manifest["presence"].values
end
test "carries the states a template declares" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (pending: false, attempts: 0) %>
<%= @query %>
<%= attempts %>
<% if pending? %>wait<% else %>done<% end %>
ERB
subject.payload(path)
manifest = states_of(path)
names = manifest["declarations"].map { |declaration| declaration["name"] }
assert_equal ["pending", "attempts"], names
assert_equal ["region"], manifest["declarations"].map { |declaration| declaration["scope"] }.uniq
assert_equal 1, manifest["reads"]["attempts"].size
conditional = manifest["conditionals"].values.first
assert_equal [{ "branch" => 0, "condition" => ["pending", nil] }], conditional["arms"]
assert_equal 1, conditional["else"]
assert_equal subject.version_for(path), manifest["version"]
end
test "records every slot a state is read into, and says nothing about which take typing" do
path = write("index.html.erb", <<~ERB)
<%# herb:state (draft: "", agreed: false) %>
<%= draft %>
ERB
manifest = states_of(path)
assert_equal 2, manifest["reads"]["draft"].size
assert_equal 1, manifest["reads"]["agreed"].size
refute manifest.key?("bound"), "a page decides that from the element a slot sits on"
end
test "counts a state read in an interpolated attribute" do
path = write("index.html.erb", %(<%# herb:state (status: "") %>
x
))
manifest = states_of(path)
assert_equal [0], manifest["reads"]["status"]
end
test "a partial carries the states it declares, whether or not the page reaches them" do
partial = write("_menu.html.erb", %(<%# herb:state (open: false) %>))
write("index.html.erb", %(
<%= render "menu" %>
))
assert_equal(["open"], states_of(partial)["declarations"].map { |declaration| declaration["name"] })
end
test "a template that declares none says nothing about states" do
path = write("index.html.erb", "
<%= @query %>
")
assert_nil states_of(path)
end
test "the map a page is driven by no longer carries what each template declares" do
path = write("index.html.erb", %(<%# herb:state (open: false) %>
<%= @query %>
))
refute subject.payload(path).key?("states"), "a template's states travel in its own manifest"
end
test "hands the map to the response rather than writing it into the body" do
entry = write("index.html.erb", %())
session = Herb::Engine::Runtime::Session.capture { subject.deliver(entry) }
channel = session.channel(Herb::Engine::Slots::Dependencies::Channel::NAME) { nil }
assert_equal :body, channel.anchor
assert_equal subject.dependencies_tag(entry), channel.to_html
end
test "carries one map for a response however many times it is handed over" do
entry = write("index.html.erb", %())
session = Herb::Engine::Runtime::Session.capture { 3.times { subject.deliver(entry) } }
channel = session.channel(Herb::Engine::Slots::Dependencies::Channel::NAME) { nil }
assert_equal 1, channel.to_html.scan("data-herb-dependencies").size
end
test "a response nobody handed a map to carries nothing" do
session = Herb::Engine::Runtime::Session.capture { nil }
assert_nil session.channel(Herb::Engine::Slots::Dependencies::Channel::NAME) { nil }
end
end
end
end