dsh-mcp-status 0.2.0 → 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.
@@ -19,6 +19,7 @@ import {
19
19
  resolveStatus,
20
20
  sectionOrder,
21
21
  serverNameFromTool,
22
+ summarizeServers,
22
23
  } from '../lib/status.js';
23
24
 
24
25
  const NOW = 1_700_000_000_000;
@@ -198,7 +199,134 @@ const cases = [
198
199
  });
199
200
  }],
200
201
 
201
- // ----------------------------------------------------------- serverNameFromTool
202
+ // ------------------------------------------------- host connection status
203
+
204
+ ['resolveStatus: host connecting → pending/client-connecting', () => {
205
+ const result = resolveStatus({
206
+ fiberState: FIBER_ACTIVE,
207
+ connection: { state: 'connecting' },
208
+ toolCount: 0,
209
+ now: NOW,
210
+ });
211
+ assert.equal(result.state, 'pending');
212
+ assert.equal(result.reason, 'client-connecting');
213
+ }],
214
+
215
+ ['resolveStatus: host reconnecting → reconnecting + attempt in evidence', () => {
216
+ const result = resolveStatus({
217
+ fiberState: FIBER_ACTIVE,
218
+ connection: { state: 'reconnecting', attempt: 3, maxAttempts: 10 },
219
+ toolCount: 0,
220
+ now: NOW,
221
+ });
222
+ assert.equal(result.state, 'reconnecting');
223
+ assert.equal(result.reason, 'client-reconnecting');
224
+ assert.deepEqual(result.evidence.at(-1), {
225
+ signal: 'client',
226
+ value: 'reconnecting',
227
+ at: null,
228
+ detail: '3/10',
229
+ });
230
+ }],
231
+
232
+ ['resolveStatus: host gave up wins over a successful probe (no more fake green)', () => {
233
+ const result = resolveStatus({
234
+ fiberState: FIBER_ACTIVE,
235
+ connection: { state: 'failed', reason: 'gave-up', maxAttempts: 10, lastError: 'connect ECONNREFUSED' },
236
+ toolCount: 0,
237
+ reachable: true,
238
+ reachableAt: NOW,
239
+ probe: { ok: true, at: NOW, toolsCount: 49 },
240
+ now: NOW,
241
+ });
242
+ assert.equal(result.state, 'failed');
243
+ assert.equal(result.reason, 'gave-up');
244
+ }],
245
+
246
+ ['resolveStatus: host failed (other reasons) → failed/client-failed', () => {
247
+ for (const reason of ['unclosed-generation', 'reconnect-disabled', undefined]) {
248
+ const result = resolveStatus({
249
+ fiberState: FIBER_ACTIVE,
250
+ connection: reason === undefined ? { state: 'failed' } : { state: 'failed', reason },
251
+ toolCount: 2,
252
+ now: NOW,
253
+ });
254
+ assert.equal(result.state, 'failed', `reason=${String(reason)}`);
255
+ assert.equal(result.reason, 'client-failed', `reason=${String(reason)}`);
256
+ }
257
+ }],
258
+
259
+ ['resolveStatus: host closed / waiting-credentials → failed', () => {
260
+ const closed = resolveStatus({
261
+ fiberState: FIBER_ACTIVE, connection: { state: 'closed' }, toolCount: 0, now: NOW,
262
+ });
263
+ assert.equal(closed.state, 'failed');
264
+ assert.equal(closed.reason, 'client-closed');
265
+
266
+ const waiting = resolveStatus({
267
+ fiberState: FIBER_ACTIVE, connection: { state: 'waiting-credentials' }, toolCount: 0, now: NOW,
268
+ });
269
+ assert.equal(waiting.state, 'failed');
270
+ assert.equal(waiting.reason, 'waiting-credentials');
271
+ }],
272
+
273
+ ['resolveStatus: host connected + tools → connected, client evidence present', () => {
274
+ const result = resolveStatus({
275
+ fiberState: FIBER_ACTIVE,
276
+ connection: { state: 'connected' },
277
+ toolCount: 49,
278
+ reachable: true,
279
+ reachDetail: 'ok',
280
+ reachableAt: NOW,
281
+ now: NOW,
282
+ });
283
+ assert.equal(result.state, 'connected');
284
+ assert.equal(result.reason, 'connected');
285
+ assert.equal(result.evidence.at(-1).signal, 'client');
286
+ }],
287
+
288
+ ['resolveStatus: host connected but a fresh failed probe still means unreachable', () => {
289
+ const result = resolveStatus({
290
+ fiberState: FIBER_ACTIVE,
291
+ connection: { state: 'connected' },
292
+ toolCount: 3,
293
+ reachable: true,
294
+ reachDetail: 'ok',
295
+ reachableAt: NOW,
296
+ probe: { ok: false, at: NOW, error: 'handshake failed' },
297
+ now: NOW,
298
+ probeAssertMs: PROBE_ASSERT_MS,
299
+ });
300
+ assert.equal(result.state, 'unreachable');
301
+ assert.equal(result.reason, 'probe-failed');
302
+ }],
303
+
304
+ ['resolveStatus: host connected + 0 tools → idle/no-tools', () => {
305
+ const result = resolveStatus({
306
+ fiberState: FIBER_ACTIVE,
307
+ connection: { state: 'connected' },
308
+ toolCount: 0,
309
+ now: NOW,
310
+ });
311
+ assert.equal(result.state, 'idle');
312
+ assert.equal(result.reason, 'no-tools');
313
+ }],
314
+
315
+ ['resolveStatus: unknown host state falls back to observational derivation', () => {
316
+ const result = resolveStatus({
317
+ fiberState: FIBER_ACTIVE,
318
+ connection: { state: 'some-future-state' },
319
+ toolCount: 4,
320
+ reachable: true,
321
+ reachDetail: 'ok',
322
+ reachableAt: NOW,
323
+ now: NOW,
324
+ });
325
+ assert.equal(result.state, 'connected');
326
+ assert.equal(result.reason, 'connected');
327
+ }],
328
+
329
+ // ---------------------------------------------------------- serverNameFromTool
202
330
 
203
331
  ['serverNameFromTool: extracts the server segment of an MCP tool name', () => {
204
332
  assert.equal(serverNameFromTool('mcp__unityMCP__manage_scene'), 'unityMCP');
@@ -278,6 +406,42 @@ const cases = [
278
406
  assert.ok(!message.includes('\n'), 'guard message must stay one line');
279
407
  }],
280
408
 
409
+ ['decideGuard: reconnecting row is refused with the attempt count', () => {
410
+ const message = decideGuard({
411
+ toolName: 'mcp__unityMCP__manage_scene',
412
+ row: {
413
+ serverName: 'unityMCP',
414
+ target: 'http://127.0.0.1:9/mcp',
415
+ state: 'reconnecting',
416
+ connection: { state: 'reconnecting', attempt: 4, maxAttempts: 10 },
417
+ },
418
+ snapshotAt: NOW - 1000,
419
+ now: NOW,
420
+ config: DEFAULT_CONFIG,
421
+ });
422
+ assert.equal(typeof message, 'string');
423
+ assert.ok(message.includes('reconnecting'), message);
424
+ assert.ok(message.includes('4 of 10'), message);
425
+ }],
426
+
427
+ ['decideGuard: gave-up row explains the attempt limit', () => {
428
+ const message = decideGuard({
429
+ toolName: 'mcp__unityMCP__manage_scene',
430
+ row: {
431
+ serverName: 'unityMCP',
432
+ target: 'http://127.0.0.1:9/mcp',
433
+ state: 'failed',
434
+ reason: 'gave-up',
435
+ connection: { state: 'failed', reason: 'gave-up', maxAttempts: 10 },
436
+ },
437
+ snapshotAt: NOW - 1000,
438
+ now: NOW,
439
+ config: DEFAULT_CONFIG,
440
+ });
441
+ assert.equal(typeof message, 'string');
442
+ assert.ok(message.includes('gave up after 10'), message);
443
+ }],
444
+
281
445
  // ------------------------------------------------------------ buildHealthText
282
446
 
283
447
  ['buildHealthText: no unhealthy rows → empty string', () => {
@@ -333,6 +497,21 @@ const cases = [
333
497
  assert.ok(text.endsWith('…'), text);
334
498
  }],
335
499
 
500
+ ['buildHealthText: gave-up row says the client stopped reconnecting', () => {
501
+ const text = buildHealthText({
502
+ servers: [{
503
+ serverName: 'unityMCP',
504
+ target: 'http://127.0.0.1:8080/mcp',
505
+ state: 'failed',
506
+ reason: 'gave-up',
507
+ toolCount: 0,
508
+ checkedAt: NOW,
509
+ }],
510
+ }, { now: NOW });
511
+ assert.ok(text.includes('reconnect gave up'), text);
512
+ assert.ok(text.includes('unityMCP'), text);
513
+ }],
514
+
336
515
  // ------------------------------------------------------------- normalizeConfig
337
516
 
338
517
  ['normalizeConfig: defaults, floors, type guards and unknown keys', () => {
@@ -382,8 +561,105 @@ const cases = [
382
561
  assert.equal(sectionOrder(undefined), 50);
383
562
  assert.equal(sectionOrder({ getSectionOrder: () => '7' }, 'MCP_SERVERS', 50), 50);
384
563
  }],
564
+
565
+ // ------------------------------------------------------------ summarizeServers
566
+ //
567
+ // 这是侧栏角标唯一的输入。它只做聚合,所以用例守的是「哪些状态配点亮侧栏」这条边界,
568
+ // 尤其是 A3:idle(没有工具)不是断线,绝不能让侧栏变红。
569
+
570
+ ['summarizeServers: all connected → ok, no attention', () => {
571
+ const summary = summarizeServers([
572
+ { id: 'a', serverName: 'alpha', state: 'connected', reason: 'connected' },
573
+ { id: 'b', serverName: 'beta', state: 'connected', reason: 'connected' },
574
+ ]);
575
+ assert.equal(summary.worst, 'ok');
576
+ assert.equal(summary.counts.total, 2);
577
+ assert.equal(summary.counts.connected, 2);
578
+ assert.deepEqual(summary.names, []);
579
+ assert.equal(summary.attention.length, 0);
580
+ }],
581
+
582
+ ['summarizeServers: failed → bad, with name and reason', () => {
583
+ const summary = summarizeServers([
584
+ { id: 'u', serverName: 'unityMCP', state: 'failed', reason: 'gave-up' },
585
+ { id: 'a', serverName: 'alpha', state: 'connected', reason: 'connected' },
586
+ ]);
587
+ assert.equal(summary.worst, 'bad');
588
+ assert.equal(summary.counts.unhealthy, 1);
589
+ assert.deepEqual(summary.names, ['unityMCP']);
590
+ assert.equal(summary.attention[0].reason, 'gave-up');
591
+ }],
592
+
593
+ ['summarizeServers: reconnecting alone → warn, failed outranks it', () => {
594
+ const warn = summarizeServers([{ id: 'a', serverName: 'alpha', state: 'reconnecting', reason: 'client-reconnecting' }]);
595
+ assert.equal(warn.worst, 'warn');
596
+ assert.equal(warn.counts.reconnecting, 1);
597
+
598
+ const bad = summarizeServers([
599
+ { id: 'a', serverName: 'alpha', state: 'reconnecting', reason: 'client-reconnecting' },
600
+ { id: 'b', serverName: 'beta', state: 'unreachable', reason: 'tcp-refused' },
601
+ ]);
602
+ assert.equal(bad.worst, 'bad');
603
+ // 先出现的 warn 不能让后面更糟的 bad 被吞掉
604
+ assert.equal(bad.attention.length, 2);
605
+ }],
606
+
607
+ ['summarizeServers: idle/pending are not errors (A3)', () => {
608
+ const idle = summarizeServers([{ id: 'a', serverName: 'alpha', state: 'idle', reason: 'no-tools' }]);
609
+ assert.equal(idle.worst, 'idle');
610
+ assert.equal(idle.counts.unhealthy, 0);
611
+ assert.equal(idle.counts.reconnecting, 0);
612
+ assert.deepEqual(idle.names, []);
613
+
614
+ const pending = summarizeServers([{ id: 'a', serverName: 'alpha', state: 'pending', reason: 'pending' }]);
615
+ assert.equal(pending.worst, 'idle');
616
+ assert.deepEqual(pending.names, []);
617
+
618
+ // 和闸门对齐:同一行 decideGuard 放行,角标就不该亮。
619
+ const row = { state: 'idle', reason: 'no-tools', serverName: 'alpha', connection: undefined };
620
+ assert.equal(decideGuard({ toolName: 'mcp__alpha__ping', row, snapshotAt: NOW, now: NOW }), undefined);
621
+ assert.notEqual(idle.worst, 'bad');
622
+ }],
623
+
624
+ ['summarizeServers: disabled excluded, empty and unknown stay quiet', () => {
625
+ const disabled = summarizeServers([{ id: 'a', serverName: 'alpha', state: 'disabled', reason: 'disabled' }]);
626
+ assert.equal(disabled.worst, 'disabled');
627
+ assert.equal(disabled.counts.disabled, 1);
628
+ assert.deepEqual(disabled.names, []);
629
+
630
+ const empty = summarizeServers([]);
631
+ assert.equal(empty.worst, 'empty');
632
+ assert.equal(empty.counts.total, 0);
633
+
634
+ // 宿主给了没见过的新状态:不猜、不报警,只记个数。
635
+ const unknown = summarizeServers([{ id: 'a', serverName: 'alpha', state: 'brand-new-state' }]);
636
+ assert.equal(unknown.counts.unknown, 1);
637
+ assert.deepEqual(unknown.names, []);
638
+ assert.equal(unknown.worst, 'empty');
639
+ }],
640
+
641
+ ['summarizeServers: names are capped, the rest become a count', () => {
642
+ const rows = Array.from({ length: 5 }, (_, index) => ({
643
+ id: `s${index}`, serverName: `srv${index}`, state: 'failed', reason: 'client-failed',
644
+ }));
645
+ const summary = summarizeServers(rows, { maxNames: 2 });
646
+ assert.deepEqual(summary.names, ['srv0', 'srv1']);
647
+ assert.equal(summary.more, 3);
648
+ assert.equal(summary.attention.length, 2);
649
+ }],
650
+
651
+ ['summarizeServers: signature tracks state and reason changes', () => {
652
+ const rows = [{ id: 'a', serverName: 'alpha', state: 'connected', reason: 'connected' }];
653
+ const before = summarizeServers(rows).signature;
654
+ const after = summarizeServers([
655
+ { id: 'a', serverName: 'alpha', state: 'failed', reason: 'client-failed' },
656
+ ]).signature;
657
+ assert.notEqual(before, after);
658
+ assert.equal(summarizeServers(rows).signature, before);
659
+ }],
385
660
  ];
386
661
 
662
+
387
663
  let failed = 0;
388
664
  for (const [name, body] of cases) {
389
665
  try {