|
| 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 | + ['key-value-stor', 'key-value-stores'], |
| 84 | + // assert order based on distance |
| 85 | + ['kvs get-values', 'kvs get-value', 'kvs set-value'], |
| 86 | + ['kvs set-values', 'kvs set-value', 'kvs get-value'], |
| 87 | + ])('command suggestions for %s', (input, ...expected) => { |
| 88 | + const suggestions = useCommandSuggestions(input); |
| 89 | + |
| 90 | + expect(suggestions).toEqual(expected); |
| 91 | + }); |
| 92 | +}); |
0 commit comments