# frozen_string_literal: true require "tmpdir" require "fileutils" require_relative "../test_helper" require_relative "../../lib/herb/dev/runner" require_relative "../../lib/herb/dev" module Dev class RunnerTest < Minitest::Spec class FakeWebSocket attr_reader :messages #: Array[Hash[Symbol, untyped]] def initialize @messages = [] end def client_count 1 end def broadcast(message, **) @messages << message end end BROKEN = "
\n
\n
\n" #: String def broadcast_for(current_content, previous_content) websocket = FakeWebSocket.new pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {}) event = Herb::Dev::Watcher::Event.new( kind: :changed, path: "/app/views/posts/index.html.erb", relative_path: "app/views/posts/index.html.erb", previous: previous_content, current: current_content ) capture_io { pipeline.handle_event(event) } websocket.messages.first end test "names the parser as what found the error, not itself as what delivered it" do error = broadcast_for(BROKEN, "")[:errors].first parsed = Herb.parse(BROKEN, strict: true, analyze: true).errors.first.to_diagnostic(template: "app/views/posts/index.html.erb") assert_equal "Herb Parser", error[:origin] assert_equal parsed.origin, error[:origin] end test "sends the code and suggestion the same error would carry off the page" do error = broadcast_for(BROKEN, "")[:errors].first parsed = Herb.parse(BROKEN, strict: true, analyze: true).errors.first.to_diagnostic(template: "app/views/posts/index.html.erb") assert_equal parsed.code, error[:code] assert_equal parsed.suggestion, error[:suggestion] assert_equal parsed.message, error[:message] end test "sends the whole file with the errors, so the panel can highlight it" do assert_equal BROKEN, broadcast_for(BROKEN, "")[:source] end test "sends the content it just read, not the content it replaced" do assert_equal BROKEN, broadcast_for(BROKEN, "
\n
\n")[:source] end test "a fixed file broadcasts a clearing schema and never a fixed message" do websocket = FakeWebSocket.new pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {}) broken = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: "
", current: BROKEN) fixed = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: BROKEN, current: "
\n \n
\n") capture_io do pipeline.handle_event(broken) pipeline.handle_event(fixed) end types = websocket.messages.map { |message| message[:type] } assert_equal ["error", "schema", "invalidate"], types schema = websocket.messages.find { |message| message[:type] == "schema" } assert_equal [], schema[:diagnostics] end test "text_changed is patchable" do diff_result = Herb.diff("
Hello
", "
World
") assert Herb::Dev::Runner.can_patch?(diff_result.operations) assert_equal 1, diff_result.operation_count assert_equal :text_changed, diff_result.operations[0].type end test "attribute_value_changed is patchable" do diff_result = Herb.diff('
Content
', '
Content
') assert Herb::Dev::Runner.can_patch?(diff_result.operations) assert_equal 1, diff_result.operation_count assert_equal :attribute_value_changed, diff_result.operations[0].type end test "attribute_added is patchable" do diff_result = Herb.diff("
Content
", '
Content
') assert Herb::Dev::Runner.can_patch?(diff_result.operations) assert_equal 1, diff_result.operation_count assert_equal :attribute_added, diff_result.operations[0].type end test "attribute_removed is patchable" do diff_result = Herb.diff('
Content
', "
Content
") assert Herb::Dev::Runner.can_patch?(diff_result.operations) assert_equal 1, diff_result.operation_count assert_equal :attribute_removed, diff_result.operations[0].type end test "node_inserted triggers reload" do diff_result = Herb.diff("
", "
New
") refute Herb::Dev::Runner.can_patch?(diff_result.operations) end test "node_removed triggers reload" do diff_result = Herb.diff("
Old
", "
") refute Herb::Dev::Runner.can_patch?(diff_result.operations) end test "erb_content_changed triggers reload" do diff_result = Herb.diff("<%= foo %>", "<%= bar %>") refute Herb::Dev::Runner.can_patch?(diff_result.operations) end test "inserting ERB node triggers reload" do diff_result = Herb.diff("
Hello
", "
<%= name %>
") refute Herb::Dev::Runner.can_patch?(diff_result.operations) end test "identical templates produce no operations" do diff_result = Herb.diff("
Hello
", "
Hello
") assert diff_result.identical? assert_equal 0, diff_result.operation_count end test "multiple patchable operations are all patchable" do diff_result = Herb.diff('
Hello
', '
World
') assert Herb::Dev::Runner.can_patch?(diff_result.operations) assert_equal 2, diff_result.operation_count end test "mixed patchable and non-patchable triggers reload" do diff_result = Herb.diff('
Hello
', '
Hello
New') refute Herb::Dev::Runner.can_patch?(diff_result.operations) end test "indexing writes no cursor escapes when stdout is not a terminal" do directory = Dir.mktmpdir("herb_dev_runner_test") File.write(File.join(directory, "index.html.erb"), "
Hello
\n") config = Herb::Configuration.load(directory) watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| event } pipeline = Herb::Dev::Pipeline.new(server: FakeWebSocket.new, compiler: -> {}) output, = capture_io do Herb::Dev::Runner.new(path: directory).send(:index_files, watcher, pipeline) end assert_equal " Files: 1 template indexed\n", output ensure FileUtils.rm_rf(directory) Herb.reset_configuration! end class TerminalOutput < StringIO def tty? true end end def index_on_terminal(process_name) directory = Dir.mktmpdir("herb_dev_runner_test") File.write(File.join(directory, "index.html.erb"), "
Hello
\n") config = Herb::Configuration.load(directory) watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| event } pipeline = Herb::Dev::Pipeline.new(server: FakeWebSocket.new, compiler: -> {}) with_process_name(process_name) do on_terminal { Herb::Dev::Runner.new(path: directory).send(:index_files, watcher, pipeline) } end ensure FileUtils.rm_rf(directory) Herb.reset_configuration! end def on_terminal previous_stdout = $stdout $stdout = TerminalOutput.new yield $stdout.string ensure $stdout = previous_stdout end def with_process_name(process_name) previous = ENV.fetch("PS", nil) process_name ? ENV["PS"] = process_name : ENV.delete("PS") yield ensure previous ? ENV["PS"] = previous : ENV.delete("PS") end test "indexing rewrites its placeholder line on a terminal of its own" do assert_equal " Indexing files...\n\e[1A\e[2K Files: 1 template indexed\n", index_on_terminal(nil) end test "indexing writes no cursor escapes on a terminal shared through a process manager" do assert_equal " Files: 1 template indexed\n", index_on_terminal("herb") end def paint_error(previous, current, broken: []) runner = Herb::Dev::Runner.new runner.instance_variable_set(:@broken_files, Set.new(broken)) classification = Herb::Dev::Classifier.new.call(previous, current) event = Herb::Dev::Watcher::Event.new( kind: :changed, path: "/app/views/posts/index.html.erb", relative_path: "app/views/posts/index.html.erb", previous: previous, current: current ) output, = capture_io do runner.send(:paint_change, event, classification, "12:00:00", "index.html.erb") end output end test "prints the diagnostic with its source context and suggestion" do output = paint_error("", BROKEN) expected = [ " 12:00:00 \u2717 error \u2718 [MissingClosingTagError] Opening tag `
` at (2:3) doesn't have a matching closing tag `
` in the same scope.", "", " app/views/posts/index.html.erb:2:3:", " 2 \u2502
", " \u2575 ~~~~~~", "", " Add the closing tag, or make it self-closing.", "", "" ].join("\n") assert_equal expected, output end test "logs nothing when the template still carries only errors it already reported" do assert_equal "", paint_error(BROKEN, "
\n \n
\n") end test "a template indexed as broken paints clear without a diff against the source that never parsed" do output = paint_error(BROKEN, "
\n \n
\n", broken: ["app/views/posts/index.html.erb"]) assert_equal " 12:00:00 \u2713 clear index.html.erb\n\n", output end test "indexing counts the templates that do not parse" do directory = Dir.mktmpdir("herb_dev_runner_test") File.write(File.join(directory, "good.html.erb"), "
Hello
\n") File.write(File.join(directory, "broken.html.erb"), BROKEN) config = Herb::Configuration.load(directory) watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| event } pipeline = Herb::Dev::Pipeline.new(server: FakeWebSocket.new, compiler: -> {}) broken = nil output, = capture_io do broken = Herb::Dev::Runner.new(path: directory).send(:index_files, watcher, pipeline) end assert_equal " Files: 2 templates indexed, 1 doesn't parse\n", output assert_equal ["broken.html.erb"], broken ensure FileUtils.rm_rf(directory) Herb.reset_configuration! end test "a template remembered as broken broadcasts a clearing schema when it is repaired" do websocket = FakeWebSocket.new pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {}) pipeline.remember_broken({ "a.html.erb" => BROKEN }) fixed = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: BROKEN, current: "
\n
\n
\n") capture_io { pipeline.handle_event(fixed) } assert_equal(["schema", "invalidate"], websocket.messages.map { |message| message[:type] }) end test "a template remembered as broken stays on the list until it is repaired" do websocket = FakeWebSocket.new pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {}) pipeline.remember_broken({ "a.html.erb" => BROKEN }) assert_equal ["a.html.erb"], pipeline.broken_files fixed = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/a", relative_path: "a.html.erb", previous: BROKEN, current: "
\n
\n
\n") capture_io { pipeline.handle_event(fixed) } assert_equal [], pipeline.broken_files end test "a template that only fails to compile is broken too, and carries its diagnostics" do websocket = FakeWebSocket.new compiled = Struct.new(:mode, :manifest, :version, :slot_entries, :statics, :static_markup, :diagnostics) diagnostics = [{ message: "slot outside a region", severity: :error }] compiler = ->(_source, _path) { compiled.new(:client, {}, "v1", [], {}, nil, diagnostics) } pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> { compiler }) event = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/b", relative_path: "b.html.erb", previous: "
Hello
\n", current: "
Bye
\n") capture_io { pipeline.handle_event(event) } assert_equal ["b.html.erb"], pipeline.broken_files assert_equal [{ file: "b.html.erb", diagnostics: diagnostics }], pipeline.broken_entries end test "a broken template carries the source and errors, so a late browser can render them" do websocket = FakeWebSocket.new pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> {}) pipeline.remember_broken({ "a.html.erb" => BROKEN }) entry = pipeline.broken_entries.first expected = Herb::Dev::Protocol.error(file: "a.html.erb", source: BROKEN, errors: Herb.parse(BROKEN, strict: true, analyze: true).errors) assert_equal BROKEN, entry[:source] assert_equal expected[:errors], entry[:errors] end test "a template that compiles clean drops off the broken list" do websocket = FakeWebSocket.new compiled = Struct.new(:mode, :manifest, :version, :slot_entries, :statics, :static_markup, :diagnostics) compiler = ->(_source, _path) { compiled.new(:client, {}, "v1", [], {}, nil, []) } pipeline = Herb::Dev::Pipeline.new(server: websocket, compiler: -> { compiler }) pipeline.remember_broken({ "b.html.erb" => BROKEN }) assert_equal ["b.html.erb"], pipeline.broken_files event = Herb::Dev::Watcher::Event.new(kind: :changed, path: "/b", relative_path: "b.html.erb", previous: BROKEN, current: "
Bye
\n") capture_io { pipeline.handle_event(event) } assert_equal [], pipeline.broken_files end test "a repeated event for unchanged content emits nothing from the watcher" do directory = Dir.mktmpdir("herb_dev_runner_test") path = File.join(directory, "index.html.erb") File.write(path, BROKEN) config = Herb::Configuration.load(directory) events = [] watcher = Herb::Dev::Watcher.new(config: config, root: directory) { |event| events << event } watcher.index raw = Struct.new(:kind, :path).new("modified", path) watcher.send(:handle, raw) watcher.send(:handle, raw) assert_empty events ensure FileUtils.rm_rf(directory) Herb.reset_configuration! end test "an error already reported is not repeated when a new one joins it" do broken_once = "
\n
\n
\n" broken_twice = "
\n \n \n
\n" output = paint_error(broken_once, broken_twice) expected = [ " 12:00:00 \u2717 error \u2718 [MissingClosingTagError] Opening tag `` at (3:3) doesn't have a matching closing tag `` in the same scope.", "", " app/views/posts/index.html.erb:3:3:", " 3 \u2502 ", " \u2575 ~~~~~~", "", " Add the closing tag, or make it self-closing.", "", "" ].join("\n") assert_equal expected, output end end end