Skip to content

Commit b6c7b82

Browse files
committed
chore: tests and sort by distance
1 parent ef6d195 commit b6c7b82

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/lib/hooks/useCommandSuggestions.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { commandRegistry } from '../command-framework/apify-command.js';
55
import { cliDebugPrint } from '../utils/cliDebugPrint.js';
66

77
export function useCommandSuggestions(inputString: string) {
8-
const allCommands = [...commandRegistry.entries()];
8+
const allCommands = [...commandRegistry.entries()].sort(([a], [b]) => a.localeCompare(b));
99

1010
const lowercasedCommandString = inputString.toLowerCase();
1111

@@ -32,15 +32,17 @@ export function useCommandSuggestions(inputString: string) {
3232
});
3333

3434
if (!isAlias) {
35-
return `${lowercased}`;
35+
return { string: `${lowercased}`, distance: jaroWinklerDistance };
3636
}
3737

38-
return `${lowercased} (alias for ${cmdClass.name})`;
38+
return { string: `${lowercased} (alias for ${cmdClass.name})`, distance: jaroWinklerDistance };
3939
}
4040

4141
return null;
4242
})
43-
.filter((item) => item !== null);
43+
.filter((item) => item !== null)
44+
.sort((a, b) => b.distance - a.distance)
45+
.map((item) => item.string);
4446

4547
return closestMatches;
4648
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint-disable max-classes-per-file */
2+
import {
3+
ApifyCommand,
4+
type BuiltApifyCommand as _BuiltApifyCommand,
5+
commandRegistry,
6+
} from '../../../src/lib/command-framework/apify-command.js';
7+
import { useCommandSuggestions } from '../../../src/lib/hooks/useCommandSuggestions.js';
8+
9+
const BuiltApifyCommand = ApifyCommand as typeof _BuiltApifyCommand;
10+
11+
const subcommands = [
12+
class KVSGetValue extends BuiltApifyCommand {
13+
static override name = 'get-value';
14+
},
15+
class KVSSetValue extends BuiltApifyCommand {
16+
static override name = 'set-value';
17+
},
18+
];
19+
20+
const fakeCommands = [
21+
class Help extends BuiltApifyCommand {
22+
static override name = 'help';
23+
},
24+
class Upgrade extends BuiltApifyCommand {
25+
static override name = 'upgrade';
26+
static override aliases = ['cv', 'check-version'];
27+
},
28+
class KeyValueStores extends BuiltApifyCommand {
29+
static override name = 'key-value-stores';
30+
static override aliases = ['kvs'];
31+
static override subcommands = subcommands;
32+
},
33+
];
34+
35+
describe('useCommandSuggestions', () => {
36+
let existingCommands: [string, typeof BuiltApifyCommand][];
37+
38+
beforeAll(() => {
39+
existingCommands = [...commandRegistry.entries()];
40+
41+
commandRegistry.clear();
42+
43+
for (const command of fakeCommands) {
44+
commandRegistry.set(command.name, command);
45+
46+
if (command.aliases?.length) {
47+
for (const alias of command.aliases) {
48+
commandRegistry.set(alias, command);
49+
50+
if (command.subcommands?.length) {
51+
for (const subcommand of command.subcommands) {
52+
commandRegistry.set(`${alias} ${subcommand.name}`, subcommand);
53+
}
54+
}
55+
}
56+
}
57+
58+
if (command.subcommands?.length) {
59+
for (const subcommand of command.subcommands) {
60+
commandRegistry.set(`${command.name} ${subcommand.name}`, subcommand);
61+
62+
if (command.aliases?.length) {
63+
for (const alias of command.aliases) {
64+
commandRegistry.set(`${command.name} ${alias}`, subcommand);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
});
71+
72+
afterAll(() => {
73+
commandRegistry.clear();
74+
75+
for (const [name, command] of existingCommands) {
76+
commandRegistry.set(name, command);
77+
}
78+
});
79+
80+
test.each([
81+
['hlp', 'help'],
82+
['kv', 'kvs (alias for key-value-stores)', 'cv (alias for upgrade)'],
83+
// assert order based on distance
84+
['kvs get-values', 'kvs get-value', 'kvs set-value'],
85+
['kvs set-values', 'kvs set-value', 'kvs get-value'],
86+
])('should return the correct command suggestions for %s', (input, ...expected) => {
87+
const suggestions = useCommandSuggestions(input);
88+
89+
expect(suggestions).toEqual(expected);
90+
});
91+
});

0 commit comments

Comments
 (0)