Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed wrong wordlist on tab completion #270

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This is a Rust port of the Python version at <https://github.com/magic-wormhole/

Features that are missing:

- Tab completion
- Text message sending
- Folder sending (we can send folders, but it will send a tar ball which the other side will have to manually unpack)
- Tor support
Expand All @@ -21,6 +20,7 @@ New features that exceed the other implementations:
- Automatically copies your code to the clipboard
- Port forwarding in addition to file transfer (experimental)
- Send a file to multiple people (experimental)
- Fuzzy wormhole code completion

## Getting started

Expand Down
108 changes: 74 additions & 34 deletions src/core/wordlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ impl Wordlist {

fn get_wordlist(&self, prefix: &str) -> &Vec<String> {
let count_dashes = prefix.matches('-').count();
&self.words[count_dashes % self.words.len()]
let index = 1 - (count_dashes % 2);
&self.words[index]
}

#[cfg(feature = "fuzzy-complete")]
Expand Down Expand Up @@ -165,6 +166,14 @@ mod test {
assert_eq!(d.words[1][255], "zulu");
}

#[test]
fn test_get_wordlist() {
let list = Wordlist::default_wordlist(2);
assert_eq!(list.words.len(), 2);
assert_eq!(list.get_wordlist("22-"), &list.words[0]);
assert_eq!(list.get_wordlist("22-dictator-"), &list.words[1]);
}

fn vec_strs(all: &str) -> Vec<&str> {
all.split_whitespace()
.map(|s| if s == "." { "" } else { s })
Expand All @@ -185,9 +194,8 @@ mod test {
let w = Wordlist::new(2, words);
assert_eq!(w.get_completions(""), Vec::<String>::new());
assert_eq!(w.get_completions("9"), Vec::<String>::new());
assert_eq!(w.get_completions("pur"), vec!["purple"]);
assert_eq!(w.get_completions("blu"), Vec::<String>::new());
assert_eq!(w.get_completions("purple-sa"), vec!["purple-sausages"]);
assert_eq!(w.get_completions("seltz"), vec!["seltzer"]);
assert_eq!(w.get_completions("sausages-yello"), vec!["sausages-yellow"]);
}

#[test]
Expand All @@ -202,6 +210,20 @@ mod test {
assert_eq!(w.choose_words().as_ref(), "purple-sausages-purple-sausages");
}

#[test]
fn test_choose_words_matches_completion() {
let few_words: Vec<Vec<String>> = vec![vec_strings("purple"), vec_strings("sausages")];

let w = Wordlist::new(2, few_words.clone());
assert_eq!(w.choose_words().as_ref(), "purple-sausages");

// Check if odd and even wordlist are correctly selected
assert_eq!(
w.get_completions("1-purple-sausages").first().unwrap(),
&format!("1-{}", w.choose_words().as_ref())
);
}

#[test]
fn test_choose_more_words() {
let more_words = vec![vec_strings("purple yellow"), vec_strings("sausages")];
Expand All @@ -227,58 +249,76 @@ mod test {

#[test]
#[cfg(feature = "fuzzy-complete")]
fn test_wormhole_code_fuzzy_completions() {
let list = Wordlist::default_wordlist(2);

assert_eq!(list.get_completions("22"), Vec::<String>::new());
assert_eq!(list.get_completions("22-"), Vec::<String>::new());

// Invalid wormhole code check
assert_eq!(list.get_completions("trj"), Vec::<String>::new());
fn test_completion_fuzzy() {
let wl = Wordlist::default_wordlist(2);
let list = wl.get_wordlist("22-");

assert_eq!(
list.get_completions("22-chisel"),
["22-chisel", "22-chairlift", "22-christmas"]
wl.fuzzy_complete("bzili", list).first().unwrap(),
"brazilian"
);

assert_eq!(
list.get_completions("22-chle"),
["22-chisel", "22-chatter", "22-checkup"]
wl.fuzzy_complete("carvan", list).first().unwrap(),
"caravan"
);

assert_eq!(list.get_completions("22-chisel-tba"), ["22-chisel-tobacco"]);
assert_ne!(
wl.fuzzy_complete("choking", list).first().unwrap(),
"choking"
)
}

#[test]
#[cfg(feature = "fuzzy-complete")]
fn test_completion_fuzzy() {
fn test_completion_normal() {
let wl = Wordlist::default_wordlist(2);
let list = wl.get_wordlist("22-");

assert_eq!(wl.fuzzy_complete("chck", list), ["checkup", "choking"]);
assert_eq!(wl.fuzzy_complete("checkp", list), ["checkup"]);
assert_eq!(
wl.fuzzy_complete("checkup", list),
["checkup", "lockup", "cleanup"]
wl.normal_complete("braz", list).first().unwrap(),
"brazilian"
);
assert_ne!(
wl.fuzzy_complete("choking", list).first().unwrap(),
"choking"
)
}

#[test]
fn test_completion_normal() {
let wl = Wordlist::default_wordlist(2);
let list = wl.get_wordlist("22-");
fn test_wormhole_code_normal_completions() {
let list = Wordlist::default_wordlist(2);

assert_eq!(wl.normal_complete("che", list), ["checkup"]);
assert_eq!(
list.get_completions("22-cmpont").first().unwrap(),
"22-component"
);
assert_eq!(
list.get_completions("22-component-chkup").first().unwrap(),
"22-component-checkup"
);
assert_ne!(list.get_completions("22-troj"), ["trojan"]);
}

#[test]
fn test_full_wormhole_completion() {
let wl = Wordlist::default_wordlist(2);
#[cfg(feature = "fuzzy-complete")]
fn test_wormhole_code_fuzzy_completions() {
let list = Wordlist::default_wordlist(2);

assert_eq!(list.get_completions("22"), Vec::<String>::new());
assert_eq!(list.get_completions("22-"), Vec::<String>::new());
assert_ne!(list.get_completions("22-trj"), ["trojan"]);

assert_eq!(
list.get_completions("22-udau").first().unwrap(),
"22-undaunted"
);

assert_eq!(
list.get_completions("22-undua").first().unwrap(),
"22-undaunted"
);

assert_eq!(wl.get_completions("22-chec").first().unwrap(), "22-checkup");
assert_eq!(
wl.get_completions("22-checkup-t").first().unwrap(),
"22-checkup-tobacco"
list.get_completions("22-undaunted-usht").first().unwrap(),
"22-undaunted-upshot"
);
}
}