dsh-delete-session 0.2.8 → 0.3.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.
@@ -21,6 +21,7 @@
21
21
  //
22
22
  // node --test "test/*.test.js"
23
23
  import assert from 'node:assert/strict'
24
+ import { readFile } from 'node:fs/promises'
24
25
  import { test } from 'node:test'
25
26
 
26
27
  // --- a DOM small enough to read, complete enough to run the plugin ----------
@@ -103,6 +104,15 @@ class StubElement {
103
104
  }
104
105
 
105
106
  insertBefore(child, reference) {
107
+ // A real DOM MOVES a node it already contains; it never keeps two copies.
108
+ // The plugin repositions its spacer and its select-all box on every decorate
109
+ // pass, so a stub that appended instead of moved doubled them: removing one
110
+ // copy left the other behind and made the cleanup look broken.
111
+ const previous = child.parentElement
112
+ if (previous !== null) {
113
+ const at = previous.children.indexOf(child)
114
+ if (at !== -1) previous.children.splice(at, 1)
115
+ }
106
116
  child.parentElement = this
107
117
  const index = reference === null || reference === undefined ? this.children.length : this.children.indexOf(reference)
108
118
  if (index === -1) this.children.push(child)
@@ -215,6 +225,14 @@ const SESSION_B = { id: 'session-bbbb1111-2222-4333-8444-555555555555', title: '
215
225
 
216
226
  let bundleFactory = null
217
227
 
228
+ // Every `loadPlugin()` swaps `globalThis.document`, but the plugin instances of
229
+ // earlier tests are still alive and their queued rAF callbacks call
230
+ // `document.querySelectorAll(...)` on whatever document is installed WHEN THEY
231
+ // FIRE. A pass that lands after the swap decorates the next test's DOM, which
232
+ // is what made three tests flaky. Each harness registers its queue here so the
233
+ // next one can drop the leftovers.
234
+ const rafQueues = []
235
+
218
236
  /**
219
237
  * The bundle registers through `window.__ModuleLoader__` once per process and ESM
220
238
  * caching means a second `import()` never re-runs it. Caching the factory is also
@@ -297,8 +315,11 @@ async function loadPlugin(options = {}) {
297
315
  // synchronous stub would leave `decorateRaf` permanently truthy and silently
298
316
  // swallow every later pass. That is a harness artefact, and it would hide the
299
317
  // very regression this file exists to catch.
318
+ // Drop anything an earlier instance queued: it would run against THIS dom.
319
+ for (const stale of rafQueues) stale.clear()
300
320
  let rafId = 0
301
321
  const rafQueue = new Map()
322
+ rafQueues.push(rafQueue)
302
323
  globalThis.requestAnimationFrame = (fn) => {
303
324
  const id = ++rafId
304
325
  rafQueue.set(id, fn)
@@ -572,7 +593,14 @@ const clickBar = (node) => node.props.onClick({ stopPropagation() {} })
572
593
  const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
573
594
 
574
595
  /** Let the queued rAF decorate passes drain. */
575
- const flush = async () => { await tick() }
596
+ const flush = async () => { await tick() }
597
+
598
+ /**
599
+ * Drain until nothing is left. `flush()` is one tick, and a decorate pass can
600
+ * queue another callback while it runs; a test that snapshots a counter right
601
+ * after a single flush occasionally catches a straggler.
602
+ */
603
+ const settle = async () => { for (let i = 0; i < 20; i += 1) await flush() }
576
604
 
577
605
  /**
578
606
  * Fire the observers multi-select installed. Outside the mode the plugin installs
@@ -669,6 +697,41 @@ test('the locale namespace is registered even when the locale service arrives la
669
697
  assert.equal(selectRow(harness).element.props.children, '多选')
670
698
  })
671
699
 
700
+ test('the Russian dictionary ships with every key the other dictionaries carry', async () => {
701
+ const harness = await loadPlugin()
702
+ const { ru, zh, en } = harness.locales[NS]
703
+ assert.ok(ru, 'ru reaches the host locale namespace')
704
+ assert.equal(ru.menu, 'Удалить сессию')
705
+ assert.equal(ru.confirm, 'Удалить')
706
+ // A key missing here would fall back to Chinese mid-dialog, so the key sets
707
+ // have to match exactly rather than merely overlap.
708
+ const keys = (dict) => Object.keys(dict).sort()
709
+ assert.deepEqual(keys(ru), keys(zh), 'ru covers exactly the zh key set')
710
+ assert.deepEqual(keys(en), keys(zh), 'en still covers the zh key set')
711
+ })
712
+
713
+ test('a Russian browser gets Russian labels from the built-in dictionary', async () => {
714
+ const harness = await loadPlugin()
715
+ const previous = globalThis.navigator
716
+ Object.defineProperty(globalThis, 'navigator', {
717
+ value: { language: 'ru-RU', languages: ['ru-RU'] },
718
+ configurable: true,
719
+ writable: true,
720
+ })
721
+ try {
722
+ // The host translator in this harness answers in zh; the rows must prefer the
723
+ // plugin's own Russian dictionary instead of following it.
724
+ assert.equal(deleteRow(harness).element.props.children, 'Удалить сессию')
725
+ assert.equal(selectRow(harness).element.props.children, 'Выбрать несколько')
726
+ } finally {
727
+ Object.defineProperty(globalThis, 'navigator', {
728
+ value: previous,
729
+ configurable: true,
730
+ writable: true,
731
+ })
732
+ }
733
+ })
734
+
672
735
  test('the delete row asks the host for its danger styling', async () => {
673
736
  const harness = await loadPlugin()
674
737
  const { element } = deleteRow(harness)
@@ -2815,4 +2878,704 @@ test('a button-shaped footer entry steps aside without hiding the bar itself', a
2815
2878
  /\[data-slot="sidebar\.footer\.action"\] > button\{display:none\}/,
2816
2879
  'never hide every button unconditionally',
2817
2880
  )
2818
- })
2881
+ })
2882
+
2883
+ test('the drop label names the conversation instead of printing the placeholder', async () => {
2884
+ const harness = await loadPlugin()
2885
+ const row = mountRow(harness, SESSION_A)
2886
+
2887
+ startRowDrag(harness, row)
2888
+ await flush()
2889
+ bar(harness).props.onDragOver(dropEvent())
2890
+ await flush()
2891
+
2892
+ const node = bar(harness)
2893
+ const text = JSON.stringify(node.props.children)
2894
+
2895
+ // The dictionary entry is `松手删除「{name}」`, but the interpolation map carries
2896
+ // `n`/`done`/`total`/`ok`/`fail`/`path` — not `name`. Passing the title as a bare
2897
+ // string therefore left the braces in the label.
2898
+ assert.doesNotMatch(text, /\{name\}/, 'no unsubstituted placeholder in the label')
2899
+ assert.match(text, /松手删除/, 'the sentence is still there')
2900
+ })
2901
+
2902
+ test('the drop label carries the dragged conversation title', async () => {
2903
+ const harness = await loadPlugin()
2904
+ const row = mountRow(harness, SESSION_A)
2905
+ row.textContent = 'BiliLearn-AI 仓库'
2906
+
2907
+ startRowDrag(harness, row)
2908
+ await flush()
2909
+ bar(harness).props.onDragOver(dropEvent())
2910
+ await flush()
2911
+
2912
+ const text = JSON.stringify(bar(harness).props.children)
2913
+ assert.match(text, /BiliLearn-AI 仓库/, 'the title reaches the sentence')
2914
+ })
2915
+ test('a workspace header offers a select-all box', async () => {
2916
+ const harness = await loadPlugin()
2917
+ const section = harness.document.createElement('div')
2918
+ section.setAttribute('role', 'group')
2919
+
2920
+ const header = harness.document.createElement('div')
2921
+ header.setAttribute('role', 'treeitem')
2922
+ header.setAttribute('data-row-key', 'workspace:ws-a')
2923
+ section.appendChild(header)
2924
+
2925
+ mountRow(harness, SESSION_A, section)
2926
+ mountRow(harness, SESSION_B, section)
2927
+ harness.document.body.appendChild(section)
2928
+
2929
+ await enterMulti(harness)
2930
+ observerPass(harness)
2931
+ await flush()
2932
+
2933
+ const box = header.querySelector('.dsdel-select-all')
2934
+ assert.ok(box, 'the header carries one')
2935
+ assert.equal(box.getAttribute('role'), 'checkbox')
2936
+ assert.equal(box.getAttribute('aria-checked'), 'false', 'nothing picked yet')
2937
+ })
2938
+
2939
+ test('the workspace box selects only its own sessions', async () => {
2940
+ const harness = await loadPlugin()
2941
+ const sectionA = harness.document.createElement('div')
2942
+ sectionA.setAttribute('role', 'group')
2943
+ const headA = harness.document.createElement('div')
2944
+ headA.setAttribute('role', 'treeitem')
2945
+ headA.setAttribute('data-row-key', 'workspace:ws-a')
2946
+ sectionA.appendChild(headA)
2947
+ mountRow(harness, SESSION_A, sectionA)
2948
+ harness.document.body.appendChild(sectionA)
2949
+
2950
+ const sectionB = harness.document.createElement('div')
2951
+ sectionB.setAttribute('role', 'group')
2952
+ const headB = harness.document.createElement('div')
2953
+ headB.setAttribute('role', 'treeitem')
2954
+ headB.setAttribute('data-row-key', 'workspace:ws-b')
2955
+ sectionB.appendChild(headB)
2956
+ mountRow(harness, SESSION_B, sectionB)
2957
+ harness.document.body.appendChild(sectionB)
2958
+
2959
+ await enterMulti(harness)
2960
+ observerPass(harness)
2961
+ await flush()
2962
+
2963
+ headA.querySelector('.dsdel-select-all').fire('click')
2964
+ await flush()
2965
+
2966
+ assert.equal(headA.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true', 'A turned on')
2967
+ const boxB = headB.querySelector('.dsdel-select-all')
2968
+ assert.equal(boxB.getAttribute('aria-checked'), 'false', 'B untouched')
2969
+ })
2970
+
2971
+ test('a second press clears the workspace again', async () => {
2972
+ const harness = await loadPlugin()
2973
+ const section = harness.document.createElement('div')
2974
+ section.setAttribute('role', 'group')
2975
+ const header = harness.document.createElement('div')
2976
+ header.setAttribute('role', 'treeitem')
2977
+ header.setAttribute('data-row-key', 'workspace:ws-a')
2978
+ section.appendChild(header)
2979
+ mountRow(harness, SESSION_A, section)
2980
+ harness.document.body.appendChild(section)
2981
+
2982
+ await enterMulti(harness)
2983
+ observerPass(harness)
2984
+ await flush()
2985
+
2986
+ const box = header.querySelector('.dsdel-select-all')
2987
+ box.fire('click')
2988
+ await flush()
2989
+ assert.equal(box.getAttribute('aria-checked'), 'true')
2990
+
2991
+ box.fire('click')
2992
+ await flush()
2993
+ assert.equal(box.getAttribute('aria-checked'), 'false')
2994
+ })
2995
+
2996
+ test('the box reports a partly picked workspace as mixed', async () => {
2997
+ const harness = await loadPlugin()
2998
+ const section = harness.document.createElement('div')
2999
+ section.setAttribute('role', 'group')
3000
+ const header = harness.document.createElement('div')
3001
+ header.setAttribute('role', 'treeitem')
3002
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3003
+ section.appendChild(header)
3004
+ const rowA = mountRow(harness, SESSION_A, section)
3005
+ mountRow(harness, SESSION_B, section)
3006
+ harness.document.body.appendChild(section)
3007
+
3008
+ await enterMulti(harness)
3009
+ observerPass(harness)
3010
+ await flush()
3011
+
3012
+ clickRowIn(harness, rowA)
3013
+ observerPass(harness)
3014
+ await flush()
3015
+
3016
+ assert.equal(header.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'mixed')
3017
+ })
3018
+
3019
+ test('the heading row selects every workspace at once', async () => {
3020
+ const harness = await loadPlugin()
3021
+ const heading = harness.document.createElement('div')
3022
+ heading.setAttribute('data-sidebar-workspace-header', '')
3023
+ harness.document.body.appendChild(heading)
3024
+
3025
+ const sectionA = harness.document.createElement('div')
3026
+ sectionA.setAttribute('role', 'group')
3027
+ const headA = harness.document.createElement('div')
3028
+ headA.setAttribute('role', 'treeitem')
3029
+ headA.setAttribute('data-row-key', 'workspace:ws-a')
3030
+ sectionA.appendChild(headA)
3031
+ mountRow(harness, SESSION_A, sectionA)
3032
+ harness.document.body.appendChild(sectionA)
3033
+
3034
+ const sectionB = harness.document.createElement('div')
3035
+ sectionB.setAttribute('role', 'group')
3036
+ const headB = harness.document.createElement('div')
3037
+ headB.setAttribute('role', 'treeitem')
3038
+ headB.setAttribute('data-row-key', 'workspace:ws-b')
3039
+ sectionB.appendChild(headB)
3040
+ mountRow(harness, SESSION_B, sectionB)
3041
+ harness.document.body.appendChild(sectionB)
3042
+
3043
+ await enterMulti(harness)
3044
+ observerPass(harness)
3045
+ await flush()
3046
+
3047
+ const all = heading.querySelector('.dsdel-select-all-workspaces')
3048
+ assert.ok(all, 'the heading carries the global box')
3049
+ all.fire('click')
3050
+ await flush()
3051
+
3052
+ assert.equal(all.getAttribute('aria-checked'), 'true', 'everything picked')
3053
+ assert.equal(headA.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true')
3054
+ assert.equal(headB.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true')
3055
+ })
3056
+
3057
+ test('both boxes go away when the mode ends', async () => {
3058
+ const harness = await loadPlugin()
3059
+ const heading = harness.document.createElement('div')
3060
+ heading.setAttribute('data-sidebar-workspace-header', '')
3061
+ harness.document.body.appendChild(heading)
3062
+
3063
+ const section = harness.document.createElement('div')
3064
+ section.setAttribute('role', 'group')
3065
+ const header = harness.document.createElement('div')
3066
+ header.setAttribute('role', 'treeitem')
3067
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3068
+ section.appendChild(header)
3069
+ mountRow(harness, SESSION_A, section)
3070
+ harness.document.body.appendChild(section)
3071
+
3072
+ await enterMulti(harness)
3073
+ observerPass(harness)
3074
+ await flush()
3075
+ assert.ok(header.querySelector('.dsdel-select-all'), 'present during the mode')
3076
+ assert.ok(heading.querySelector('.dsdel-select-all-workspaces'))
3077
+
3078
+ harness.document.dispatchToDocument('keydown', { key: 'Escape', stopPropagation() {} })
3079
+ observerPass(harness)
3080
+ await flush()
3081
+
3082
+ assert.equal(header.querySelector('.dsdel-select-all'), null, 'removed with the mode')
3083
+ assert.equal(heading.querySelector('.dsdel-select-all-workspaces'), null)
3084
+ })
3085
+
3086
+ test('the select-all boxes do not lean on the page token scope', async () => {
3087
+ const harness = await loadPlugin()
3088
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3089
+
3090
+ // The boxes render inside the SIDEBAR. The `--dsdel-*` tokens are declared on
3091
+ // `.dsdel-page`, `.dsdel-modal` and `.dsdel-bar` only, so any rule outside those
3092
+ // scopes that reads them gets an invalid value and loses the whole declaration.
3093
+ // That is how the first version shipped an invisible button: `border` was
3094
+ // dropped, leaving no box and nothing to click.
3095
+ const rules = [...css.matchAll(/(?:^|\})\s*\.dsdel-select-all[^{]*\{([^}]*)\}/g)]
3096
+ assert.ok(rules.length > 0, 'the rules are in the stylesheet')
3097
+ for (const match of rules) {
3098
+ assert.equal(match[1].includes('--dsdel-'), false,
3099
+ 'a sidebar rule must not read the page tokens: ' + match[0].slice(0, 60))
3100
+ }
3101
+
3102
+ // Every host alias carries a literal fallback, so the box still draws when the
3103
+ // host renames its palette.
3104
+ assert.match(css, /\.dsdel-select-all[^}]*var\(--dsw-alias-border-l2,[^)]*\)/, 'border has a fallback')
3105
+ assert.match(css, /\.dsdel-select-all[^}]*var\(--dsw-alias-brand-primary,[^)]*\)/, 'brand has a fallback')
3106
+ })
3107
+
3108
+ test('a box always keeps a visible 15px square', async () => {
3109
+ const harness = await loadPlugin()
3110
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3111
+
3112
+ // A flex child with no width collapses to its content, and the empty state has
3113
+ // no content: `width` plus `min-width` plus content-box sizing is what makes the
3114
+ // square exist before anything is ticked.
3115
+ assert.match(css, /\.dsdel-select-all[^}]*width:15px/, 'a fixed width')
3116
+ assert.match(css, /\.dsdel-select-all[^}]*height:15px/, 'and a fixed height')
3117
+ assert.match(css, /\.dsdel-select-all[^}]*min-width:15px/, 'that flex cannot shrink away')
3118
+ assert.match(css, /\.dsdel-select-all[^}]*box-sizing:border-box/, 'with the border inside it')
3119
+ })
3120
+
3121
+ test('the workspace box sits beside the name, not at the row edge', async () => {
3122
+ const harness = await loadPlugin()
3123
+ const section = harness.document.createElement('div')
3124
+ section.setAttribute('role', 'group')
3125
+
3126
+ const header = harness.document.createElement('div')
3127
+ header.setAttribute('role', 'treeitem')
3128
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3129
+
3130
+ // The host row: label block, then the action cluster (which carries margin-left:auto).
3131
+ const label = harness.document.createElement('span')
3132
+ label.className = 'YDXeBa_projectText'
3133
+ header.appendChild(label)
3134
+ const actions = harness.document.createElement('span')
3135
+ actions.className = 'YDXeBa_rowActions'
3136
+ header.appendChild(actions)
3137
+
3138
+ section.appendChild(header)
3139
+ mountRow(harness, SESSION_A, section)
3140
+ harness.document.body.appendChild(section)
3141
+
3142
+ await enterMulti(harness)
3143
+ observerPass(harness)
3144
+ await flush()
3145
+
3146
+ const box = header.querySelector('.dsdel-select-all')
3147
+ assert.ok(box, 'the box exists')
3148
+
3149
+ // Appending put it after the action cluster, whose auto margin pushes it into the
3150
+ // far right of the row — that is the "靠右" the screenshot showed. It has to be
3151
+ // inserted before the actions instead.
3152
+ const index = header.children.indexOf(box)
3153
+ const actionsIndex = header.children.indexOf(actions)
3154
+ assert.equal(index < actionsIndex, true, 'the box comes before the action cluster')
3155
+ assert.equal(header.children[index - 1].className, 'YDXeBa_projectText', 'and right after the label')
3156
+ })
3157
+
3158
+ test('the global box sits after the heading label, before the search slot', async () => {
3159
+ const harness = await loadPlugin()
3160
+
3161
+ const heading = harness.document.createElement('div')
3162
+ heading.setAttribute('data-sidebar-workspace-header', '')
3163
+
3164
+ const label = harness.document.createElement('span')
3165
+ label.className = 'bhn1Oq_sectionLabel'
3166
+ heading.appendChild(label)
3167
+ const search = harness.document.createElement('div')
3168
+ search.className = 'bhn1Oq_searchSlot'
3169
+ heading.appendChild(search)
3170
+
3171
+ harness.document.body.appendChild(heading)
3172
+
3173
+ const section = harness.document.createElement('div')
3174
+ section.setAttribute('role', 'group')
3175
+ const header = harness.document.createElement('div')
3176
+ header.setAttribute('role', 'treeitem')
3177
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3178
+ section.appendChild(header)
3179
+ mountRow(harness, SESSION_A, section)
3180
+ harness.document.body.appendChild(section)
3181
+
3182
+ await enterMulti(harness)
3183
+ observerPass(harness)
3184
+ await flush()
3185
+
3186
+ const box = heading.querySelector('.dsdel-select-all-workspaces')
3187
+ assert.ok(box, 'the global box exists')
3188
+ const index = heading.children.indexOf(box)
3189
+ assert.equal(index < heading.children.indexOf(search), true, 'before the search slot')
3190
+ assert.equal(heading.children[index - 1].className, 'bhn1Oq_sectionLabel', 'and after the label')
3191
+ })
3192
+
3193
+ test('a box left at the wrong edge is moved on the next pass', async () => {
3194
+ const harness = await loadPlugin()
3195
+ const section = harness.document.createElement('div')
3196
+ section.setAttribute('role', 'group')
3197
+
3198
+ const header = harness.document.createElement('div')
3199
+ header.setAttribute('role', 'treeitem')
3200
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3201
+
3202
+ const label = harness.document.createElement('span')
3203
+ label.className = 'wnFoaW_projectText'
3204
+ header.appendChild(label)
3205
+ const actions = harness.document.createElement('span')
3206
+ actions.className = 'wnFoaW_rowActions'
3207
+ header.appendChild(actions)
3208
+
3209
+ // An earlier build left its box AFTER the actions: the far edge.
3210
+ const stale = harness.document.createElement('button')
3211
+ stale.className = 'dsdel-select-all'
3212
+ header.appendChild(stale)
3213
+
3214
+ section.appendChild(header)
3215
+ mountRow(harness, SESSION_A, section)
3216
+ harness.document.body.appendChild(section)
3217
+
3218
+ await enterMulti(harness)
3219
+ observerPass(harness)
3220
+ await flush()
3221
+
3222
+ // The insert used to live inside the "created" branch, so an existing box was
3223
+ // never repositioned: the heading box moved and the workspace box stayed put.
3224
+ const index = header.children.indexOf(stale)
3225
+ assert.equal(index < header.children.indexOf(actions), true, 'moved before the actions')
3226
+ assert.equal(header.children[index - 1].className, 'wnFoaW_projectText', 'and beside the label')
3227
+ })
3228
+
3229
+ test('the workspace name stops absorbing the row while the mode runs', async () => {
3230
+ const harness = await loadPlugin()
3231
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3232
+
3233
+ // `.projectText` carries `flex:1`, so it fills whatever the actions do not take.
3234
+ // A box inserted after it therefore lands at the row's far edge even though it
3235
+ // is early in the child list — that is why the workspace box stayed right while
3236
+ // the header box (whose label is not flexed) moved. The rule only applies while
3237
+ // the mode runs, so the host keeps its normal layout otherwise.
3238
+ assert.match(css, /body\.dsdel-selecting \[class\*="_projectText"\]\{[^}]*flex:0 1 auto/,
3239
+ 'the name sizes to its text during the mode')
3240
+ assert.match(css, /body\.dsdel-selecting \[class\*="_projectText"\]\{[^}]*min-width:0/,
3241
+ 'and can still truncate a long name')
3242
+ })
3243
+
3244
+ test('a spacer keeps the right-hand cluster in place', async () => {
3245
+ const harness = await loadPlugin()
3246
+ const section = harness.document.createElement('div')
3247
+ section.setAttribute('role', 'group')
3248
+
3249
+ const header = harness.document.createElement('div')
3250
+ header.setAttribute('role', 'treeitem')
3251
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3252
+
3253
+ const label = harness.document.createElement('span')
3254
+ label.className = 'wnFoaW_projectText'
3255
+ header.appendChild(label)
3256
+ // The cluster that must NOT move: count, menu, new-session.
3257
+ const actions = harness.document.createElement('span')
3258
+ actions.className = 'wnFoaW_rowActions'
3259
+ header.appendChild(actions)
3260
+
3261
+ section.appendChild(header)
3262
+ mountRow(harness, SESSION_A, section)
3263
+ harness.document.body.appendChild(section)
3264
+
3265
+ await enterMulti(harness)
3266
+ observerPass(harness)
3267
+ await flush()
3268
+
3269
+ // Order has to be: label, box, spacer, actions. With the spacer growing, the
3270
+ // name keeps its natural width and the cluster stays at the row's end; without
3271
+ // it, every flex child simply packs left and the count/menu/new buttons slid
3272
+ // over with the box.
3273
+ const kids = header.children
3274
+ const box = header.querySelector('.dsdel-select-all')
3275
+ const spacer = header.querySelector('.dsdel-flex-spacer')
3276
+ assert.ok(box, 'the box exists')
3277
+ assert.ok(spacer, 'the spacer exists')
3278
+
3279
+ assert.equal(kids.indexOf(box) < kids.indexOf(spacer), true, 'box before spacer')
3280
+ assert.equal(kids.indexOf(spacer) < kids.indexOf(actions), true, 'spacer before the cluster')
3281
+ assert.equal(kids.indexOf(label) < kids.indexOf(box), true, 'label first')
3282
+ })
3283
+
3284
+ test('the spacer is removed with the mode', async () => {
3285
+ const harness = await loadPlugin()
3286
+ const section = harness.document.createElement('div')
3287
+ section.setAttribute('role', 'group')
3288
+ const header = harness.document.createElement('div')
3289
+ header.setAttribute('role', 'treeitem')
3290
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3291
+ const label = harness.document.createElement('span')
3292
+ label.className = 'wnFoaW_projectText'
3293
+ header.appendChild(label)
3294
+ const actions = harness.document.createElement('span')
3295
+ actions.className = 'wnFoaW_rowActions'
3296
+ header.appendChild(actions)
3297
+ section.appendChild(header)
3298
+ mountRow(harness, SESSION_A, section)
3299
+ harness.document.body.appendChild(section)
3300
+
3301
+ await enterMulti(harness)
3302
+ observerPass(harness)
3303
+ await flush()
3304
+ assert.ok(header.querySelector('.dsdel-flex-spacer'), 'spacer present during the mode')
3305
+
3306
+ // The bar's Cancel is the mode's own exit route. Escape would also reach
3307
+ // handlers left behind by earlier tests: the stub never unregisters one.
3308
+ clickBar(barCancel(harness))
3309
+ observerPass(harness)
3310
+ await flush()
3311
+ assert.equal(header.querySelector('.dsdel-flex-spacer'), null, 'gone with the mode')
3312
+ assert.equal(header.querySelector('.dsdel-select-all'), null, 'and so is the box')
3313
+ })
3314
+
3315
+ test('PROBE exit removes spacer', async () => {
3316
+ const harness = await loadPlugin()
3317
+ const section = harness.document.createElement('div')
3318
+ section.setAttribute('role', 'group')
3319
+ const header = harness.document.createElement('div')
3320
+ header.setAttribute('role', 'treeitem')
3321
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3322
+ const actions = harness.document.createElement('span')
3323
+ actions.className = 'wnFoaW_rowActions'
3324
+ header.appendChild(actions)
3325
+ section.appendChild(header)
3326
+ mountRow(harness, SESSION_A, section)
3327
+ harness.document.body.appendChild(section)
3328
+
3329
+ await enterMulti(harness)
3330
+ observerPass(harness)
3331
+ await flush()
3332
+
3333
+ const before = harness.document.querySelectorAll('.dsdel-flex-spacer').length
3334
+ console.log('PROBE 装饰后 spacer 数:', before)
3335
+ for (const el of harness.document.querySelectorAll('.dsdel-flex-spacer')) {
3336
+ console.log('PROBE spacer parent:', el.parentElement && el.parentElement.getAttribute('data-row-key'), 'idx:', el.parentElement ? el.parentElement.children.indexOf(el) : -1)
3337
+ }
3338
+ const cancel = barCancel(harness)
3339
+ console.log('PROBE cancel 节点:', cancel && cancel.props && cancel.props.className)
3340
+ clickBar(cancel)
3341
+ await flush()
3342
+ console.log('PROBE 退出后 spacer 数:', harness.document.querySelectorAll('.dsdel-flex-spacer').length)
3343
+ assert.ok(true)
3344
+ })
3345
+
3346
+ test('the spacer is the growing child, so the cluster keeps its seat', async () => {
3347
+ const harness = await loadPlugin()
3348
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3349
+
3350
+ // Without a growing child the flex row simply packs left, which drags the count
3351
+ // and the action buttons along with the box. The spacer is what holds them at
3352
+ // the row's end while the box sits beside the name.
3353
+ assert.match(css, /\.dsdel-flex-spacer\{[^}]*flex:1/, 'the spacer grows')
3354
+ assert.match(css, /\.dsdel-flex-spacer\{[^}]*min-width:0/, 'and can collapse when space is tight')
3355
+ assert.match(css, /\.dsdel-select-all[^}]*flex:0 0 auto/, 'the box itself does not grow')
3356
+ })
3357
+
3358
+ test('a document click on a plugin-drawn element leaves the session alone', async () => {
3359
+ const harness = await loadPlugin()
3360
+ const row = mountRow(harness, SESSION_A)
3361
+ await enterMulti(harness)
3362
+ observerPass(harness)
3363
+ await flush()
3364
+ assert.equal(harness.document.body.className.includes('dsdel-selecting'), true)
3365
+
3366
+ // The document capture listener runs before any element handler. It must ignore
3367
+ // elements this plugin drew — a box inside a workspace row is the everyday case,
3368
+ // and inside a session row it would otherwise toggle that session.
3369
+ const box = harness.document.createElement('button')
3370
+ box.className = 'dsdel-select-all'
3371
+ box.setAttribute('data-dsh-delete-session', 'select-all')
3372
+ row.appendChild(box)
3373
+
3374
+ clickRowIn(harness, box)
3375
+ observerPass(harness)
3376
+ await flush()
3377
+
3378
+ const check = row.querySelector('.dsdel-check')
3379
+ assert.equal(check.classList.contains('dsdel-check-on'), false, 'no session was toggled')
3380
+ })
3381
+
3382
+ test('the row handler still works for a real session row', async () => {
3383
+ const harness = await loadPlugin()
3384
+ const row = mountRow(harness, SESSION_A)
3385
+ await enterMulti(harness)
3386
+ observerPass(harness)
3387
+ await flush()
3388
+
3389
+ clickRowIn(harness, row)
3390
+ await flush()
3391
+
3392
+ const check = row.querySelector('.dsdel-check')
3393
+ assert.equal(check.classList.contains('dsdel-check-on'), true, 'a plain row still toggles')
3394
+ })
3395
+
3396
+ test('a decorate pass with nothing to move keeps the box where it is', async () => {
3397
+ const harness = await loadPlugin()
3398
+ const section = harness.document.createElement('div')
3399
+ section.setAttribute('role', 'group')
3400
+ const header = harness.document.createElement('div')
3401
+ header.setAttribute('role', 'treeitem')
3402
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3403
+ const label = harness.document.createElement('span')
3404
+ label.className = 'wnFoaW_projectText'
3405
+ header.appendChild(label)
3406
+ const actions = harness.document.createElement('span')
3407
+ actions.className = 'wnFoaW_rowActions'
3408
+ header.appendChild(actions)
3409
+ section.appendChild(header)
3410
+ mountRow(harness, SESSION_A, section)
3411
+ harness.document.body.appendChild(section)
3412
+
3413
+ await enterMulti(harness)
3414
+ observerPass(harness)
3415
+ await flush()
3416
+
3417
+ // Every insertBefore is a mutation, and the list observer counted it as a
3418
+ // reason to scan again: each pass scheduled the next one, forever, and the box
3419
+ // was detached and re-attached under the pointer. Nothing may move here — the
3420
+ // box and its spacer are already in the right place.
3421
+ const box = header.querySelector('.dsdel-select-all')
3422
+ const spacer = header.querySelector('.dsdel-flex-spacer')
3423
+ const index = header.children.indexOf(box)
3424
+
3425
+ // Re-inserting a node where it already sits leaves its index unchanged, so the
3426
+ // index alone cannot prove the pass was quiet. Count the calls instead: a real
3427
+ // insertBefore produces a MutationRecord, and the observer used to feed on them.
3428
+ const realInsert = header.insertBefore.bind(header)
3429
+ let inserts = 0
3430
+ header.insertBefore = (child, ref) => { inserts += 1; return realInsert(child, ref) }
3431
+
3432
+ observerPass(harness)
3433
+ observerPass(harness)
3434
+ await flush()
3435
+
3436
+ assert.equal(inserts, 0, 'a settled row is left untouched')
3437
+
3438
+ assert.equal(header.children.indexOf(box), index, 'the box did not move')
3439
+ assert.equal(header.children.indexOf(spacer), index + 1, 'nor did the spacer')
3440
+ assert.equal(header.children.filter((el) => el === box).length, 1, 'and there is only one')
3441
+ })
3442
+
3443
+ test('a mutation naming only our own nodes schedules no pass', async () => {
3444
+ const harness = await loadPlugin()
3445
+
3446
+ // Drive the frame queue by hand: a timer-based flush made this test flaky,
3447
+ // because a pass can queue another callback while it runs.
3448
+ //
3449
+ // Note the drain: emptying the queue without running its callbacks leaves the
3450
+ // plugin's `decorateRaf` handle truthy, and every later `scheduleDecorate`
3451
+ // silently returns. That is a harness trap, not a product bug.
3452
+ const queued = []
3453
+ const realRaf = globalThis.requestAnimationFrame
3454
+ const realCancel = globalThis.cancelAnimationFrame
3455
+ globalThis.requestAnimationFrame = (fn) => { queued.push(fn); return queued.length }
3456
+ globalThis.cancelAnimationFrame = () => {}
3457
+ const drain = () => { let guard = 0; while (queued.length > 0 && guard < 50) { guard += 1; queued.shift()() } }
3458
+
3459
+ try {
3460
+ await enterMulti(harness)
3461
+ drain()
3462
+
3463
+ // A row that arrives after the last pass carries no checkbox, so whether a
3464
+ // pass runs is readable straight off the DOM.
3465
+ const section = harness.document.createElement('div')
3466
+ section.setAttribute('role', 'group')
3467
+ const hostRow = harness.document.createElement('div')
3468
+ hostRow.setAttribute('role', 'treeitem')
3469
+ hostRow.setAttribute('data-row-key', 'session:session-cccc1111-2222-4333-8444-555555555555')
3470
+ section.appendChild(hostRow)
3471
+ harness.document.body.appendChild(section)
3472
+ assert.equal(hostRow.querySelector('.dsdel-check'), null, 'the row is undecorated')
3473
+
3474
+ // Decorate inserts checkboxes, and the list observer reports them right back.
3475
+ // Every such record used to schedule another pass, so the loop never settled:
3476
+ // the box was re-inserted under the pointer and a click often missed.
3477
+ const own = harness.document.createElement('span')
3478
+ own.setAttribute('data-dsh-delete-session', 'check')
3479
+ harness.observers[0].callback([{ addedNodes: [own], removedNodes: [] }])
3480
+ assert.equal(queued.length, 0, 'our own node schedules nothing')
3481
+
3482
+ // A host change must still get through.
3483
+ harness.observers[0].callback([{ addedNodes: [hostRow], removedNodes: [] }])
3484
+ assert.equal(queued.length, 1, 'a real host row schedules one pass')
3485
+ drain()
3486
+ assert.ok(hostRow.querySelector('.dsdel-check'), 'and that pass decorates it')
3487
+ } finally {
3488
+ globalThis.requestAnimationFrame = realRaf
3489
+ globalThis.cancelAnimationFrame = realCancel
3490
+ }
3491
+ })
3492
+
3493
+ test('the plugin never calls array methods on a live child list', async () => {
3494
+ const source = await readFile(new URL('../src/client.js', import.meta.url), 'utf8')
3495
+
3496
+ // In a browser `element.children` is an HTMLCollection: it has `length` and
3497
+ // indexing, and none of the Array methods. The test stub uses a plain array, so
3498
+ // `children.indexOf(...)` passed here and threw a TypeError in the real host —
3499
+ // the decorate pass died before inserting anything, and the select-all box never
3500
+ // appeared. Index it by hand (`childIndex`) or spread it first.
3501
+ const offenders = []
3502
+ for (const match of source.matchAll(/\.children\.([A-Za-z]+)\s*\(/g)) {
3503
+ offenders.push(match[0])
3504
+ }
3505
+ assert.deepEqual(offenders, [], 'no Array methods on `children`')
3506
+ })
3507
+
3508
+ test('the workspace box reaches past a wrapping element', async () => {
3509
+ const harness = await loadPlugin()
3510
+ const section = harness.document.createElement('div')
3511
+ section.setAttribute('role', 'group')
3512
+
3513
+ // The real host wraps the workspace row in HoverCard's <span>, so the row's
3514
+ // parentElement is that span, not the section. A subtree query from the span
3515
+ // returns only the workspace row itself and the box ends up with no ids —
3516
+ // `toggleGroup` then returns without doing anything, which is exactly the
3517
+ // "single-workspace select-all does nothing" report.
3518
+ const wrapper = harness.document.createElement('span')
3519
+ wrapper.className = 'HoverCard_root'
3520
+ const header = harness.document.createElement('div')
3521
+ header.setAttribute('role', 'treeitem')
3522
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3523
+ const label = harness.document.createElement('span')
3524
+ label.className = 'wnFoaW_projectText'
3525
+ header.appendChild(label)
3526
+ const actions = harness.document.createElement('span')
3527
+ actions.className = 'wnFoaW_rowActions'
3528
+ header.appendChild(actions)
3529
+ wrapper.appendChild(header)
3530
+ section.appendChild(wrapper)
3531
+
3532
+ mountRow(harness, SESSION_A, section)
3533
+ mountRow(harness, SESSION_B, section)
3534
+ harness.document.body.appendChild(section)
3535
+
3536
+ await enterMulti(harness)
3537
+ observerPass(harness)
3538
+ await flush()
3539
+
3540
+ const box = header.querySelector('.dsdel-select-all')
3541
+ assert.ok(box, 'the box exists')
3542
+ assert.deepEqual(box.__dsdelIds, [SESSION_A.id, SESSION_B.id], 'both sessions were found')
3543
+
3544
+ box.fire('click')
3545
+ await flush()
3546
+ assert.equal(box.getAttribute('aria-checked'), 'true', 'and pressing it selects them')
3547
+ })
3548
+
3549
+ test('a workspace box never reaches into a nested workspace', async () => {
3550
+ const harness = await loadPlugin()
3551
+ const section = harness.document.createElement('div')
3552
+ section.setAttribute('role', 'group')
3553
+
3554
+ const header = harness.document.createElement('div')
3555
+ header.setAttribute('role', 'treeitem')
3556
+ header.setAttribute('data-row-key', 'workspace:outer')
3557
+ const actions = harness.document.createElement('span')
3558
+ actions.className = 'wnFoaW_rowActions'
3559
+ header.appendChild(actions)
3560
+ section.appendChild(header)
3561
+ mountRow(harness, SESSION_A, section)
3562
+
3563
+ // A nested workspace is a `div[role="group"]` holding its own rows.
3564
+ const nested = harness.document.createElement('div')
3565
+ nested.setAttribute('role', 'group')
3566
+ const nestedHeader = harness.document.createElement('div')
3567
+ nestedHeader.setAttribute('role', 'treeitem')
3568
+ nestedHeader.setAttribute('data-row-key', 'workspace:inner')
3569
+ nested.appendChild(nestedHeader)
3570
+ mountRow(harness, SESSION_B, nested)
3571
+ section.appendChild(nested)
3572
+
3573
+ harness.document.body.appendChild(section)
3574
+
3575
+ await enterMulti(harness)
3576
+ observerPass(harness)
3577
+ await flush()
3578
+
3579
+ const box = header.querySelector('.dsdel-select-all')
3580
+ assert.deepEqual(box.__dsdelIds, [SESSION_A.id], 'only the outer group\'s own session')
3581
+ })