dsh-knit 0.26.0 → 0.27.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.
@@ -315,6 +315,10 @@ test('纯:commentLines 严格照 §四 的文本格式(1 位小数、不截
315
315
  // 工作区图同时有 name 和 rel 时**优先 rel**(比文件名唯一,agent 能直接定位)
316
316
  assert.equal(commentLines(list.slice(0, 1), { name: 'G3.png', rel: 'sub/G3.png' })[0], '图像 1:sub/G3.png')
317
317
 
318
+ // 编号跟着输入框附件顺序走:第二张附件就是「图像 2」
319
+ assert.equal(commentLines(list.slice(0, 1), { rel: 'b.png', index: 2 })[0], '图像 2:b.png')
320
+ assert.equal(commentLines(list.slice(0, 1), { rel: 'c.png', index: 0 })[0], '图像 1:c.png', '非法序号回落 1')
321
+
318
322
  // 没有 sha(工作区图)⇒ 不带那对括号,但身份(rel)必须在
319
323
  assert.deepEqual(commentLines(list.slice(0, 1), { rel: 'sub/G3.png' }), [
320
324
  '图像 1:sub/G3.png',
@@ -355,6 +359,27 @@ test('纯:commentsFor / withComments 按身份存、删空不留垃圾键、
355
359
  assert.deepEqual(commentsFor(null, 'a.png'), [])
356
360
  })
357
361
 
362
+ test('纯:composerState 数出输入框里已有几张附件、有没有正文', () => {
363
+ const { composerState } = testApi
364
+ const card = {
365
+ querySelectorAll: () => [
366
+ { src: 'blob:http://x/1' },
367
+ { src: 'https://x/y', parentElement: { className: 'abc_thumbnail' } },
368
+ { src: 'https://x/icon', parentElement: { className: 'icon' } },
369
+ ],
370
+ }
371
+ const input = {
372
+ textContent: '已有正文',
373
+ closest: (selector) => (selector === '[data-composer-card]' ? card : null),
374
+ }
375
+ const doc = { querySelector: (selector) => (selector === '[data-composer-input]' ? input : null) }
376
+ assert.deepEqual(composerState(doc), { count: 2, hasText: true })
377
+ assert.deepEqual(composerState({ querySelector: () => null }), { count: 0, hasText: false })
378
+ assert.deepEqual(composerState(null), { count: 0, hasText: false })
379
+ input.textContent = ' '
380
+ assert.deepEqual(composerState(doc), { count: 2, hasText: false }, '纯空白不算正文')
381
+ })
382
+
358
383
  /* ── ① 纯函数:投递通道 ───────────────────────────────── */
359
384
 
360
385
  test('投递:合成 paste 到 [data-composer-input],文本逐字进输入框', async () => {
@@ -428,6 +453,25 @@ test('投递:评论要**把原图一起**作为附件 paste 进输入框(不
428
453
  assert.equal(input.textContent, text)
429
454
  })
430
455
 
456
+ test('投递:输入框已有正文时,新评论以**独立段落**投递(前面空两行)', async () => {
457
+ const { deliverCommentText } = testApi
458
+ const events = []
459
+ const input = {
460
+ textContent: '上一段',
461
+ focus() {},
462
+ dispatchEvent(event) {
463
+ events.push(event)
464
+ this.textContent = `${this.textContent}${event.clipboardData.getData('text/plain')}`
465
+ return false
466
+ },
467
+ }
468
+ const doc = { querySelector: () => input, dispatchEvent: () => true }
469
+ const result = await deliverCommentText('图像 2:b.png', { document: doc, window: {} })
470
+ assert.deepEqual(result, { ok: true, via: 'paste', image: false })
471
+ assert.equal(events[0].clipboardData.getData('text/plain'), '\n\n图像 2:b.png', '前面空两行 = 新段落')
472
+ assert.equal(input.textContent, '上一段\n\n图像 2:b.png')
473
+ })
474
+
431
475
  test('投递:图片读不到时仍投文本,并如实报告 image:false(不假装附件成功)', async () => {
432
476
  const { deliverCommentText } = testApi
433
477
  const input = { textContent: '', focus() {}, dispatchEvent() { this.textContent = 'x'; return false } }
@@ -932,12 +976,23 @@ test('守卫:面板画布与整屏查看器共用同一份 DOM(docked 只换
932
976
  test('守卫:发送必须带原图附件(只发文字会被 agent 当成「不知道是哪张图」)', () => {
933
977
  assert.match(SOURCE, /attachment: \{\s*src: srcFor\(lightboxDoc\)/,
934
978
  'sendComments 必须把图片地址交给投递层')
935
- assert.match(SOURCE, /buildPasteEvent\(payload, win, file\)/,
979
+ assert.match(SOURCE, /buildPasteEvent\(body, win, file\)/,
936
980
  'paste 事件必须同时带文本与 File')
937
981
  assert.match(SOURCE, /async function imageFileFrom\(/, '图片要读成 File')
938
982
  assert.match(SOURCE, /credentials: 'same-origin'/, '只读同源图片,不碰外网')
939
983
  })
940
984
 
985
+ test('守卫:多张图的编号跟附件顺序一致,且每次投递是独立段落', () => {
986
+ assert.match(SOURCE, /composerState\(typeof document === 'undefined' \? null : document\)/,
987
+ '发送前必须读输入框的附件数')
988
+ assert.match(SOURCE, /index: composer\.count \+ 1/,
989
+ '新图编号 = 已有附件数 + 1(附件一 = 图像一)')
990
+ assert.match(SOURCE, /图像 \$\{index\}:/,
991
+ '头行的编号是变量,不是写死 1')
992
+ assert.match(SOURCE, /existing \? `\\n\\n\$\{payload\}` : payload/,
993
+ '已有正文时前面空两行,形成独立段落')
994
+ })
995
+
941
996
  test('守卫:评论 P0 不落盘 —— 不新增 localStorage 键,也不碰 dsh-knit:pack', () => {
942
997
  const start = SOURCE.indexOf('const [commentOn, setCommentOn]')
943
998
  const end = SOURCE.indexOf('return h(\'div\', {', start)
@@ -25,14 +25,14 @@ import { join } from 'node:path'
25
25
 
26
26
  import {
27
27
  parseStatusZ, kindsOf, createGitReader, spawnGit, gitEnv,
28
- GIT_STATUS_ARGS, GIT_PREFIX_ARGS, GIT_ENV_ALLOW, MUTATING_VERBS,
29
- MAX_CHANGED_FILES, MAX_REPORTED_FILES,
28
+ GIT_STATUS_ARGS, GIT_PREFIX_ARGS, GIT_NUMSTAT_ARGS, GIT_DIFF_ARGS, GIT_ENV_ALLOW, MUTATING_VERBS,
29
+ MAX_CHANGED_FILES, MAX_REPORTED_FILES, parseNumstatZ, parseUnifiedDiff,
30
30
  } from '../src/host/git.js'
31
31
  import {
32
32
  splitSeeds, ghostCandidates, projectChangeRelated, MAX_CHANGE_RELATED,
33
33
  } from '../src/host/changes.js'
34
34
  import { buildRelationFacts, resetRelationCache, testsOf } from '../src/host/relations.js'
35
- import { changesPayload, corpusForChanges, scanMemo, resetScanMemo, resetGitReaderCache } from '../src/host/index.js'
35
+ import { changesPayload, statusPayload, diffPayload, corpusForChanges, scanMemo, resetScanMemo, resetGitReaderCache } from '../src/host/index.js'
36
36
  import { createIndex } from '../src/host/links.js'
37
37
  import { startKnitServer } from './http-harness.mjs'
38
38
  import { scan, buildContextFor, publicScanPayload } from '../src/host/index.js'
@@ -1236,3 +1236,197 @@ test('安全(证伪):**不净化**环境时,上面那三个注入确实
1236
1236
  rmSync(decoy, { recursive: true, force: true })
1237
1237
  }
1238
1238
  })
1239
+
1240
+
1241
+ /* ── v0.26.x「变更」档:numstat 与 status 载荷 ───────────────── */
1242
+
1243
+ test('git numstat: `--numstat -z --no-renames HEAD` 解析(含二进制、前缀、工作区外)', () => {
1244
+ const raw = '2\t1\ta.md\0-\t-\tb.png\x000\t1\tsrc/c.ts\0'
1245
+ const map = parseNumstatZ(raw, { prefix: '' })
1246
+ assert.deepEqual(map.get('a.md'), { added: 2, deleted: 1, binary: false })
1247
+ assert.deepEqual(map.get('b.png'), { added: null, deleted: null, binary: true })
1248
+ assert.deepEqual(map.get('src/c.ts'), { added: 0, deleted: 1, binary: false })
1249
+ const trimmed = parseNumstatZ(raw, { prefix: 'src/' })
1250
+ assert.equal(trimmed.has('a.md'), false, '前缀外丢弃')
1251
+ assert.equal(trimmed.has('c.ts'), true, '前缀减掉后是工作区相对路径')
1252
+ assert.equal(trimmed.get('c.ts').deleted, 1)
1253
+ })
1254
+
1255
+ test('git: readDetailed 只多跑一条 numstat,read() 仍只有 status', async () => {
1256
+ const calls = []
1257
+ const impl = async (args) => {
1258
+ calls.push(args[0])
1259
+ if (args[0] === 'rev-parse') return { code: 0, stdout: '', stderr: '' }
1260
+ if (args[0] === 'status') return { code: 0, stdout: ' M a.md\0?? b.ts\0', stderr: '' }
1261
+ if (args[0] === 'diff') return { code: 0, stdout: '2\t1\ta.md\0', stderr: '' }
1262
+ return { code: 1, stdout: '', stderr: 'unexpected' }
1263
+ }
1264
+ const reader = createGitReader({ exec: impl, now: () => 0 })
1265
+ const statusOnly = await reader.read('/w')
1266
+ assert.deepEqual(calls, ['rev-parse', 'status'], 'read() 不跑 numstat')
1267
+ assert.equal(statusOnly.files.length, 2)
1268
+ const detailed = await reader.readDetailed('/w')
1269
+ assert.deepEqual(calls.slice(0, 1), ['rev-parse'])
1270
+ assert.ok(calls.includes('diff'))
1271
+ const byRel = new Map(detailed.files.map((f) => [f.rel, f]))
1272
+ assert.equal(byRel.get('a.md').added, 2)
1273
+ assert.equal(byRel.get('a.md').deleted, 1)
1274
+ assert.equal(byRel.get('b.ts').untracked, true)
1275
+ assert.equal(byRel.get('b.ts').added, null, '未跟踪文件没有 HEAD 基线')
1276
+ assert.equal(detailed.statsAvailable, true)
1277
+ })
1278
+
1279
+ test('git 安全:numstat 也是只读 argv,三个子命令都没有写动词', () => {
1280
+ assert.equal(GIT_NUMSTAT_ARGS[0], 'diff')
1281
+ assert.ok(GIT_NUMSTAT_ARGS.includes('--numstat'))
1282
+ assert.ok(GIT_NUMSTAT_ARGS.includes('HEAD'))
1283
+ for (const verb of MUTATING_VERBS) {
1284
+ assert.ok(!GIT_STATUS_ARGS.includes(verb))
1285
+ assert.ok(!GIT_PREFIX_ARGS.includes(verb))
1286
+ assert.ok(!GIT_NUMSTAT_ARGS.includes(verb), `numstat 的 argv 里出现了写动词 ${verb}`)
1287
+ }
1288
+ })
1289
+
1290
+ test('statusPayload: 变更文件 → 列表行(状态 / 行数 / kind / 越界路径)', async () => {
1291
+ const root = mkdtempSync(join(tmpdir(), 'knit-status-'))
1292
+ try {
1293
+ writeFileSync(join(root, 'a.md'), 'one\ntwo\n')
1294
+ mkdirSync(join(root, 'src')) // 目录不参与
1295
+ writeFileSync(join(root, 'src', 'b.ts'), 'x\ny\nz\n')
1296
+ const reader = {
1297
+ readDetailed: async () => ({
1298
+ available: true, reason: null, filesTotal: 2, truncated: false, statsAvailable: true,
1299
+ files: [
1300
+ { rel: 'a.md', kinds: ['modified'], added: 2, deleted: 1, binary: false },
1301
+ { rel: 'src/b.ts', kinds: ['untracked'], added: null, deleted: null, binary: false, untracked: true },
1302
+ ],
1303
+ }),
1304
+ }
1305
+ const payload = await statusPayload(root, { gitReader: reader })
1306
+ assert.equal(payload.ok, true)
1307
+ assert.equal(payload.mode, 'status')
1308
+ assert.equal(payload.git.available, true)
1309
+ assert.equal(payload.git.statsAvailable, true)
1310
+ const byRel = new Map(payload.git.files.map((f) => [f.rel, f]))
1311
+ assert.equal(byRel.get('a.md').kind, 'md')
1312
+ assert.equal(byRel.get('a.md').previewable, true)
1313
+ assert.equal(byRel.get('a.md').added, 2)
1314
+ assert.equal(byRel.get('a.md').removed, 1)
1315
+ assert.equal(byRel.get('src/b.ts').kind, 'code')
1316
+ assert.equal(byRel.get('src/b.ts').untracked, true)
1317
+ assert.equal(byRel.get('src/b.ts').added, 3, '未跟踪文件按行数补 +N')
1318
+ assert.equal(byRel.get('src/b.ts').removed, 0)
1319
+ } finally { rmSync(root, { recursive: true, force: true }) }
1320
+ })
1321
+
1322
+ test('statusPayload: numstat 失败时只降级 statsAvailable,文件清单仍然可用', async () => {
1323
+ const root = mkdtempSync(join(tmpdir(), 'knit-status-'))
1324
+ try {
1325
+ writeFileSync(join(root, 'a.md'), 'one\n')
1326
+ const reader = {
1327
+ readDetailed: async () => ({
1328
+ available: true, reason: null, filesTotal: 1, truncated: false, statsAvailable: false,
1329
+ files: [{ rel: 'a.md', kinds: ['modified'] }],
1330
+ }),
1331
+ }
1332
+ const payload = await statusPayload(root, { gitReader: reader })
1333
+ assert.equal(payload.git.available, true)
1334
+ assert.equal(payload.git.statsAvailable, false)
1335
+ assert.equal(payload.git.files.length, 1)
1336
+ assert.equal(payload.git.files[0].added, null)
1337
+ assert.equal(payload.git.files[0].removed, null)
1338
+ } finally { rmSync(root, { recursive: true, force: true }) }
1339
+ })
1340
+
1341
+
1342
+ test('diff: 解析 unified diff(上下文 / 新增 / 删除 / hunk 行号)', () => {
1343
+ const raw = [
1344
+ 'diff --git a/src/a.ts b/src/a.ts',
1345
+ 'index 111..222 100644',
1346
+ '--- a/src/a.ts',
1347
+ '+++ b/src/a.ts',
1348
+ '@@ -1,3 +1,4 @@',
1349
+ ' one',
1350
+ '-two',
1351
+ '+TWO',
1352
+ '+three',
1353
+ ' four',
1354
+ ].join('\n')
1355
+ const out = parseUnifiedDiff(raw)
1356
+ assert.equal(out.hunks, 1)
1357
+ assert.equal(out.added, 2)
1358
+ assert.equal(out.removed, 1)
1359
+ assert.deepEqual(out.lines.map((line) => line.type), ['hunk', 'context', 'del', 'add', 'add', 'context'])
1360
+ assert.deepEqual(out.lines.slice(1).map((line) => [line.oldNo, line.newNo]), [[1, 1], [2, null], [null, 2], [null, 3], [3, 4]])
1361
+ assert.equal(out.lines[2].text, 'two')
1362
+ assert.equal(out.lines[3].text, 'TWO')
1363
+ })
1364
+
1365
+ test('diff: 未跟踪文件由宿主读成全新增行;逐行 diff 失败只返回错误码', async () => {
1366
+ const root = mkdtempSync(join(tmpdir(), 'knit-diff-'))
1367
+ try {
1368
+ writeFileSync(join(root, 'new.md'), 'a\nb\n')
1369
+ const reader = {
1370
+ readDetailed: async () => ({
1371
+ available: true, files: [{ rel: 'new.md', kinds: ['untracked'] }], filesTotal: 1,
1372
+ }),
1373
+ readDiff: async () => ({ available: false, reason: 'failed-fatal', stdout: '' }),
1374
+ }
1375
+ const payload = await diffPayload(root, 'new.md', { gitReader: reader })
1376
+ assert.equal(payload.ok, true)
1377
+ assert.equal(payload.added, 2)
1378
+ assert.equal(payload.removed, 0)
1379
+ assert.deepEqual(payload.lines.map((line) => line.type), ['add', 'add'])
1380
+ assert.deepEqual(payload.lines.map((line) => line.newNo), [1, 2])
1381
+ const bad = await diffPayload(root, '../outside.md', { gitReader: reader })
1382
+ assert.equal(bad.ok, false)
1383
+ assert.equal(bad.code, 'knit/not-found')
1384
+ } finally { rmSync(root, { recursive: true, force: true }) }
1385
+ })
1386
+
1387
+ test('diff 安全:diff argv 固定只读,带 --no-ext-diff / --no-textconv', () => {
1388
+ assert.equal(GIT_DIFF_ARGS[0], 'diff')
1389
+ assert.ok(GIT_DIFF_ARGS.includes('--no-ext-diff'))
1390
+ assert.ok(GIT_DIFF_ARGS.includes('--no-textconv'))
1391
+ assert.equal(GIT_DIFF_ARGS[GIT_DIFF_ARGS.length - 1], '--')
1392
+ for (const verb of MUTATING_VERBS) assert.ok(!GIT_DIFF_ARGS.includes(verb))
1393
+ })
1394
+
1395
+
1396
+ test('变更:workspace 根不是仓库时,自动用一层子目录里的 Git 仓库(如 08_Knit/knit)', async () => {
1397
+ const root = mkdtempSync(join(tmpdir(), 'knit-nested-'))
1398
+ try {
1399
+ mkdirSync(join(root, 'knit', '.git'), { recursive: true })
1400
+ writeFileSync(join(root, 'knit', 'a.md'), 'one\ntwo\n')
1401
+ const calls = { detail: [], diff: [] }
1402
+ const reader = {
1403
+ readDetailed: async (repo) => {
1404
+ calls.detail.push(repo)
1405
+ if (!repo.endsWith('/knit')) return { available: false, reason: 'not-a-repo', files: [], filesTotal: 0 }
1406
+ return {
1407
+ available: true, reason: null, filesTotal: 1, truncated: false, statsAvailable: true,
1408
+ files: [{ rel: 'a.md', kinds: ['modified'], added: 2, deleted: 1, binary: false }],
1409
+ }
1410
+ },
1411
+ readDiff: async (repo, rel) => {
1412
+ calls.diff.push([repo, rel])
1413
+ return { available: true, reason: null, stdout: '@@ -1,2 +1,2 @@\n one\n-two\n+TWO\n' }
1414
+ },
1415
+ }
1416
+ const status = await statusPayload(root, { gitReader: reader })
1417
+ assert.equal(status.git.available, true)
1418
+ assert.equal(status.git.filesTotal, 1)
1419
+ assert.equal(status.git.files[0].rel, 'knit/a.md')
1420
+ assert.equal(status.git.files[0].repo, 'knit')
1421
+ assert.equal(status.git.files[0].kind, 'md')
1422
+ assert.equal(status.git.files[0].previewable, true)
1423
+ assert.ok(status.git.files[0].mtimeMs > 0)
1424
+
1425
+ const diff = await diffPayload(root, 'knit/a.md', { gitReader: reader })
1426
+ assert.equal(diff.ok, true)
1427
+ assert.deepEqual(calls.diff.at(-1), [join(root, 'knit'), 'a.md'], 'diff 在子仓库里跑,路径是仓库相对路径')
1428
+ assert.equal(diff.added, 1)
1429
+ assert.equal(diff.removed, 1)
1430
+ assert.deepEqual(diff.lines.at(-1), { type: 'add', oldNo: null, newNo: 2, text: 'TWO' })
1431
+ } finally { rmSync(root, { recursive: true, force: true }) }
1432
+ })
package/test/harness.mjs CHANGED
@@ -112,7 +112,7 @@ export function loadClientModule(options = {}) {
112
112
  // v0.26 图内评论:坐标 / 归一化 / §四 文本 / 身份锚定 + 投递通道
113
113
  + 'contentRect, pctFromPoint, pointFromPct, normalizeComments, nextNumber, commentSummary, '
114
114
  + 'commentLines, anchorIdFor, shaOfDoc, commentsFor, withComments, '
115
- + 'contentViewportOf, commentPopoverBox, '
115
+ + 'contentViewportOf, commentPopoverBox, composerState, caretToEnd, changeStatusOfFile, changeDocsOf, KIND_CHANGES, '
116
116
  + 'commentClickPct, commentEditorTarget, deliverCommentText, buildPasteEvent, '
117
117
  + 'COMMENT_MAX_CHARS, COMMENT_MAX, COMMENT_SUMMARY_CHARS, COMMENT_POP_W, COMMENT_POP_H, '
118
118
  + 'mountLightbox, '