dsh-delete-session 0.2.2 → 0.2.4

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.
@@ -191,6 +191,10 @@ function matches(node, selector) {
191
191
  return node._classes.has(selector.slice(1))
192
192
  }
193
193
 
194
+ // `[class*="_footArea"]` - how the bar finds the host's sidebar footer.
195
+ const classContains = /^\[class\*="([^"]*)"\]$/.exec(selector)
196
+ if (classContains !== null) return node.className.includes(classContains[1])
197
+
194
198
  const attrEquals = /^\[([\w-]+)="([^"]*)"\]$/.exec(selector)
195
199
  if (attrEquals !== null) return node.attributes[attrEquals[1]] === attrEquals[2]
196
200
 
@@ -265,6 +269,9 @@ async function loadPlugin(options = {}) {
265
269
  location: { reload() {} },
266
270
  dispatchEvent() {},
267
271
  Event: class { constructor(type) { this.type = type } },
272
+ // Viewport height: the docked bar is anchored to the footer's bottom edge,
273
+ // so the distance has to be computed against a real number.
274
+ innerHeight: 800,
268
275
  }
269
276
  globalThis.localStorage = makeStorage()
270
277
  // Node exposes `navigator` as a getter-only global, so it needs defining rather
@@ -275,9 +282,14 @@ async function loadPlugin(options = {}) {
275
282
  writable: true,
276
283
  })
277
284
  globalThis.MutationObserver = class {
278
- constructor(callback) { this.callback = callback; observers.push(this) }
279
- observe() {}
280
- disconnect() {}
285
+ constructor(callback) {
286
+ this.callback = callback
287
+ this.target = null
288
+ this.options = null
289
+ observers.push(this)
290
+ }
291
+ observe(target, options) { this.target = target === undefined ? null : target; this.options = options === undefined ? null : options }
292
+ disconnect() { this.target = null }
281
293
  }
282
294
  // The plugin coalesces decorate passes through rAF. Run the callback on a real
283
295
  // microtask-free tick rather than synchronously: `scheduleDecorate` assigns the
@@ -324,7 +336,12 @@ async function loadPlugin(options = {}) {
324
336
  if (href.endsWith('/dsh-delete-session/pick')) {
325
337
  const pick = options.pick ?? { cancelled: true, path: null }
326
338
  if (pick.unsupported === true) {
327
- return { ok: false, status: 501, json: async () => ({ ok: false, code: 'unsupported', error: '系统文件夹对话框只支持 Windows' }) }
339
+ return { ok: false, status: 200, json: async () => ({ ok: false, code: 'unsupported', error: '宿主没有 directoryPicker 服务' }) }
340
+ }
341
+ // A browse deployment: the host has no OS chooser to raise, so it tells the
342
+ // page to draw its own picker. Normal for a remote client.
343
+ if (pick.browse === true) {
344
+ return { ok: false, status: 200, json: async () => ({ ok: false, code: 'browse', error: '浏览式宿主' }) }
328
345
  }
329
346
  return { ok: true, status: 200, json: async () => ({ ok: true, ...pick }) }
330
347
  }
@@ -404,9 +421,17 @@ async function loadPlugin(options = {}) {
404
421
  effect: (fn) => { const dispose = fn(); return () => { if (typeof dispose === 'function') dispose() } },
405
422
  })
406
423
 
424
+ // Mirrors `dsh-client-locale`: a function-valued entry is handed back
425
+ // UNTOUCHED (the caller invokes it), and a string is returned as-is when no
426
+ // params are given. Invoking here would hide the difference that once broke
427
+ // the real host.
407
428
  const t = (key, a, b) => {
408
429
  const value = locales[NS]?.zh?.[key]
409
- return typeof value === 'function' ? value(a, b) : value ?? key
430
+ if (value === undefined) return key
431
+ if (typeof value === 'function') return value
432
+ if (a === undefined) return value
433
+ const params = a !== null && typeof a === 'object' ? a : { n: a, done: a, total: b, ok: a, fail: b, path: a }
434
+ return value.replace(/{(w+)}/g, (match, name) => (params[name] === undefined ? match : String(params[name])))
410
435
  }
411
436
 
412
437
  return { document, requests, rows, observers, slotRegistrations, locales, primitives: primitivesStub, t }
@@ -427,12 +452,20 @@ function makeStorage() {
427
452
  * from `data-row-key`, not from a React fiber; without this the plugin finds no
428
453
  * id and skips the row, which would make every assertion below pass vacuously.
429
454
  */
430
- function mountRow(harness, session = SESSION_A) {
455
+ /** The host wraps the session rows in this container; the plugin watches it. */
456
+ function mountList(harness) {
457
+ const list = harness.document.createElement('div')
458
+ list.className = 'hHd-Xa_regionArea'
459
+ harness.document.body.appendChild(list)
460
+ return list
461
+ }
462
+
463
+ function mountRow(harness, session = SESSION_A, parent = null) {
431
464
  const row = harness.document.createElement('div')
432
465
  row.className = 'session_row'
433
466
  row.setAttribute('role', 'treeitem')
434
467
  row.setAttribute('data-row-key', `session:${session.id}`)
435
- harness.document.body.appendChild(row)
468
+ ;(parent ?? harness.document.body).appendChild(row)
436
469
  harness.rows.push(row)
437
470
  return row
438
471
  }
@@ -507,8 +540,31 @@ const totalCheckboxes = (harness) => harness.document.querySelectorAll('.dsdel-c
507
540
  const selectedCheckboxes = (harness) =>
508
541
  harness.document.querySelectorAll('.dsdel-check').filter((el) => el.classList.contains('dsdel-check-on')).length
509
542
 
510
- const bar = (harness) =>
511
- walk(harness.document.body).find((node) => node._classes.has('dsdel-bar')) ?? null
543
+ const FOOTER_SLOT = 'sidebar.footer.action'
544
+
545
+ /**
546
+ * The batch bar is a slot occupant now: render the registered component the way
547
+ * the host would and inspect the element tree it returns. `null` means the
548
+ * footer is unoccupied, which is what "not selecting" looks like.
549
+ */
550
+ const footerEntry = (harness) =>
551
+ harness.slotRegistrations.find((entry) => entry.spec.name === FOOTER_SLOT) ?? null
552
+
553
+ const bar = (harness) => {
554
+ const entry = footerEntry(harness)
555
+ if (entry === null) return null
556
+ const node = entry.Component({})
557
+ return node === null || node === undefined ? null : node
558
+ }
559
+
560
+ const barChild = (harness, key) => {
561
+ const node = bar(harness)
562
+ if (node === null || node.props === undefined) return null
563
+ return findByKey(node.props.children, key)[0] ?? null
564
+ }
565
+
566
+ /** Invoke a bar button the way the host would dispatch a click. */
567
+ const clickBar = (node) => node.props.onClick({ stopPropagation() {} })
512
568
 
513
569
  const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
514
570
 
@@ -778,18 +834,16 @@ test('leaving multi-select strips every checkbox', async () => {
778
834
 
779
835
  selectRow(harness).element.props.onSelect()
780
836
  await flush()
781
- const control = bar(harness)
782
- assert.ok(control, 'the bar exists')
837
+ assert.ok(bar(harness), 'the bar exists')
783
838
  assert.equal(totalCheckboxes(harness), 2)
784
839
 
785
840
  // Cancel is the bar's non-danger button.
786
- const cancel = control.querySelector('.dsdel-bar-btn:not(.dsdel-bar-danger)')
787
- cancel.fire('click', { stopPropagation() {} })
841
+ clickBar(barCancel(harness))
788
842
  await flush()
789
843
 
790
844
  assert.equal(totalCheckboxes(harness), 0, 'no checkbox survives the mode')
791
845
  assert.deepEqual(rowCheckboxes(harness), [0, 0])
792
- assert.notEqual(control.style.display, 'flex', 'the bar is hidden again')
846
+ assert.equal(bar(harness), null, 'and the footer is empty again')
793
847
  })
794
848
 
795
849
  // --- the two modes must not leak into each other ----------------------------
@@ -801,8 +855,7 @@ test('entering, leaving, then deleting plants nothing', async () => {
801
855
 
802
856
  selectRow(harness).element.props.onSelect()
803
857
  await flush()
804
- const control = bar(harness)
805
- control.querySelector('.dsdel-bar-btn:not(.dsdel-bar-danger)').fire('click', { stopPropagation() {} })
858
+ clickBar(barCancel(harness))
806
859
  await flush()
807
860
 
808
861
  // Leaving the mode removed the observer that decorates; a later delete must
@@ -810,7 +863,7 @@ test('entering, leaving, then deleting plants nothing', async () => {
810
863
  await decorateViaSingleDelete(harness, SESSION_A)
811
864
 
812
865
  assert.equal(totalCheckboxes(harness), 0, 'the mode leaves no residue behind')
813
- assert.notEqual(control.style.display, 'flex', 'the bar stays hidden')
866
+ assert.equal(bar(harness), null, 'the footer stays empty')
814
867
  })
815
868
 
816
869
  test('a row whose row key changes still gets no checkbox outside the mode', async () => {
@@ -1352,11 +1405,16 @@ test('the browse button opens a picker that walks directories and commits one',
1352
1405
  // button appears between cancel and delete; when it is not, the bar is exactly
1353
1406
  // what it was before and no other logic changes.
1354
1407
 
1355
- const barButtons = (harness) => bar(harness).querySelectorAll('.dsdel-bar-btn')
1356
- const barMove = (harness) => bar(harness).querySelector('.dsdel-bar-move')
1357
- const barCancel = (harness) => bar(harness).querySelector('.dsdel-bar-cancel')
1358
- const barDelete = (harness) => bar(harness).querySelector('.dsdel-bar-danger')
1359
- const barCount = (harness) => bar(harness).querySelector('.dsdel-bar-count')
1408
+ const barButtons = (harness) => {
1409
+ const node = bar(harness)
1410
+ if (node === null || node.props === undefined) return []
1411
+ return (node.props.children || []).filter((child) => child !== null && child !== undefined
1412
+ && child.props !== undefined && String(child.props.className || '').includes('dsdel-bar-btn'))
1413
+ }
1414
+ const barMove = (harness) => barChild(harness, 'move')
1415
+ const barCancel = (harness) => barChild(harness, 'cancel')
1416
+ const barDelete = (harness) => barChild(harness, 'delete')
1417
+ const barCount = (harness) => barChild(harness, 'count')
1360
1418
  const toasts = (harness) => harness.document.querySelectorAll('.dsdel-toast').map((el) => el.textContent)
1361
1419
 
1362
1420
  function installRelocate(moveSelection) {
@@ -1378,7 +1436,7 @@ test('without dsh-session-relocate the bar keeps exactly cancel and delete', asy
1378
1436
  await enterMulti(harness)
1379
1437
 
1380
1438
  assert.equal(barMove(harness), null, 'no move button is offered')
1381
- assert.deepEqual(barButtons(harness).map((el) => el.textContent), ['取消', '删除'])
1439
+ assert.deepEqual(barButtons(harness).map((el) => el.props.children), ['取消', '删除'])
1382
1440
  })
1383
1441
 
1384
1442
  test('a relocate plugin that does not claim bulk move changes nothing', async () => {
@@ -1386,7 +1444,7 @@ test('a relocate plugin that does not claim bulk move changes nothing', async ()
1386
1444
  globalThis.window.__DSH_SESSION_RELOCATE__ = { version: '0.2.0', supportsBulkMove: false, moveSelection: async () => ({}) }
1387
1445
  mountRow(harness, SESSION_A)
1388
1446
  await enterMulti(harness)
1389
- assert.deepEqual(barButtons(harness).map((el) => el.textContent), ['取消', '删除'])
1447
+ assert.deepEqual(barButtons(harness).map((el) => el.props.children), ['取消', '删除'])
1390
1448
  })
1391
1449
 
1392
1450
  test('with dsh-session-relocate the move button sits between cancel and delete', async () => {
@@ -1395,7 +1453,7 @@ test('with dsh-session-relocate the move button sits between cancel and delete',
1395
1453
  mountRow(harness, SESSION_A)
1396
1454
  await enterMulti(harness)
1397
1455
 
1398
- assert.deepEqual(barButtons(harness).map((el) => el.textContent), ['取消', '移动', '删除'], 'left to right')
1456
+ assert.deepEqual(barButtons(harness).map((el) => el.props.children), ['取消', '移动', '删除'], 'left to right')
1399
1457
  })
1400
1458
 
1401
1459
  test('the move button is disabled until something is selected', async () => {
@@ -1404,13 +1462,13 @@ test('the move button is disabled until something is selected', async () => {
1404
1462
  const first = mountRow(harness, SESSION_A)
1405
1463
  await enterMulti(harness)
1406
1464
 
1407
- assert.equal(barMove(harness).disabled, true, 'nothing selected')
1408
- assert.equal(barDelete(harness).disabled, true, 'and delete agrees')
1409
- assert.equal(barCancel(harness).disabled, false, 'cancel is always live')
1465
+ assert.equal(barMove(harness).props.disabled, true, 'nothing selected')
1466
+ assert.equal(barDelete(harness).props.disabled, true, 'and delete agrees')
1467
+ assert.equal(barCancel(harness).props.disabled, false, 'cancel is always live')
1410
1468
 
1411
1469
  clickRowIn(harness, first)
1412
1470
  await flush()
1413
- assert.equal(barMove(harness).disabled, false, 'one selection is enough')
1471
+ assert.equal(barMove(harness).props.disabled, false, 'one selection is enough')
1414
1472
  })
1415
1473
 
1416
1474
  test('a move reports progress, then summarises, exits and refreshes', async () => {
@@ -1430,23 +1488,23 @@ test('a move reports progress, then summarises, exits and refreshes', async () =
1430
1488
  clickRowIn(harness, second)
1431
1489
  await flush()
1432
1490
 
1433
- barMove(harness).fire('click', { stopPropagation() {} })
1491
+ clickBar(barMove(harness))
1434
1492
  await flush()
1435
1493
 
1436
1494
  assert.deepEqual(seen.ids, [SESSION_A.id, SESSION_B.id], 'the whole selection is handed over')
1437
- assert.equal(barCount(harness).textContent, '正在移动 0/2…')
1438
- assert.equal(barMove(harness).disabled, true, 'inert while the move runs')
1439
- assert.equal(barDelete(harness).disabled, true)
1495
+ assert.equal(barCount(harness).props.children, '正在移动 0/2…')
1496
+ assert.equal(barMove(harness).props.disabled, true, 'inert while the move runs')
1497
+ assert.equal(barDelete(harness).props.disabled, true)
1440
1498
 
1441
1499
  seen.hooks.onProgress(1, 2)
1442
- assert.equal(barCount(harness).textContent, '正在移动 1/2…')
1500
+ assert.equal(barCount(harness).props.children, '正在移动 1/2…')
1443
1501
 
1444
1502
  finish({ moved: 2, failed: 0, skipped: 0, total: 2, results: [] })
1445
1503
  await flush()
1446
1504
  await flush()
1447
1505
 
1448
1506
  assert.deepEqual(toasts(harness), ['已移动 2 个'], 'one summary line')
1449
- assert.notEqual(bar(harness).style.display, 'flex', 'multi-select exited')
1507
+ assert.equal(bar(harness), null, 'multi-select exited')
1450
1508
  })
1451
1509
 
1452
1510
  test('a partial move reports both numbers', async () => {
@@ -1457,7 +1515,7 @@ test('a partial move reports both numbers', async () => {
1457
1515
  clickRowIn(harness, first)
1458
1516
  await flush()
1459
1517
 
1460
- barMove(harness).fire('click', { stopPropagation() {} })
1518
+ clickBar(barMove(harness))
1461
1519
  await flush()
1462
1520
  await flush()
1463
1521
 
@@ -1472,14 +1530,14 @@ test('cancelling the target picker keeps the selection and says nothing', async
1472
1530
  clickRowIn(harness, first)
1473
1531
  await flush()
1474
1532
 
1475
- barMove(harness).fire('click', { stopPropagation() {} })
1533
+ clickBar(barMove(harness))
1476
1534
  await flush()
1477
1535
  await flush()
1478
1536
 
1479
1537
  assert.deepEqual(toasts(harness), [], 'nothing happened, so nothing is reported')
1480
- assert.equal(bar(harness).style.display, 'flex', 'multi-select is still open')
1481
- assert.equal(barCount(harness).textContent, '已选 1 个', 'the count is back')
1482
- assert.equal(barMove(harness).disabled, false, 'the retry is available')
1538
+ assert.ok(bar(harness), 'multi-select is still open')
1539
+ assert.equal(barCount(harness).props.children, '已选 1 个', 'the count is back')
1540
+ assert.equal(barMove(harness).props.disabled, false, 'the retry is available')
1483
1541
  })
1484
1542
 
1485
1543
  test('a throwing relocate call ends the batch and reports the error', async () => {
@@ -1490,11 +1548,11 @@ test('a throwing relocate call ends the batch and reports the error', async () =
1490
1548
  clickRowIn(harness, first)
1491
1549
  await flush()
1492
1550
 
1493
- barMove(harness).fire('click', { stopPropagation() {} })
1551
+ clickBar(barMove(harness))
1494
1552
  await flush()
1495
1553
  await flush()
1496
1554
 
1497
- assert.equal(bar(harness).style.display, 'flex', 'still in multi-select')
1555
+ assert.ok(bar(harness), 'still in multi-select')
1498
1556
  assert.equal(toasts(harness).length, 1, 'the failure is reported')
1499
1557
  assert.match(toasts(harness)[0], /relocate exploded/)
1500
1558
  })
@@ -1508,17 +1566,17 @@ test('Escape stays inert while a move is running', async () => {
1508
1566
  clickRowIn(harness, first)
1509
1567
  await flush()
1510
1568
 
1511
- barMove(harness).fire('click', { stopPropagation() {} })
1569
+ clickBar(barMove(harness))
1512
1570
  await flush()
1513
1571
 
1514
1572
  harness.document.dispatchToDocument('keydown', { key: 'Escape', stopPropagation() {} })
1515
1573
  await flush()
1516
- assert.equal(bar(harness).style.display, 'flex', 'Escape is ignored mid-move')
1574
+ assert.ok(bar(harness), 'Escape is ignored mid-move')
1517
1575
 
1518
1576
  finish({ moved: 1, failed: 0, skipped: 0, total: 1, results: [] })
1519
1577
  await flush()
1520
1578
  await flush()
1521
- assert.notEqual(bar(harness).style.display, 'flex', 'and the mode ends when the move does')
1579
+ assert.equal(bar(harness), null, 'and the mode ends when the move does')
1522
1580
  })
1523
1581
 
1524
1582
  // --- the result contract is enforced, not guessed ---------------------------
@@ -1531,12 +1589,12 @@ test('the older "cancelled" spelling of the cancel flag is still honoured', asyn
1531
1589
  clickRowIn(harness, first)
1532
1590
  await flush()
1533
1591
 
1534
- barMove(harness).fire('click', { stopPropagation() {} })
1592
+ clickBar(barMove(harness))
1535
1593
  await flush()
1536
1594
  await flush()
1537
1595
 
1538
1596
  assert.deepEqual(toasts(harness), [], 'silent, exactly like the "canceled" spelling')
1539
- assert.equal(bar(harness).style.display, 'flex', 'still in multi-select')
1597
+ assert.ok(bar(harness), 'still in multi-select')
1540
1598
  })
1541
1599
 
1542
1600
  test('a result without the agreed counts is reported instead of guessed', async () => {
@@ -1549,7 +1607,7 @@ test('a result without the agreed counts is reported instead of guessed', async
1549
1607
  clickRowIn(harness, first)
1550
1608
  await flush()
1551
1609
 
1552
- barMove(harness).fire('click', { stopPropagation() {} })
1610
+ clickBar(barMove(harness))
1553
1611
  await flush()
1554
1612
  await flush()
1555
1613
 
@@ -1866,12 +1924,12 @@ const childChecks = (harness) => {
1866
1924
  return section === undefined ? [] : collectChecks([section], harness)
1867
1925
  }
1868
1926
 
1869
- test('the page offers to take child sessions with their parent, off by default', async () => {
1927
+ test('the page offers to take child sessions with their parent, on by default', async () => {
1870
1928
  const harness = await loadPlugin()
1871
1929
  const box = pageCheck(harness, 'syncChildren')
1872
1930
 
1873
1931
  assert.ok(box, 'the switch renders with the other delete controls')
1874
- assert.equal(box.props.checked, false, 'asking is the default')
1932
+ assert.equal(box.props.checked, true, 'taking them along is the default')
1875
1933
  assert.equal(box.props.label, '同时删除子会话')
1876
1934
 
1877
1935
  box.props.onChange(true)
@@ -1880,6 +1938,7 @@ test('the page offers to take child sessions with their parent, off by default',
1880
1938
 
1881
1939
  test('with the switch off the dialog lists the children, ticked, for you to narrow', async () => {
1882
1940
  const harness = await loadPlugin({ children: CHILDREN })
1941
+ localStorage.setItem(SYNC_CHILDREN_KEY, '0')
1883
1942
  mountRow(harness, SESSION_A)
1884
1943
 
1885
1944
  const { element } = deleteRow(harness, SESSION_A)
@@ -1893,6 +1952,7 @@ test('with the switch off the dialog lists the children, ticked, for you to narr
1893
1952
 
1894
1953
  test('unticking a child keeps it out of the delete', async () => {
1895
1954
  const harness = await loadPlugin({ children: CHILDREN })
1955
+ localStorage.setItem(SYNC_CHILDREN_KEY, '0')
1896
1956
  mountRow(harness, SESSION_A)
1897
1957
 
1898
1958
  const { element } = deleteRow(harness, SESSION_A)
@@ -1945,3 +2005,501 @@ test('the page ends with a quiet link to the repository', async () => {
1945
2005
  assert.equal(cta.props.rel, 'noreferrer')
1946
2006
  assert.equal(cta.props.children, '行,给你一颗 Star')
1947
2007
  })
2008
+
2009
+ // --- the batch bar occupies the official footer seat ------------------------
2010
+ //
2011
+ // It used to be a floating card injected into `document.body`, and for a while it
2012
+ // measured the sidebar footer and covered it. Both were the same mistake: a plugin
2013
+ // guessing at host layout. The bar is a slot occupant now - the host places it, so
2014
+ // there is nothing to measure, dock, hide or restore.
2015
+
2016
+ test('the bar registers into the official footer slot with a locale', async () => {
2017
+ const harness = await loadPlugin()
2018
+ const entry = footerEntry(harness)
2019
+
2020
+ assert.ok(entry, 'the footer seat is occupied by a registration')
2021
+ assert.equal(entry.spec.id, 'dsh-delete-session-bulkbar')
2022
+ assert.equal(entry.spec.locale, NS, 'and it asks for the plugin locale namespace')
2023
+ assert.equal(typeof entry.Component, 'function')
2024
+ })
2025
+
2026
+ test('the footer stays empty until multi-select starts', async () => {
2027
+ const harness = await loadPlugin()
2028
+ mountRow(harness, SESSION_A)
2029
+
2030
+ assert.equal(bar(harness), null, 'nothing is rendered into the footer outside the mode')
2031
+
2032
+ selectRow(harness).element.props.onSelect()
2033
+ await flush()
2034
+
2035
+ assert.ok(bar(harness), 'and it appears the moment the mode starts')
2036
+ })
2037
+
2038
+ test('the footer is empty again once the mode ends', async () => {
2039
+ const harness = await loadPlugin()
2040
+ mountRow(harness, SESSION_A)
2041
+
2042
+ selectRow(harness).element.props.onSelect()
2043
+ await flush()
2044
+ assert.ok(bar(harness), 'the bar is there while selecting')
2045
+
2046
+ clickBar(barCancel(harness))
2047
+
2048
+ assert.equal(bar(harness), null, 'and leaves the footer as it found it')
2049
+ })
2050
+
2051
+ test('the bar reads the count from the store, not from the DOM', async () => {
2052
+ const harness = await loadPlugin()
2053
+ const first = mountRow(harness, SESSION_A)
2054
+ mountRow(harness, SESSION_B)
2055
+
2056
+ selectRow(harness).element.props.onSelect()
2057
+ await flush()
2058
+
2059
+ assert.equal(barCount(harness).props.children, '已选 0 个', 'nothing selected yet')
2060
+
2061
+ harness.document.dispatchToDocument('click', {
2062
+ target: first,
2063
+ preventDefault() {},
2064
+ stopPropagation() {},
2065
+ })
2066
+ await flush()
2067
+
2068
+ assert.equal(barCount(harness).props.children, '已选 1 个', 'the same store the rows write to')
2069
+ })
2070
+
2071
+ test('the plugin no longer injects a bar element into the document', async () => {
2072
+ const harness = await loadPlugin()
2073
+ mountRow(harness, SESSION_A)
2074
+
2075
+ selectRow(harness).element.props.onSelect()
2076
+ await flush()
2077
+
2078
+ // Render the occupant first: a component that only misbehaves when the host
2079
+ // actually mounts it would otherwise slip past this check.
2080
+ assert.ok(bar(harness), 'the footer occupant renders')
2081
+
2082
+ const injected = walk(harness.document.body).filter((node) => node._classes.has('dsdel-bar'))
2083
+ assert.deepEqual(injected, [], 'no DOM bar: the footer slot is the only home')
2084
+ })
2085
+
2086
+ // --- the bar replaces the account row instead of stacking above it ----------
2087
+
2088
+ test('selecting puts a marker on the page so the account row can step aside', async () => {
2089
+ const harness = await loadPlugin()
2090
+ mountRow(harness, SESSION_A)
2091
+
2092
+ selectRow(harness).element.props.onSelect()
2093
+ await flush()
2094
+
2095
+ // The bar shares the sidebar foot with the account chip, so leaving the chip in
2096
+ // place stacks two rows. The marker is what lets one stylesheet rule hide it,
2097
+ // instead of measuring the host's box and covering it.
2098
+ assert.equal(harness.document.body._classes.has('dsdel-selecting'), true, 'the page is marked')
2099
+ })
2100
+
2101
+ test('leaving selection takes the marker away again', async () => {
2102
+ const harness = await loadPlugin()
2103
+ mountRow(harness, SESSION_A)
2104
+
2105
+ selectRow(harness).element.props.onSelect()
2106
+ await flush()
2107
+ clickBar(barCancel(harness))
2108
+
2109
+ assert.equal(harness.document.body._classes.has('dsdel-selecting'), false, 'the account row comes back')
2110
+ })
2111
+
2112
+ test('the injected stylesheet hides the account row while the marker is on', async () => {
2113
+ const harness = await loadPlugin()
2114
+ // `ensureStyle` tags its <style> through `dataset.dshDeleteSession = '1'`.
2115
+ const style = harness.document.head.querySelector('[data-dsh-delete-session="1"]')
2116
+ assert.ok(style, 'the plugin injected its stylesheet')
2117
+ const css = String(style._text ?? '')
2118
+
2119
+ // The foot stacks the action row, the usage card and the account row; only the
2120
+ // action row survives, so the bar ends the sidebar where the account row did.
2121
+ assert.match(
2122
+ css,
2123
+ /body\.dsdel-selecting \[class\*="_footArea"\] > \*:not\(\[class\*="_footerActions"\]\)/,
2124
+ 'the usage card and the account row step aside',
2125
+ )
2126
+ // The other footer buttons live in a slot container marked `display: contents`.
2127
+ // They are named POSITIVELY — by the attributes their own plugins set — never by
2128
+ // "everything except the bar": the bar is not a direct child of that container,
2129
+ // and an exclusion rule hid it together with its Cancel button.
2130
+ assert.match(
2131
+ css,
2132
+ /body\.dsdel-selecting \[data-slot="sidebar\.footer\.action"\] > \[data-dsh-plugin\]/,
2133
+ 'other plugins\' footer buttons are named one by one',
2134
+ )
2135
+ assert.doesNotMatch(
2136
+ css,
2137
+ /\[data-slot="sidebar\.footer\.action"\] > \*:not\(/,
2138
+ 'no exclusion rule may touch that slot — it cannot tell the bar apart',
2139
+ )
2140
+ assert.match(css, /display:\s*none/, 'both rules hide rather than shift')
2141
+ })
2142
+
2143
+ test('the stylesheet never hides the bar itself', async () => {
2144
+ const harness = await loadPlugin()
2145
+ const style = harness.document.head.querySelector('[data-dsh-delete-session="1"]')
2146
+ const css = String(style._text ?? '')
2147
+
2148
+ // Nothing may hide the footer by exclusion. Three attempts at "everything
2149
+ // except the bar" took the bar itself down, because the bar is not a direct
2150
+ // child of the container those rules addressed.
2151
+ assert.doesNotMatch(
2152
+ css,
2153
+ /\[class\*="_footerActions"\] > \*:not\(\.dsdel-bar\)/,
2154
+ 'no rule may target the bar as a direct child of the action row',
2155
+ )
2156
+ assert.doesNotMatch(
2157
+ css,
2158
+ /\[class\*="_footerActions"\] > \*:not\(:has/,
2159
+ 'nor hide the action row wholesale',
2160
+ )
2161
+ assert.doesNotMatch(
2162
+ css,
2163
+ /\[data-slot="sidebar\.footer\.action"\] > \*:not\(\.dsdel-bar\)/,
2164
+ 'nor the slot container by exclusion',
2165
+ )
2166
+ // The bar itself must be addressed by nothing but its own appearance rules.
2167
+ assert.doesNotMatch(
2168
+ css,
2169
+ /body\.dsdel-selecting [^{]*\.dsdel-bar\b(?!-)/,
2170
+ 'no selecting-state rule names the bar at all',
2171
+ )
2172
+ })
2173
+
2174
+ test('an interpolating host translator never receives our function entries', async () => {
2175
+ const harness = await loadPlugin()
2176
+ const dict = harness.locales[NS].zh
2177
+ // `dsh-client-locale` interpolates whatever it finds as soon as params are
2178
+ // given: it calls `template.replace(...)`. Handing it a function-valued entry
2179
+ // throws "template.replace is not a function", and a slot entry that throws is
2180
+ // replaced by the host's `data-slot-error` anchor — the bar vanishes whole.
2181
+ const strict = (key, params) => {
2182
+ const value = dict[key]
2183
+ if (value === undefined) return key
2184
+ if (params === undefined) return value
2185
+ return value.replace(/\{(\w+)\}/g, (match, name) => (params && name in params ? String(params[name]) : match))
2186
+ }
2187
+
2188
+ mountRow(harness, SESSION_A)
2189
+ selectRow(harness).element.props.onSelect()
2190
+ await flush()
2191
+
2192
+ const node = footerEntry(harness).Component({ t: strict })
2193
+ assert.ok(node, 'the bar still renders')
2194
+ assert.equal(node.props.children[0].props.children, '已选 0 个', 'and the count is formatted locally')
2195
+ })
2196
+
2197
+ test('a placeholder string is interpolated locally, not by the host', async () => {
2198
+ const harness = await loadPlugin({ children: CHILDREN })
2199
+ localStorage.setItem(SYNC_CHILDREN_KEY, '0')
2200
+ const dict = harness.locales[NS].zh
2201
+ const strict = (key, params) => {
2202
+ const value = dict[key]
2203
+ if (value === undefined) return key
2204
+ if (params === undefined) return value
2205
+ return value.replace(/{(w+)}/g, (match, name) => (params && name in params ? String(params[name]) : match))
2206
+ }
2207
+
2208
+ const row = mountRow(harness, SESSION_A)
2209
+ const { element } = deleteRow(harness, SESSION_A)
2210
+ element.props.onSelect({ shiftKey: false })
2211
+ await flush()
2212
+
2213
+ // `childrenHint` is the one template-with-placeholder in the dictionary; the
2214
+ // host must not be asked to fill it, or it would receive a function entry.
2215
+ const dialogEntry = harness.slotRegistrations.find(
2216
+ (entry) => entry.spec.name === 'shell.overlay' && entry.spec.id === `${NS}-dialog`,
2217
+ )
2218
+ const modal = dialogEntry.Component({ t: strict })
2219
+ const kids = (modal.props.children ?? []).filter(Boolean)
2220
+ const section = kids.find((child) => child.props && child.props.key === 'children')
2221
+ assert.ok(section, 'the child list renders')
2222
+ assert.equal(section.props.children[0].props.children, '这个对话有 2 个子会话(子代理),勾选的会一起删除。')
2223
+ assert.ok(row)
2224
+ })
2225
+
2226
+ test('the bar buttons keep their width instead of wrapping', async () => {
2227
+ const harness = await loadPlugin()
2228
+ const style = harness.document.head.querySelector('[data-dsh-delete-session="1"]')
2229
+ const css = String(style._text ?? '')
2230
+
2231
+ // In a flex row the buttons are the only shrinkable things next to a growing
2232
+ // count, so a long progress label squeezed them into two-character columns.
2233
+ assert.match(css, /\.dsdel-bar-btn\{[^}]*flex:0 0 auto/, 'buttons never shrink')
2234
+ assert.match(css, /\.dsdel-bar-btn\{[^}]*white-space:nowrap/, 'and never wrap their label')
2235
+ assert.match(css, /\.dsdel-bar-count\{[^}]*text-overflow:ellipsis/, 'the count gives way instead')
2236
+ })
2237
+
2238
+ // --- drag a session onto the footer bar to delete it ------------------------
2239
+
2240
+ /** Drag one mounted row the way the host starts a session drag. */
2241
+ const startRowDrag = (harness, row) => harness.document.dispatchToDocument('dragstart', {
2242
+ target: row,
2243
+ preventDefault() {},
2244
+ stopPropagation() {},
2245
+ })
2246
+
2247
+ const dropEvent = () => ({ preventDefault() {}, stopPropagation() {}, dataTransfer: { dropEffect: '' } })
2248
+
2249
+ test('dragging a session row turns the footer into a drop target', async () => {
2250
+ const harness = await loadPlugin()
2251
+ const row = mountRow(harness, SESSION_A)
2252
+
2253
+ assert.equal(bar(harness), null, 'the footer is untouched before the drag')
2254
+
2255
+ startRowDrag(harness, row)
2256
+ await flush()
2257
+
2258
+ const node = bar(harness)
2259
+ assert.ok(node, 'the bar appears while a session is being dragged')
2260
+ assert.match(String(node.props.className), /\bdsdel-drop\b/, 'as a drop target, not the selection bar')
2261
+ assert.equal(harness.document.body._classes.has('dsdel-selecting'), true, 'and the account row steps aside')
2262
+ })
2263
+
2264
+ test('hovering the bar shows that releasing will delete', async () => {
2265
+ const harness = await loadPlugin()
2266
+ const row = mountRow(harness, SESSION_A)
2267
+
2268
+ startRowDrag(harness, row)
2269
+ await flush()
2270
+ assert.doesNotMatch(String(bar(harness).props.className), /dsdel-drop-over/, 'not armed yet')
2271
+
2272
+ bar(harness).props.onDragOver(dropEvent())
2273
+ await flush()
2274
+
2275
+ const node = bar(harness)
2276
+ assert.match(String(node.props.className), /dsdel-drop-over/, 'armed once the pointer is over it')
2277
+ const text = JSON.stringify(node.props.children)
2278
+ assert.match(text, /松手删除/, 'and the line says so')
2279
+ })
2280
+
2281
+ test('leaving the bar disarms it again', async () => {
2282
+ const harness = await loadPlugin()
2283
+ const row = mountRow(harness, SESSION_A)
2284
+
2285
+ startRowDrag(harness, row)
2286
+ await flush()
2287
+ bar(harness).props.onDragOver(dropEvent())
2288
+ await flush()
2289
+ bar(harness).props.onDragLeave(dropEvent())
2290
+ await flush()
2291
+
2292
+ assert.doesNotMatch(String(bar(harness).props.className), /dsdel-drop-over/)
2293
+ })
2294
+
2295
+ test('dropping asks first unless the user turned asking off', async () => {
2296
+ const harness = await loadPlugin()
2297
+ const row = mountRow(harness, SESSION_A)
2298
+
2299
+ startRowDrag(harness, row)
2300
+ await flush()
2301
+ bar(harness).props.onDrop(dropEvent())
2302
+ await flush()
2303
+
2304
+ assert.ok(dialogParts(harness), 'the confirmation dialog opens — the same rule as the menu row')
2305
+ assert.equal(deleteCalls(harness), 0, 'and nothing is deleted yet')
2306
+ })
2307
+
2308
+ test('dropping deletes straight away when asking is off', async () => {
2309
+ const harness = await loadPlugin()
2310
+ localStorage.setItem(SKIP_KEY, '1')
2311
+ const row = mountRow(harness, SESSION_A)
2312
+
2313
+ startRowDrag(harness, row)
2314
+ await flush()
2315
+ bar(harness).props.onDrop(dropEvent())
2316
+ await flush()
2317
+
2318
+ assert.equal(deleteCalls(harness), 1, 'one delete goes out')
2319
+ assert.equal(dialogParts(harness), null, 'with no dialog')
2320
+ })
2321
+
2322
+ test('ending the drag without dropping puts the footer back', async () => {
2323
+ const harness = await loadPlugin()
2324
+ const row = mountRow(harness, SESSION_A)
2325
+
2326
+ startRowDrag(harness, row)
2327
+ await flush()
2328
+ harness.document.dispatchToDocument('dragend', { target: row, preventDefault() {}, stopPropagation() {} })
2329
+ await flush()
2330
+
2331
+ assert.equal(bar(harness), null, 'the footer is empty again')
2332
+ assert.equal(harness.document.body._classes.has('dsdel-selecting'), false, 'and the account row is back')
2333
+ })
2334
+
2335
+ test('a drag started during multi-select is left alone', async () => {
2336
+ const harness = await loadPlugin()
2337
+ mountRow(harness, SESSION_A)
2338
+ selectRow(harness).element.props.onSelect()
2339
+ await flush()
2340
+
2341
+ const barBefore = bar(harness)
2342
+ assert.match(String(barBefore.props.className), /\bdsdel-bar\b/)
2343
+
2344
+ startRowDrag(harness, harness.rows[0])
2345
+ await flush()
2346
+
2347
+ assert.doesNotMatch(String(bar(harness).props.className), /\bdsdel-drop\b/, 'reordering inside the mode is not a delete')
2348
+ })
2349
+
2350
+ // --- performance: the decorate pass must stay cheap --------------------------
2351
+
2352
+ test('the row observer watches the session list, not the whole document', async () => {
2353
+ const harness = await loadPlugin()
2354
+ const list = mountList(harness)
2355
+ mountRow(harness, SESSION_A, list)
2356
+ selectRow(harness).element.props.onSelect()
2357
+ await flush()
2358
+
2359
+ const observer = harness.observers[0]
2360
+ assert.ok(observer, 'multi-select installs one observer')
2361
+ // Observing `document.body` means every streamed token in the transcript wakes
2362
+ // this callback. The list is the only subtree that can gain or lose rows.
2363
+ assert.equal(
2364
+ observer.target,
2365
+ list,
2366
+ 'the list container is the observation root',
2367
+ )
2368
+ })
2369
+
2370
+ test('a mutation with no added or removed nodes schedules no pass', async () => {
2371
+ const harness = await loadPlugin()
2372
+ mountRow(harness, SESSION_A)
2373
+ selectRow(harness).element.props.onSelect()
2374
+ await flush()
2375
+
2376
+ const observer = harness.observers[0]
2377
+ let scanned = 0
2378
+ const realQuery = harness.document.querySelectorAll.bind(harness.document)
2379
+ harness.document.querySelectorAll = (selector) => {
2380
+ if (selector === '[role="treeitem"]') scanned += 1
2381
+ return realQuery(selector)
2382
+ }
2383
+
2384
+ // A character-data mutation: text streaming, nothing structural.
2385
+ observer.callback([{ type: 'characterData', addedNodes: [], removedNodes: [] }])
2386
+ await flush()
2387
+ assert.equal(scanned, 0, 'no row scan for a text-only mutation')
2388
+
2389
+ // A structural one still has to run.
2390
+ observer.callback([{ type: 'childList', addedNodes: [harness.rows[0]], removedNodes: [] }])
2391
+ await flush()
2392
+ assert.equal(scanned, 1, 'one scan when a row arrives')
2393
+ })
2394
+
2395
+ test('the decorate pass does not run a selector engine over every row', async () => {
2396
+ const harness = await loadPlugin()
2397
+ mountRow(harness, SESSION_A)
2398
+ selectRow(harness).element.props.onSelect()
2399
+ await flush()
2400
+
2401
+ const row = harness.rows[0]
2402
+ let rowSelectors = 0
2403
+ const realQuery = row.querySelector.bind(row)
2404
+ row.querySelector = (selector) => { rowSelectors += 1; return realQuery(selector) }
2405
+
2406
+ harness.observers[0].callback([{ type: 'childList', addedNodes: [row], removedNodes: [] }])
2407
+ await flush()
2408
+
2409
+ // The checkbox is always the first element child; a per-row selector call per
2410
+ // frame is the pass's dominant cost with a long list.
2411
+ assert.equal(rowSelectors, 0, 'the checkbox is found without querying')
2412
+ })
2413
+
2414
+ test('a browse-only host falls back to the in-page picker too', async () => {
2415
+ // The host answered `browse`: it has a listing API but no OS chooser to raise.
2416
+ // That is the normal shape for a remote client, so it must not read as an error.
2417
+ const listing = { path: 'C:/ws', parent: 'C:/', entries: [], roots: ['C:/'], truncated: false, defaultRoot: 'C:/home' }
2418
+ const harness = await loadPlugin({ browse: listing, pick: { browse: true } })
2419
+ localStorage.setItem(OFF_KEY, '0')
2420
+ localStorage.setItem(ROOT_KEY, 'C:/ws')
2421
+
2422
+ pageRootBrowse(harness).props.onClick()
2423
+ await tick()
2424
+ await tick()
2425
+
2426
+ assert.ok(pickerElement(harness), 'the in-page picker opened')
2427
+ assert.ok(browseRequests(harness).length > 0, 'and it asked the host for a listing')
2428
+ })
2429
+
2430
+ test('a failing folder dialog says so instead of blaming the delete', async () => {
2431
+ const harness = await loadPlugin()
2432
+ localStorage.setItem(OFF_KEY, '0')
2433
+ const originalFetch = globalThis.fetch
2434
+ globalThis.fetch = async (url, init) => {
2435
+ if (String(url).endsWith('/dsh-delete-session/pick')) {
2436
+ harness.requests.push({ url: String(url), init })
2437
+ return {
2438
+ ok: false,
2439
+ status: 500,
2440
+ json: async () => ({ ok: false, code: 'shell', error: "Cannot read properties of undefined (reading 'aborted')" }),
2441
+ }
2442
+ }
2443
+ return originalFetch(url, init)
2444
+ }
2445
+
2446
+ pageRootBrowse(harness).props.onClick()
2447
+ await tick()
2448
+ await tick()
2449
+
2450
+ const last = toasts(harness).at(-1)
2451
+ assert.match(last, /选择文件夹失败/, 'the picker is named')
2452
+ assert.doesNotMatch(last, /删除失败/, 'and deleting is not blamed for it')
2453
+ assert.match(last, /aborted/, 'the host message is still carried through')
2454
+ })
2455
+
2456
+ // --- first install: the four defaults --------------------------------------
2457
+
2458
+ test('a fresh install ticks all four delete switches', async () => {
2459
+ // No storage at all: this is what a new user sees on the plugin page.
2460
+ const harness = await loadPlugin()
2461
+
2462
+ const expected = [
2463
+ ['ask', '删除时询问', 'ask first'],
2464
+ ['recycle', '删除至回收站', 'recycle bin'],
2465
+ ['syncChildren', '同时删除子会话', 'take the subagent sessions along'],
2466
+ ['off', '不处理产出文件(保持当前行为)', 'leave produced files alone'],
2467
+ ]
2468
+
2469
+ for (const [key, label, why] of expected) {
2470
+ const box = pageCheck(harness, key)
2471
+ assert.ok(box, key + ' renders')
2472
+ assert.equal(box.props.label, label)
2473
+ assert.equal(box.props.checked, true, why)
2474
+ }
2475
+ })
2476
+
2477
+ test('unticking "take the child sessions along" is remembered', async () => {
2478
+ const harness = await loadPlugin()
2479
+ const box = pageCheck(harness, 'syncChildren')
2480
+ assert.equal(box.props.checked, true, 'on by default')
2481
+
2482
+ box.props.onChange(false)
2483
+ // The default is now "on", so the off choice has to be written explicitly:
2484
+ // deleting the key would read back as on again.
2485
+ assert.equal(localStorage.getItem(SYNC_CHILDREN_KEY), '0', 'off is written, not cleared')
2486
+ })
2487
+
2488
+ test('a fresh install deletes the children without listing them', async () => {
2489
+ const harness = await loadPlugin({ children: CHILDREN })
2490
+ mountRow(harness, SESSION_A)
2491
+
2492
+ const { element } = deleteRow(harness, SESSION_A)
2493
+ element.props.onSelect({ shiftKey: false })
2494
+ await flush()
2495
+
2496
+ assert.deepEqual(childChecks(harness), [], 'no per-child ticks to narrow, because they all go')
2497
+ dialogParts(harness).confirm.props.onClick()
2498
+ await flush()
2499
+
2500
+ assert.deepEqual(
2501
+ deleteBody(harness).childSessionIds,
2502
+ ['session-child-one', 'session-child-two'],
2503
+ 'and the delete carries them',
2504
+ )
2505
+ })