| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require_relative 'lib/common' |
| 4 |
|
| 5 |
# Test cases for API server |
| 6 |
class TestServer < TestInteractive |
| 7 |
def test_listen |
| 8 |
{ '--listen 6266' => -> { URI('http://localhost:6266') }, |
| 9 |
"--listen --sync --bind 'start:execute-silent:echo $FZF_PORT > /tmp/fzf-port'" => |
| 10 |
-> { URI("http://localhost:#{File.read('/tmp/fzf-port').chomp}") } }.each do |opts, fn| |
| 11 |
tmux.send_keys "seq 10 | fzf #{opts}", :Enter |
| 12 |
tmux.until { |lines| assert_equal 10, lines.match_count } |
| 13 |
state = JSON.parse(Net::HTTP.get(fn.call), symbolize_names: true) |
| 14 |
assert_equal 10, state[:totalCount] |
| 15 |
assert_equal 10, state[:matchCount] |
| 16 |
assert_empty state[:query] |
| 17 |
assert_equal({ index: 0, text: '1' }, state[:current]) |
| 18 |
|
| 19 |
# No positions when query is empty |
| 20 |
state[:matches].each do |m| |
| 21 |
assert_nil m[:positions] |
| 22 |
end |
| 23 |
assert_nil state[:current][:positions] if state[:current] |
| 24 |
|
| 25 |
# Positions with a single-character query |
| 26 |
Net::HTTP.post(fn.call, 'change-query(1)') |
| 27 |
tmux.until { |lines| assert_equal 2, lines.match_count } |
| 28 |
state = JSON.parse(Net::HTTP.get(fn.call), symbolize_names: true) |
| 29 |
assert_equal [0], state[:current][:positions] |
| 30 |
state[:matches].each do |m| |
| 31 |
assert_includes m[:text], '1' |
| 32 |
assert_equal [m[:text].index('1')], m[:positions] |
| 33 |
end |
| 34 |
|
| 35 |
# Positions with a multi-character query; verify sorted ascending |
| 36 |
Net::HTTP.post(fn.call, 'change-query(10)') |
| 37 |
tmux.until { |lines| assert_equal 1, lines.match_count } |
| 38 |
state = JSON.parse(Net::HTTP.get(fn.call), symbolize_names: true) |
| 39 |
assert_equal '10', state[:current][:text] |
| 40 |
assert_equal [0, 1], state[:current][:positions] |
| 41 |
assert_equal state[:current][:positions], state[:current][:positions].sort |
| 42 |
|
| 43 |
# No match - no current item |
| 44 |
Net::HTTP.post(fn.call, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ') |
| 45 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 46 |
tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] } |
| 47 |
state = JSON.parse(Net::HTTP.get(fn.call), symbolize_names: true) |
| 48 |
assert_equal 100, state[:totalCount] |
| 49 |
assert_equal 0, state[:matchCount] |
| 50 |
assert_equal 'yo', state[:query] |
| 51 |
assert_nil state[:current] |
| 52 |
|
| 53 |
teardown |
| 54 |
setup |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
def test_listen_progress |
| 59 |
tmux.send_keys "seq 10 | #{FZF} --listen 6266", :Enter |
| 60 |
tmux.until { |lines| assert_equal 10, lines.match_count } |
| 61 |
uri = URI('http://localhost:6266') |
| 62 |
state = -> { JSON.parse(Net::HTTP.get(uri), symbolize_names: true) } |
| 63 |
|
| 64 |
# Idle: the last search is complete |
| 65 |
assert_equal 100, state.call[:progress] |
| 66 |
|
| 67 |
# While a search is running, progress is not left at 100 from the |
| 68 |
# previous one, so it can tell a settled snapshot from a stale one |
| 69 |
Net::HTTP.post(uri, 'reload(sleep 1; seq 100)') |
| 70 |
tmux.until { assert_equal 0, state.call[:progress] } |
| 71 |
|
| 72 |
# Settled: matches belong to the snapshot that reports 100 |
| 73 |
tmux.until { |lines| assert_equal 100, lines.match_count } |
| 74 |
tmux.until do |
| 75 |
st = state.call |
| 76 |
assert_equal 100, st[:progress] |
| 77 |
assert_equal 100, st[:matchCount] |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
def test_listen_with_api_key |
| 82 |
uri = URI('http://localhost:6266') |
| 83 |
tmux.send_keys 'seq 10 | FZF_API_KEY=123abc fzf --listen 6266', :Enter |
| 84 |
tmux.until { |lines| assert_equal 10, lines.match_count } |
| 85 |
# Incorrect API Key |
| 86 |
[nil, { 'x-api-key' => '' }, { 'x-api-key' => '124abc' }].each do |headers| |
| 87 |
res = Net::HTTP.post(uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers) |
| 88 |
assert_equal '401', res.code |
| 89 |
assert_equal 'Unauthorized', res.message |
| 90 |
assert_equal "invalid api key\n", res.body |
| 91 |
|
| 92 |
res = Net::HTTP.get_response(uri, headers) |
| 93 |
assert_equal '401', res.code |
| 94 |
assert_equal 'Unauthorized', res.message |
| 95 |
assert_equal "invalid api key\n", res.body |
| 96 |
end |
| 97 |
|
| 98 |
# Valid API Key |
| 99 |
[{ 'x-api-key' => '123abc' }, { 'X-API-Key' => '123abc' }].each do |headers| |
| 100 |
res = Net::HTTP.post(uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers) |
| 101 |
assert_equal '200', res.code |
| 102 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 103 |
tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] } |
| 104 |
|
| 105 |
res = Net::HTTP.get_response(uri, headers) |
| 106 |
assert_equal '200', res.code |
| 107 |
assert_equal 'yo', JSON.parse(res.body, symbolize_names: true)[:query] |
| 108 |
end |
| 109 |
end |
| 110 |
end |