initial commit

This commit is contained in:
betty2310
2026-10-20 12:45:50 +07:00
commit 5f3fb1543b
78 changed files with 2574 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
import 'package:device_code_app/device_code.dart';
import 'package:device_code_app/secret.dart';
import 'package:flutter_test/flutter_test.dart';
/// Reference vectors produced by running the real backend
/// `DeviceController.GetDeviceCode` (under faketime for each date).
/// The uppercase-MAC column intentionally differs: the MAC is hashed as typed.
const _macs = [
'74:25:84:c0:02:4a',
'00:00:00:00:00:00',
'ff:ff:ff:ff:ff:ff',
'a1:b2:c3:d4:e5:f6',
'74:25:84:C0:02:4A',
];
const _vectors = <String, List<String>>{
'20261020': ['24720116', '05200455', '56623865', '18749676', '63294203'],
'20261231': ['78353344', '41529234', '71527760', '38956422', '45456286'],
'20270101': ['55726780', '77367158', '08109311', '93484586', '89704380'],
'20280229': ['43234334', '83967713', '74278897', '83465745', '89773533'],
};
void main() {
group('generateDeviceCode matches backend vectors', () {
for (final entry in _vectors.entries) {
for (var i = 0; i < _macs.length; i++) {
test('${entry.key} ${_macs[i]} -> ${entry.value[i]}', () {
expect(
generateDeviceCode(
mac: _macs[i],
date: entry.key,
secret: deviceCodeSecret,
),
entry.value[i],
);
});
}
}
});
test('a different secret gives a different code (secret is really used)', () {
expect(
generateDeviceCode(mac: _macs[0], date: '20261020', secret: 'wrong'),
isNot('24720116'),
);
});
test('formatDateForCode is yyyyMMdd with zero padding', () {
expect(formatDateForCode(DateTime(2027, 1, 1)), '20270101');
expect(formatDateForCode(DateTime(2028, 2, 29)), '20280229');
});
group('formatMac mirrors the web page', () {
test('strips, lowercases, caps at 12 and joins pairs', () {
expect(formatMac('742584C0024A'), '74:25:84:c0:02:4a');
expect(formatMac('74-25-84-C0-02-4A'), '74:25:84:c0:02:4a');
expect(formatMac('74:25:84:c0:02:4a:ff:ee'), '74:25:84:c0:02:4a');
expect(formatMac(' zz 74 25 '), '74:25');
});
test('formats partial input progressively', () {
expect(formatMac(''), '');
expect(formatMac('7'), '7');
expect(formatMac('742'), '74:2');
expect(formatMac('7425'), '74:25');
});
test('isCompleteMac only accepts 12 formatted hex digits', () {
expect(isCompleteMac('74:25:84:c0:02:4a'), isTrue);
expect(isCompleteMac('74:25:84:c0:02:4'), isFalse);
expect(isCompleteMac('74:25:84:C0:02:4A'), isFalse);
});
});
}
+114
View File
@@ -0,0 +1,114 @@
import 'package:device_code_app/app.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final fixedDate = DateTime(2026, 10, 20);
// Each test runs once per platform; the variant sets and restores
// debugDefaultTargetPlatformOverride around the test body.
const variant = TargetPlatformVariant({
TargetPlatform.android,
TargetPlatform.iOS,
});
Future<void> pumpApp(WidgetTester tester) async {
await tester.pumpWidget(DeviceCodeApp(initialDate: fixedDate));
await tester.pumpAndSettle();
}
testWidgets('uses the native widget set', (tester) async {
await pumpApp(tester);
final platform = debugDefaultTargetPlatformOverride;
if (platform == TargetPlatform.iOS) {
expect(find.byType(CupertinoApp), findsOneWidget);
expect(find.byType(CupertinoTextFormFieldRow), findsOneWidget);
expect(find.byType(MaterialApp), findsNothing);
} else {
expect(find.byType(MaterialApp), findsOneWidget);
expect(find.byType(TextField), findsOneWidget);
expect(
Theme.of(tester.element(find.byType(Scaffold))).useMaterial3,
isTrue,
);
expect(find.byType(CupertinoApp), findsNothing);
}
}, variant: variant);
testWidgets('shows the date and no code until the MAC is complete', (
tester,
) async {
await pumpApp(tester);
expect(find.textContaining('20 Oct 2026'), findsWidgets);
expect(find.textContaining('20261020'), findsWidgets);
expect(find.text('••••••••'), findsOneWidget);
expect(find.text('24720116'), findsNothing);
await tester.enterText(find.byKey(const Key('mac-field')), '742584C002');
await tester.pumpAndSettle();
expect(find.text('••••••••'), findsOneWidget);
expect(find.text('24720116'), findsNothing);
}, variant: variant);
testWidgets(
'typing unformatted uppercase 742584C0024A on 20261020 shows 24720116',
(tester) async {
await pumpApp(tester);
await tester.enterText(
find.byKey(const Key('mac-field')),
'742584C0024A',
);
await tester.pumpAndSettle();
final field = tester.widget<EditableText>(
find.descendant(
of: find.byKey(const Key('mac-field')),
matching: find.byType(EditableText),
),
);
expect(field.controller.text, '74:25:84:c0:02:4a');
final code = tester.widget<SelectableText>(
find.byKey(const Key('device-code')),
);
expect(code.data, '24720116');
expect(find.text('24720116'), findsOneWidget);
},
variant: variant,
);
testWidgets('copy button puts the code on the clipboard', (tester) async {
await pumpApp(tester);
String? copied;
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method == 'Clipboard.setData') {
copied = (call.arguments as Map)['text'] as String;
}
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
null,
),
);
await tester.enterText(
find.byKey(const Key('mac-field')),
'ff:ff:ff:ff:ff:ff',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('copy-button')));
await tester.pumpAndSettle();
expect(copied, '56623865');
expect(find.text('Copied'), findsOneWidget);
}, variant: variant);
}