Restore local chat layouts and add compact or hidden chat sidebar
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/server_scope.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_sidebar_controls.dart';
|
||||
import 'package:pipelines_app/features/chat/screen.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
|
||||
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
||||
import 'package:pipelines_app/shell/destination.dart';
|
||||
|
||||
import 'conversation/chat_thread_fixtures.dart';
|
||||
|
||||
class _Gateway extends FakeChatGateway {
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
if (type == 'chat_sessions') {
|
||||
return Future.value({
|
||||
'chat_sessions': [
|
||||
for (final id in ['a', 'b', 'linked'])
|
||||
{'id': id, 'title': 'Chat $id'},
|
||||
],
|
||||
});
|
||||
}
|
||||
if (type == 'chat_shells') return Future.value({'chat_shells': []});
|
||||
return super.call(type, fields);
|
||||
}
|
||||
}
|
||||
|
||||
ChatWorkspaceController _workspace(WidgetTester tester) =>
|
||||
tester.widget<ChatWorkspace>(find.byType(ChatWorkspace)).controller;
|
||||
|
||||
Future<void> _mount(
|
||||
WidgetTester tester,
|
||||
_Gateway gateway, {
|
||||
String? route,
|
||||
bool enabled = true,
|
||||
ValueChanged<String>? onSelected,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: AdaptiveShell(
|
||||
destination: AppDestination.chat,
|
||||
destinationBodies: {
|
||||
AppDestination.chat: (_) => ChatScreen(
|
||||
gateway: gateway,
|
||||
persistWorkspace: enabled,
|
||||
initialSessionId: route,
|
||||
onSessionSelected: onSelected,
|
||||
),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final width in [1800.0, 430.0]) {
|
||||
testWidgets('restores layout and saves geometry/sidebar on $width host', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = Size(width, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final saved = ChatWorkspaceController()..open('a');
|
||||
saved.splitActive('b', ChatSplitAxis.vertical);
|
||||
final source = saved.snapshot(sidebarMode: 'hidden')!;
|
||||
SharedPreferences.setMockInitialValues({
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): source
|
||||
.encode(),
|
||||
});
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
bindServerIdentity(gateway, 'server-a');
|
||||
addTearDown(gateway.close);
|
||||
await _mount(tester, gateway);
|
||||
final controller = _workspace(tester);
|
||||
expect(controller.panes.map((pane) => pane.sessionId), ['a', 'b']);
|
||||
expect(controller.activePane?.sessionId, 'b');
|
||||
if (width > 1000) {
|
||||
expect(
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.mode,
|
||||
ChatSidebarMode.hidden,
|
||||
);
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.onChanged(ChatSidebarMode.compact);
|
||||
} else {
|
||||
expect(find.byType(ChatSidebarButton), findsNothing);
|
||||
}
|
||||
controller.resize(controller.root!.id, .42);
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
final store = SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: 'server-a',
|
||||
);
|
||||
final persisted = (await store.read())!;
|
||||
expect((persisted.root as ChatWorkspaceSplit).ratio, .42);
|
||||
expect(persisted.sidebarMode, width > 1000 ? 'compact' : 'hidden');
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
await _mount(tester, gateway);
|
||||
expect((_workspace(tester).root as ChatWorkspaceSplit).ratio, .42);
|
||||
if (width > 1000) {
|
||||
expect(
|
||||
tester
|
||||
.widget<ChatSidebarButton>(find.byType(ChatSidebarButton).first)
|
||||
.mode,
|
||||
ChatSidebarMode.compact,
|
||||
);
|
||||
}
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('switching logical servers restores only the new server layout', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final savedA = ChatWorkspaceController()..open('a');
|
||||
final savedB = ChatWorkspaceController()..open('b');
|
||||
SharedPreferences.setMockInitialValues({
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-a'): savedA
|
||||
.snapshot()!
|
||||
.encode(),
|
||||
scopedServerKey(chatWorkspacePreferenceKey, 'server-b'): savedB
|
||||
.snapshot()!
|
||||
.encode(),
|
||||
});
|
||||
final gatewayA = _Gateway()..history = (_) async => chatPage([]);
|
||||
final gatewayB = _Gateway()..history = (_) async => chatPage([]);
|
||||
bindServerIdentity(gatewayA, 'server-a');
|
||||
bindServerIdentity(gatewayB, 'server-b');
|
||||
addTearDown(gatewayA.close);
|
||||
addTearDown(gatewayB.close);
|
||||
await _mount(tester, gatewayA);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'a');
|
||||
await _mount(tester, gatewayB, route: 'linked');
|
||||
expect(_workspace(tester).panes.map((pane) => pane.sessionId), [
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'linked');
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
final original = (await const SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: 'server-a',
|
||||
).read())!;
|
||||
expect((original.root as ChatWorkspacePane).sessionId, 'a');
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
|
||||
testWidgets('initial deep link wins without discarding restored chats', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1800, 1000);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final saved = ChatWorkspaceController()..open('a');
|
||||
saved.splitActive('b', ChatSplitAxis.vertical);
|
||||
SharedPreferences.setMockInitialValues({
|
||||
chatWorkspacePreferenceKey: saved.snapshot()!.encode(),
|
||||
});
|
||||
final gateway = _Gateway()..history = (_) async => chatPage([]);
|
||||
addTearDown(gateway.close);
|
||||
final selected = <String>[];
|
||||
await _mount(tester, gateway, route: 'linked', onSelected: selected.add);
|
||||
expect(_workspace(tester).panes.map((pane) => pane.sessionId), [
|
||||
'a',
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(_workspace(tester).activePane?.sessionId, 'linked');
|
||||
expect(selected, isNot(contains('b')));
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_binding.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_snapshot.dart';
|
||||
import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart';
|
||||
|
||||
class _Store implements ChatWorkspaceStore {
|
||||
final readResult = Completer<ChatWorkspaceSnapshot?>();
|
||||
final writes = <ChatWorkspaceSnapshot>[];
|
||||
final gates = <Completer<void>>[];
|
||||
bool blockWrites = false;
|
||||
@override
|
||||
Future<ChatWorkspaceSnapshot?> read() => readResult.future;
|
||||
@override
|
||||
Future<bool> write(ChatWorkspaceSnapshot snapshot) async {
|
||||
writes.add(snapshot);
|
||||
if (blockWrites) {
|
||||
final gate = Completer<void>();
|
||||
gates.add(gate);
|
||||
await gate.future;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
ChatWorkspaceSnapshot _saved() {
|
||||
final controller = ChatWorkspaceController()..open('a');
|
||||
controller.splitActive('b', ChatSplitAxis.vertical);
|
||||
return controller.snapshot(sidebarMode: 'hidden')!;
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('deep link preserves stored panes and becomes active', () async {
|
||||
final controller = ChatWorkspaceController()..open('linked');
|
||||
final store = _Store()..readResult.complete(_saved());
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (snapshot) => mode = snapshot.sidebarMode,
|
||||
);
|
||||
await binding.start(initialSessionId: 'linked');
|
||||
expect(controller.panes.map((pane) => pane.sessionId), [
|
||||
'a',
|
||||
'b',
|
||||
'linked',
|
||||
]);
|
||||
expect(controller.activePane?.sessionId, 'linked');
|
||||
expect(mode, 'hidden');
|
||||
await binding.flush();
|
||||
expect(store.writes.single.activePaneId, controller.activePaneId);
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
});
|
||||
|
||||
test('pending load cannot overwrite chat or sidebar user changes', () async {
|
||||
for (final sidebarEdit in [false, true]) {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store();
|
||||
var mode = 'expanded';
|
||||
var restored = false;
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (_) => restored = true,
|
||||
);
|
||||
final loading = binding.start();
|
||||
if (sidebarEdit) {
|
||||
mode = 'compact';
|
||||
binding.changed();
|
||||
} else {
|
||||
controller.open('user');
|
||||
}
|
||||
store.readResult.complete(_saved());
|
||||
await loading;
|
||||
expect(restored, isFalse);
|
||||
expect(controller.activePane?.sessionId, sidebarEdit ? null : 'user');
|
||||
await binding.flush();
|
||||
expect(store.writes.single.sidebarMode, mode);
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
test(
|
||||
'ordered writes include resize and sidebar; dispose flushes edits',
|
||||
() async {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store()..readResult.complete(_saved());
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (snapshot) => mode = snapshot.sidebarMode,
|
||||
);
|
||||
await binding.start();
|
||||
store.blockWrites = true;
|
||||
final root = controller.root! as ChatWorkspaceSplit;
|
||||
controller.resize(root.id, .31);
|
||||
final firstSave = binding.flush();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
mode = 'compact';
|
||||
binding.changed();
|
||||
final secondSave = binding.flush();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(1));
|
||||
store.gates.first.complete();
|
||||
await firstSave;
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(2));
|
||||
expect((store.writes.last.root as ChatWorkspaceSplit).ratio, .31);
|
||||
expect(store.writes.last.sidebarMode, 'compact');
|
||||
store.gates.last.complete();
|
||||
await secondSave;
|
||||
controller.close(controller.panes.last.id);
|
||||
binding.dispose();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(store.writes, hasLength(3));
|
||||
store.gates.last.complete();
|
||||
controller.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'debounces mutations and preserves an edit when leaving during load',
|
||||
(tester) async {
|
||||
final controller = ChatWorkspaceController();
|
||||
final store = _Store()..readResult.complete(null);
|
||||
var mode = 'expanded';
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => mode,
|
||||
onRestored: (_) {},
|
||||
);
|
||||
await binding.start();
|
||||
controller.open('a');
|
||||
controller.splitActive('b', ChatSplitAxis.horizontal);
|
||||
mode = 'hidden';
|
||||
binding.changed();
|
||||
await tester.pump(const Duration(milliseconds: 200));
|
||||
expect(store.writes, isEmpty);
|
||||
await tester.pump(const Duration(milliseconds: 100));
|
||||
expect(store.writes, hasLength(1));
|
||||
expect(store.writes.single.sidebarMode, 'hidden');
|
||||
binding.dispose();
|
||||
controller.dispose();
|
||||
|
||||
final pendingController = ChatWorkspaceController();
|
||||
final pendingStore = _Store();
|
||||
final pending = ChatWorkspaceBinding(
|
||||
controller: pendingController,
|
||||
store: pendingStore,
|
||||
sidebarMode: () => 'expanded',
|
||||
onRestored: (_) => fail('disposed binding restored'),
|
||||
);
|
||||
final loading = pending.start();
|
||||
pendingController.open('user');
|
||||
pending.dispose();
|
||||
await tester.pump();
|
||||
expect(pendingStore.writes.single.root, isA<ChatWorkspacePane>());
|
||||
pendingStore.readResult.complete(_saved());
|
||||
await loading;
|
||||
pendingController.dispose();
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'leaving during unread storage does not overwrite stored workspace',
|
||||
() async {
|
||||
final controller = ChatWorkspaceController()..open('route');
|
||||
final store = _Store();
|
||||
final binding = ChatWorkspaceBinding(
|
||||
controller: controller,
|
||||
store: store,
|
||||
sidebarMode: () => 'expanded',
|
||||
onRestored: (_) => fail('disposed binding restored'),
|
||||
);
|
||||
final loading = binding.start(initialSessionId: 'route');
|
||||
binding.dispose();
|
||||
store.readResult.complete(_saved());
|
||||
await loading;
|
||||
expect(store.writes, isEmpty);
|
||||
controller.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user