Files
flutter_server_box/test/uptime_test.dart
lollipopkit🏳️‍⚧️ 3a615449e3 feat: Windows compatibility (#836)
* feat: win compatibility

* fix

* fix: uptime parse

* opt.: linux uptime accuracy

* fix: windows temperature fetching

* opt.

* opt.: powershell exec

* refactor: address PR review feedback and improve code quality

### Major Improvements:
- **Refactored Windows status parsing**: Broke down large `_getWindowsStatus` method into 13 smaller, focused helper methods for better maintainability and readability
- **Extracted system detection logic**: Created dedicated `SystemDetector` helper class to separate OS detection concerns from ServerProvider
- **Improved concurrency handling**: Implemented proper synchronization for server updates using Future-based locks to prevent race conditions

### Bug Fixes:
- **Fixed CPU percentage parsing**: Removed incorrect '*100' multiplication in BSD CPU parsing (values were already percentages)
- **Enhanced memory parsing**: Added validation and error handling to BSD memory fallback parsing with proper logging
- **Improved uptime parsing**: Added support for multiple Windows date formats and robust error handling with validation
- **Fixed division by zero**: Added safety checks in Swap.usedPercent getter

### Code Quality Enhancements:
- **Added comprehensive documentation**: Documented Windows CPU counter limitations and approach
- **Strengthened error handling**: Added detailed logging and validation throughout parsing methods
- **Improved robustness**: Enhanced BSD CPU parsing with percentage validation and warnings
- **Better separation of concerns**: Each parsing method now has single responsibility

### Files Changed:
- `lib/data/helper/system_detector.dart` (new): System detection helper
- `lib/data/model/server/cpu.dart`: Fixed percentage parsing and added validation
- `lib/data/model/server/memory.dart`: Enhanced fallback parsing and division-by-zero protection
- `lib/data/model/server/server_status_update_req.dart`: Refactored into 13 focused parsing methods
- `lib/data/provider/server.dart`: Improved synchronization and extracted system detection

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: parse & shell fn struct

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-08-08 16:56:36 +08:00

87 lines
2.8 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
void main() {
group('Linux uptime parsing tests', () {
test('should parse uptime with days and hours:minutes', () {
const raw = '19:39:15 up 61 days, 18:16, 1 user, load average: 0.00, 0.00, 0.00';
final result = _testParseUpTime(raw);
expect(result, '61 days, 18:16');
});
test('should parse uptime with single day and hours:minutes', () {
const raw = '19:39:15 up 1 day, 2:34, 1 user, load average: 0.00, 0.00, 0.00';
final result = _testParseUpTime(raw);
expect(result, '1 day, 2:34');
});
test('should parse uptime with only hours:minutes', () {
const raw = '19:39:15 up 2:34, 1 user, load average: 0.00, 0.00, 0.00';
final result = _testParseUpTime(raw);
expect(result, '2:34');
});
test('should parse uptime with only minutes', () {
const raw = '19:39:15 up 34 min, 1 user, load average: 0.00, 0.00, 0.00';
final result = _testParseUpTime(raw);
expect(result, '34 min');
});
test('should parse uptime with days only (no time part)', () {
const raw = '19:39:15 up 5 days, 1 user, load average: 0.00, 0.00, 0.00';
final result = _testParseUpTime(raw);
expect(result, '5 days');
});
test('should return null for invalid format', () {
const raw = 'invalid uptime format';
final result = _testParseUpTime(raw);
expect(result, null);
});
test('should handle edge case with empty string', () {
const raw = '';
final result = _testParseUpTime(raw);
expect(result, null);
});
});
}
// Helper function to test the private _parseUpTime function
String? _testParseUpTime(String raw) {
final splitedUp = raw.split('up ');
if (splitedUp.length == 2) {
final uptimePart = splitedUp[1];
final splitedComma = uptimePart.split(', ');
if (splitedComma.isEmpty) return null;
// Handle different uptime formats
final firstPart = splitedComma[0].trim();
// Case 1: "61 days" or "1 day" - need to get the time part from next segment
if (firstPart.contains('day')) {
if (splitedComma.length >= 2) {
final timePart = splitedComma[1].trim();
// Check if it's in HH:MM format
if (timePart.contains(':') && !timePart.contains('user') && !timePart.contains('load')) {
return '$firstPart, $timePart';
}
}
return firstPart;
}
// Case 2: "2:34" (hours:minutes) - already in good format
if (firstPart.contains(':') && !firstPart.contains('user') && !firstPart.contains('load')) {
return firstPart;
}
// Case 3: "34 min" - already in good format
if (firstPart.contains('min')) {
return firstPart;
}
// Fallback: return first part
return firstPart;
}
return null;
}