2026-11-22 14:53:33 +08:00
|
|
|
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,
|
2026-11-22 15:23:21 +08:00
|
|
|
{String status = 'running',
|
|
|
|
|
int createdAt = 0,
|
|
|
|
|
String cwd = '/repo',
|
|
|
|
|
String purpose = 'terminal'}) =>
|
2026-11-22 14:53:33 +08:00
|
|
|
ServerTerminal(
|
2026-11-22 15:23:21 +08:00
|
|
|
sessionId: id,
|
|
|
|
|
cwd: cwd,
|
|
|
|
|
status: status,
|
|
|
|
|
createdAt: createdAt,
|
|
|
|
|
purpose: purpose);
|
|
|
|
|
|
|
|
|
|
SavedTerminalTab _saved(String id, String label) => SavedTerminalTab(id, label);
|
2026-11-22 14:53:33 +08:00
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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');
|
2026-11-22 14:53:33 +08:00
|
|
|
});
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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), ['构建', '日志']);
|
2026-11-22 14:53:33 +08:00
|
|
|
});
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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');
|
2026-11-22 14:53:33 +08:00
|
|
|
});
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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);
|
2026-11-22 14:53:33 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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 {
|
2026-11-22 14:53:33 +08:00
|
|
|
SharedPreferences.setMockInitialValues({});
|
|
|
|
|
final storage = Storage();
|
|
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
expect(await storage.terminalTabs('srv_a', '/repo'), isNull);
|
2026-11-22 14:53:33 +08:00
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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');
|
2026-11-22 14:53:33 +08:00
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
// Another server's directory is a different set.
|
|
|
|
|
expect(await storage.terminalTabs('srv_b', '/repo'), isNull);
|
2026-11-22 14:53:33 +08:00
|
|
|
|
2026-11-22 15:23:21 +08:00
|
|
|
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');
|
2026-11-22 14:53:33 +08:00
|
|
|
});
|
|
|
|
|
}
|