Skip to content
Myr

test_server.rb

This file

Path

test/test_server.rb

110 lines · 4.4 KiB · Ruby

Last changed by

Fix stale progress in --listen status payload (#4907)Junegunn Choibefore adoption7e63399

History of this file

Take it

1# frozen_string_literal: true23require_relative 'lib/common'45# Test cases for API server6class TestServer < TestInteractive7  def test_listen8    { '--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}", :Enter12      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])1819      # No positions when query is empty20      state[:matches].each do |m|21        assert_nil m[:positions]22      end23      assert_nil state[:current][:positions] if state[:current]2425      # Positions with a single-character query26      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      end3435      # Positions with a multi-character query; verify sorted ascending36      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].sort4243      # No match - no current item44      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]5253      teardown54      setup55    end56  end5758  def test_listen_progress59    tmux.send_keys "seq 10 | #{FZF} --listen 6266", :Enter60    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) }6364    # Idle: the last search is complete65    assert_equal 100, state.call[:progress]6667    # While a search is running, progress is not left at 100 from the68    # previous one, so it can tell a settled snapshot from a stale one69    Net::HTTP.post(uri, 'reload(sleep 1; seq 100)')70    tmux.until { assert_equal 0, state.call[:progress] }7172    # Settled: matches belong to the snapshot that reports 10073    tmux.until { |lines| assert_equal 100, lines.match_count }74    tmux.until do75      st = state.call76      assert_equal 100, st[:progress]77      assert_equal 100, st[:matchCount]78    end79  end8081  def test_listen_with_api_key82    uri = URI('http://localhost:6266')83    tmux.send_keys 'seq 10 | FZF_API_KEY=123abc fzf --listen 6266', :Enter84    tmux.until { |lines| assert_equal 10, lines.match_count }85    # Incorrect API Key86    [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.code89      assert_equal 'Unauthorized', res.message90      assert_equal "invalid api key\n", res.body9192      res = Net::HTTP.get_response(uri, headers)93      assert_equal '401', res.code94      assert_equal 'Unauthorized', res.message95      assert_equal "invalid api key\n", res.body96    end9798    # Valid API Key99    [{ '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.code102      tmux.until { |lines| assert_equal 100, lines.item_count }103      tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] }104105      res = Net::HTTP.get_response(uri, headers)106      assert_equal '200', res.code107      assert_equal 'yo', JSON.parse(res.body, symbolize_names: true)[:query]108    end109  end110end