feat(tui): 菜单渲染与循环(第一轮第四块·下)
形态是行内(ADR-0010):菜单按普通行往下打,之后每次按键只把那几行原地重画(记下起始行再定位回去)。不做全屏、不切备用缓冲区 —— 崩了的时候滚屏里还留着上文,而"终端被 TUI 弄乱"是这类代码最难排查的故障。 没有控制台时只打一次、不重画、不抛错,靠 -Driver 里的 -InputScript 驱动 —— 同一个循环在真终端与门禁里走**同一条代码路径**,断言看的是返回值(Action/Index/Chosen/Checked),不需要捕获控制台输出。 两处刻意的不猜:空菜单直接返回 cancel 并说明原因(没有可选项时不该等按键 —— 第一次使用时归档目录就是空的,在计划任务里等按键等于挂起);绝不 exit、绝不碰退出码(TUI 的异常不该污染备份结果)。 这块的断言按**数量分档**写(0 项 / 1 项 / 3 项)——上一块的单键 Bug 正是藏在"1 项"这个档位里,而当时只跑了 3 项的序列。 验收:断言 10 条;test.ps1 9/9 全绿(5.1 与 7);真实清单只读冒烟 4/4。
This commit is contained in:
1 parent
6315c72f69
commit
835fb0cf2b
4 files changed
+129
No files matched your search
@@ -1523,6 +1523,39 @@ Test-Case '菜单状态机:绕回、空格切换、确认/取消、空菜单
|
||||
|
||||
|
||||
|
||||
Test-Case '菜单循环:按数量分档验(0 项 / 1 项 / 3 项),单键与多键序列都能走完' {
|
||||
$items = @(
|
||||
[pscustomobject]@{ Key = 'a'; Text = '第一个' },
|
||||
[pscustomobject]@{ Key = 'b'; Text = '第二个' },
|
||||
[pscustomobject]@{ Key = 'c'; Text = '第三个' }
|
||||
)
|
||||
|
||||
# 按数量分档是踩出来的:单键序列曾经因为"单元素数组被拆成字符串"而变成首字母,
|
||||
# 而只跑多键序列的断言看不见它。所以 1 项的档位与 0 项的档位各自都要有断言。
|
||||
$esc = Invoke-BakNRetMenu -Title '测试' -Items $items -Driver (New-BakNRetInputDriver -Script @('Esc'))
|
||||
Assert-Equal 'cancel' $esc.Action '单键 Esc 应当直接取消'
|
||||
Assert-Equal 'confirm' (Invoke-BakNRetMenu -Title '测试' -Items $items -Driver (New-BakNRetInputDriver -Script @('Enter'))).Action '单键回车应当确认'
|
||||
|
||||
# 1 项:上下键都停在 0,回车仍能确认
|
||||
$oneItem = @([pscustomobject]@{ Key = 'only'; Text = '只有一项' })
|
||||
$single = Invoke-BakNRetMenu -Title '测试' -Items $oneItem -Driver (New-BakNRetInputDriver -Script @('Up', 'Enter'))
|
||||
Assert-Equal 'confirm' $single.Action '只有一项时也能确认'
|
||||
Assert-Equal 0 $single.Index '只有一项时按上键仍停在 0'
|
||||
|
||||
# 3 项:下移 + 勾选 + 确认
|
||||
$result = Invoke-BakNRetMenu -Title '测试' -Items $items -Driver (New-BakNRetInputDriver -Script @('Down', 'Space', 'Enter'))
|
||||
Assert-Equal 'confirm' $result.Action '回车确认'
|
||||
Assert-Equal 1 $result.Index '下移一格'
|
||||
Assert-Equal 'b' $result.Chosen.Key '选中的是第二项'
|
||||
Assert-True $result.Checked.ContainsKey('b') 'b 被勾上'
|
||||
|
||||
# 0 项:驱动器里一个键都没有 —— 若它读键就会抛错,所以"不抛错且返回 cancel"同时证明它没等按键
|
||||
$emptyList = @()
|
||||
$empty = Invoke-BakNRetMenu -Title '测试' -Items $emptyList -Driver (New-BakNRetInputDriver -Script @())
|
||||
Assert-Equal 'cancel' $empty.Action '空菜单直接返回 cancel'
|
||||
Assert-Equal 'empty' $empty.Reason '原因标为 empty'
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
Remove-Item -LiteralPath $sandbox -Recurse -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item -LiteralPath $itemSandbox -Recurse -Force -ErrorAction SilentlyContinue
|
||||
|
||||
Reference in new issue
Block a user