dsh-delete-session 0.2.9 → 0.3.1

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,20 @@ 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
+
236
+ // Timers started by a plugin instance outlive the test that started them. The
237
+ // batch path schedules a 2.5 s settle, and its callback decorates whatever
238
+ // `globalThis.document` is when it fires - the next test's DOM. Track and
239
+ // clear them so one test cannot reach into another.
240
+ const pendingTimers = []
241
+
218
242
  /**
219
243
  * The bundle registers through `window.__ModuleLoader__` once per process and ESM
220
244
  * caching means a second `import()` never re-runs it. Caching the factory is also
@@ -297,8 +321,18 @@ async function loadPlugin(options = {}) {
297
321
  // synchronous stub would leave `decorateRaf` permanently truthy and silently
298
322
  // swallow every later pass. That is a harness artefact, and it would hide the
299
323
  // very regression this file exists to catch.
324
+ // Drop anything an earlier instance queued: it would run against THIS dom.
325
+ for (const stale of rafQueues) stale.clear()
326
+ while (pendingTimers.length > 0) clearTimeout(pendingTimers.pop())
327
+ const realSetTimeout = globalThis.setTimeout
328
+ globalThis.setTimeout = (fn, ms) => {
329
+ const id = realSetTimeout(fn, ms)
330
+ pendingTimers.push(id)
331
+ return id
332
+ }
300
333
  let rafId = 0
301
334
  const rafQueue = new Map()
335
+ rafQueues.push(rafQueue)
302
336
  globalThis.requestAnimationFrame = (fn) => {
303
337
  const id = ++rafId
304
338
  rafQueue.set(id, fn)
@@ -574,6 +608,13 @@ const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
574
608
  /** Let the queued rAF decorate passes drain. */
575
609
  const flush = async () => { await tick() }
576
610
 
611
+ /**
612
+ * Drain until nothing is left. `flush()` is one tick, and a decorate pass can
613
+ * queue another callback while it runs; a test that snapshots a counter right
614
+ * after a single flush occasionally catches a straggler.
615
+ */
616
+ const settle = async () => { for (let i = 0; i < 20; i += 1) await flush() }
617
+
577
618
  /**
578
619
  * Fire the observers multi-select installed. Outside the mode the plugin installs
579
620
  * none at all now that the menu is a slot rather than a portalled DOM node, so an
@@ -2850,37 +2891,773 @@ test('a button-shaped footer entry steps aside without hiding the bar itself', a
2850
2891
  /\[data-slot="sidebar\.footer\.action"\] > button\{display:none\}/,
2851
2892
  'never hide every button unconditionally',
2852
2893
  )
2894
+ })
2895
+
2896
+ test('the drop label names the conversation instead of printing the placeholder', async () => {
2897
+ const harness = await loadPlugin()
2898
+ const row = mountRow(harness, SESSION_A)
2899
+
2900
+ startRowDrag(harness, row)
2901
+ await flush()
2902
+ bar(harness).props.onDragOver(dropEvent())
2903
+ await flush()
2904
+
2905
+ const node = bar(harness)
2906
+ const text = JSON.stringify(node.props.children)
2907
+
2908
+ // The dictionary entry is `松手删除「{name}」`, but the interpolation map carries
2909
+ // `n`/`done`/`total`/`ok`/`fail`/`path` — not `name`. Passing the title as a bare
2910
+ // string therefore left the braces in the label.
2911
+ assert.doesNotMatch(text, /\{name\}/, 'no unsubstituted placeholder in the label')
2912
+ assert.match(text, /松手删除/, 'the sentence is still there')
2913
+ })
2914
+
2915
+ test('the drop label carries the dragged conversation title', async () => {
2916
+ const harness = await loadPlugin()
2917
+ const row = mountRow(harness, SESSION_A)
2918
+ row.textContent = 'BiliLearn-AI 仓库'
2919
+
2920
+ startRowDrag(harness, row)
2921
+ await flush()
2922
+ bar(harness).props.onDragOver(dropEvent())
2923
+ await flush()
2924
+
2925
+ const text = JSON.stringify(bar(harness).props.children)
2926
+ assert.match(text, /BiliLearn-AI 仓库/, 'the title reaches the sentence')
2927
+ })
2928
+ test('a workspace header offers a select-all box', async () => {
2929
+ const harness = await loadPlugin()
2930
+ const section = harness.document.createElement('div')
2931
+ section.setAttribute('role', 'group')
2932
+
2933
+ const header = harness.document.createElement('div')
2934
+ header.setAttribute('role', 'treeitem')
2935
+ header.setAttribute('data-row-key', 'workspace:ws-a')
2936
+ section.appendChild(header)
2937
+
2938
+ mountRow(harness, SESSION_A, section)
2939
+ mountRow(harness, SESSION_B, section)
2940
+ harness.document.body.appendChild(section)
2941
+
2942
+ await enterMulti(harness)
2943
+ observerPass(harness)
2944
+ await flush()
2945
+
2946
+ const box = header.querySelector('.dsdel-select-all')
2947
+ assert.ok(box, 'the header carries one')
2948
+ assert.equal(box.getAttribute('role'), 'checkbox')
2949
+ assert.equal(box.getAttribute('aria-checked'), 'false', 'nothing picked yet')
2950
+ })
2951
+
2952
+ test('the workspace box selects only its own sessions', async () => {
2953
+ const harness = await loadPlugin()
2954
+ const sectionA = harness.document.createElement('div')
2955
+ sectionA.setAttribute('role', 'group')
2956
+ const headA = harness.document.createElement('div')
2957
+ headA.setAttribute('role', 'treeitem')
2958
+ headA.setAttribute('data-row-key', 'workspace:ws-a')
2959
+ sectionA.appendChild(headA)
2960
+ mountRow(harness, SESSION_A, sectionA)
2961
+ harness.document.body.appendChild(sectionA)
2962
+
2963
+ const sectionB = harness.document.createElement('div')
2964
+ sectionB.setAttribute('role', 'group')
2965
+ const headB = harness.document.createElement('div')
2966
+ headB.setAttribute('role', 'treeitem')
2967
+ headB.setAttribute('data-row-key', 'workspace:ws-b')
2968
+ sectionB.appendChild(headB)
2969
+ mountRow(harness, SESSION_B, sectionB)
2970
+ harness.document.body.appendChild(sectionB)
2971
+
2972
+ await enterMulti(harness)
2973
+ observerPass(harness)
2974
+ await flush()
2975
+
2976
+ headA.querySelector('.dsdel-select-all').fire('click')
2977
+ await flush()
2978
+
2979
+ assert.equal(headA.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true', 'A turned on')
2980
+ const boxB = headB.querySelector('.dsdel-select-all')
2981
+ assert.equal(boxB.getAttribute('aria-checked'), 'false', 'B untouched')
2982
+ })
2983
+
2984
+ test('a second press clears the workspace again', async () => {
2985
+ const harness = await loadPlugin()
2986
+ const section = harness.document.createElement('div')
2987
+ section.setAttribute('role', 'group')
2988
+ const header = harness.document.createElement('div')
2989
+ header.setAttribute('role', 'treeitem')
2990
+ header.setAttribute('data-row-key', 'workspace:ws-a')
2991
+ section.appendChild(header)
2992
+ mountRow(harness, SESSION_A, section)
2993
+ harness.document.body.appendChild(section)
2994
+
2995
+ await enterMulti(harness)
2996
+ observerPass(harness)
2997
+ await flush()
2998
+
2999
+ const box = header.querySelector('.dsdel-select-all')
3000
+ box.fire('click')
3001
+ await flush()
3002
+ assert.equal(box.getAttribute('aria-checked'), 'true')
3003
+
3004
+ box.fire('click')
3005
+ await flush()
3006
+ assert.equal(box.getAttribute('aria-checked'), 'false')
3007
+ })
3008
+
3009
+ test('the box reports a partly picked workspace as mixed', async () => {
3010
+ const harness = await loadPlugin()
3011
+ const section = harness.document.createElement('div')
3012
+ section.setAttribute('role', 'group')
3013
+ const header = harness.document.createElement('div')
3014
+ header.setAttribute('role', 'treeitem')
3015
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3016
+ section.appendChild(header)
3017
+ const rowA = mountRow(harness, SESSION_A, section)
3018
+ mountRow(harness, SESSION_B, section)
3019
+ harness.document.body.appendChild(section)
3020
+
3021
+ await enterMulti(harness)
3022
+ observerPass(harness)
3023
+ await flush()
3024
+
3025
+ clickRowIn(harness, rowA)
3026
+ observerPass(harness)
3027
+ await flush()
3028
+
3029
+ assert.equal(header.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'mixed')
3030
+ })
3031
+
3032
+ test('the heading row selects every workspace at once', async () => {
3033
+ const harness = await loadPlugin()
3034
+ const heading = harness.document.createElement('div')
3035
+ heading.setAttribute('data-sidebar-workspace-header', '')
3036
+ harness.document.body.appendChild(heading)
3037
+
3038
+ const sectionA = harness.document.createElement('div')
3039
+ sectionA.setAttribute('role', 'group')
3040
+ const headA = harness.document.createElement('div')
3041
+ headA.setAttribute('role', 'treeitem')
3042
+ headA.setAttribute('data-row-key', 'workspace:ws-a')
3043
+ sectionA.appendChild(headA)
3044
+ mountRow(harness, SESSION_A, sectionA)
3045
+ harness.document.body.appendChild(sectionA)
3046
+
3047
+ const sectionB = harness.document.createElement('div')
3048
+ sectionB.setAttribute('role', 'group')
3049
+ const headB = harness.document.createElement('div')
3050
+ headB.setAttribute('role', 'treeitem')
3051
+ headB.setAttribute('data-row-key', 'workspace:ws-b')
3052
+ sectionB.appendChild(headB)
3053
+ mountRow(harness, SESSION_B, sectionB)
3054
+ harness.document.body.appendChild(sectionB)
3055
+
3056
+ await enterMulti(harness)
3057
+ observerPass(harness)
3058
+ await flush()
3059
+
3060
+ const all = heading.querySelector('.dsdel-select-all-workspaces')
3061
+ assert.ok(all, 'the heading carries the global box')
3062
+ all.fire('click')
3063
+ await flush()
3064
+
3065
+ assert.equal(all.getAttribute('aria-checked'), 'true', 'everything picked')
3066
+ assert.equal(headA.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true')
3067
+ assert.equal(headB.querySelector('.dsdel-select-all').getAttribute('aria-checked'), 'true')
3068
+ })
3069
+
3070
+ test('both boxes go away when the mode ends', async () => {
3071
+ const harness = await loadPlugin()
3072
+ const heading = harness.document.createElement('div')
3073
+ heading.setAttribute('data-sidebar-workspace-header', '')
3074
+ harness.document.body.appendChild(heading)
3075
+
3076
+ const section = harness.document.createElement('div')
3077
+ section.setAttribute('role', 'group')
3078
+ const header = harness.document.createElement('div')
3079
+ header.setAttribute('role', 'treeitem')
3080
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3081
+ section.appendChild(header)
3082
+ mountRow(harness, SESSION_A, section)
3083
+ harness.document.body.appendChild(section)
3084
+
3085
+ await enterMulti(harness)
3086
+ observerPass(harness)
3087
+ await flush()
3088
+ assert.ok(header.querySelector('.dsdel-select-all'), 'present during the mode')
3089
+ assert.ok(heading.querySelector('.dsdel-select-all-workspaces'))
3090
+
3091
+ harness.document.dispatchToDocument('keydown', { key: 'Escape', stopPropagation() {} })
3092
+ observerPass(harness)
3093
+ await flush()
3094
+
3095
+ assert.equal(header.querySelector('.dsdel-select-all'), null, 'removed with the mode')
3096
+ assert.equal(heading.querySelector('.dsdel-select-all-workspaces'), null)
3097
+ })
3098
+
3099
+ test('the select-all boxes do not lean on the page token scope', async () => {
3100
+ const harness = await loadPlugin()
3101
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3102
+
3103
+ // The boxes render inside the SIDEBAR. The `--dsdel-*` tokens are declared on
3104
+ // `.dsdel-page`, `.dsdel-modal` and `.dsdel-bar` only, so any rule outside those
3105
+ // scopes that reads them gets an invalid value and loses the whole declaration.
3106
+ // That is how the first version shipped an invisible button: `border` was
3107
+ // dropped, leaving no box and nothing to click.
3108
+ const rules = [...css.matchAll(/(?:^|\})\s*\.dsdel-select-all[^{]*\{([^}]*)\}/g)]
3109
+ assert.ok(rules.length > 0, 'the rules are in the stylesheet')
3110
+ for (const match of rules) {
3111
+ assert.equal(match[1].includes('--dsdel-'), false,
3112
+ 'a sidebar rule must not read the page tokens: ' + match[0].slice(0, 60))
3113
+ }
3114
+
3115
+ // Every host alias carries a literal fallback, so the box still draws when the
3116
+ // host renames its palette.
3117
+ assert.match(css, /\.dsdel-select-all[^}]*var\(--dsw-alias-border-l2,[^)]*\)/, 'border has a fallback')
3118
+ assert.match(css, /\.dsdel-select-all[^}]*var\(--dsw-alias-brand-primary,[^)]*\)/, 'brand has a fallback')
3119
+ })
3120
+
3121
+ test('a box always keeps a visible 15px square', async () => {
3122
+ const harness = await loadPlugin()
3123
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3124
+
3125
+ // A flex child with no width collapses to its content, and the empty state has
3126
+ // no content: `width` plus `min-width` plus content-box sizing is what makes the
3127
+ // square exist before anything is ticked.
3128
+ assert.match(css, /\.dsdel-select-all[^}]*width:15px/, 'a fixed width')
3129
+ assert.match(css, /\.dsdel-select-all[^}]*height:15px/, 'and a fixed height')
3130
+ assert.match(css, /\.dsdel-select-all[^}]*min-width:15px/, 'that flex cannot shrink away')
3131
+ assert.match(css, /\.dsdel-select-all[^}]*box-sizing:border-box/, 'with the border inside it')
3132
+ })
3133
+
3134
+ test('the workspace box sits beside the name, not at the row edge', async () => {
3135
+ const harness = await loadPlugin()
3136
+ const section = harness.document.createElement('div')
3137
+ section.setAttribute('role', 'group')
3138
+
3139
+ const header = harness.document.createElement('div')
3140
+ header.setAttribute('role', 'treeitem')
3141
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3142
+
3143
+ // The host row: label block, then the action cluster (which carries margin-left:auto).
3144
+ const label = harness.document.createElement('span')
3145
+ label.className = 'YDXeBa_projectText'
3146
+ header.appendChild(label)
3147
+ const actions = harness.document.createElement('span')
3148
+ actions.className = 'YDXeBa_rowActions'
3149
+ header.appendChild(actions)
3150
+
3151
+ section.appendChild(header)
3152
+ mountRow(harness, SESSION_A, section)
3153
+ harness.document.body.appendChild(section)
3154
+
3155
+ await enterMulti(harness)
3156
+ observerPass(harness)
3157
+ await flush()
3158
+
3159
+ const box = header.querySelector('.dsdel-select-all')
3160
+ assert.ok(box, 'the box exists')
3161
+
3162
+ // Appending put it after the action cluster, whose auto margin pushes it into the
3163
+ // far right of the row — that is the "靠右" the screenshot showed. It has to be
3164
+ // inserted before the actions instead.
3165
+ const index = header.children.indexOf(box)
3166
+ const actionsIndex = header.children.indexOf(actions)
3167
+ assert.equal(index < actionsIndex, true, 'the box comes before the action cluster')
3168
+ assert.equal(header.children[index - 1].className, 'YDXeBa_projectText', 'and right after the label')
3169
+ })
3170
+
3171
+ test('the global box sits after the heading label, before the search slot', async () => {
3172
+ const harness = await loadPlugin()
3173
+
3174
+ const heading = harness.document.createElement('div')
3175
+ heading.setAttribute('data-sidebar-workspace-header', '')
3176
+
3177
+ const label = harness.document.createElement('span')
3178
+ label.className = 'bhn1Oq_sectionLabel'
3179
+ heading.appendChild(label)
3180
+ const search = harness.document.createElement('div')
3181
+ search.className = 'bhn1Oq_searchSlot'
3182
+ heading.appendChild(search)
3183
+
3184
+ harness.document.body.appendChild(heading)
3185
+
3186
+ const section = harness.document.createElement('div')
3187
+ section.setAttribute('role', 'group')
3188
+ const header = harness.document.createElement('div')
3189
+ header.setAttribute('role', 'treeitem')
3190
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3191
+ section.appendChild(header)
3192
+ mountRow(harness, SESSION_A, section)
3193
+ harness.document.body.appendChild(section)
3194
+
3195
+ await enterMulti(harness)
3196
+ observerPass(harness)
3197
+ await flush()
3198
+
3199
+ const box = heading.querySelector('.dsdel-select-all-workspaces')
3200
+ assert.ok(box, 'the global box exists')
3201
+ const index = heading.children.indexOf(box)
3202
+ assert.equal(index < heading.children.indexOf(search), true, 'before the search slot')
3203
+ assert.equal(heading.children[index - 1].className, 'bhn1Oq_sectionLabel', 'and after the label')
3204
+ })
3205
+
3206
+ test('a box left at the wrong edge is moved on the next pass', async () => {
3207
+ const harness = await loadPlugin()
3208
+ const section = harness.document.createElement('div')
3209
+ section.setAttribute('role', 'group')
3210
+
3211
+ const header = harness.document.createElement('div')
3212
+ header.setAttribute('role', 'treeitem')
3213
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3214
+
3215
+ const label = harness.document.createElement('span')
3216
+ label.className = 'wnFoaW_projectText'
3217
+ header.appendChild(label)
3218
+ const actions = harness.document.createElement('span')
3219
+ actions.className = 'wnFoaW_rowActions'
3220
+ header.appendChild(actions)
3221
+
3222
+ // An earlier build left its box AFTER the actions: the far edge.
3223
+ const stale = harness.document.createElement('button')
3224
+ stale.className = 'dsdel-select-all'
3225
+ header.appendChild(stale)
3226
+
3227
+ section.appendChild(header)
3228
+ mountRow(harness, SESSION_A, section)
3229
+ harness.document.body.appendChild(section)
3230
+
3231
+ await enterMulti(harness)
3232
+ observerPass(harness)
3233
+ await flush()
3234
+
3235
+ // The insert used to live inside the "created" branch, so an existing box was
3236
+ // never repositioned: the heading box moved and the workspace box stayed put.
3237
+ const index = header.children.indexOf(stale)
3238
+ assert.equal(index < header.children.indexOf(actions), true, 'moved before the actions')
3239
+ assert.equal(header.children[index - 1].className, 'wnFoaW_projectText', 'and beside the label')
3240
+ })
3241
+
3242
+ test('the workspace name stops absorbing the row while the mode runs', async () => {
3243
+ const harness = await loadPlugin()
3244
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3245
+
3246
+ // `.projectText` carries `flex:1`, so it fills whatever the actions do not take.
3247
+ // A box inserted after it therefore lands at the row's far edge even though it
3248
+ // is early in the child list — that is why the workspace box stayed right while
3249
+ // the header box (whose label is not flexed) moved. The rule only applies while
3250
+ // the mode runs, so the host keeps its normal layout otherwise.
3251
+ assert.match(css, /body\.dsdel-selecting \[class\*="_projectText"\]\{[^}]*flex:0 1 auto/,
3252
+ 'the name sizes to its text during the mode')
3253
+ assert.match(css, /body\.dsdel-selecting \[class\*="_projectText"\]\{[^}]*min-width:0/,
3254
+ 'and can still truncate a long name')
3255
+ })
3256
+
3257
+ test('a spacer keeps the right-hand cluster in place', async () => {
3258
+ const harness = await loadPlugin()
3259
+ const section = harness.document.createElement('div')
3260
+ section.setAttribute('role', 'group')
3261
+
3262
+ const header = harness.document.createElement('div')
3263
+ header.setAttribute('role', 'treeitem')
3264
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3265
+
3266
+ const label = harness.document.createElement('span')
3267
+ label.className = 'wnFoaW_projectText'
3268
+ header.appendChild(label)
3269
+ // The cluster that must NOT move: count, menu, new-session.
3270
+ const actions = harness.document.createElement('span')
3271
+ actions.className = 'wnFoaW_rowActions'
3272
+ header.appendChild(actions)
3273
+
3274
+ section.appendChild(header)
3275
+ mountRow(harness, SESSION_A, section)
3276
+ harness.document.body.appendChild(section)
3277
+
3278
+ await enterMulti(harness)
3279
+ observerPass(harness)
3280
+ await flush()
3281
+
3282
+ // Order has to be: label, box, spacer, actions. With the spacer growing, the
3283
+ // name keeps its natural width and the cluster stays at the row's end; without
3284
+ // it, every flex child simply packs left and the count/menu/new buttons slid
3285
+ // over with the box.
3286
+ const kids = header.children
3287
+ const box = header.querySelector('.dsdel-select-all')
3288
+ const spacer = header.querySelector('.dsdel-flex-spacer')
3289
+ assert.ok(box, 'the box exists')
3290
+ assert.ok(spacer, 'the spacer exists')
3291
+
3292
+ assert.equal(kids.indexOf(box) < kids.indexOf(spacer), true, 'box before spacer')
3293
+ assert.equal(kids.indexOf(spacer) < kids.indexOf(actions), true, 'spacer before the cluster')
3294
+ assert.equal(kids.indexOf(label) < kids.indexOf(box), true, 'label first')
3295
+ })
3296
+
3297
+ test('the spacer is removed with the mode', async () => {
3298
+ const harness = await loadPlugin()
3299
+ const section = harness.document.createElement('div')
3300
+ section.setAttribute('role', 'group')
3301
+ const header = harness.document.createElement('div')
3302
+ header.setAttribute('role', 'treeitem')
3303
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3304
+ const label = harness.document.createElement('span')
3305
+ label.className = 'wnFoaW_projectText'
3306
+ header.appendChild(label)
3307
+ const actions = harness.document.createElement('span')
3308
+ actions.className = 'wnFoaW_rowActions'
3309
+ header.appendChild(actions)
3310
+ section.appendChild(header)
3311
+ mountRow(harness, SESSION_A, section)
3312
+ harness.document.body.appendChild(section)
3313
+
3314
+ await enterMulti(harness)
3315
+ observerPass(harness)
3316
+ await flush()
3317
+ assert.ok(header.querySelector('.dsdel-flex-spacer'), 'spacer present during the mode')
3318
+
3319
+ // The bar's Cancel is the mode's own exit route. Escape would also reach
3320
+ // handlers left behind by earlier tests: the stub never unregisters one.
3321
+ clickBar(barCancel(harness))
3322
+ observerPass(harness)
3323
+ await flush()
3324
+ assert.equal(header.querySelector('.dsdel-flex-spacer'), null, 'gone with the mode')
3325
+ assert.equal(header.querySelector('.dsdel-select-all'), null, 'and so is the box')
3326
+ })
3327
+
3328
+ test('PROBE exit removes spacer', async () => {
3329
+ const harness = await loadPlugin()
3330
+ const section = harness.document.createElement('div')
3331
+ section.setAttribute('role', 'group')
3332
+ const header = harness.document.createElement('div')
3333
+ header.setAttribute('role', 'treeitem')
3334
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3335
+ const actions = harness.document.createElement('span')
3336
+ actions.className = 'wnFoaW_rowActions'
3337
+ header.appendChild(actions)
3338
+ section.appendChild(header)
3339
+ mountRow(harness, SESSION_A, section)
3340
+ harness.document.body.appendChild(section)
3341
+
3342
+ await enterMulti(harness)
3343
+ observerPass(harness)
3344
+ await flush()
3345
+
3346
+ const before = harness.document.querySelectorAll('.dsdel-flex-spacer').length
3347
+ console.log('PROBE 装饰后 spacer 数:', before)
3348
+ for (const el of harness.document.querySelectorAll('.dsdel-flex-spacer')) {
3349
+ console.log('PROBE spacer parent:', el.parentElement && el.parentElement.getAttribute('data-row-key'), 'idx:', el.parentElement ? el.parentElement.children.indexOf(el) : -1)
3350
+ }
3351
+ const cancel = barCancel(harness)
3352
+ console.log('PROBE cancel 节点:', cancel && cancel.props && cancel.props.className)
3353
+ clickBar(cancel)
3354
+ await flush()
3355
+ console.log('PROBE 退出后 spacer 数:', harness.document.querySelectorAll('.dsdel-flex-spacer').length)
3356
+ assert.ok(true)
3357
+ })
3358
+
3359
+ test('the spacer is the growing child, so the cluster keeps its seat', async () => {
3360
+ const harness = await loadPlugin()
3361
+ const css = String(harness.document.head.querySelector('[data-dsh-delete-session="1"]')._text ?? '')
3362
+
3363
+ // Without a growing child the flex row simply packs left, which drags the count
3364
+ // and the action buttons along with the box. The spacer is what holds them at
3365
+ // the row's end while the box sits beside the name.
3366
+ assert.match(css, /\.dsdel-flex-spacer\{[^}]*flex:1/, 'the spacer grows')
3367
+ assert.match(css, /\.dsdel-flex-spacer\{[^}]*min-width:0/, 'and can collapse when space is tight')
3368
+ assert.match(css, /\.dsdel-select-all[^}]*flex:0 0 auto/, 'the box itself does not grow')
3369
+ })
3370
+
3371
+ test('a document click on a plugin-drawn element leaves the session alone', async () => {
3372
+ const harness = await loadPlugin()
3373
+ const row = mountRow(harness, SESSION_A)
3374
+ await enterMulti(harness)
3375
+ observerPass(harness)
3376
+ await flush()
3377
+ assert.equal(harness.document.body.className.includes('dsdel-selecting'), true)
3378
+
3379
+ // The document capture listener runs before any element handler. It must ignore
3380
+ // elements this plugin drew — a box inside a workspace row is the everyday case,
3381
+ // and inside a session row it would otherwise toggle that session.
3382
+ const box = harness.document.createElement('button')
3383
+ box.className = 'dsdel-select-all'
3384
+ box.setAttribute('data-dsh-delete-session', 'select-all')
3385
+ row.appendChild(box)
3386
+
3387
+ clickRowIn(harness, box)
3388
+ observerPass(harness)
3389
+ await flush()
3390
+
3391
+ const check = row.querySelector('.dsdel-check')
3392
+ assert.equal(check.classList.contains('dsdel-check-on'), false, 'no session was toggled')
3393
+ })
3394
+
3395
+ test('the row handler still works for a real session row', async () => {
3396
+ const harness = await loadPlugin()
3397
+ const row = mountRow(harness, SESSION_A)
3398
+ await enterMulti(harness)
3399
+ observerPass(harness)
3400
+ await flush()
3401
+
3402
+ clickRowIn(harness, row)
3403
+ await flush()
3404
+
3405
+ const check = row.querySelector('.dsdel-check')
3406
+ assert.equal(check.classList.contains('dsdel-check-on'), true, 'a plain row still toggles')
3407
+ })
3408
+
3409
+ test('a decorate pass with nothing to move keeps the box where it is', async () => {
3410
+ const harness = await loadPlugin()
3411
+ const section = harness.document.createElement('div')
3412
+ section.setAttribute('role', 'group')
3413
+ const header = harness.document.createElement('div')
3414
+ header.setAttribute('role', 'treeitem')
3415
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3416
+ const label = harness.document.createElement('span')
3417
+ label.className = 'wnFoaW_projectText'
3418
+ header.appendChild(label)
3419
+ const actions = harness.document.createElement('span')
3420
+ actions.className = 'wnFoaW_rowActions'
3421
+ header.appendChild(actions)
3422
+ section.appendChild(header)
3423
+ mountRow(harness, SESSION_A, section)
3424
+ harness.document.body.appendChild(section)
3425
+
3426
+ await enterMulti(harness)
3427
+ observerPass(harness)
3428
+ await flush()
3429
+
3430
+ // Every insertBefore is a mutation, and the list observer counted it as a
3431
+ // reason to scan again: each pass scheduled the next one, forever, and the box
3432
+ // was detached and re-attached under the pointer. Nothing may move here — the
3433
+ // box and its spacer are already in the right place.
3434
+ const box = header.querySelector('.dsdel-select-all')
3435
+ const spacer = header.querySelector('.dsdel-flex-spacer')
3436
+ const index = header.children.indexOf(box)
3437
+
3438
+ // Re-inserting a node where it already sits leaves its index unchanged, so the
3439
+ // index alone cannot prove the pass was quiet. Count the calls instead: a real
3440
+ // insertBefore produces a MutationRecord, and the observer used to feed on them.
3441
+ const realInsert = header.insertBefore.bind(header)
3442
+ let inserts = 0
3443
+ header.insertBefore = (child, ref) => { inserts += 1; return realInsert(child, ref) }
3444
+
3445
+ observerPass(harness)
3446
+ observerPass(harness)
3447
+ await flush()
3448
+
3449
+ assert.equal(inserts, 0, 'a settled row is left untouched')
3450
+
3451
+ assert.equal(header.children.indexOf(box), index, 'the box did not move')
3452
+ assert.equal(header.children.indexOf(spacer), index + 1, 'nor did the spacer')
3453
+ assert.equal(header.children.filter((el) => el === box).length, 1, 'and there is only one')
3454
+ })
3455
+
3456
+ test('a mutation naming only our own nodes schedules no pass', async () => {
3457
+ const harness = await loadPlugin()
3458
+
3459
+ // Drive the frame queue by hand: a timer-based flush made this test flaky,
3460
+ // because a pass can queue another callback while it runs.
3461
+ //
3462
+ // Note the drain: emptying the queue without running its callbacks leaves the
3463
+ // plugin's `decorateRaf` handle truthy, and every later `scheduleDecorate`
3464
+ // silently returns. That is a harness trap, not a product bug.
3465
+ const queued = []
3466
+ const realRaf = globalThis.requestAnimationFrame
3467
+ const realCancel = globalThis.cancelAnimationFrame
3468
+ globalThis.requestAnimationFrame = (fn) => { queued.push(fn); return queued.length }
3469
+ globalThis.cancelAnimationFrame = () => {}
3470
+ const drain = () => { let guard = 0; while (queued.length > 0 && guard < 50) { guard += 1; queued.shift()() } }
3471
+
3472
+ try {
3473
+ await enterMulti(harness)
3474
+ drain()
3475
+
3476
+ // A row that arrives after the last pass carries no checkbox, so whether a
3477
+ // pass runs is readable straight off the DOM.
3478
+ const section = harness.document.createElement('div')
3479
+ section.setAttribute('role', 'group')
3480
+ const hostRow = harness.document.createElement('div')
3481
+ hostRow.setAttribute('role', 'treeitem')
3482
+ hostRow.setAttribute('data-row-key', 'session:session-cccc1111-2222-4333-8444-555555555555')
3483
+ section.appendChild(hostRow)
3484
+ harness.document.body.appendChild(section)
3485
+ assert.equal(hostRow.querySelector('.dsdel-check'), null, 'the row is undecorated')
3486
+
3487
+ // Decorate inserts checkboxes, and the list observer reports them right back.
3488
+ // Every such record used to schedule another pass, so the loop never settled:
3489
+ // the box was re-inserted under the pointer and a click often missed.
3490
+ const own = harness.document.createElement('span')
3491
+ own.setAttribute('data-dsh-delete-session', 'check')
3492
+ harness.observers[0].callback([{ addedNodes: [own], removedNodes: [] }])
3493
+ assert.equal(queued.length, 0, 'our own node schedules nothing')
3494
+
3495
+ // A host change must still get through.
3496
+ harness.observers[0].callback([{ addedNodes: [hostRow], removedNodes: [] }])
3497
+ assert.equal(queued.length, 1, 'a real host row schedules one pass')
3498
+ drain()
3499
+ assert.ok(hostRow.querySelector('.dsdel-check'), 'and that pass decorates it')
3500
+ } finally {
3501
+ globalThis.requestAnimationFrame = realRaf
3502
+ globalThis.cancelAnimationFrame = realCancel
3503
+ }
3504
+ })
3505
+
3506
+ test('the plugin never calls array methods on a live child list', async () => {
3507
+ const source = await readFile(new URL('../src/client.js', import.meta.url), 'utf8')
3508
+
3509
+ // In a browser `element.children` is an HTMLCollection: it has `length` and
3510
+ // indexing, and none of the Array methods. The test stub uses a plain array, so
3511
+ // `children.indexOf(...)` passed here and threw a TypeError in the real host —
3512
+ // the decorate pass died before inserting anything, and the select-all box never
3513
+ // appeared. Index it by hand (`childIndex`) or spread it first.
3514
+ const offenders = []
3515
+ for (const match of source.matchAll(/\.children\.([A-Za-z]+)\s*\(/g)) {
3516
+ offenders.push(match[0])
3517
+ }
3518
+ assert.deepEqual(offenders, [], 'no Array methods on `children`')
3519
+ })
3520
+
3521
+ test('the workspace box reaches past a wrapping element', async () => {
3522
+ const harness = await loadPlugin()
3523
+ const section = harness.document.createElement('div')
3524
+ section.setAttribute('role', 'group')
3525
+
3526
+ // The real host wraps the workspace row in HoverCard's <span>, so the row's
3527
+ // parentElement is that span, not the section. A subtree query from the span
3528
+ // returns only the workspace row itself and the box ends up with no ids —
3529
+ // `toggleGroup` then returns without doing anything, which is exactly the
3530
+ // "single-workspace select-all does nothing" report.
3531
+ const wrapper = harness.document.createElement('span')
3532
+ wrapper.className = 'HoverCard_root'
3533
+ const header = harness.document.createElement('div')
3534
+ header.setAttribute('role', 'treeitem')
3535
+ header.setAttribute('data-row-key', 'workspace:ws-a')
3536
+ const label = harness.document.createElement('span')
3537
+ label.className = 'wnFoaW_projectText'
3538
+ header.appendChild(label)
3539
+ const actions = harness.document.createElement('span')
3540
+ actions.className = 'wnFoaW_rowActions'
3541
+ header.appendChild(actions)
3542
+ wrapper.appendChild(header)
3543
+ section.appendChild(wrapper)
3544
+
3545
+ mountRow(harness, SESSION_A, section)
3546
+ mountRow(harness, SESSION_B, section)
3547
+ harness.document.body.appendChild(section)
3548
+
3549
+ await enterMulti(harness)
3550
+ observerPass(harness)
3551
+ await flush()
3552
+
3553
+ const box = header.querySelector('.dsdel-select-all')
3554
+ assert.ok(box, 'the box exists')
3555
+ assert.deepEqual(box.__dsdelIds, [SESSION_A.id, SESSION_B.id], 'both sessions were found')
3556
+
3557
+ box.fire('click')
3558
+ await flush()
3559
+ assert.equal(box.getAttribute('aria-checked'), 'true', 'and pressing it selects them')
3560
+ })
3561
+
3562
+ test('a workspace box never reaches into a nested workspace', async () => {
3563
+ const harness = await loadPlugin()
3564
+ const section = harness.document.createElement('div')
3565
+ section.setAttribute('role', 'group')
3566
+
3567
+ const header = harness.document.createElement('div')
3568
+ header.setAttribute('role', 'treeitem')
3569
+ header.setAttribute('data-row-key', 'workspace:outer')
3570
+ const actions = harness.document.createElement('span')
3571
+ actions.className = 'wnFoaW_rowActions'
3572
+ header.appendChild(actions)
3573
+ section.appendChild(header)
3574
+ mountRow(harness, SESSION_A, section)
3575
+
3576
+ // A nested workspace is a `div[role="group"]` holding its own rows.
3577
+ const nested = harness.document.createElement('div')
3578
+ nested.setAttribute('role', 'group')
3579
+ const nestedHeader = harness.document.createElement('div')
3580
+ nestedHeader.setAttribute('role', 'treeitem')
3581
+ nestedHeader.setAttribute('data-row-key', 'workspace:inner')
3582
+ nested.appendChild(nestedHeader)
3583
+ mountRow(harness, SESSION_B, nested)
3584
+ section.appendChild(nested)
3585
+
3586
+ harness.document.body.appendChild(section)
3587
+
3588
+ await enterMulti(harness)
3589
+ observerPass(harness)
3590
+ await flush()
3591
+
3592
+ const box = header.querySelector('.dsdel-select-all')
3593
+ assert.deepEqual(box.__dsdelIds, [SESSION_A.id], 'only the outer group\'s own session')
2853
3594
  })
2854
3595
 
2855
- test('the drop label names the conversation instead of printing the placeholder', async () => {
2856
- const harness = await loadPlugin()
2857
- const row = mountRow(harness, SESSION_A)
2858
-
2859
- startRowDrag(harness, row)
2860
- await flush()
2861
- bar(harness).props.onDragOver(dropEvent())
3596
+ test('multi-select delete carries the artifact switch to every request', async () => {
3597
+ const harness = await loadPlugin({ plan: PLAN })
3598
+ localStorage.setItem(OFF_KEY, '0')
3599
+ localStorage.setItem(AUTO_KEY, '1')
3600
+ localStorage.setItem(SKIP_KEY, '1')
3601
+ const first = mountRow(harness, SESSION_A)
3602
+ const second = mountRow(harness, SESSION_B)
3603
+ await enterMulti(harness)
3604
+ clickRowIn(harness, first)
3605
+ clickRowIn(harness, second)
2862
3606
  await flush()
2863
3607
 
2864
- const node = bar(harness)
2865
- const text = JSON.stringify(node.props.children)
3608
+ clickBar(barDelete(harness))
3609
+ // The batch runs several workers and settles asynchronously; waiting only one
3610
+ // tick left them running into the NEXT test, whose harness swapped the document
3611
+ // out from under them. Drain until the bar is gone.
3612
+ await settle()
3613
+ for (let i = 0; i < 40 && bar(harness) !== null; i += 1) await flush()
2866
3614
 
2867
- // The dictionary entry is `松手删除「{name}」`, but the interpolation map carries
2868
- // `n`/`done`/`total`/`ok`/`fail`/`path` — not `name`. Passing the title as a bare
2869
- // string therefore left the braces in the label.
2870
- assert.doesNotMatch(text, /\{name\}/, 'no unsubstituted placeholder in the label')
2871
- assert.match(text, /松手删除/, 'the sentence is still there')
3615
+ // The single-delete path sends `quarantine`; batch used to call postDelete(id)
3616
+ // with nothing, so "quarantine the produced files" was silently skipped while
3617
+ // that same switch stayed on. Every request here has to carry it.
3618
+ const bodies = harness.requests
3619
+ .filter((call) => call.url === '/dsh-delete-session/delete')
3620
+ .map((call) => JSON.parse(call.init.body))
3621
+ assert.equal(bodies.length, 2, 'one request per selected session')
3622
+ for (const body of bodies) {
3623
+ assert.deepEqual(body.quarantine, { scope: 'created' }, 'the produced files follow the switch')
3624
+ }
2872
3625
  })
2873
3626
 
2874
- test('the drop label carries the dragged conversation title', async () => {
3627
+ test('multi-select delete leaves the files alone when the switch is off', async () => {
2875
3628
  const harness = await loadPlugin()
2876
- const row = mountRow(harness, SESSION_A)
2877
- row.textContent = 'BiliLearn-AI 仓库'
2878
-
2879
- startRowDrag(harness, row)
3629
+ const first = mountRow(harness, SESSION_A)
3630
+ await enterMulti(harness)
3631
+ clickRowIn(harness, first)
2880
3632
  await flush()
2881
- bar(harness).props.onDragOver(dropEvent())
3633
+
3634
+ clickBar(barDelete(harness))
3635
+ await settle()
3636
+ for (let i = 0; i < 40 && bar(harness) !== null; i += 1) await flush()
3637
+
3638
+ const bodies = harness.requests
3639
+ .filter((call) => call.url === '/dsh-delete-session/delete')
3640
+ .map((call) => JSON.parse(call.init.body))
3641
+ assert.equal(bodies.length, 1, 'the delete went out')
3642
+ assert.equal('quarantine' in bodies[0], false, 'and does not move any file')
3643
+ })
3644
+
3645
+ test('a batch in asking mode says the files were left alone', async () => {
3646
+ const harness = await loadPlugin()
3647
+ localStorage.setItem(OFF_KEY, '0')
3648
+ localStorage.setItem(AUTO_KEY, '0')
3649
+ const first = mountRow(harness, SESSION_A)
3650
+ await enterMulti(harness)
3651
+ clickRowIn(harness, first)
2882
3652
  await flush()
2883
3653
 
2884
- const text = JSON.stringify(bar(harness).props.children)
2885
- assert.match(text, /BiliLearn-AI 仓库/, 'the title reaches the sentence')
3654
+ clickBar(barDelete(harness))
3655
+ await settle()
3656
+ for (let i = 0; i < 40 && bar(harness) !== null; i += 1) await flush()
3657
+
3658
+ // Asking means a per-file checklist, which a batch has nowhere to show. Silence
3659
+ // would look like the files had been handled.
3660
+ const said = toasts(harness).join(' | ')
3661
+ assert.equal(said.includes('产出文件没有处理'), true, 'the user is told: ' + said)
3662
+ assert.equal('quarantine' in deleteBody(harness), false, 'and nothing was moved')
2886
3663
  })