require 'spec_helper'

describe "mixer#cache#fetch" do
  it "caches the results of a complete function" do
    declare_complete_function("First", "['A', 'B']")

    expect(vim.echo("mixer#cache#fetch('', 'First')"))
      .to eq "['A', 'B']"

    declare_complete_function("First", "['B', 'C']")

    expect(vim.echo("mixer#cache#fetch('', 'First')"))
      .to eq "['A', 'B']"
  end

  it "invalidates the cache if the keyword_base does not start with the previous one" do
    declare_complete_function("First", "['A', 'B']")

    expect(vim.echo("mixer#cache#fetch('foo', 'First')"))
      .to eq "['A', 'B']"

    declare_complete_function("First", "['B', 'C']")

    expect(vim.echo("mixer#cache#fetch('bar', 'First')"))
      .to eq "['B', 'C']"
  end

  it "does not invalidate if new keyword is a superstring" do
    declare_complete_function("First", "['X', 'Y']")

    expect(vim.echo("mixer#cache#fetch('razzle', 'First')"))
      .to eq "['X', 'Y']"

    declare_complete_function("First", "['Z', 'Q']")

    expect(vim.echo("mixer#cache#fetch('razzledazzle', 'First')"))
      .to eq "['X', 'Y']"
  end

  after do
    vim.command("call mixer#cache#clear()")
  end
end
