A directory now has a TerminalWorkspace holding a list of tabs instead of a
single session. The status bar's left side became a dropdown: it shows the
active tab and its state ("终端 2 · 运行中") and lists the other tabs (the
active one checked) plus "新建终端". New tabs take the smallest free number,
so closing one frees its name.
Only the active tab keeps a socket: switching detaches the old tab and
attaches the new one, whose screen is replayed from the server snapshot; the
shell itself keeps running. The tab's model stays in memory, so switching
back is immediate.
The tab list (ids, labels, active one) is saved per connection and directory.
On the first open the server's sessions for the directory are reconciled with
it: saved labels and order are kept for sessions that still exist, sessions
opened elsewhere (web UI) are appended in creation order, and tabs whose
session is gone are dropped. Project-action command runners are not shown as
tabs. The 0.4.2 single-session value is read as one tab.
0.4.3 (403).
127 lines
4.5 KiB
Dart
127 lines
4.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:openchamber/api/terminal.dart';
|
|
import 'package:openchamber/storage.dart';
|
|
import 'package:openchamber/terminal_state.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
ServerTerminal _term(String id,
|
|
{String status = 'running',
|
|
int createdAt = 0,
|
|
String cwd = '/repo',
|
|
String purpose = 'terminal'}) =>
|
|
ServerTerminal(
|
|
sessionId: id,
|
|
cwd: cwd,
|
|
status: status,
|
|
createdAt: createdAt,
|
|
purpose: purpose);
|
|
|
|
SavedTerminalTab _saved(String id, String label) => SavedTerminalTab(id, label);
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
group('planTabs', () {
|
|
test('nothing saved and nothing on the server means one new tab', () {
|
|
final plans =
|
|
TerminalWorkspace.planTabs(saved: const [], server: const []);
|
|
expect(plans, hasLength(1));
|
|
expect(plans.single.id, isNull);
|
|
expect(plans.single.label, '终端 1');
|
|
});
|
|
|
|
test('saved labels stick to the sessions that are still there', () {
|
|
final plans = TerminalWorkspace.planTabs(
|
|
saved: [_saved('a', '构建'), _saved('b', '日志')],
|
|
server: [_term('a', createdAt: 1), _term('b', createdAt: 2)],
|
|
);
|
|
expect(plans.map((p) => p.id), ['a', 'b']);
|
|
expect(plans.map((p) => p.label), ['构建', '日志']);
|
|
});
|
|
|
|
test('a saved session the server no longer has is dropped', () {
|
|
final plans = TerminalWorkspace.planTabs(
|
|
saved: [_saved('gone', '终端 1'), _saved('alive', '终端 2')],
|
|
server: [_term('alive', createdAt: 1)],
|
|
);
|
|
expect(plans.map((p) => p.id), ['alive']);
|
|
expect(plans.single.label, '终端 2');
|
|
});
|
|
|
|
test('sessions opened elsewhere become tabs in creation order', () {
|
|
final plans = TerminalWorkspace.planTabs(
|
|
saved: const [],
|
|
server: [
|
|
_term('new', createdAt: 30),
|
|
_term('old', createdAt: 10),
|
|
_term('mid', createdAt: 20),
|
|
_term('action', createdAt: 5, purpose: 'project-action'),
|
|
],
|
|
);
|
|
expect(plans.map((p) => p.id), ['old', 'mid', 'new']);
|
|
expect(plans.map((p) => p.label), ['终端 1', '终端 2', '终端 3']);
|
|
});
|
|
|
|
test('a failed listing keeps the saved tabs', () {
|
|
final plans = TerminalWorkspace.planTabs(
|
|
saved: [_saved('a', '终端 1'), _saved('b', '终端 2')],
|
|
server: null,
|
|
);
|
|
expect(plans.map((p) => p.id), ['a', 'b']);
|
|
expect(plans.map((p) => p.label), ['终端 1', '终端 2']);
|
|
});
|
|
|
|
test('a failed listing with nothing saved still opens one tab', () {
|
|
final plans = TerminalWorkspace.planTabs(saved: const [], server: null);
|
|
expect(plans, hasLength(1));
|
|
expect(plans.single.id, isNull);
|
|
});
|
|
});
|
|
|
|
group('freeLabel', () {
|
|
test('picks the first unused number so a closed tab frees its name', () {
|
|
expect(TerminalWorkspace.freeLabel([]), '终端 1');
|
|
expect(TerminalWorkspace.freeLabel(['终端 1', '终端 3']), '终端 2');
|
|
expect(TerminalWorkspace.freeLabel(['终端 1', '终端 2']), '终端 3');
|
|
expect(TerminalWorkspace.freeLabel(['构建']), '终端 1');
|
|
});
|
|
});
|
|
|
|
test('terminal tabs are kept per connection and directory', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final storage = Storage();
|
|
|
|
expect(await storage.terminalTabs('srv_a', '/repo'), isNull);
|
|
|
|
await storage.saveTerminalTabs(
|
|
'srv_a',
|
|
'/repo',
|
|
const SavedTerminalTabs(
|
|
[SavedTerminalTab('a', '终端 1'), SavedTerminalTab('b', '构建')],
|
|
'b',
|
|
),
|
|
);
|
|
final loaded = await storage.terminalTabs('srv_a', '/repo');
|
|
expect(loaded, isNotNull);
|
|
expect(loaded!.tabs.map((t) => t.id), ['a', 'b']);
|
|
expect(loaded.tabs.map((t) => t.label), ['终端 1', '构建']);
|
|
expect(loaded.activeId, 'b');
|
|
|
|
// Another server's directory is a different set.
|
|
expect(await storage.terminalTabs('srv_b', '/repo'), isNull);
|
|
|
|
await storage.clearTerminalTabs('srv_a', '/repo');
|
|
expect(await storage.terminalTabs('srv_a', '/repo'), isNull);
|
|
});
|
|
|
|
test('a 0.4.2 single-session value is read as one tab', () async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'openchamber.terminalSessions.srv_a': '{"\\/repo":"term-1"}',
|
|
});
|
|
final loaded = await Storage().terminalTabs('srv_a', '/repo');
|
|
expect(loaded, isNotNull);
|
|
expect(loaded!.tabs.single.id, 'term-1');
|
|
expect(loaded.activeId, 'term-1');
|
|
});
|
|
}
|