Merge #834: codeman tui attaches by session name

codeman tui <name> attaches by an exact session name as well as by list number.
This commit is contained in:
Codeman maintainer committed 2026-11-20 04:00:05 +01:00
commit 1207c316c0
11 files changed
+201 -17

No files matched your search

+57
View File
@@ -13,6 +13,7 @@ import { describe, it, expect } from 'vitest';
import {
applyLiveMetrics,
applyMuxNames,
attachTargetError,
beginAttachHandoff,
buildAttachBanner,
buildListLines,
@@ -30,6 +31,7 @@ import {
planResume,
previewIntervalMs,
previewNoteFor,
resolveAttachTarget,
resyncDelayMs,
sameFrame,
samePreview,
@@ -915,3 +917,58 @@ describe('planResume', () => {
});
});
});
describe('resolveAttachTarget (codeman tui <session>)', () => {
function named(name: string, group: TuiRow['group'], sessionId: string): TuiRow {
return {
session: { sessionId, sources: ['live'], name },
state: group === 'recent' ? 'recent' : 'idle',
group,
since: 0,
};
}
const rows: TuiRow[] = [
named('api', 'working', 'aaaaaaaa01'),
named('docs', 'idle', 'bbbbbbbb02'),
named('twin', 'idle', 'cccccccc03'),
named('twin', 'needs-you', 'dddddddd04'),
named('old', 'recent', 'eeeeeeee05'),
named('docs', 'recent', 'ffffffff06'),
];
it('keeps digits-only as a list position, as a number or as a string', () => {
expect(resolveAttachTarget(rows, 2)).toEqual({ kind: 'row', row: rows[1] });
expect(resolveAttachTarget(rows, '2')).toEqual({ kind: 'row', row: rows[1] });
expect(resolveAttachTarget(rows, '7')).toEqual({ kind: 'none' });
});
it('attaches the one live session with that exact name, ignoring a finished namesake', () => {
expect(resolveAttachTarget(rows, 'api')).toEqual({ kind: 'row', row: rows[0] });
expect(resolveAttachTarget(rows, 'docs')).toEqual({ kind: 'row', row: rows[1] });
});
it('matches case-sensitively and never by prefix', () => {
expect(resolveAttachTarget(rows, 'API')).toEqual({ kind: 'none' });
expect(resolveAttachTarget(rows, 'ap')).toEqual({ kind: 'none' });
});
it('refuses a name two live sessions share, naming their positions', () => {
expect(resolveAttachTarget(rows, 'twin')).toEqual({ kind: 'ambiguous', positions: [3, 4] });
});
it('says a name only history carries is finished, not missing', () => {
expect(resolveAttachTarget(rows, 'old')).toEqual({ kind: 'finished' });
expect(resolveAttachTarget(rows, 'nope')).toEqual({ kind: 'none' });
});
it('words each refusal for a human and points at --list', () => {
expect(attachTargetError({ kind: 'ambiguous', positions: [3, 4] }, 'twin')).toMatch(
/2 live sessions are named twin \(3, 4 in the list\)[\s\S]*codeman tui 3/
);
expect(attachTargetError({ kind: 'finished' }, 'old')).toContain('no live session named old');
expect(attachTargetError({ kind: 'none' }, 'nope')).toContain('there is no session named nope');
expect(attachTargetError({ kind: 'none' }, 9)).toContain('there is no session 9');
for (const kind of ['finished', 'none'] as const)
expect(attachTargetError({ kind }, 'x')).toContain('codeman tui --list');
});
});
+41
View File
@@ -778,6 +778,47 @@ describe('codeman tui --list', () => {
}, 30_000);
});
describe('codeman tui <session name>', () => {
it('resolves an exact live name before asking for a terminal', async () => {
const result = await runPiped(['tui', 'w1-alpha']);
expect(result.code).toBe(1);
// Got past resolution: the only refusal left is the missing TTY.
expect(result.stderr).toContain('attaching needs an interactive terminal');
expect(result.stderr).not.toContain('no session');
}, 30_000);
it('says a name nothing carries is missing, and points at --list', async () => {
const result = await runPiped(['tui', 'w9-nope']);
expect(result.code).toBe(1);
expect(result.stderr).toContain('there is no session named w9-nope');
expect(result.stderr).toContain('codeman tui --list');
}, 30_000);
it('says a name only history carries is finished', async () => {
const result = await runPiped(['tui', 'w3-gamma']);
expect(result.code).toBe(1);
expect(result.stderr).toContain('no live session named w3-gamma, only a finished one');
}, 30_000);
it('refuses a name two live sessions share and names their numbers', async () => {
sessions.push({ ...sessions[1], sessionId: 'eeee5555-0000-0000-0000-000000000000' });
try {
const result = await runPiped(['tui', 'w1-alpha']);
expect(result.code).toBe(1);
expect(result.stderr).toMatch(/2 live sessions are named w1-alpha \(\d+, \d+ in the list\)/);
expect(result.stderr).toContain('Attach by number instead');
} finally {
resetSessions();
}
}, 30_000);
it('still reads digits as a list number', async () => {
const result = await runPiped(['tui', '42']);
expect(result.code).toBe(1);
expect(result.stderr).toContain('there is no session 42');
}, 30_000);
});
describe('codeman tui without a terminal', () => {
it('refuses to open the dashboard and points at the fast paths', async () => {
const result = await runPiped(['tui']);