feat: migrate Flutter to Cupertino with daily logs and computer groups

This commit is contained in:
tongtong committed 2026-11-13 11:27:46 +08:00
1 parent c568749d23
commit 87e1fff59b
22 files changed
+808 -55

No files matched your search

@@ -0,0 +1,67 @@
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ads_manager/ui/app_widgets.dart';
import 'package:ads_manager/ui/manager_model.dart';
import 'package:ads_manager/ui/manager_view.dart';
import 'package:ads_manager/data/manager_repository.dart';
void main() {
testWidgets(
'Cupertino workbench shows computers without refresh layout shift',
(tester) async {
tester.view.physicalSize = const Size(1280, 820);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final m = ManagerModel();
m.devices = [
const Device('phone-one', false, agentId: 'home', computerName: '家里电脑'),
const Device(
'phone-two',
false,
agentId: 'office',
computerName: '办公室电脑',
),
];
final key = GlobalKey();
await tester.pumpWidget(
RepaintBoundary(
key: key,
child: AdsCupertinoApp(
home: ManagerView(model: m, onQuit: () async {}),
),
),
);
await tester.pumpAndSettle();
expect(find.textContaining('办公室电脑'), findsWidgets);
final before = tester.getTopLeft(find.text('phone-one'));
await m.action(() async {
m.refreshing = true;
});
await tester.pump();
expect(tester.getTopLeft(find.text('phone-one')), before);
await m.action(() async {
m.refreshing = false;
});
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
if (const bool.fromEnvironment('ADS_CAPTURE')) {
await tester.runAsync(() async {
final image =
await (key.currentContext!.findRenderObject()!
as RenderRepaintBoundary)
.toImage();
final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
await File('../work/flutter-cupertino.png')
.writeAsBytes(bytes!.buffer.asUint8List());
image.dispose();
});
}
await tester.pumpWidget(const SizedBox());
m.dispose();
},
);
}
+65
View File
@@ -0,0 +1,65 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:ads_manager/data/daily_log.dart';
import 'package:ads_manager/ui/manager_model.dart';
void main() {
test('memory and persisted logs redact connection credentials', () async {
final dir = await Directory.systemTemp.createTemp('ads-redact');
final model = ManagerModel(logWriter: DailyLog(dir));
try {
final secret = 'c' * 64;
model.repository.token = secret;
model.log('connection error $secret');
await model.diskLog.flush();
expect(model.logs.single, contains('[REDACTED]'));
final file = (await dir.list().toList()).whereType<File>().single;
final value = await file.readAsString();
expect(value, isNot(contains(secret)));
} finally {
model.dispose();
await dir.delete(recursive: true);
}
});
test('daily files, bounded size rotation and retention', () async {
final folder = await Directory.systemTemp.createTemp('ads-log-test');
try {
var now = DateTime(2026, 11, 13, 23, 59);
await File('${folder.path}/ads-2026-10-01.log').writeAsString('expired');
await File('${folder.path}/other.txt').writeAsString('keep');
final log = DailyLog(
folder,
maxBytes: 1,
maxParts: 2,
keepDays: 14,
clock: () => now,
);
log.add('first');
log.add('second');
log.add('third');
await log.flush();
expect(
await File('${folder.path}/ads-2026-11-13.log').readAsString(),
contains('third'),
);
expect(
await File('${folder.path}/ads-2026-11-13.1.log').readAsString(),
contains('second'),
);
expect(await File('${folder.path}/ads-2026-11-13.2.log').exists(), false);
expect(await File('${folder.path}/ads-2026-10-01.log').exists(), false);
expect(await File('${folder.path}/other.txt').exists(), true);
now = DateTime(2026, 11, 14);
log.add('new day');
await log.flush();
expect(
await File('${folder.path}/ads-2026-11-14.log').readAsString(),
contains('new day'),
);
expect(log.error, isNull);
} finally {
await folder.delete(recursive: true);
}
});
}
+1 -1
View File
@@ -10,7 +10,7 @@ Future<void> main(List<String> args) async {
repo.settings.relayOnly = args.contains('--relay-only');
final devices = await repo.devices();
stdout.writeln(
'API devices: ${devices.map((d) => "${d.id} busy=${d.busy}").join(", ")}',
'API devices: ${devices.map((d) => "${d.id} busy=${d.busy} computer=${d.computerName} agent=${d.agentId}").join(", ")}',
);
final nodes = await repo.get('v2/nodes') as Map<String, dynamic>;
stdout.writeln('Relay nodes: ${(nodes["nodes"] as List<dynamic>).length}');
+6 -2
View File
@@ -1,5 +1,9 @@
import 'dart:io';
import 'package:ads_manager/ui/app_widgets.dart' show AdsCupertinoApp;
import 'package:cupertino_ui/cupertino_ui.dart' show CupertinoTextField;
import 'package:material_ui/material_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ads_manager/data/manager_repository.dart';
@@ -49,13 +53,13 @@ void main() {
const Device('busy-phone', true),
];
await tester.pumpWidget(
MaterialApp(
AdsCupertinoApp(
home: ManagerView(model: m, onQuit: () async {}),
),
);
expect(find.text('test-phone'), findsOneWidget);
expect(find.text('其他客户端占用'), findsOneWidget);
await tester.enterText(find.byType(TextField), 'test');
await tester.enterText(find.byType(CupertinoTextField), 'test');
await tester.pumpAndSettle();
expect(find.text('busy-phone'), findsNothing);
await tester.tap(find.text('连接配置'));
+7 -2
View File
@@ -1,5 +1,7 @@
import 'dart:io';
import 'package:ads_manager/ui/app_widgets.dart' as ads;
import 'package:material_ui/material_ui.dart';
import 'package:ads_manager/ui/manager_model.dart';
import 'package:ads_manager/ui/manager_view.dart';
@@ -24,7 +26,10 @@ void main() {
),
),
);
final toggle = find.widgetWithText(SwitchListTile, '挂载为 USB 设备 · Windows');
final toggle = find.widgetWithText(
ads.SwitchListTile,
'挂载为 USB 设备 · Windows',
);
await tester.ensureVisible(toggle);
await tester.tap(toggle);
await tester.pumpAndSettle();
@@ -32,7 +37,7 @@ void main() {
expect(find.text('安装驱动'), findsOneWidget);
await tester.tap(find.text('暂不安装'));
await tester.pumpAndSettle();
expect(tester.widget<SwitchListTile>(toggle).value, false);
expect(tester.widget<ads.SwitchListTile>(toggle).value, false);
expect(model.repository.settings.usbMode, false);
await tester.pumpWidget(const SizedBox());
model.dispose();