Extract auto-complete logic into testable library; add tests