feat(shell): add quote/cursor-aware completion, Ctrl+P fuzzy picker, and opt-in history
- Add quote-aware, mid-cursor, and command/subcommand-aware Tab completion in shell - Add Ctrl+P keybinding and pick command for interactive fuzzy search via search/suggest and path-picker - Add explicit opt-in persistent shell history (pages shell --history) stored in 0700/0600 files with secret-line filtering - Add unit tests and update README.md and docs/client-contract.md
This commit is contained in:
1 parent
a1956dcc6a
commit
44be7c6382
4 files changed
+488
-32
No files matched your search
+112
@@ -1324,6 +1324,118 @@ class RemoteTests(unittest.TestCase):
|
||||
m['execute'](client, profile, ['mput', str(big_file), '/wiki', '--lang', 'ja'])
|
||||
create_mock.assert_not_called()
|
||||
|
||||
def test_shell_completions_quotes_mid_cursor_and_commands(self):
|
||||
client = Client('https://example.test', 'token')
|
||||
def fake_listing(path, prefix=''):
|
||||
data = {
|
||||
'/': [
|
||||
{'name': 'my folder', 'path': '/my folder', 'kind': 'folder'},
|
||||
{'name': 'my doc', 'path': '/my doc', 'kind': 'file'},
|
||||
{'name': 'docs', 'path': '/docs', 'kind': 'folder'},
|
||||
{'name': 'bad\x1bctrl', 'path': '/bad\x1bctrl', 'kind': 'file'},
|
||||
],
|
||||
'/docs': [
|
||||
{'name': 'guide', 'path': '/docs/guide', 'kind': 'file'},
|
||||
{'name': 'sub dir', 'path': '/docs/sub dir', 'kind': 'folder'},
|
||||
],
|
||||
}
|
||||
entries = [e for e in data.get(path, []) if e['name'].startswith(prefix)]
|
||||
return {'entries': entries}
|
||||
|
||||
with patch.object(client, 'listing', side_effect=fake_listing):
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'ed'), ['edit'])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'm'), ['mget', 'mput'])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'task pr'), ['task preferences', 'task project-add', 'task projects'])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'cd "my fol'), ['cd "my folder/'])
|
||||
self.assertEqual(m['shell_completions'](client, '/', "edit 'my d"), ["edit 'my doc'"])
|
||||
unquoted = m['shell_completions'](client, '/', 'cat my')
|
||||
self.assertEqual(unquoted, ["cat 'my folder/'", "cat 'my doc'"])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'edit docs/gu'), ['edit docs/guide'])
|
||||
self.assertEqual(m['shell_completions'](client, '/docs', 'ls sub'), ["ls 'sub dir/'"])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'edit --l'), [])
|
||||
self.assertEqual(m['shell_completions'](client, '/', 'cat uuid:123'), [])
|
||||
|
||||
def test_fuzzy_pick_ctrl_p_and_opt_in_history(self):
|
||||
client = Client('https://example.test', 'token')
|
||||
suggest_items = [
|
||||
{'path': '/docs/guide', 'label': 'User Guide', 'kind': 'page'},
|
||||
{'path': '/notes/design', 'label': 'Architecture', 'kind': 'page'},
|
||||
{'path': '/evil\x07path', 'label': 'Evil', 'kind': 'page'},
|
||||
]
|
||||
listing_items = [
|
||||
{'name': 'local-note', 'path': '/local-note', 'kind': 'file'},
|
||||
]
|
||||
def fake_json(route, **kwargs):
|
||||
self.assertTrue(route.startswith('/_/api/v1/search/suggest?'))
|
||||
return {'items': suggest_items}
|
||||
with patch.object(client, 'json', side_effect=fake_json), \
|
||||
patch.object(client, 'listing', return_value={'entries': listing_items}):
|
||||
cands = m['_gather_pick_candidates'](client, '/', 'dg')
|
||||
paths = [c['path'] for c in cands]
|
||||
self.assertEqual(paths[0], '/docs/guide')
|
||||
self.assertNotIn('/evil\x07path', paths)
|
||||
|
||||
profile = type('P', (), {'name': 'test', 'data': {'cwd': '/', 'expires': time.time() + 600}})()
|
||||
fake_tty = type('TTY', (), {'isatty': lambda self: True})()
|
||||
with patch.object(m['sys'], 'stdin', fake_tty), \
|
||||
patch.object(client, 'json', return_value={'items': suggest_items}), \
|
||||
patch.object(client, 'listing', return_value={'entries': listing_items}):
|
||||
with patch('builtins.input', side_effect=['1']), contextlib.redirect_stdout(io.StringIO()):
|
||||
res = m['fuzzy_pick'](client, profile, initial_query='dg', initial_line='edit ')
|
||||
self.assertEqual(res, 'edit /docs/guide')
|
||||
with patch('builtins.input', side_effect=['dg', '1', 'c']), \
|
||||
patch.dict(m['fuzzy_pick'].__globals__, {'execute': lambda c, p, w: p.data.__setitem__('cwd', w[1])}), \
|
||||
contextlib.redirect_stdout(io.StringIO()):
|
||||
res2 = m['fuzzy_pick'](client, profile)
|
||||
self.assertIsNone(res2)
|
||||
self.assertEqual(profile.data['cwd'], '/docs/guide')
|
||||
|
||||
with patch.object(m['sys'], 'stdin', io.StringIO('')):
|
||||
with self.assertRaisesRegex(Failure, 'requires a TTY'):
|
||||
m['fuzzy_pick'](client, profile)
|
||||
|
||||
class FakeReadline:
|
||||
def __init__(self, items=None):
|
||||
self.items = list(items or [])
|
||||
self.delims = ''
|
||||
def set_history_length(self, n): pass
|
||||
def set_completer_delims(self, d): self.delims = d
|
||||
def set_completer(self, fn): pass
|
||||
def parse_and_bind(self, s): pass
|
||||
def set_completion_display_matches_hook(self, fn): pass
|
||||
def set_pre_input_hook(self, fn): pass
|
||||
def get_current_history_length(self): return len(self.items)
|
||||
def get_history_item(self, i): return self.items[i - 1]
|
||||
def add_history(self, s): self.items.append(s)
|
||||
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
fake_rl = FakeReadline()
|
||||
with patch.dict('sys.modules', {'readline': fake_rl}), \
|
||||
patch('builtins.input', side_effect=['pwd', 'exit']), \
|
||||
contextlib.redirect_stdout(io.StringIO()):
|
||||
m['shell'](client, profile, persist_history=False, history_root=root)
|
||||
self.assertFalse((Path(root) / 'pages-cli/history').exists())
|
||||
|
||||
fake_rl2 = FakeReadline(['ls /docs', 'login', 'cat /a Bearer secret', 'pwd', 'edit pw_api_123'])
|
||||
with patch.dict('sys.modules', {'readline': fake_rl2}), \
|
||||
patch('builtins.input', side_effect=['\x1fedit dg', 'exit']), \
|
||||
patch.dict(m['shell'].__globals__, {'fuzzy_pick': lambda c, p, initial_query='', initial_line='': initial_line + '/docs/guide'}), \
|
||||
contextlib.redirect_stdout(io.StringIO()):
|
||||
m['shell'](client, profile, persist_history=True, history_root=root)
|
||||
hist_file = Path(root) / 'pages-cli/history/test.history'
|
||||
self.assertTrue(hist_file.exists())
|
||||
self.assertEqual(stat.S_IMODE(hist_file.parent.stat().st_mode), 0o700)
|
||||
self.assertEqual(stat.S_IMODE(hist_file.stat().st_mode), 0o600)
|
||||
saved_lines = hist_file.read_text(encoding='utf-8').splitlines()
|
||||
self.assertEqual(saved_lines, ['ls /docs', 'pwd'])
|
||||
|
||||
loader_rl = FakeReadline()
|
||||
m['_load_shell_history'](loader_rl, hist_file)
|
||||
self.assertEqual(loader_rl.items, ['ls /docs', 'pwd'])
|
||||
hist_file.chmod(0o644)
|
||||
with self.assertRaisesRegex(Failure, '0600'):
|
||||
m['_load_shell_history'](FakeReadline(), hist_file)
|
||||
|
||||
|
||||
def oidc_integration():
|
||||
fixture=json.load(sys.stdin)
|
||||
|
||||
Reference in new issue
Block a user