dsh-knit 0.25.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.
@@ -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
@@ -109,6 +109,12 @@ export function loadClientModule(options = {}) {
109
109
  + 'hoverShotBox, HOVER_SHOT_MAX, HOVER_SHOT_GAP, HOVER_SHOT_MARGIN, HOVER_SHOT_DELAY_MS, '
110
110
  // v0.25 媒体档「双击全屏」:左侧带子的窗口算法是纯函数
111
111
  + 'lightboxRail, LIGHTBOX_RAIL, LIGHTBOX_RAIL_LEAD, '
112
+ // v0.26 图内评论:坐标 / 归一化 / §四 文本 / 身份锚定 + 投递通道
113
+ + 'contentRect, pctFromPoint, pointFromPct, normalizeComments, nextNumber, commentSummary, '
114
+ + 'commentLines, anchorIdFor, shaOfDoc, commentsFor, withComments, '
115
+ + 'contentViewportOf, commentPopoverBox, composerState, caretToEnd, changeStatusOfFile, changeDocsOf, KIND_CHANGES, '
116
+ + 'commentClickPct, commentEditorTarget, deliverCommentText, buildPasteEvent, '
117
+ + 'COMMENT_MAX_CHARS, COMMENT_MAX, COMMENT_SUMMARY_CHARS, COMMENT_POP_W, COMMENT_POP_H, '
112
118
  + 'mountLightbox, '
113
119
 
114
120
  + 'DOC_NEEDLE_CHARS, DOC_NEEDLE_MIN, DOC_NEEDLE_MAX, '