Files
clide/test/builtin/claude/session_lifecycle_test.dart
T
jpmschweitzerandClaude a0636c2e5a add the team chat inbox over the broker
Renders broker traffic as a chat timeline and makes the user a first-class
participant. The broker grows a Stream<TeamMessage> and a recipient field,
auto-registers a virtual `user` member, and gains sendAsUser. A Flutter-free
TeamChatModel (owned by the orchestrator) accumulates the feed and exposes
postAsUser with @-routing (a new at_commands helper mirroring slash) and an
optional interrupt that cancels the target's turn before delivery. One model
backs two surfaces: a compact cockpit widget that pops out into a full
workspace chat pane. CLI parity via clide.team-chat.open / .post.

T-180.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-05-31 10:49:04 +02:00

280 lines
9.2 KiB
Dart

/// Session lifecycle tests for T-167 (--resume model, /clear, /resume,
/// kill-all-sessions via orchestrator).
///
/// Pure Dart (no Flutter): exercises the session argv selection, the
/// spawn-spec logic for clear vs resume, and the kill-all-sessions command
/// behaviour — all without spawning a real `claude` process.
library;
import 'dart:async';
import 'dart:io';
import 'package:clide/builtin/claude/src/session_naming.dart';
import 'package:clide/builtin/claude/src/session_orchestrator.dart';
import 'package:clide/builtin/claude/src/stream_json_session.dart';
import 'package:test/test.dart';
// ---------------------------------------------------------------------------
// Minimal fake process — same as session_orchestrator_test.dart.
// ---------------------------------------------------------------------------
class _FakeProc implements StreamJsonProcess {
final _ctl = StreamController<String>.broadcast();
final List<String> writes = [];
bool killed = false;
@override
Stream<String> get lines => _ctl.stream;
@override
void writeLine(String line) => writes.add(line);
@override
Future<void> kill() async => killed = true;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
ClaudeSessionOrchestrator _orch(List<_FakeProc> created) {
return ClaudeSessionOrchestrator(
processFactory: ({required sessionArgs, required cwd, env}) async {
final p = _FakeProc();
created.add(p);
return p;
},
);
}
SpawnSpec _spec(String id, {bool resume = false, String? transcriptPath}) => SpawnSpec(
id: id,
role: id,
sessionId: '$id-uuid',
cwd: '/repo',
resume: resume,
transcriptPath: transcriptPath,
);
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
void main() {
// ---- argv selection (D-77 / T-161) -------------------------------------
group('claudeLaunchArgs — argv selection', () {
// These mirror session_naming_test.dart but put the semantics in context.
test('/clear → fresh session — uses --session-id', () {
// /clear spawns a brand-new session: transcript does not exist yet.
final args = claudeLaunchArgs('new-uuid', resume: false);
expect(args, ['--session-id', 'new-uuid']);
});
test('/resume → existing session — uses --resume', () {
// /resume picks a past session whose transcript is already on disk.
final args = claudeLaunchArgs('past-uuid', resume: true);
expect(args, ['--resume', 'past-uuid']);
});
test('restart after transcript exists → --resume preserves continuity', () {
// On restart, the primary's transcript is on disk → resume:true.
const id = 'stable-uuid';
expect(claudeLaunchArgs(id, resume: true).first, '--resume');
});
test('secondary spawn → always --session-id (fresh)', () {
// Secondaries always start clean: freshSessionId() + resume:false.
final id = freshSessionId();
expect(claudeLaunchArgs(id, resume: false).first, '--session-id');
});
});
// ---- primarySessionId stability (migration safety) ----------------------
group('primarySessionId — stable across restart', () {
test('same repo root always yields the same UUID', () {
const root = '/home/user/projects/myapp';
expect(primarySessionId(root), primarySessionId(root));
});
test('different repos yield different UUIDs', () {
expect(primarySessionId('/home/user/a'), isNot(primarySessionId('/home/user/b')));
});
});
// ---- /clear — spawn a fresh session via orchestrator --------------------
group('/clear — fresh session via orchestrator', () {
late List<_FakeProc> created;
late ClaudeSessionOrchestrator orch;
setUp(() {
created = [];
orch = _orch(created);
});
tearDown(() => orch.dispose());
test('spawns a new session with resume:false (empty conversation)', () async {
final managed = await orch.spawn(_spec('primary', resume: false));
expect(managed.id, 'primary');
expect(managed.conversation.items, isEmpty);
expect(created, hasLength(1));
// The process receives --session-id (not --resume) in its init args.
// The factory was given the right spec; verify the session is new.
expect(managed.sessionId, 'primary-uuid');
});
test('close old + spawn new resets the session (clear flow)', () async {
await orch.spawn(_spec('primary', resume: false));
// /clear: close the current session and spawn a fresh one.
await orch.close('primary');
expect(orch.byId('primary'), isNull);
expect(created.first.killed, isTrue);
await orch.spawn(_spec('primary', resume: false));
expect(orch.sessions, hasLength(1));
expect(created, hasLength(2)); // a second process was created
});
});
// ---- /resume — bind to an existing session via orchestrator -------------
group('/resume — resume an existing session via orchestrator', () {
late List<_FakeProc> created;
late ClaudeSessionOrchestrator orch;
setUp(() {
created = [];
orch = _orch(created);
});
tearDown(() => orch.dispose());
test('spawns with resume:true and seeds conversation from transcript', () async {
final tmp = await Directory.systemTemp.createTemp('clide-resume-');
final file = File('${tmp.path}/session.jsonl');
await file.writeAsString(
'{"type":"user","uuid":"u1","timestamp":"2026-05-01T00:00:00Z","isSidechain":false,'
'"message":{"role":"user","content":"hello from the past"}}\n',
);
final managed = await orch.spawn(_spec(
'primary',
resume: true,
transcriptPath: file.path,
));
expect(managed.conversation.items, hasLength(1));
await tmp.delete(recursive: true);
});
test('close old + spawn resumed resets to picked session (resume flow)', () async {
await orch.spawn(_spec('primary', resume: false));
await orch.close('primary');
// /resume picked a past session id; re-spawn with resume:true.
final picked = SpawnSpec(
id: 'primary',
role: 'primary',
sessionId: 'picked-past-uuid',
cwd: '/repo',
resume: true,
);
final managed = await orch.spawn(picked);
expect(managed.sessionId, 'picked-past-uuid');
expect(created, hasLength(2));
});
});
// ---- claude.kill-all-sessions via orchestrator --------------------------
group('claude.kill-all-sessions via orchestrator (T-167)', () {
late List<_FakeProc> created;
late ClaudeSessionOrchestrator orch;
setUp(() {
created = [];
orch = _orch(created);
});
tearDown(() => orch.dispose());
test('kills the primary session through the orchestrator', () async {
await orch.spawn(_spec('primary'));
expect(orch.sessions, hasLength(1));
final ids = orch.sessions.map((m) => m.id).toList();
for (final id in ids) {
await orch.close(id);
}
expect(orch.sessions, isEmpty);
await Future<void>.delayed(Duration.zero);
expect(created.single.killed, isTrue);
});
test('kills primary + all secondaries and leaves orchestrator empty', () async {
await orch.spawn(_spec('primary'));
await orch.spawn(_spec('secondary-1'));
await orch.spawn(_spec('secondary-2'));
expect(orch.sessions, hasLength(3));
// Simulate what _killAllSessions does in extension.dart.
final ids = orch.sessions.map((m) => m.id).toList();
for (final id in ids) {
await orch.close(id);
}
expect(orch.sessions, isEmpty);
await Future<void>.delayed(Duration.zero);
expect(created.every((p) => p.killed), isTrue);
});
test('kill-all on empty orchestrator is a no-op', () async {
// No sessions — the loop is a no-op, no crash.
final ids = orch.sessions.map((m) => m.id).toList();
for (final id in ids) {
await orch.close(id);
}
expect(orch.sessions, isEmpty);
expect(created, isEmpty);
});
test('kill-all includes team sessions', () async {
await orch.spawn(SpawnSpec(
id: 'primary',
role: 'lead',
sessionId: 'primary-uuid',
cwd: '/repo',
team: true,
memberName: 'lead',
));
await orch.spawn(SpawnSpec(
id: 'teammate:tyre',
role: 'teammate',
sessionId: 'tyre-uuid',
cwd: '/repo',
team: true,
memberName: 'tyre',
));
expect(orch.sessions, hasLength(2));
// Broker has 2 agent members + 1 virtual 'user' member (T-180).
final agentMembers = orch.broker.members.where((m) => m.id != 'user');
expect(agentMembers, hasLength(2));
final ids = orch.sessions.map((m) => m.id).toList();
for (final id in ids) {
await orch.close(id);
}
expect(orch.sessions, isEmpty);
// Only the virtual 'user' member remains after closing all sessions.
final agentMembersAfter = orch.broker.members.where((m) => m.id != 'user');
expect(agentMembersAfter, isEmpty);
});
});
}