dsh-vscode-mode 0.39.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,485 @@
1
+ /**
2
+ * dsh-vscode-mode client — 侧栏「测试验收」面板。
3
+ *
4
+ * 五块内容(自上而下):使用说明、专属技能、项目测试规范、当前验收项、全部验收单。
5
+ * 取数、轮询与三个动作在 `useTestCheckData` 里;本文件只负责摆位置、两处浮窗与文案。
6
+ * 侧栏很窄,所以两张列表都给固定最大高度 + 内部滚动,不把整页撑长。
7
+ * @author ddj 2026年11月26号
8
+ */
9
+ import React from 'react'
10
+ import { copyText } from '../../copyText.js'
11
+ import { rpc } from '../../rpc.js'
12
+ import type { TestCheckSheetSummary, TestCheckView } from '../../../shared/testCheck.js'
13
+ import type { SidebarCtx, SidebarPanelDef } from '../types.js'
14
+ import { ModalShell } from '../../ui/ModalShell.js'
15
+ import { useTestCheckData } from './useTestCheckData.js'
16
+ import {
17
+ canSavePolicy,
18
+ isPolicyOversize,
19
+ policyBytesOf,
20
+ policySaveLabel,
21
+ policySizeText,
22
+ policyStatusText,
23
+ sheetRowHint,
24
+ sheetTallyOf,
25
+ type PolicyView,
26
+ } from '../../testCheckPanelState.js'
27
+
28
+ //region 常量
29
+
30
+ /** 随包技能名(与 `skills/` 下的目录、frontmatter 的 name 一致)。 */
31
+ const SKILL_NAME = 'dsh-vscodemode-test-check'
32
+
33
+ /** 「使用说明」浮窗正文(写死在客户端;改文案只动这里)。 */
34
+ const HELP_LINES: readonly string[] = [
35
+ '1. 用 /test-check <任务> 进入测试验收模式;任务文本会派生出验收单 id,同一句话再进模式回到同一张单。',
36
+ '2. 底部栏出现「验收 x/y」chip,点它在文件编辑里打开验收单页签;页签里能看条目、手动上报、导出快照。',
37
+ '3. AI 每验一条必须调用 test_check_report,参数 item / status / summary 都必填;summary 就是证据(测试名+通过数、命令+退出码、日志路径)。',
38
+ '4. 状态取 pass / fail / blocked / skip。没有证据的 pass 只显示「无证据」,不计入通过;被验文件改过会变成「需复验」。',
39
+ '5. 全部验完调用 test_check_finish:导出 docs/acceptance-<id>.html 并退出模式;还要补验就带 stay: true。',
40
+ '6. 手动退出用 /test-check off(底部栏 chip 旁提示等价)。退出后不要再上报。',
41
+ '7. 项目验收要求就在这个面板的「项目测试规范」里改:保存后下一条消息生效,不用重构建插件。',
42
+ ]
43
+
44
+ //endregion
45
+
46
+ //region 图标
47
+
48
+ /**
49
+ * 「测试验收」活动栏图标:清单行 + 勾(16×16,跟随 currentColor)。
50
+ * @author ddj 2026年11月26号
51
+ * @param props 尺寸与类名
52
+ * @returns 图标元素
53
+ */
54
+ export function IconTestCheck16(props: { size?: number; className?: string }): React.ReactElement {
55
+ const size = props?.size ?? 16
56
+ return React.createElement('svg', {
57
+ width: size,
58
+ height: size,
59
+ viewBox: '0 0 16 16',
60
+ fill: 'none',
61
+ stroke: 'currentColor',
62
+ strokeWidth: 1.2,
63
+ strokeLinecap: 'round',
64
+ strokeLinejoin: 'round',
65
+ className: props?.className,
66
+ 'aria-hidden': true,
67
+ focusable: false,
68
+ },
69
+ React.createElement('path', { d: 'M2.6 3.9h3.6M2.6 8h3.6M2.6 12.1h3.6' }),
70
+ React.createElement('path', { d: 'M9 8.2l2.1 2.1 3.3-3.6' }))
71
+ }
72
+
73
+ //endregion
74
+
75
+ //region 子组件
76
+
77
+ /** 说明与技能入口的 props。 */
78
+ interface GuideProps {
79
+ /** 打开「使用说明」浮窗。 */
80
+ onHelp: () => void
81
+ /** 打开「专属技能」浮窗。 */
82
+ onSkill: () => void
83
+ }
84
+
85
+ /**
86
+ * 前两块:使用说明(「!」按钮进浮窗)与专属技能(只读查看)。
87
+ * @author ddj 2026年11月26号
88
+ * @param props 两个入口回调
89
+ * @returns 区块元素
90
+ */
91
+ function GuideSection(props: GuideProps): React.ReactElement {
92
+ return React.createElement('div', { className: 'edrv-tc-block' },
93
+ React.createElement('div', { className: 'edrv-tc-row' },
94
+ React.createElement('span', { className: 'edrv-tc-label' }, '使用说明'),
95
+ React.createElement('button', {
96
+ type: 'button',
97
+ className: 'edrv-tc-icon-btn',
98
+ title: '测试验收怎么用(Esc 或点遮罩关闭)',
99
+ 'aria-label': '打开使用说明',
100
+ onClick: props.onHelp,
101
+ }, '!')),
102
+ React.createElement('div', { className: 'edrv-tc-row' },
103
+ React.createElement('span', { className: 'edrv-tc-label' }, '专属技能'),
104
+ React.createElement('span', { className: 'edrv-tc-dim' }, SKILL_NAME + ' · 只读'),
105
+ React.createElement('button', {
106
+ type: 'button',
107
+ className: 'edrv-tc-btn',
108
+ title: '查看随插件发布的技能正文',
109
+ onClick: props.onSkill,
110
+ }, '查看')))
111
+ }
112
+
113
+ /** 规范编辑区的 props。 */
114
+ interface PolicyProps {
115
+ /** 当前披露的规范状态(没有会话时为 null)。 */
116
+ policy: PolicyView | null
117
+ /** 编辑框草稿。 */
118
+ draft: string
119
+ /** 读取或保存的错误。 */
120
+ error: string
121
+ /** 是否正在保存。 */
122
+ saving: boolean
123
+ /** 会话工作区(没有时禁用编辑)。 */
124
+ cwd: string | null
125
+ /** 草稿变更。 */
126
+ onDraft: (text: string) => void
127
+ /** 保存。 */
128
+ onSave: () => void
129
+ /** 恢复默认。 */
130
+ onReset: () => void
131
+ }
132
+
133
+ /**
134
+ * 项目测试规范:等宽编辑框 + 状态行 + 体积提示 + 保存/重置。
135
+ * @author ddj 2026年11月26号
136
+ * @param props 数据与回调
137
+ * @returns 区块元素
138
+ */
139
+ function PolicySection(props: PolicyProps): React.ReactElement {
140
+ const policy = props.policy
141
+ const disabled = !props.cwd || !policy
142
+ const savable = policy !== null && canSavePolicy({
143
+ cwd: props.cwd,
144
+ draft: props.draft,
145
+ savedText: policy.text,
146
+ isDefault: policy.isDefault,
147
+ })
148
+ const status = policy
149
+ ? policyStatusText(policy)
150
+ : '还没读到项目规则:先打开一个带工作区的会话'
151
+ const bytes = policyBytesOf(props.draft)
152
+ const oversize = isPolicyOversize(bytes)
153
+
154
+ return React.createElement('div', { className: 'edrv-tc-block' },
155
+ React.createElement('div', { className: 'edrv-tc-row' },
156
+ React.createElement('span', { className: 'edrv-tc-label' }, '项目测试规范'),
157
+ React.createElement('span', { className: 'edrv-tc-dim' }, '保存后下一条消息生效')),
158
+ React.createElement('textarea', {
159
+ className: 'edrv-tc-editor',
160
+ spellCheck: false,
161
+ value: props.draft,
162
+ disabled,
163
+ placeholder: '写本项目自己的验收要求:必须跑的用例、证据放哪、哪些条不许口头过……',
164
+ onChange: (event: React.ChangeEvent<HTMLTextAreaElement>) => props.onDraft(event.target.value),
165
+ }),
166
+ React.createElement('div', { className: 'edrv-tc-status', title: policy?.path ?? '' }, status),
167
+ React.createElement('div', { className: 'edrv-tc-hint' },
168
+ '保存空内容 = 删除自定义规则、回到默认;.dsh/test-check/.gitignore 默认忽略本文件,团队共享需加一行 !policy.md'),
169
+ oversize
170
+ ? React.createElement('div', { className: 'edrv-tc-hint warn' },
171
+ '当前 ' + policySizeText(bytes) + ':超出的部分不会进入规则段,请精简后再保存')
172
+ : null,
173
+ props.error ? React.createElement('div', { className: 'edrv-tc-error' }, props.error) : null,
174
+ React.createElement('div', { className: 'edrv-tc-actions' },
175
+ React.createElement('button', {
176
+ type: 'button',
177
+ className: 'edrv-tc-btn primary',
178
+ disabled: disabled || props.saving || !savable,
179
+ title: savable ? '' : '没有改动或没有工作区',
180
+ onClick: props.onSave,
181
+ }, props.saving ? '保存中…' : policySaveLabel(props.draft)),
182
+ React.createElement('button', {
183
+ type: 'button',
184
+ className: 'edrv-tc-btn',
185
+ disabled,
186
+ onClick: props.onReset,
187
+ }, '重置')))
188
+ }
189
+
190
+ /** 当前验收项的 props。 */
191
+ interface CurrentProps {
192
+ /** 当前单的派生视图(没有时为 null)。 */
193
+ view: TestCheckView | null
194
+ /** 当前单读取错误。 */
195
+ error: string
196
+ /** 打开验收单页签。 */
197
+ onOpen: () => void
198
+ }
199
+
200
+ /**
201
+ * 当前验收项:一行一条(状态点 + 标题 + 状态标签),点行打开验收单页签。
202
+ * @author ddj 2026年11月26号
203
+ * @param props 视图、错误与打开回调
204
+ * @returns 区块元素
205
+ */
206
+ function CurrentSection(props: CurrentProps): React.ReactElement {
207
+ const view = props.view
208
+ const items = view ? view.groups.flatMap((group) => group.items) : []
209
+ const head = React.createElement('div', { className: 'edrv-tc-row' },
210
+ React.createElement('span', { className: 'edrv-tc-label' }, '当前验收项'),
211
+ React.createElement('span', { className: 'edrv-tc-dim', title: view?.title ?? '' }, view ? sheetTallyOf(view.counts) : '—'),
212
+ React.createElement('button', {
213
+ type: 'button',
214
+ className: 'edrv-tc-btn',
215
+ title: '在文件编辑里打开验收单页签',
216
+ onClick: props.onOpen,
217
+ }, '打开'))
218
+
219
+ if (!items.length) {
220
+ const empty = props.error
221
+ ? props.error
222
+ : (view?.state === 'invalid' ? 'items.json 有错:' + String(view.error ?? '') : '还没有验收单(进入模式后让 AI 建单)')
223
+ return React.createElement('div', { className: 'edrv-tc-block' }, head,
224
+ React.createElement('div', { className: 'edrv-tc-empty' }, empty))
225
+ }
226
+
227
+ return React.createElement('div', { className: 'edrv-tc-block' }, head,
228
+ React.createElement('div', { className: 'edrv-tc-list' },
229
+ items.map((item) => React.createElement('div', {
230
+ key: item.id,
231
+ className: 'edrv-tc-item',
232
+ role: 'button',
233
+ tabIndex: 0,
234
+ title: item.id + ' · ' + (item.evidence ? '有证据' : '无证据'),
235
+ onClick: props.onOpen,
236
+ onKeyDown: (event: React.KeyboardEvent) => {
237
+ if (event.key === 'Enter' || event.key === ' ') props.onOpen()
238
+ },
239
+ },
240
+ React.createElement('span', { className: 'edrv-tc-dot is-' + item.verdict, 'aria-hidden': true }, '●'),
241
+ React.createElement('span', { className: 'edrv-tc-name' }, item.title),
242
+ React.createElement('span', { className: 'edrv-tc-tag' }, item.label)))))
243
+ }
244
+
245
+ /** 全部验收单的 props。 */
246
+ interface SheetsProps {
247
+ /** 行列表。 */
248
+ rows: readonly TestCheckSheetSummary[]
249
+ /** 是否正在刷新。 */
250
+ busy: boolean
251
+ /** 读取错误。 */
252
+ error: string
253
+ /** 刷新。 */
254
+ onRefresh: () => void
255
+ /** 打开快照。 */
256
+ onOpen: (row: TestCheckSheetSummary) => void
257
+ /** 移除(已确认)。 */
258
+ onRemove: (row: TestCheckSheetSummary) => void
259
+ }
260
+
261
+ /**
262
+ * 全部验收单:一行一张,点行打开快照;快照缺失的行只留「移除」。
263
+ * @author ddj 2026年11月26号
264
+ * @param props 行数据与三个回调
265
+ * @returns 区块元素
266
+ */
267
+ function SheetsSection(props: SheetsProps): React.ReactElement {
268
+ const head = React.createElement('div', { className: 'edrv-tc-row' },
269
+ React.createElement('span', { className: 'edrv-tc-label' }, '全部验收单'),
270
+ React.createElement('span', { className: 'edrv-tc-dim' }, String(props.rows.length) + ' 张'),
271
+ React.createElement('button', {
272
+ type: 'button',
273
+ className: 'edrv-tc-btn',
274
+ disabled: props.busy,
275
+ title: '重新扫描 .dsh/test-check 与 docs',
276
+ onClick: props.onRefresh,
277
+ }, props.busy ? '刷新中…' : '刷新'))
278
+
279
+ if (!props.rows.length) {
280
+ return React.createElement('div', { className: 'edrv-tc-block' }, head,
281
+ React.createElement('div', { className: 'edrv-tc-empty' }, props.error || '项目里还没有验收单'))
282
+ }
283
+
284
+ return React.createElement('div', { className: 'edrv-tc-block' }, head,
285
+ props.error ? React.createElement('div', { className: 'edrv-tc-error' }, props.error) : null,
286
+ React.createElement('div', { className: 'edrv-tc-list' },
287
+ props.rows.map((row) => React.createElement('div', {
288
+ key: row.id,
289
+ className: 'edrv-tc-sheet' + (row.hasSnapshot ? '' : ' is-missing'),
290
+ role: 'button',
291
+ tabIndex: 0,
292
+ title: sheetRowHint(row.hasSnapshot) + '\n' + row.snapshotPath,
293
+ onClick: () => props.onOpen(row),
294
+ onKeyDown: (event: React.KeyboardEvent) => {
295
+ if (event.key === 'Enter' || event.key === ' ') props.onOpen(row)
296
+ },
297
+ },
298
+ React.createElement('span', { className: 'edrv-tc-name' }, row.title),
299
+ React.createElement('span', { className: 'edrv-tc-dim' }, sheetTallyOf(row.counts)),
300
+ row.hasSnapshot
301
+ ? React.createElement('span', { className: 'edrv-tc-tag' }, '打开')
302
+ : React.createElement('span', { className: 'edrv-tc-tag warn' }, '快照缺失'),
303
+ React.createElement('button', {
304
+ type: 'button',
305
+ className: 'edrv-tc-btn danger',
306
+ title: '删除这张验收单与它的快照',
307
+ onClick: (event: React.MouseEvent) => {
308
+ event.stopPropagation()
309
+ props.onRemove(row)
310
+ },
311
+ }, '移除')))))
312
+ }
313
+
314
+ /** 说明浮窗的 props。 */
315
+ interface HelpProps {
316
+ /** 关闭。 */
317
+ onClose: () => void
318
+ }
319
+
320
+ /**
321
+ * 使用说明浮窗(ModalShell 外壳:Esc 与点遮罩都能关)。
322
+ * @author ddj 2026年11月26号
323
+ * @param props 关闭回调
324
+ * @returns 浮窗元素
325
+ */
326
+ function HelpModal(props: HelpProps): React.ReactElement {
327
+ return React.createElement(ModalShell, {
328
+ dialogClass: 'edrv-tc-dialog',
329
+ width: 'min(760px, calc(100vw - 40px))',
330
+ onClose: props.onClose,
331
+ },
332
+ React.createElement('div', { className: 'edrv-tc-dialog-head' },
333
+ React.createElement('span', { className: 'edrv-tc-dialog-title' }, '测试验收怎么用'),
334
+ React.createElement('button', {
335
+ type: 'button',
336
+ className: 'edrv-tc-icon-btn',
337
+ title: '关闭(Esc)',
338
+ onClick: props.onClose,
339
+ }, '✕')),
340
+ React.createElement('div', { className: 'edrv-tc-dialog-body' },
341
+ HELP_LINES.map((line) => React.createElement('p', { key: line }, line))))
342
+ }
343
+
344
+ /** 技能浮窗的 props。 */
345
+ interface SkillProps {
346
+ /** 技能全文(未加载时为 null)。 */
347
+ skill: { name: string; path: string; content: string } | null
348
+ /** 读取错误。 */
349
+ error: string
350
+ /** 复制正文。 */
351
+ onCopy: () => void
352
+ /** 关闭。 */
353
+ onClose: () => void
354
+ }
355
+
356
+ /**
357
+ * 专属技能浮窗:只读 `<pre>` 全文(含 frontmatter),提供复制。
358
+ * @author ddj 2026年11月26号
359
+ * @param props 正文、错误与回调
360
+ * @returns 浮窗元素
361
+ */
362
+ function SkillModal(props: SkillProps): React.ReactElement {
363
+ return React.createElement(ModalShell, {
364
+ dialogClass: 'edrv-tc-dialog',
365
+ width: 'min(860px, calc(100vw - 40px))',
366
+ onClose: props.onClose,
367
+ },
368
+ React.createElement('div', { className: 'edrv-tc-dialog-head' },
369
+ React.createElement('span', { className: 'edrv-tc-dialog-title' }, '专属技能 · ' + SKILL_NAME),
370
+ React.createElement('button', {
371
+ type: 'button',
372
+ className: 'edrv-tc-btn',
373
+ disabled: !props.skill,
374
+ onClick: props.onCopy,
375
+ }, '复制'),
376
+ React.createElement('button', {
377
+ type: 'button',
378
+ className: 'edrv-tc-icon-btn',
379
+ title: '关闭(Esc)',
380
+ onClick: props.onClose,
381
+ }, '✕')),
382
+ React.createElement('div', { className: 'edrv-tc-dialog-sub', title: props.skill?.path ?? '' },
383
+ props.skill ? '随插件版本更新:' + props.skill.path : '正在读取…'),
384
+ props.error
385
+ ? React.createElement('div', { className: 'edrv-tc-dialog-body' }, React.createElement('div', { className: 'edrv-tc-error' }, props.error))
386
+ : React.createElement('div', { className: 'edrv-tc-dialog-body' },
387
+ React.createElement('pre', { className: 'edrv-tc-pre' }, props.skill?.content ?? '正在读取…')))
388
+ }
389
+
390
+ //endregion
391
+
392
+ //region 面板
393
+
394
+ /**
395
+ * 面板主体:取数交给 `useTestCheckData`,这里只管浮窗状态与拼装。
396
+ * @author ddj 2026年11月26号
397
+ * @param props 侧栏共享上下文
398
+ * @returns 面板元素
399
+ */
400
+ export function TestCheckPanel(props: { ctx?: SidebarCtx }): React.ReactElement {
401
+ const ctx = props?.ctx
402
+ const notify = ctx?.notify
403
+ const data = useTestCheckData(ctx)
404
+ const [modal, setModal] = React.useState<'help' | 'skill' | null>(null)
405
+ const [skill, setSkill] = React.useState<{ name: string; path: string; content: string } | null>(null)
406
+ const [skillError, setSkillError] = React.useState('')
407
+ const aliveRef = React.useRef(true)
408
+
409
+ /** 每次打开都重读技能(插件升级后不会停在旧文)。 */
410
+ async function loadSkill(): Promise<void> {
411
+ setSkillError('')
412
+ try {
413
+ const raw = await rpc('edrv.testCheck.skill', {})
414
+ if (!aliveRef.current) return
415
+ if (!raw.ok) { setSkillError(raw.error); return }
416
+ setSkill({ name: raw.name, path: raw.path, content: raw.content })
417
+ } catch (error) {
418
+ if (aliveRef.current) setSkillError('读取技能失败:' + (error instanceof Error ? error.message : String(error)))
419
+ }
420
+ }
421
+
422
+ React.useEffect(() => () => { aliveRef.current = false }, [])
423
+
424
+ return React.createElement('div', { className: 'edrv-tc-panel' },
425
+ React.createElement(GuideSection, {
426
+ onHelp: () => setModal('help'),
427
+ onSkill: () => {
428
+ setModal('skill')
429
+ void loadSkill()
430
+ },
431
+ }),
432
+ React.createElement(PolicySection, {
433
+ policy: data.policy,
434
+ draft: data.draft,
435
+ error: data.policyError,
436
+ saving: data.saving,
437
+ cwd: ctx?.cwd ?? null,
438
+ onDraft: data.setDraft,
439
+ onSave: () => { void data.savePolicy() },
440
+ onReset: () => { void data.resetPolicy() },
441
+ }),
442
+ React.createElement(CurrentSection, {
443
+ view: data.view,
444
+ error: data.currentError,
445
+ onOpen: () => { void data.openSheetTab() },
446
+ }),
447
+ React.createElement(SheetsSection, {
448
+ rows: data.rows,
449
+ busy: data.sheetsBusy,
450
+ error: data.sheetsError,
451
+ onRefresh: () => { void data.refreshAll(true) },
452
+ onOpen: (row) => { void data.openSnapshot(row) },
453
+ onRemove: (row) => { void data.removeRow(row) },
454
+ }),
455
+ modal === 'help' ? React.createElement(HelpModal, { onClose: () => setModal(null) }) : null,
456
+ modal === 'skill'
457
+ ? React.createElement(SkillModal, {
458
+ skill,
459
+ error: skillError,
460
+ onCopy: () => { if (skill) void copyText(skill.content, '技能正文已复制', notify) },
461
+ onClose: () => setModal(null),
462
+ })
463
+ : null)
464
+ }
465
+
466
+ //endregion
467
+
468
+ //region 注册
469
+
470
+ /**
471
+ * 构造「测试验收」侧栏面板定义。
472
+ * @author ddj 2026年11月26号
473
+ * @returns 面板定义(常驻可见;无工作区时面板内提示)
474
+ */
475
+ export function createTestCheckPanel(): SidebarPanelDef {
476
+ return {
477
+ id: 'testcheck',
478
+ title: '测试验收',
479
+ icon: IconTestCheck16,
480
+ order: 21,
481
+ render: (ctx: SidebarCtx) => React.createElement(TestCheckPanel, { ctx }),
482
+ }
483
+ }
484
+
485
+ //endregion