dsh-delete-session 0.2.3 → 0.2.5

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.
@@ -1215,3 +1215,241 @@ test('a child that is still open is left alone and reported as skipped', async (
1215
1215
  assert.equal(fs.existsSync(child.dir), true, 'a busy child is not deleted under its writer')
1216
1216
  assert.equal(result.children[0].skipped, 'busy', 'and the report says why')
1217
1217
  })
1218
+
1219
+ // --- the folder picker belongs to the host, not to this plugin ---------------
1220
+
1221
+ /**
1222
+ * The host mounts `@deepseek-ai/dsh-host-directory-picker-auto` as
1223
+ * `ctx.directoryPicker`; its capability says which interaction the deployment
1224
+ * supports. Native deployments open one OS chooser, browse deployments serve a
1225
+ * listing the page renders itself.
1226
+ */
1227
+ const pickerContext = (capability) => ({
1228
+ get: (name) => (name === 'directoryPicker' ? { capability: () => capability } : undefined),
1229
+ })
1230
+
1231
+ test('a native host capability answers with the chosen directory', async () => {
1232
+ // A real directory: the plugin still validates what the chooser hands back, so
1233
+ // a path that vanished between the click and the answer is not accepted.
1234
+ const chosen = fs.mkdtempSync(path.join(os.tmpdir(), 'dsdel-pick-'))
1235
+ const ctx = pickerContext({ kind: 'native', pick: async () => chosen })
1236
+
1237
+ const result = await internals.pickFolder(ctx)
1238
+
1239
+ assert.deepEqual(result, { cancelled: false, path: path.resolve(chosen) })
1240
+ fs.rmSync(chosen, { recursive: true, force: true })
1241
+ })
1242
+
1243
+ test('a cancelled native chooser is reported, not thrown', async () => {
1244
+ const ctx = pickerContext({ kind: 'native', pick: async () => null })
1245
+
1246
+ const result = await internals.pickFolder(ctx)
1247
+
1248
+ assert.deepEqual(result, { cancelled: true, path: null })
1249
+ })
1250
+
1251
+ test('a browse-only host asks the page to render its own picker', async () => {
1252
+ const ctx = pickerContext({ kind: 'browse', list: async () => [] })
1253
+
1254
+ const result = await internals.pickFolder(ctx)
1255
+
1256
+ assert.equal(result.browse, true, 'the in-page picker takes over')
1257
+ })
1258
+
1259
+ test('a host without the service is reported as unsupported', async () => {
1260
+ for (const ctx of [{ get: () => undefined }, {}, null]) {
1261
+ const result = await internals.pickFolder(ctx)
1262
+ assert.equal(result.unsupported, true)
1263
+ }
1264
+ })
1265
+
1266
+ test('a capability that throws does not take the request down', async () => {
1267
+ const ctx = { get: () => ({ capability: () => { throw new Error('boom') } }) }
1268
+
1269
+ const result = await internals.pickFolder(ctx)
1270
+
1271
+ assert.equal(result.unsupported, true)
1272
+ })
1273
+
1274
+ test('this plugin no longer compiles or shells out a picker of its own', () => {
1275
+ // The C# source, the compiler probe and the PowerShell fallback all existed
1276
+ // only because the host's own chooser was not being used.
1277
+ const source = fs.readFileSync(new URL('../src/index.js', import.meta.url), 'utf8')
1278
+ // `findCsc` stays: the recycle-bin helper still compiles a C# program. Only the
1279
+ // picker-specific machinery is gone.
1280
+ for (const gone of ['PICK_CSHARP', 'PICK_EXE_SOURCE', 'PICK_SCRIPT', 'ensurePickerExe', 'pickFolderWithPowershell', 'pickFolderNative']) {
1281
+ assert.equal(source.includes(gone), false, gone + ' should be gone')
1282
+ }
1283
+ })
1284
+
1285
+ // --- the picker call must hand the host a real signal ------------------------
1286
+
1287
+ test('the native chooser is given a live abort signal', async () => {
1288
+ // The host implementation starts with `if (signal.aborted) throw …`, so calling
1289
+ // `pick()` with no argument throws "Cannot read properties of undefined
1290
+ // (reading 'aborted')" — which surfaced as a bogus delete failure.
1291
+ let seen
1292
+ const ctx = {
1293
+ get: (name) =>
1294
+ name === 'directoryPicker'
1295
+ ? {
1296
+ capability: () => ({
1297
+ kind: 'native',
1298
+ pick: async (signal) => {
1299
+ seen = signal
1300
+ return null
1301
+ },
1302
+ }),
1303
+ }
1304
+ : undefined,
1305
+ }
1306
+
1307
+ await internals.pickFolder(ctx)
1308
+
1309
+ assert.ok(seen, 'a signal was passed')
1310
+ assert.equal(typeof seen.aborted, 'boolean', 'and it is an AbortSignal')
1311
+ assert.equal(seen.aborted, false, 'not already aborted')
1312
+ })
1313
+
1314
+ // --- both subagent creation modes are deletable, and only they -------------
1315
+
1316
+ /**
1317
+ * A subagent started in "fork" mode: it inherits a seeded prefix of the parent
1318
+ * log, so the header carries `isSeeded: true` — but `origin` is still `subagent`,
1319
+ * and the host writes it for both creation modes
1320
+ * (`childSessionMeta` in dsh-subagent sets `origin: "subagent"` unconditionally,
1321
+ * with `isSeeded` recording which mode was used).
1322
+ */
1323
+ const forkedSubagent = (id, parentSession) => ({
1324
+ id,
1325
+ parentSession,
1326
+ origin: 'subagent',
1327
+ isSeeded: true,
1328
+ delegationDepth: 1,
1329
+ })
1330
+
1331
+ test('a subagent started by fork is still a child of its parent', () => {
1332
+ const headers = [
1333
+ header('session-parent'),
1334
+ child('session-child-new', 'session-parent'),
1335
+ forkedSubagent('session-child-fork', 'session-parent'),
1336
+ ]
1337
+
1338
+ const found = internals.collectDescendants(headers, 'session-parent')
1339
+
1340
+ assert.deepEqual(
1341
+ found.map((h) => h.id),
1342
+ ['session-child-new', 'session-child-fork'],
1343
+ 'both creation modes are picked up',
1344
+ )
1345
+ })
1346
+
1347
+ test('a forked subagent can be deleted on its own', () => {
1348
+ const headers = [header('session-parent'), forkedSubagent('session-child-fork', 'session-parent')]
1349
+
1350
+ const picked = internals.selectChildDeletes(headers, 'session-parent', ['session-child-fork'])
1351
+
1352
+ assert.deepEqual(picked, ['session-child-fork'], 'deleting the parent may take the fork-mode child')
1353
+ })
1354
+
1355
+ test('the two modes are told apart from a user fork, which stays untouched', () => {
1356
+ const headers = [
1357
+ header('session-parent'),
1358
+ child('session-child-new', 'session-parent'),
1359
+ forkedSubagent('session-child-fork', 'session-parent'),
1360
+ fork('session-user-fork', 'session-parent'),
1361
+ ]
1362
+
1363
+ const found = internals.collectDescendants(headers, 'session-parent').map((h) => h.id)
1364
+
1365
+ assert.deepEqual(found, ['session-child-new', 'session-child-fork'], 'subagents only')
1366
+ assert.equal(found.includes('session-user-fork'), false, 'a conversation the user branched is not dragged along')
1367
+ })
1368
+ // --- deleting one produced file, with the same recycle choice ----------------
1369
+
1370
+ test('only the files the dialog listed may be deleted one by one', () => {
1371
+ const plan = {
1372
+ created: [{ path: 'C:/ws/new-a.md', ops: 1 }],
1373
+ modified: [{ path: 'C:/ws/old-c.md', ops: 2 }],
1374
+ }
1375
+
1376
+ const candidates = internals.artifactCandidates(plan)
1377
+
1378
+ assert.deepEqual(candidates.sort(), ['C:/ws/new-a.md', 'C:/ws/old-c.md'])
1379
+ // A path the plan never offered is refused by the route; this is the list it
1380
+ // checks against, so it has to cover both groups.
1381
+ assert.equal(candidates.includes('C:/ws/secret.md'), false)
1382
+ })
1383
+
1384
+ test('a permanent single-file delete really removes the file', async () => {
1385
+ const file = path.join(os.tmpdir(), `dsdel-one-${Date.now()}.md`)
1386
+ fs.writeFileSync(file, 'x')
1387
+
1388
+ const result = await internals.removeOneFile(file, false)
1389
+
1390
+ assert.equal(result.recycled, false)
1391
+ assert.equal(fs.existsSync(file), false, 'gone')
1392
+ })
1393
+
1394
+ test('a missing file is reported instead of throwing raw', async () => {
1395
+ const file = path.join(os.tmpdir(), `dsdel-one-missing-${Date.now()}.md`)
1396
+
1397
+ await assert.rejects(
1398
+ () => internals.removeOneFile(file, false),
1399
+ (error) => error instanceof Error,
1400
+ )
1401
+ })
1402
+
1403
+ test('a directory is refused: this button deletes files, not folders', async () => {
1404
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dsdel-one-dir-'))
1405
+ try {
1406
+ await assert.rejects(
1407
+ () => internals.removeOneFile(dir, false),
1408
+ (error) => error && error.code === 'not-a-file',
1409
+ )
1410
+ } finally {
1411
+ fs.rmSync(dir, { recursive: true, force: true })
1412
+ }
1413
+ })
1414
+
1415
+ test('a path the plan never listed is refused', () => {
1416
+ const plan = { created: [{ path: 'C:/ws/listed.md' }], modified: [] }
1417
+
1418
+ assert.equal(internals.assertListedPath(plan, 'C:/ws/listed.md'), path.resolve('C:/ws/listed.md'))
1419
+ assert.throws(
1420
+ () => internals.assertListedPath(plan, 'C:/ws/secret.md'),
1421
+ (error) => error && error.code === 'not-listed',
1422
+ )
1423
+ })
1424
+ test('a session that names thousands of files is scanned up to a limit, not forever', async () => {
1425
+ // Every distinct path the log names costs a realpath. A runaway session (an
1426
+ // agent that walked a large tree) can name tens of thousands, and the dialog
1427
+ // would sit on "正在扫描…" while all of them are resolved. The cap bounds that
1428
+ // work; the plan reports `truncated`, which the dialog already shows.
1429
+ const events = []
1430
+ for (let i = 0; i < internals.MAX_SCANNED + 50; i += 1) {
1431
+ events.push({
1432
+ type: 'tool/call',
1433
+ data: {
1434
+ name: 'write',
1435
+ arguments: JSON.stringify({ path: `C:/ws/file-${i}.md` }),
1436
+ },
1437
+ })
1438
+ }
1439
+
1440
+ const result = await internals.collectSessionPaths(events, 'C:/ws', new Map())
1441
+
1442
+ assert.equal(result.overflow, true, 'the scan says it stopped early')
1443
+ assert.equal(result.named <= internals.MAX_SCANNED, true, 'and resolved no more than the cap')
1444
+ })
1445
+
1446
+ test('a small session is not flagged as truncated', async () => {
1447
+ const events = [
1448
+ { type: 'tool/call', data: { name: 'write', arguments: JSON.stringify({ path: 'C:/ws/a.md' }) } },
1449
+ { type: 'tool/call', data: { name: 'write', arguments: JSON.stringify({ path: 'C:/ws/b.md' }) } },
1450
+ ]
1451
+
1452
+ const result = await internals.collectSessionPaths(events, 'C:/ws', new Map())
1453
+
1454
+ assert.equal(result.overflow, false)
1455
+ })