Move editor UI into the app and release verified Core dependencies #14

Merged
lingjhf merged 14 commits from codex/workspace-inspector-refinements into main 2026-10-11 19:59:50 +00:00
Showing only changes of commit d81c4b19d7 - Show all commits
+124 -36
View File
@@ -22,6 +22,12 @@ import 'package:flutter_test/flutter_test.dart';
class _Model implements ModelAdapter {
final requests = <ModelRequest>[];
final streams = <StreamController<ModelEvent>>[];
final _requestWaiters = <int, Completer<void>>{};
Future<void> waitForRequests(int count) {
if (requests.length >= count) return Future<void>.value();
return _requestWaiters.putIfAbsent(count, Completer<void>.new).future;
}
@override
Stream<ModelEvent> stream(
@@ -31,6 +37,9 @@ class _Model implements ModelAdapter {
requests.add(request);
final stream = StreamController<ModelEvent>();
streams.add(stream);
for (final count in _requestWaiters.keys.toList()) {
if (requests.length >= count) _requestWaiters.remove(count)!.complete();
}
unawaited(cancellation.whenCancelled.then((_) => stream.close()));
return stream.stream;
}
@@ -82,12 +91,46 @@ class _DelayedRestoreConversation extends AgentConversationService {
}
}
Future<void> _until(bool Function() check) async {
for (var attempt = 0; attempt < 1000; attempt++) {
if (check()) return;
await Future<void>.delayed(const Duration(milliseconds: 1));
Future<void> _waitForRequests(
ProviderContainer container,
ChatControllerProvider provider,
_Model model,
int count,
) async {
if (model.requests.length >= count) return;
final settled = Completer<void>();
void onState(ChatState state) {
if (!state.busy && model.requests.length < count && !settled.isCompleted) {
settled.completeError(
TestFailure('Send settled before model request $count: ${state.error}'),
);
}
}
final subscription = container.listen(provider, (_, state) => onState(state));
onState(container.read(provider));
try {
await Future.any([model.waitForRequests(count), settled.future]);
} finally {
subscription.close();
}
}
Future<void> _waitForState(
ProviderContainer container,
ChatControllerProvider provider,
bool Function(ChatState) check,
) async {
if (check(container.read(provider))) return;
final ready = Completer<void>();
final subscription = container.listen(provider, (_, state) {
if (check(state) && !ready.isCompleted) ready.complete();
});
try {
await ready.future;
} finally {
subscription.close();
}
fail('Expected state was not reached');
}
void main() {
@@ -95,6 +138,7 @@ void main() {
late Directory conversationsRoot;
late ConversationRepository conversationRepository;
late ProviderContainer container;
late AgentConversationRegistry registry;
setUp(() async {
model = _Model();
conversationsRoot = await Directory.systemTemp.createTemp('chat-test-');
@@ -131,8 +175,11 @@ void main() {
),
],
);
registry = container.read(agentConversationRegistryProvider);
});
tearDown(() async {
// Disposal initiates cancellation, but files remain open until the run settles.
await registry.cancelAll();
container.dispose();
await conversationsRoot.delete(recursive: true);
});
@@ -391,7 +438,12 @@ void main() {
);
await tester.runAsync(() async {
await tester.tap(find.byTooltip('发送消息'));
await _until(() => model.requests.isNotEmpty);
await _waitForRequests(
container,
chatControllerProvider('lib'),
model,
1,
);
});
await tester.pumpAndSettle();
expect(model.requests.single.messages.single.text, '测试发送');
@@ -461,11 +513,10 @@ void main() {
);
await tester.runAsync(() async {
model.streams.single.add(const TextDelta('正在生成的内容'));
await _until(
() => container
.read(chatControllerProvider('lib'))
.partialResponse
.isNotEmpty,
await _waitForState(
container,
chatControllerProvider('lib'),
(state) => state.partialResponse.isNotEmpty,
);
});
await tester.pumpAndSettle();
@@ -473,7 +524,11 @@ void main() {
await tester.runAsync(() async {
await tester.tap(find.byTooltip('停止回复'));
await container.read(chatControllerProvider('lib').notifier).stop();
await _until(() => !container.read(chatControllerProvider('lib')).busy);
await _waitForState(
container,
chatControllerProvider('lib'),
(state) => !state.busy,
);
});
await tester.pumpAndSettle();
expect(find.text('回复已中断'), findsOneWidget);
@@ -514,7 +569,11 @@ void main() {
addTearDown(subscription.close);
await tester.pump();
await tester.runAsync(
() => _until(() => isolated.read(provider).messages.length == 2),
() => _waitForState(
isolated,
provider,
(state) => state.messages.length == 2,
),
);
final controller = isolated.read(provider.notifier);
expect(isolated.read(provider).messages.last.elapsedMs, 23000);
@@ -621,7 +680,12 @@ void main() {
await tester.pump();
await tester.runAsync(() async {
await tester.tap(find.byTooltip('发送消息'));
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(
container,
chatControllerProvider('project'),
model,
1,
);
});
await tester.pump();
expect(find.byKey(const Key('test-project-selector')), findsNothing);
@@ -629,8 +693,10 @@ void main() {
await tester.runAsync(() async {
model.streams.single.add(ModelResponse(text: '已经开始'));
await model.streams.single.close();
await _until(
() => !container.read(chatControllerProvider('project')).busy,
await _waitForState(
container,
chatControllerProvider('project'),
(state) => !state.busy,
);
});
await tester.pumpAndSettle();
@@ -697,7 +763,12 @@ void main() {
);
await tester.runAsync(() async {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await _until(() => model.requests.isNotEmpty);
await _waitForRequests(
container,
chatControllerProvider('lib'),
model,
1,
);
});
await tester.pumpAndSettle();
expect(
@@ -709,7 +780,11 @@ void main() {
await tester.runAsync(() async {
model.streams.single.add(ModelResponse(text: 'ok'));
await model.streams.single.close();
await _until(() => !container.read(chatControllerProvider('lib')).busy);
await _waitForState(
container,
chatControllerProvider('lib'),
(state) => !state.busy,
);
});
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
@@ -725,11 +800,15 @@ void main() {
addTearDown(subscription.close);
final controller = container.read(provider.notifier);
final sending = controller.send('你好', timelineId: 'first');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
await controller.send('重复', timelineId: 'first');
expect(model.requests, hasLength(1));
model.streams[0].add(const TextDelta('你好呀'));
await _until(() => container.read(provider).partialResponse == '你好呀');
await _waitForState(
container,
provider,
(state) => state.partialResponse == '你好呀',
);
expect(container.read(provider).busy, isTrue);
model.streams[0].add(ModelResponse(text: '你好呀!'));
await model.streams[0].close();
@@ -738,7 +817,7 @@ void main() {
expect(container.read(provider).partialResponse, isEmpty);
final second = controller.send('接着说', timelineId: 'second');
await _until(() => model.requests.length == 2);
await _waitForRequests(container, provider, model, 2);
expect(model.requests[1].messages.map((m) => m.text), [
'你好',
'你好呀!',
@@ -758,7 +837,7 @@ void main() {
final other = container
.read(otherProvider.notifier)
.send('新会话', timelineId: 'timeline');
await _until(() => model.requests.length == 3);
await _waitForRequests(container, otherProvider, model, 3);
expect(model.requests.last.messages.map((m) => m.text), ['新会话']);
model.streams[2].add(ModelResponse(text: '新回答'));
await model.streams[2].close();
@@ -773,16 +852,20 @@ void main() {
addTearDown(subscription.close);
final controller = container.read(provider.notifier);
final sending = controller.send('开始', timelineId: 'timeline');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
model.streams[0].add(const TextDelta('部分回复'));
await _until(() => container.read(provider).partialResponse.isNotEmpty);
await _waitForState(
container,
provider,
(state) => state.partialResponse.isNotEmpty,
);
controller.stop();
await sending;
expect(container.read(provider).busy, isFalse);
expect(container.read(provider).messages.last.text, '部分回复');
expect(container.read(provider).messages.last.interrupted, isTrue);
final next = controller.send('再来', timelineId: 'timeline');
await _until(() => model.streams.length == 2);
await _waitForRequests(container, provider, model, 2);
model.streams[1].add(ModelResponse(text: '完成'));
await model.streams[1].close();
await next;
@@ -805,14 +888,14 @@ void main() {
final first = container
.read(firstProvider.notifier)
.send('第一条', timelineId: 'timeline');
await _until(() => model.streams.length == 1);
await _waitForRequests(container, firstProvider, model, 1);
model.streams[0].add(ModelResponse(text: '第一答'));
await model.streams[0].close();
await first;
final second = container
.read(secondProvider.notifier)
.send('第二条', timelineId: 'timeline');
await _until(() => model.streams.length == 2);
await _waitForRequests(container, secondProvider, model, 2);
expect(model.requests[1].messages.map((message) => message.text), ['第二条']);
model.streams[1].add(ModelResponse(text: '第二答'));
await model.streams[1].close();
@@ -835,7 +918,7 @@ void main() {
final first = container
.read(provider.notifier)
.send('保留这段对话', timelineId: 'timeline');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
model.streams.single.add(ModelResponse(text: '已经保存'));
await model.streams.single.close();
await first;
@@ -877,13 +960,18 @@ void main() {
),
],
);
registry = container.read(agentConversationRegistryProvider);
final restoredProvider = chatControllerProvider(
'project',
conversationId: conversationId,
);
subscription = container.listen(restoredProvider, (_, _) {});
addTearDown(subscription.close);
await _until(() => container.read(restoredProvider).messages.length == 2);
await _waitForState(
container,
restoredProvider,
(state) => state.messages.length == 2,
);
expect(
container
.read(restoredProvider)
@@ -895,7 +983,7 @@ void main() {
final continued = container
.read(restoredProvider.notifier)
.send('继续', timelineId: 'timeline');
await _until(() => model.requests.length == 2);
await _waitForRequests(container, restoredProvider, model, 2);
expect(model.requests.last.messages.map((message) => message.text), [
'保留这段对话',
'已经保存',
@@ -949,7 +1037,7 @@ void main() {
final first = switchingContainer
.read(provider.notifier)
.send('旧会话', timelineId: 'timeline');
await _until(() => firstModel.streams.isNotEmpty);
await _waitForRequests(switchingContainer, provider, firstModel, 1);
firstModel.streams.single.add(ModelResponse(text: '旧回答'));
await firstModel.streams.single.close();
await first;
@@ -964,7 +1052,7 @@ void main() {
final second = switchingContainer
.read(provider.notifier)
.send('继续会话', timelineId: 'timeline');
await _until(() => firstModel.streams.length == 2);
await _waitForRequests(switchingContainer, provider, firstModel, 2);
expect(firstModel.requests.last.messages.map((message) => message.text), [
'旧会话',
'旧回答',
@@ -1004,7 +1092,7 @@ void main() {
final sending = container
.read(provider.notifier)
.send('hello', timelineId: 'timeline');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
model.streams.single.addError(entry.key);
await model.streams.single.close();
await sending;
@@ -1018,7 +1106,7 @@ void main() {
addTearDown(subscription.close);
final controller = container.read(provider.notifier);
final sending = controller.send('开始', timelineId: 'timeline');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
model.streams[0].addError(StateError('sensitive remote payload'));
await model.streams[0].close();
await sending;
@@ -1032,7 +1120,7 @@ void main() {
['开始'],
);
final next = controller.send('重试', timelineId: 'timeline');
await _until(() => model.streams.length == 2);
await _waitForRequests(container, provider, model, 2);
model.streams[1].add(ModelResponse(text: '成功'));
await model.streams[1].close();
await next;
@@ -1048,7 +1136,7 @@ void main() {
await early;
expect(container.read(provider).busy, isFalse);
final later = controller.send('再次开始', timelineId: 'timeline');
await _until(() => model.streams.isNotEmpty);
await _waitForRequests(container, provider, model, 1);
subscription.close();
container.dispose();
await later;