dsh-vscode-mode 0.39.0 → 0.41.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.
- package/README.md +11 -8
- package/lib/client.js +83 -64
- package/lib/client.js.map +1 -1
- package/lib/index.js +4765 -1886
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/dsh-vscodemode-test-check/SKILL.md +82 -0
- package/skills/dsh-vscodemode-ui-kit/SKILL.md +84 -0
- package/skills/dsh-vscodemode-ui-kit/fixtures/selftest/spec.json +65 -0
- package/skills/dsh-vscodemode-ui-kit/references/detect.md +90 -0
- package/skills/dsh-vscodemode-ui-kit/references/lib.md +48 -0
- package/skills/dsh-vscodemode-ui-kit/references/questions.md +68 -0
- package/skills/dsh-vscodemode-ui-kit/references/spec.md +99 -0
- package/skills/dsh-vscodemode-ui-kit/references/verify.md +37 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/build.mjs +348 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/css/framework.css +398 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/01-core.js +182 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/02-state.js +96 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/03-tree.js +144 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/04-canvas.js +194 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/05-inspector.js +241 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/06-diff.js +52 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/07-reply.js +71 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/js/08-main.js +147 -0
- package/skills/dsh-vscodemode-ui-kit/runtime/spec.schema.json +79 -0
- package/src/attachmentBytes.ts +84 -0
- package/src/client/index.ts +6 -0
- package/src/client/openReceipt.ts +5 -2
- package/src/client/sidebar/panels/TestCheckPanel.ts +485 -0
- package/src/client/sidebar/panels/useTestCheckData.ts +414 -0
- package/src/client/sidebar/types.ts +2 -0
- package/src/client/styles/editor.css +78 -0
- package/src/client/testCheck/TestCheckChip.ts +181 -0
- package/src/client/testCheck/TestCheckPanel.ts +339 -0
- package/src/client/testCheckPanelState.ts +173 -0
- package/src/client/ui/EditorView.ts +69 -6
- package/src/htmlPlanAttachments.ts +4 -1
- package/src/htmlPlanCommand.ts +4 -62
- package/src/index.ts +4 -0
- package/src/rpc.ts +3 -0
- package/src/shared/rpc.ts +44 -0
- package/src/shared/testCheck.ts +759 -0
- package/src/shared/testCheckHtml.ts +276 -0
- package/src/shared/testCheckPolicyDefault.ts +37 -0
- package/src/shared/testCheckSnippets.ts +197 -0
- package/src/testCheckCommand.ts +287 -0
- package/src/testCheckGuidance.ts +79 -0
- package/src/testCheckMode.ts +654 -0
- package/src/testCheckPolicy.ts +141 -0
- package/src/testCheckRpc.ts +196 -0
- package/src/testCheckStore.ts +822 -0
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-vscode-mode host — `/test-check` 的数据层:项目根发现、定义与事件读取、增量监听、快照导出。
|
|
3
|
+
*
|
|
4
|
+
* 契约(来自模式设计):
|
|
5
|
+
* - 数据在 `<项目根>/.dsh/test-check/<id>/`,不进版本库;快照导出到 `docs/acceptance-<id>.html`(进库)。
|
|
6
|
+
* - 每条结果一个 JSON 文件,文件名 `<时间戳>-<随机>.json`,并发写不会互相覆盖。
|
|
7
|
+
* - 目录用 `fs.watch` 标脏,读的时候按需重扫;watch 漏事件时 5 秒兜底重扫一次,
|
|
8
|
+
* 客户端还有 1 秒轮询,所以「写文件后 1 秒内反映」不依赖 watch 是否可靠。
|
|
9
|
+
* @author ddj 2026年11月25号
|
|
10
|
+
*/
|
|
11
|
+
import { createHash } from 'node:crypto'
|
|
12
|
+
import { existsSync, statSync, watch, type Dirent, type FSWatcher } from 'node:fs'
|
|
13
|
+
import { mkdir, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'
|
|
14
|
+
import { dirname, join, resolve } from 'node:path'
|
|
15
|
+
import { log } from './log.js'
|
|
16
|
+
import {
|
|
17
|
+
TEST_CHECK_BASE_DIR,
|
|
18
|
+
TEST_CHECK_DEFAULT_ID,
|
|
19
|
+
TEST_CHECK_DOCS,
|
|
20
|
+
TEST_CHECK_ENV,
|
|
21
|
+
TEST_CHECK_EVENTS,
|
|
22
|
+
TEST_CHECK_ITEMS,
|
|
23
|
+
TEST_CHECK_MAX_EVENTS,
|
|
24
|
+
TEST_CHECK_SNIPPETS,
|
|
25
|
+
checkDefinition,
|
|
26
|
+
deriveSheetView,
|
|
27
|
+
emptyView,
|
|
28
|
+
eventFileNameOf,
|
|
29
|
+
idProblemOf,
|
|
30
|
+
isSheetId,
|
|
31
|
+
normalizeStatus,
|
|
32
|
+
snapshotNameOf,
|
|
33
|
+
subjectHashLines,
|
|
34
|
+
type TestCheckDefinition,
|
|
35
|
+
type TestCheckEvent,
|
|
36
|
+
type TestCheckEventFile,
|
|
37
|
+
type TestCheckEvidence,
|
|
38
|
+
type TestCheckIgnored,
|
|
39
|
+
type TestCheckSheetSummary,
|
|
40
|
+
type TestCheckView,
|
|
41
|
+
} from './shared/testCheck.js'
|
|
42
|
+
import { snippetsMarkdown } from './shared/testCheckSnippets.js'
|
|
43
|
+
import { renderSheetHtml } from './shared/testCheckHtml.js'
|
|
44
|
+
|
|
45
|
+
//region 结构面
|
|
46
|
+
|
|
47
|
+
/** 一个单子的路径集合(全部绝对路径)。 */
|
|
48
|
+
export interface SheetPaths {
|
|
49
|
+
/** 项目根。 */
|
|
50
|
+
root: string
|
|
51
|
+
/** 数据根目录(`.dsh/test-check` 或环境变量覆盖值)。 */
|
|
52
|
+
baseDir: string
|
|
53
|
+
/** 单子目录。 */
|
|
54
|
+
dir: string
|
|
55
|
+
/** 定义文件。 */
|
|
56
|
+
itemsPath: string
|
|
57
|
+
/** 结果目录。 */
|
|
58
|
+
eventsDir: string
|
|
59
|
+
/** 上报片段文件。 */
|
|
60
|
+
snippetsPath: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 读视图的入参。 */
|
|
64
|
+
export interface ReadViewOptions {
|
|
65
|
+
/** 会话工作区(扫项目根用)。 */
|
|
66
|
+
cwd: string
|
|
67
|
+
/** 单子 id;缺省按目录自动认领。 */
|
|
68
|
+
id?: string
|
|
69
|
+
/** 跳过缓存(导出、「马上重扫」用)。 */
|
|
70
|
+
force?: boolean
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 写一条结果的入参。 */
|
|
74
|
+
export interface WriteEventOptions {
|
|
75
|
+
/** 会话工作区。 */
|
|
76
|
+
cwd: string
|
|
77
|
+
/** 单子 id。 */
|
|
78
|
+
id: string
|
|
79
|
+
/** 测试点 id。 */
|
|
80
|
+
item: string
|
|
81
|
+
/** 结果状态(已归一化)。 */
|
|
82
|
+
status: 'pass' | 'fail' | 'blocked' | 'skip'
|
|
83
|
+
/** 证据(缺省表示没证据)。 */
|
|
84
|
+
evidence?: TestCheckEvidence
|
|
85
|
+
/** 时间(缺省用当下)。 */
|
|
86
|
+
at?: Date
|
|
87
|
+
/** 被验对象指纹(`sha256:` 前缀)。 */
|
|
88
|
+
hash?: string
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 缓存里的一个事件文件。 */
|
|
92
|
+
interface EventCacheRow {
|
|
93
|
+
mtimeMs: number
|
|
94
|
+
size: number
|
|
95
|
+
entry?: TestCheckEventFile
|
|
96
|
+
reason?: string
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 一个单子的缓存与监听状态。 */
|
|
100
|
+
interface SheetCache {
|
|
101
|
+
/** 上次扫描产出的视图。 */
|
|
102
|
+
view: TestCheckView | null
|
|
103
|
+
/** 上次扫描时间(毫秒)。 */
|
|
104
|
+
scannedAt: number
|
|
105
|
+
/** watch 标过脏,下次读必须重扫。 */
|
|
106
|
+
dirty: boolean
|
|
107
|
+
/** 事件文件缓存:文件名 → 元数据。 */
|
|
108
|
+
events: Map<string, EventCacheRow>
|
|
109
|
+
/** 目录监听器:目录绝对路径 → 监听器(单子目录与 events 子目录各一个)。 */
|
|
110
|
+
watchers: Map<string, FSWatcher>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 事件读取结果。 */
|
|
114
|
+
interface EventScan {
|
|
115
|
+
events: TestCheckEventFile[]
|
|
116
|
+
ignored: TestCheckIgnored[]
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//endregion
|
|
120
|
+
|
|
121
|
+
//region 常量与缓存
|
|
122
|
+
|
|
123
|
+
/** watch 在位时的兜底重扫间隔(毫秒):watch 标脏即时生效,TTL 只防漏事件。 */
|
|
124
|
+
const STORE_TTL_MS = 5_000
|
|
125
|
+
|
|
126
|
+
/** watch 挂不上时的重扫间隔:客户端每秒轮询,必须每秒真扫一次才满足「写文件 1 秒内反映」。 */
|
|
127
|
+
const POLL_TTL_MS = 1_000
|
|
128
|
+
|
|
129
|
+
/** 单子缓存:单子目录 → 缓存。 */
|
|
130
|
+
const sheets = new Map<string, SheetCache>()
|
|
131
|
+
|
|
132
|
+
/** 文件内容 sha256 缓存:绝对路径 → 指纹(按 mtime/size 失效)。 */
|
|
133
|
+
const fileHashes = new Map<string, { mtimeMs: number; size: number; sha: string }>()
|
|
134
|
+
|
|
135
|
+
/** 文件指纹缓存上限(超了整体清空,避免长跑实例无限涨)。 */
|
|
136
|
+
const FILE_HASH_MAX = 4_000
|
|
137
|
+
|
|
138
|
+
//endregion
|
|
139
|
+
|
|
140
|
+
//region 路径
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 算某段字节的 sha256(十六进制)。
|
|
144
|
+
* @author ddj 2026年11月25号
|
|
145
|
+
* @param data 字节或字符串
|
|
146
|
+
* @returns 十六进制摘要
|
|
147
|
+
*/
|
|
148
|
+
function sha256Of(data: Buffer | string): string {
|
|
149
|
+
return createHash('sha256').update(data).digest('hex')
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 从会话 cwd 向上找项目根:第一个含 `.dsh/test-check` 的目录。
|
|
154
|
+
* @author ddj 2026年11月25号
|
|
155
|
+
* @param cwd 会话工作区
|
|
156
|
+
* @returns 项目根绝对路径;都没找到时就是 cwd
|
|
157
|
+
*/
|
|
158
|
+
export function projectRootOf(cwd: string): string {
|
|
159
|
+
const start = resolve(cwd)
|
|
160
|
+
let dir = start
|
|
161
|
+
for (;;) {
|
|
162
|
+
if (existsSync(join(dir, TEST_CHECK_BASE_DIR))) return dir
|
|
163
|
+
const parent = dirname(dir)
|
|
164
|
+
if (parent === dir) return start
|
|
165
|
+
dir = parent
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 数据根目录:`DSH_TEST_CHECK_DIR` 覆盖优先,缺省 `<root>/.dsh/test-check`。
|
|
171
|
+
* @author ddj 2026年11月25号
|
|
172
|
+
* @param root 项目根
|
|
173
|
+
* @param env 环境变量(测试可注入)
|
|
174
|
+
* @returns 绝对路径
|
|
175
|
+
*/
|
|
176
|
+
export function baseDirOf(root: string, env: NodeJS.ProcessEnv = process.env): string {
|
|
177
|
+
const override = env[TEST_CHECK_ENV]
|
|
178
|
+
const text = typeof override === 'string' ? override.trim() : ''
|
|
179
|
+
return text ? resolve(text) : join(root, TEST_CHECK_BASE_DIR)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 取一个单子的全部路径。
|
|
184
|
+
* @author ddj 2026年11月25号
|
|
185
|
+
* @param root 项目根
|
|
186
|
+
* @param id 单子 id
|
|
187
|
+
* @param env 环境变量
|
|
188
|
+
* @returns 路径集合
|
|
189
|
+
*/
|
|
190
|
+
export function sheetPathsOf(root: string, id: string, env: NodeJS.ProcessEnv = process.env): SheetPaths {
|
|
191
|
+
const baseDir = baseDirOf(root, env)
|
|
192
|
+
const dir = join(baseDir, id)
|
|
193
|
+
return {
|
|
194
|
+
root,
|
|
195
|
+
baseDir,
|
|
196
|
+
dir,
|
|
197
|
+
itemsPath: join(dir, TEST_CHECK_ITEMS),
|
|
198
|
+
eventsDir: join(dir, TEST_CHECK_EVENTS),
|
|
199
|
+
snippetsPath: join(dir, TEST_CHECK_SNIPPETS),
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 认领单子 id:调用方给了合法 id 就用它;否则目录里只有一个单子时用它;再否则用默认 `check`。
|
|
205
|
+
* @author ddj 2026年11月25号
|
|
206
|
+
* @param baseDir 数据根目录
|
|
207
|
+
* @param id 调用方给的 id
|
|
208
|
+
* @returns 单子 id
|
|
209
|
+
*/
|
|
210
|
+
export async function pickSheetId(baseDir: string, id?: string): Promise<string> {
|
|
211
|
+
if (isSheetId(id)) return String(id)
|
|
212
|
+
try {
|
|
213
|
+
const entries = await readdir(baseDir, { withFileTypes: true })
|
|
214
|
+
const dirs = entries.filter((entry) => entry.isDirectory() && !entry.name.startsWith('.')).map((entry) => entry.name)
|
|
215
|
+
if (dirs.length === 1) return dirs[0]
|
|
216
|
+
} catch {
|
|
217
|
+
/* 还没建过任何单子 */
|
|
218
|
+
}
|
|
219
|
+
return TEST_CHECK_DEFAULT_ID
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 校验单子 id(写盘前必过:id 会拼进路径,绝不能带分隔符或 `..`)。
|
|
224
|
+
* @author ddj 2026年11月25号
|
|
225
|
+
* @param id 调用方给的 id
|
|
226
|
+
* @returns 合法的 id
|
|
227
|
+
* @throws id 非法时抛错(文案可直接展示)
|
|
228
|
+
*/
|
|
229
|
+
function strictSheetId(id: unknown): string {
|
|
230
|
+
if (!isSheetId(id)) throw new Error('单子 id 不合法(只允许小写字母、数字与连字符):' + String(id))
|
|
231
|
+
return String(id)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 把绝对路径统一成 `/` 分隔(只用于展示与协议,fs 调用仍用原生路径)。
|
|
236
|
+
* @author ddj 2026年11月25号
|
|
237
|
+
* @param path 绝对路径
|
|
238
|
+
* @returns `/` 分隔的路径
|
|
239
|
+
*/
|
|
240
|
+
export function slashPath(path: string): string {
|
|
241
|
+
return path.replace(/\\/g, '/')
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
//endregion
|
|
245
|
+
|
|
246
|
+
//region 监听
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 给一个目录挂 watch:只负责标脏,不做解析。目录不存在时返回 null(下次读时再挂)。
|
|
250
|
+
* @author ddj 2026年11月25号
|
|
251
|
+
* @param paths 单子路径
|
|
252
|
+
* @param cache 单子缓存
|
|
253
|
+
* @param dir 要监听的目录
|
|
254
|
+
* @returns 监听器或 null
|
|
255
|
+
*/
|
|
256
|
+
function watchDirOf(paths: SheetPaths, cache: SheetCache, dir: string): FSWatcher | null {
|
|
257
|
+
if (!existsSync(dir)) return null
|
|
258
|
+
try {
|
|
259
|
+
const watcher = watch(dir, { persistent: false }, () => { cache.dirty = true })
|
|
260
|
+
watcher.on('error', (error: unknown) => {
|
|
261
|
+
log.warn('/test-check 监听失败(' + slashPath(dir) + ',改为轮询兜底):'
|
|
262
|
+
+ (error instanceof Error ? error.message : String(error)))
|
|
263
|
+
try { watcher.close() } catch { /* 已关闭 */ }
|
|
264
|
+
cache.watchers.delete(dir)
|
|
265
|
+
})
|
|
266
|
+
return watcher
|
|
267
|
+
} catch (error) {
|
|
268
|
+
log.warn('/test-check 监听未挂上(' + slashPath(dir) + ',改为轮询兜底):'
|
|
269
|
+
+ (error instanceof Error ? error.message : String(error)))
|
|
270
|
+
return null
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 挂齐两个监听:单子目录(items.json 改动)与 events 子目录(事件文件)。
|
|
276
|
+
*
|
|
277
|
+
* Windows 的 `fs.watch` 默认不递归,只盯单子目录收不到 `events/` 里新写的文件——
|
|
278
|
+
* 真机实测那样首屏要等 5 秒兜底重扫,达不到「写文件 1 秒内反映」,所以必须分别挂。
|
|
279
|
+
* @author ddj 2026年11月25号
|
|
280
|
+
* @param paths 单子路径
|
|
281
|
+
* @param cache 单子缓存
|
|
282
|
+
* @returns 无
|
|
283
|
+
*/
|
|
284
|
+
function startWatch(paths: SheetPaths, cache: SheetCache): void {
|
|
285
|
+
for (const dir of [paths.dir, paths.eventsDir]) {
|
|
286
|
+
if (cache.watchers.has(dir)) continue
|
|
287
|
+
const watcher = watchDirOf(paths, cache, dir)
|
|
288
|
+
if (watcher) cache.watchers.set(dir, watcher)
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 取(或建)单子缓存。
|
|
294
|
+
* @author ddj 2026年11月25号
|
|
295
|
+
* @param paths 单子路径
|
|
296
|
+
* @returns 缓存项
|
|
297
|
+
*/
|
|
298
|
+
function cacheOf(paths: SheetPaths): SheetCache {
|
|
299
|
+
const existing = sheets.get(paths.dir)
|
|
300
|
+
if (existing) return existing
|
|
301
|
+
const created: SheetCache = { view: null, scannedAt: 0, dirty: true, events: new Map(), watchers: new Map() }
|
|
302
|
+
sheets.set(paths.dir, created)
|
|
303
|
+
return created
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
//endregion
|
|
307
|
+
|
|
308
|
+
//region 读取定义与事件
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 读并校验定义文件。
|
|
312
|
+
* @author ddj 2026年11月25号
|
|
313
|
+
* @param paths 单子路径
|
|
314
|
+
* @returns 状态、定义或逐条错误
|
|
315
|
+
*/
|
|
316
|
+
async function readDefinition(paths: SheetPaths): Promise<{ state: 'ready' | 'missing' | 'invalid'; definition?: TestCheckDefinition; error?: string }> {
|
|
317
|
+
let text: string
|
|
318
|
+
try {
|
|
319
|
+
text = await readFile(paths.itemsPath, 'utf8')
|
|
320
|
+
} catch (error) {
|
|
321
|
+
const code = (error as { code?: unknown } | null)?.code
|
|
322
|
+
if (code === 'ENOENT') return { state: 'missing' }
|
|
323
|
+
return { state: 'invalid', error: '读取 items.json 失败:' + (error instanceof Error ? error.message : String(error)) }
|
|
324
|
+
}
|
|
325
|
+
let raw: unknown
|
|
326
|
+
try {
|
|
327
|
+
raw = JSON.parse(text.replace(/^\uFEFF/, ''))
|
|
328
|
+
} catch (error) {
|
|
329
|
+
return { state: 'invalid', error: 'items.json 不是合法 JSON:' + (error instanceof Error ? error.message : String(error)) }
|
|
330
|
+
}
|
|
331
|
+
const checked = checkDefinition(raw, paths.dir.split(/[\\/]/).pop() ?? '')
|
|
332
|
+
if (checked.errors.length) return { state: 'invalid', error: checked.errors.join('\n') }
|
|
333
|
+
return { state: 'ready', definition: checked.definition }
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 校验一条事件的内容;不合法时返回原因。
|
|
338
|
+
* @author ddj 2026年11月25号
|
|
339
|
+
* @param raw 解析后的 JSON
|
|
340
|
+
* @returns 事件或原因
|
|
341
|
+
*/
|
|
342
|
+
function eventOf(raw: unknown): { event: TestCheckEvent } | { reason: string } {
|
|
343
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return { reason: '内容不是对象' }
|
|
344
|
+
const row = raw as Record<string, unknown>
|
|
345
|
+
const item = typeof row.item === 'string' ? row.item.trim() : ''
|
|
346
|
+
if (!item) return { reason: '缺少 item' }
|
|
347
|
+
const status = normalizeStatus(row.status)
|
|
348
|
+
if (!status) return { reason: 'status 只能是 pass / fail / blocked / skip' }
|
|
349
|
+
const problem = idProblemOf(item)
|
|
350
|
+
if (problem) return { reason: 'item 不是合法测试点 id(' + problem + '):' + item }
|
|
351
|
+
const event: TestCheckEvent = { item, status }
|
|
352
|
+
if (typeof row.at === 'string' && row.at.trim()) event.at = row.at.trim()
|
|
353
|
+
const evidence = row.evidence
|
|
354
|
+
if (evidence !== null && typeof evidence === 'object' && !Array.isArray(evidence)) {
|
|
355
|
+
const source = evidence as Record<string, unknown>
|
|
356
|
+
const picked: TestCheckEvidence = {}
|
|
357
|
+
if (typeof source.kind === 'string' && source.kind.trim()) picked.kind = source.kind.trim()
|
|
358
|
+
if (typeof source.summary === 'string' && source.summary.trim()) picked.summary = source.summary.trim()
|
|
359
|
+
if (typeof source.command === 'string' && source.command.trim()) picked.command = source.command.trim()
|
|
360
|
+
if (typeof source.exitCode === 'number' && Number.isFinite(source.exitCode)) picked.exitCode = source.exitCode
|
|
361
|
+
if (typeof source.path === 'string' && source.path.trim()) picked.path = source.path.trim()
|
|
362
|
+
if (Object.keys(picked).length) event.evidence = picked
|
|
363
|
+
}
|
|
364
|
+
const subject = row.subject
|
|
365
|
+
if (subject !== null && typeof subject === 'object') {
|
|
366
|
+
const hash = (subject as Record<string, unknown>).hash
|
|
367
|
+
if (typeof hash === 'string' && hash.trim()) event.subject = { hash: hash.trim() }
|
|
368
|
+
}
|
|
369
|
+
return { event }
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 扫一遍结果目录:只读新增/改过的文件,返回事件与被忽略的坏文件。
|
|
374
|
+
* @author ddj 2026年11月25号
|
|
375
|
+
* @param paths 单子路径
|
|
376
|
+
* @param ids 合法测试点 id 集合
|
|
377
|
+
* @param cache 单子缓存
|
|
378
|
+
* @returns 事件与被忽略清单
|
|
379
|
+
*/
|
|
380
|
+
async function readEvents(paths: SheetPaths, ids: Set<string>, cache: SheetCache): Promise<EventScan> {
|
|
381
|
+
const scan: EventScan = { events: [], ignored: [] }
|
|
382
|
+
let names: string[]
|
|
383
|
+
try {
|
|
384
|
+
names = (await readdir(paths.eventsDir, { withFileTypes: true }))
|
|
385
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith('.json') && !entry.name.startsWith('.'))
|
|
386
|
+
.map((entry) => entry.name)
|
|
387
|
+
} catch {
|
|
388
|
+
return scan
|
|
389
|
+
}
|
|
390
|
+
names.sort((left, right) => (left < right ? 1 : left > right ? -1 : 0))
|
|
391
|
+
const keep = new Set(names.slice(0, TEST_CHECK_MAX_EVENTS))
|
|
392
|
+
for (const name of [...cache.events.keys()]) if (!keep.has(name)) cache.events.delete(name)
|
|
393
|
+
|
|
394
|
+
for (const name of names.slice(0, TEST_CHECK_MAX_EVENTS)) {
|
|
395
|
+
const absolute = join(paths.eventsDir, name)
|
|
396
|
+
let info: { mtimeMs: number; size: number }
|
|
397
|
+
try {
|
|
398
|
+
const row = await stat(absolute)
|
|
399
|
+
info = { mtimeMs: row.mtimeMs, size: row.size }
|
|
400
|
+
} catch {
|
|
401
|
+
cache.events.delete(name)
|
|
402
|
+
continue
|
|
403
|
+
}
|
|
404
|
+
let cached = cache.events.get(name)
|
|
405
|
+
if (!cached || cached.mtimeMs !== info.mtimeMs || cached.size !== info.size) {
|
|
406
|
+
cached = { ...info }
|
|
407
|
+
try {
|
|
408
|
+
const parsed = eventOf(JSON.parse((await readFile(absolute, 'utf8')).replace(/^\uFEFF/, '')))
|
|
409
|
+
if ('reason' in parsed) cached.reason = parsed.reason
|
|
410
|
+
else if (!ids.has(parsed.event.item)) cached.reason = '未知测试点:' + parsed.event.item
|
|
411
|
+
else {
|
|
412
|
+
const at = typeof parsed.event.at === 'string' ? Date.parse(parsed.event.at) : Number.NaN
|
|
413
|
+
cached.entry = {
|
|
414
|
+
file: name,
|
|
415
|
+
at: Number.isFinite(at) ? at : info.mtimeMs,
|
|
416
|
+
event: parsed.event,
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
} catch (error) {
|
|
420
|
+
cached.reason = '不是合法 JSON:' + (error instanceof Error ? error.message : String(error))
|
|
421
|
+
}
|
|
422
|
+
cache.events.set(name, cached)
|
|
423
|
+
}
|
|
424
|
+
if (cached.entry) scan.events.push(cached.entry)
|
|
425
|
+
else scan.ignored.push({ file: name, reason: cached.reason ?? '无法解析' })
|
|
426
|
+
}
|
|
427
|
+
return scan
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* 算一条验收点的当前指纹(`files` 都读到才算得出来)。
|
|
432
|
+
* @author ddj 2026年11月25号
|
|
433
|
+
* @param root 项目根
|
|
434
|
+
* @param files 相对路径清单
|
|
435
|
+
* @returns `sha256:<hex>`;任一文件读不到时返回空串
|
|
436
|
+
*/
|
|
437
|
+
async function itemHashOf(root: string, files: readonly string[]): Promise<string> {
|
|
438
|
+
if (!files.length) return ''
|
|
439
|
+
const parts: Array<{ path: string; sha256: string }> = []
|
|
440
|
+
for (const relative of files) {
|
|
441
|
+
const absolute = join(root, relative)
|
|
442
|
+
let info: { mtimeMs: number; size: number }
|
|
443
|
+
try {
|
|
444
|
+
const row = await stat(absolute)
|
|
445
|
+
info = { mtimeMs: row.mtimeMs, size: row.size }
|
|
446
|
+
} catch {
|
|
447
|
+
return ''
|
|
448
|
+
}
|
|
449
|
+
const cached = fileHashes.get(absolute)
|
|
450
|
+
if (cached && cached.mtimeMs === info.mtimeMs && cached.size === info.size) {
|
|
451
|
+
parts.push({ path: relative, sha256: cached.sha })
|
|
452
|
+
continue
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
const sha = sha256Of(await readFile(absolute))
|
|
456
|
+
if (fileHashes.size > FILE_HASH_MAX) fileHashes.clear()
|
|
457
|
+
fileHashes.set(absolute, { ...info, sha })
|
|
458
|
+
parts.push({ path: relative, sha256: sha })
|
|
459
|
+
} catch {
|
|
460
|
+
return ''
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return 'sha256:' + sha256Of(subjectHashLines(parts))
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
//endregion
|
|
467
|
+
|
|
468
|
+
//region 对外读写
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* 读单子视图(带缓存与 watch 标脏;force 时强制重扫)。
|
|
472
|
+
* @author ddj 2026年11月25号
|
|
473
|
+
* @param options 会话工作区、单子 id 与强制标记
|
|
474
|
+
* @returns 派生视图
|
|
475
|
+
*/
|
|
476
|
+
export async function readView(options: ReadViewOptions): Promise<TestCheckView> {
|
|
477
|
+
const root = projectRootOf(options.cwd)
|
|
478
|
+
const baseDir = baseDirOf(root)
|
|
479
|
+
const id = await pickSheetId(baseDir, options.id)
|
|
480
|
+
const paths = sheetPathsOf(root, id)
|
|
481
|
+
const cache = cacheOf(paths)
|
|
482
|
+
startWatch(paths, cache)
|
|
483
|
+
|
|
484
|
+
const fresh = cache.view !== null && !cache.dirty && !options.force
|
|
485
|
+
&& Date.now() - cache.scannedAt < (cache.watchers.size >= 2 ? STORE_TTL_MS : POLL_TTL_MS)
|
|
486
|
+
if (fresh) return cache.view as TestCheckView
|
|
487
|
+
|
|
488
|
+
cache.dirty = false
|
|
489
|
+
cache.scannedAt = Date.now()
|
|
490
|
+
const detected = await readDefinition(paths)
|
|
491
|
+
if (detected.state !== 'ready' || !detected.definition) {
|
|
492
|
+
const view = emptyView({
|
|
493
|
+
id,
|
|
494
|
+
title: id,
|
|
495
|
+
root: slashPath(root),
|
|
496
|
+
dir: slashPath(paths.dir),
|
|
497
|
+
itemsPath: slashPath(paths.itemsPath),
|
|
498
|
+
eventsDir: slashPath(paths.eventsDir),
|
|
499
|
+
}, detected.state === 'missing' ? 'missing' : 'invalid', detected.error)
|
|
500
|
+
cache.view = view
|
|
501
|
+
return view
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const definition = detected.definition
|
|
505
|
+
const ids = new Set(definition.items.filter((item) => !item.retired).map((item) => item.id))
|
|
506
|
+
const scan = await readEvents(paths, ids, cache)
|
|
507
|
+
const hashes: Record<string, string> = {}
|
|
508
|
+
for (const item of definition.items) {
|
|
509
|
+
if (item.retired || !item.files?.length) continue
|
|
510
|
+
const hash = await itemHashOf(root, item.files)
|
|
511
|
+
if (hash) hashes[item.id] = hash
|
|
512
|
+
}
|
|
513
|
+
const view = deriveSheetView({
|
|
514
|
+
id,
|
|
515
|
+
title: definition.title,
|
|
516
|
+
root: slashPath(root),
|
|
517
|
+
dir: slashPath(paths.dir),
|
|
518
|
+
itemsPath: slashPath(paths.itemsPath),
|
|
519
|
+
eventsDir: slashPath(paths.eventsDir),
|
|
520
|
+
definition,
|
|
521
|
+
events: scan.events,
|
|
522
|
+
hashes,
|
|
523
|
+
ignored: scan.ignored,
|
|
524
|
+
})
|
|
525
|
+
cache.view = view
|
|
526
|
+
return view
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* 建单:创建单子目录与事件目录,写 `.gitignore` 与上报片段(都不覆盖已有内容)。
|
|
531
|
+
* @author ddj 2026年11月25号
|
|
532
|
+
* @param options 会话工作区、单子 id 与标题
|
|
533
|
+
* @returns 路径集合与新建标记
|
|
534
|
+
*/
|
|
535
|
+
export async function ensureSheet(options: { cwd: string; id: string; title?: string }): Promise<{ paths: SheetPaths; created: boolean }> {
|
|
536
|
+
const root = projectRootOf(options.cwd)
|
|
537
|
+
const paths = sheetPathsOf(root, options.id)
|
|
538
|
+
const created = !existsSync(paths.dir)
|
|
539
|
+
await mkdir(paths.eventsDir, { recursive: true })
|
|
540
|
+
const ignorePath = join(paths.baseDir, '.gitignore')
|
|
541
|
+
if (!existsSync(ignorePath)) {
|
|
542
|
+
await mkdir(paths.baseDir, { recursive: true })
|
|
543
|
+
await writeFile(ignorePath, '*\n', 'utf8')
|
|
544
|
+
}
|
|
545
|
+
if (!existsSync(paths.snippetsPath)) {
|
|
546
|
+
await writeFile(paths.snippetsPath, snippetsMarkdown(options.id, options.title), 'utf8')
|
|
547
|
+
}
|
|
548
|
+
const cache = cacheOf(paths)
|
|
549
|
+
cache.dirty = true
|
|
550
|
+
startWatch(paths, cache)
|
|
551
|
+
resetSheetListCache()
|
|
552
|
+
return { paths, created }
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* 写一条结果:一条一个文件,文件名带时间戳与随机后缀。
|
|
557
|
+
*
|
|
558
|
+
* 只接受定义里存在且未退役的测试点 id——写错一个字母不会被悄悄丢掉,而是报错让上报方修。
|
|
559
|
+
* @author ddj 2026年11月25号
|
|
560
|
+
* @param options 上报内容
|
|
561
|
+
* @returns 事件文件名与最新视图
|
|
562
|
+
* @throws 定义缺失/有错、测试点不存在时抛错(文案可直接展示)
|
|
563
|
+
*/
|
|
564
|
+
export async function writeEvent(options: WriteEventOptions): Promise<{ file: string; view: TestCheckView }> {
|
|
565
|
+
const id = strictSheetId(options.id)
|
|
566
|
+
const root = projectRootOf(options.cwd)
|
|
567
|
+
const paths = sheetPathsOf(root, id)
|
|
568
|
+
const detected = await readDefinition(paths)
|
|
569
|
+
if (detected.state === 'missing') throw new Error('还没有 items.json:先在 ' + slashPath(paths.itemsPath) + ' 建单')
|
|
570
|
+
if (detected.state !== 'ready' || !detected.definition) throw new Error('items.json 有错,先修好:' + String(detected.error ?? ''))
|
|
571
|
+
const target = detected.definition.items.find((item) => item.id === options.item)
|
|
572
|
+
if (!target) throw new Error('定义里没有这个测试点:' + options.item)
|
|
573
|
+
if (target.retired) throw new Error('测试点已退役,不再接收结果:' + options.item)
|
|
574
|
+
|
|
575
|
+
const payload: Record<string, unknown> = { item: options.item, status: options.status }
|
|
576
|
+
payload.at = (options.at ?? new Date()).toISOString()
|
|
577
|
+
if (options.evidence && Object.keys(options.evidence).length) payload.evidence = options.evidence
|
|
578
|
+
// 上报时没给指纹就按定义里的 files 现场复算一次:AI 用工具上报也自动进入「需复验」判定,
|
|
579
|
+
// 不必要求模型自己算 sha256(真机走查发现缺口:只有脚本上报才带 hash 时,第 4 条永远不触发)。
|
|
580
|
+
const hash = options.hash ?? (target.files?.length ? await itemHashOf(root, target.files) : '')
|
|
581
|
+
if (hash) payload.subject = { hash }
|
|
582
|
+
|
|
583
|
+
await mkdir(paths.eventsDir, { recursive: true })
|
|
584
|
+
const at = options.at ?? new Date()
|
|
585
|
+
let file = eventFileNameOf(at)
|
|
586
|
+
for (let attempt = 0; attempt < 3 && existsSync(join(paths.eventsDir, file)); attempt += 1) {
|
|
587
|
+
file = eventFileNameOf(at)
|
|
588
|
+
}
|
|
589
|
+
await writeFile(join(paths.eventsDir, file), JSON.stringify(payload, null, 2) + '\n', 'utf8')
|
|
590
|
+
|
|
591
|
+
const cache = cacheOf(paths)
|
|
592
|
+
cache.dirty = true
|
|
593
|
+
resetSheetListCache()
|
|
594
|
+
const view = await readView({ cwd: options.cwd, id, force: true })
|
|
595
|
+
return { file, view }
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* 导出交付快照:`docs/acceptance-<id>.html`(单文件、无脚本、脱离 DSH 可独立打开)。
|
|
600
|
+
* @author ddj 2026年11月25号
|
|
601
|
+
* @param options 会话工作区与单子 id
|
|
602
|
+
* @returns 导出路径与导出时的视图
|
|
603
|
+
*/
|
|
604
|
+
export async function exportSnapshot(options: { cwd: string; id: string }): Promise<{ path: string; view: TestCheckView }> {
|
|
605
|
+
const id = strictSheetId(options.id)
|
|
606
|
+
const view = await readView({ cwd: options.cwd, id, force: true })
|
|
607
|
+
const root = projectRootOf(options.cwd)
|
|
608
|
+
const docsDir = join(root, TEST_CHECK_DOCS)
|
|
609
|
+
await mkdir(docsDir, { recursive: true })
|
|
610
|
+
const target = join(docsDir, snapshotNameOf(id))
|
|
611
|
+
await writeFile(target, renderSheetHtml(view, { exported: true }), 'utf8')
|
|
612
|
+
resetSheetListCache()
|
|
613
|
+
return { path: slashPath(target), view }
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
//endregion
|
|
617
|
+
|
|
618
|
+
//region 列表与移除
|
|
619
|
+
|
|
620
|
+
/** 全部单列表上限:每张单都要扫一次定义与事件,防长跑实例把面板拖住。 */
|
|
621
|
+
const SHEET_LIST_CAP = 50
|
|
622
|
+
|
|
623
|
+
/** 列表缓存有效期:面板每次切回来都会重挂,30 秒内不重复全扫。 */
|
|
624
|
+
const SHEET_LIST_TTL_MS = 30_000
|
|
625
|
+
|
|
626
|
+
/** 列表缓存:项目根 → 上次扫描时间与结果。 */
|
|
627
|
+
const sheetListCache = new Map<string, { at: number; result: SheetListResult }>()
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* 清掉全部单列表缓存(写盘、建单、删除与测试隔离都调它)。
|
|
631
|
+
* @author ddj 2026年11月26号
|
|
632
|
+
*/
|
|
633
|
+
export function resetSheetListCache(): void {
|
|
634
|
+
sheetListCache.clear()
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/** 列表入参。 */
|
|
638
|
+
export interface ListSheetsOptions {
|
|
639
|
+
/** 会话工作区。 */
|
|
640
|
+
cwd: string
|
|
641
|
+
/** 跳过缓存(面板「刷新」按钮用)。 */
|
|
642
|
+
force?: boolean
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/** 列表结果。 */
|
|
646
|
+
export interface SheetListResult {
|
|
647
|
+
/** 项目根(`/` 分隔)。 */
|
|
648
|
+
root: string
|
|
649
|
+
/** 按最近活动倒序的验收单。 */
|
|
650
|
+
sheets: TestCheckSheetSummary[]
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* 列出项目内全部验收单:计数复用 `readView`,与验收单页签完全一致。
|
|
655
|
+
*
|
|
656
|
+
* 单子目录名必须是合法 id(点开头的 `.gitignore` 之类直接跳过);
|
|
657
|
+
* 单张读失败只出一行空计数,不让整张列表消失。
|
|
658
|
+
*
|
|
659
|
+
* 结果按项目根缓存 30 秒:面板每次切回来都会重挂,没必要次次全扫;
|
|
660
|
+
* 建单、写结果、导出与删除都会主动清缓存,外部改文件最多滞后 30 秒。
|
|
661
|
+
* @author ddj 2026年11月26号
|
|
662
|
+
* @param options 会话工作区与是否跳过缓存
|
|
663
|
+
* @returns 项目根与行列表(最近活动在前)
|
|
664
|
+
*/
|
|
665
|
+
export async function listSheets(options: ListSheetsOptions): Promise<SheetListResult> {
|
|
666
|
+
const root = projectRootOf(options.cwd)
|
|
667
|
+
const cached = sheetListCache.get(root)
|
|
668
|
+
if (!options.force && cached !== undefined && Date.now() - cached.at < SHEET_LIST_TTL_MS) {
|
|
669
|
+
return cached.result
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const baseDir = baseDirOf(root)
|
|
673
|
+
let entries: Dirent[]
|
|
674
|
+
try {
|
|
675
|
+
entries = await readdir(baseDir, { withFileTypes: true })
|
|
676
|
+
} catch {
|
|
677
|
+
const empty: SheetListResult = { root: slashPath(root), sheets: [] }
|
|
678
|
+
sheetListCache.set(root, { at: Date.now(), result: empty })
|
|
679
|
+
return empty
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const ids = entries
|
|
683
|
+
.filter((entry) => entry.isDirectory() && isSheetId(entry.name))
|
|
684
|
+
.map((entry) => entry.name)
|
|
685
|
+
.sort()
|
|
686
|
+
.slice(0, SHEET_LIST_CAP)
|
|
687
|
+
|
|
688
|
+
const sheets: TestCheckSheetSummary[] = []
|
|
689
|
+
for (const id of ids) sheets.push(await summaryOf(root, id))
|
|
690
|
+
sheets.sort((left, right) => right.updatedAt - left.updatedAt)
|
|
691
|
+
|
|
692
|
+
const result: SheetListResult = { root: slashPath(root), sheets }
|
|
693
|
+
sheetListCache.set(root, { at: Date.now(), result })
|
|
694
|
+
return result
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* 汇总一张验收单(读失败时保留空计数,标题退回 id)。
|
|
699
|
+
* @author ddj 2026年11月26号
|
|
700
|
+
* @param root 项目根
|
|
701
|
+
* @param id 单子 id
|
|
702
|
+
* @returns 列表行
|
|
703
|
+
*/
|
|
704
|
+
async function summaryOf(root: string, id: string): Promise<TestCheckSheetSummary> {
|
|
705
|
+
const paths = sheetPathsOf(root, id)
|
|
706
|
+
const snapshot = join(root, TEST_CHECK_DOCS, snapshotNameOf(id))
|
|
707
|
+
const snapshotAt = mtimeOf(snapshot)
|
|
708
|
+
const row: TestCheckSheetSummary = {
|
|
709
|
+
id,
|
|
710
|
+
title: id,
|
|
711
|
+
counts: { total: 0, pass: 0, fail: 0, blocked: 0, todo: 0, stale: 0 },
|
|
712
|
+
itemsPath: slashPath(paths.itemsPath),
|
|
713
|
+
snapshotPath: slashPath(snapshot),
|
|
714
|
+
hasSnapshot: snapshotAt > 0,
|
|
715
|
+
updatedAt: snapshotAt || mtimeOf(paths.dir),
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
try {
|
|
719
|
+
const view = await readView({ cwd: root, id })
|
|
720
|
+
row.title = view.title
|
|
721
|
+
row.counts = view.counts
|
|
722
|
+
} catch (error) {
|
|
723
|
+
log.warn('验收单列表读取失败(' + id + '):' + (error instanceof Error ? error.message : String(error)))
|
|
724
|
+
}
|
|
725
|
+
return row
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* 文件或目录的 mtime(毫秒);不存在或读失败返回 0。
|
|
730
|
+
* @author ddj 2026年11月26号
|
|
731
|
+
* @param path 路径
|
|
732
|
+
* @returns mtimeMs 或 0
|
|
733
|
+
*/
|
|
734
|
+
function mtimeOf(path: string): number {
|
|
735
|
+
try {
|
|
736
|
+
return statSync(path).mtimeMs
|
|
737
|
+
} catch {
|
|
738
|
+
return 0
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** 移除入参。 */
|
|
743
|
+
export interface RemoveSheetOptions {
|
|
744
|
+
/** 会话工作区。 */
|
|
745
|
+
cwd: string
|
|
746
|
+
/** 单子 id。 */
|
|
747
|
+
id: string
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* 删除一张验收单目录与对应交付快照。
|
|
752
|
+
*
|
|
753
|
+
* 只按 id 现算路径,调用方传不了别的目录;目录先关监听再删(Windows 下开着句柄删不掉)。
|
|
754
|
+
* 已经不存在的单不算错,返回空 `removed` 让面板照常刷新。
|
|
755
|
+
* @author ddj 2026年11月26号
|
|
756
|
+
* @param options 会话工作区与单子 id
|
|
757
|
+
* @returns 实际删掉的路径(`/` 分隔)
|
|
758
|
+
* @throws id 非法(文案可直接展示)
|
|
759
|
+
*/
|
|
760
|
+
export async function removeSheet(options: RemoveSheetOptions): Promise<{ removed: string[] }> {
|
|
761
|
+
const id = strictSheetId(options.id)
|
|
762
|
+
const root = projectRootOf(options.cwd)
|
|
763
|
+
const paths = sheetPathsOf(root, id)
|
|
764
|
+
const snapshot = join(root, TEST_CHECK_DOCS, snapshotNameOf(id))
|
|
765
|
+
const removed: string[] = []
|
|
766
|
+
|
|
767
|
+
closeSheetCache(paths)
|
|
768
|
+
|
|
769
|
+
if (existsSync(paths.dir)) {
|
|
770
|
+
await rm(paths.dir, { recursive: true, force: true })
|
|
771
|
+
removed.push(slashPath(paths.dir))
|
|
772
|
+
}
|
|
773
|
+
if (existsSync(snapshot)) {
|
|
774
|
+
await rm(snapshot, { force: true })
|
|
775
|
+
removed.push(slashPath(snapshot))
|
|
776
|
+
}
|
|
777
|
+
resetSheetListCache()
|
|
778
|
+
return { removed }
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* 关掉一张单的目录监听并丢弃缓存(删目录前必须先关,否则旧视图与句柄都会赖着)。
|
|
783
|
+
* @author ddj 2026年11月26号
|
|
784
|
+
* @param paths 单子路径
|
|
785
|
+
* @returns 无
|
|
786
|
+
*/
|
|
787
|
+
function closeSheetCache(paths: SheetPaths): void {
|
|
788
|
+
const cache = sheets.get(paths.dir)
|
|
789
|
+
if (!cache) return
|
|
790
|
+
for (const watcher of cache.watchers.values()) {
|
|
791
|
+
try {
|
|
792
|
+
watcher.close()
|
|
793
|
+
} catch {
|
|
794
|
+
/* 已关闭 */
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
cache.watchers.clear()
|
|
798
|
+
sheets.delete(paths.dir)
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
//endregion
|
|
802
|
+
|
|
803
|
+
//region 释放
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* 释放所有目录监听与缓存(插件卸载时调用)。
|
|
807
|
+
* @author ddj 2026年11月25号
|
|
808
|
+
* @returns 无
|
|
809
|
+
*/
|
|
810
|
+
export function disposeTestCheck(): void {
|
|
811
|
+
for (const cache of sheets.values()) {
|
|
812
|
+
for (const watcher of cache.watchers.values()) {
|
|
813
|
+
try { watcher.close() } catch { /* 已关闭 */ }
|
|
814
|
+
}
|
|
815
|
+
cache.watchers.clear()
|
|
816
|
+
}
|
|
817
|
+
sheets.clear()
|
|
818
|
+
fileHashes.clear()
|
|
819
|
+
resetSheetListCache()
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
//endregion
|