File: test_watcher.rb

package info (click to toggle)
ruby-adsf 1.5.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: ruby: 1,016; javascript: 976; makefile: 12
file content (84 lines) | stat: -rw-r--r-- 2,113 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true

require 'helper'

class Adsf::Live::WatcherTest < Minitest::Test
  include Rack::Test::Methods
  include Adsf::Test::Helpers

  def run_watcher(root_dir: nil)
    FileUtils.mkdir_p('public')
    root_dir ||= File.expand_path('public')
    watcher = Adsf::Live::Watcher.new(root_dir: root_dir)

    begin
      watcher.start
      yield
    ensure
      watcher.stop
    end
  end

  def test_receives_hello
    run_watcher do
      ws = Faye::WebSocket::Client.new('ws://127.0.0.1:35729/')

      queue = SizedQueue.new(2)
      ws.on :open do |_event|
      end
      ws.on :message do |event|
        queue << event.data
      end
      ws.on :close do |_event|
      end

      messages = []
      1.times { messages << queue.pop }

      expected_hello_data = {
        'command' => 'hello',
        'protocols' => ['http://livereload.com/protocols/official-7'],
        'serverName' => 'nanoc-view',
      }
      assert_equal expected_hello_data, JSON.parse(messages[0])
    end
  end

  def test_requires_absolute_path
    assert_raises(ArgumentError, 'Watcher#initialize: The root_path argument must be an absolute path') do
      run_watcher(root_dir: 'public') {}
    end
  end

  def test_receives_update
    run_watcher do
      ws = Faye::WebSocket::Client.new('ws://127.0.0.1:35729/')

      queue = SizedQueue.new(2)
      ws.on :open do |_event|
        sleep 0.5
        File.write('public/index.html', 'hello thear')
      end
      ws.on :message do |event|
        queue << event.data
      end
      ws.on :close do |event|
        raise "unexpected close: #{event.code}" unless event.code == 1006
      end

      messages = []
      2.times { messages << queue.pop }

      expected_hello_data = {
        'command' => 'hello',
        'protocols' => ['http://livereload.com/protocols/official-7'],
        'serverName' => 'nanoc-view',
      }

      assert_equal expected_hello_data, JSON.parse(messages[0])

      assert_equal 'reload', JSON.parse(messages[1])['command']
      assert_equal '/index.html', JSON.parse(messages[1])['path']
    end
  end
end