Files
flutter_server_box/test/sensors_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

146 lines
5.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:server_box/data/model/server/sensors.dart';
const _sensorsRaw = '''
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +56.0°C (high = +105.0°C, crit = +105.0°C)
Core 0: +45.0°C (high = +105.0°C, crit = +105.0°C)
Core 1: +45.0°C (high = +105.0°C, crit = +105.0°C)
Core 2: +45.0°C (high = +105.0°C, crit = +105.0°C)
Core 3: +44.0°C (high = +105.0°C, crit = +105.0°C)
acpitz-acpi-0
Adapter: ACPI interface
temp1: +27.8°C (crit = +119.0°C)
iwlwifi_1-virtual-0
Adapter: Virtual device
temp1: +56.0°C
nvme-pci-0400
Adapter: PCI adapter
Composite: +45.9°C (low = -273.1°C, high = +83.8°C)
(crit = +84.8°C)
Sensor 1: +45.9°C (low = -273.1°C, high = +65261.8°C)
Sensor 2: +47.9°C (low = -273.1°C, high = +65261.8°C)
''';
const _sensorsRaw2 = '''
asusec-isa-0000
Adapter: ISA adapter
CPU Core: 1.26 V
Chipset: 2473 RPM
Chipset: +60.0°C
CPU: +36.0°C
Motherboard: +34.0°C
T_Sensor: -40.0°C
VRM: +20.0°C
CPU: 35.00 A
nct6798-isa-0290
Adapter: ISA adapter
in0: 1.19 V (min = +0.00 V, max = +1.74 V)
in1: 1000.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in2: 3.34 V (min = +0.00 V, max = +0.00 V) ALARM
in3: 3.28 V (min = +0.00 V, max = +0.00 V) ALARM
in4: 1000.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in5: 856.00 mV (min = +0.00 V, max = +0.00 V)
in6: 232.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in7: 3.34 V (min = +0.00 V, max = +0.00 V) ALARM
in8: 3.23 V (min = +0.00 V, max = +0.00 V) ALARM
in9: 1.78 V (min = +0.00 V, max = +0.00 V) ALARM
in10: 848.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in11: 880.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in12: 1.03 V (min = +0.00 V, max = +0.00 V) ALARM
in13: 320.00 mV (min = +0.00 V, max = +0.00 V) ALARM
in14: 240.00 mV (min = +0.00 V, max = +0.00 V) ALARM
fan1: 0 RPM (min = 0 RPM)
fan2: 1764 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
fan4: 0 RPM (min = 0 RPM)
fan5: 0 RPM (min = 0 RPM)
fan6: 0 RPM (min = 0 RPM)
SYSTIN: +34.0°C (high = +80.0°C, hyst = +75.0°C)
(crit = +125.0°C) sensor = thermistor
CPUTIN: +35.0°C (high = +80.0°C, hyst = +75.0°C)
(crit = +125.0°C) sensor = thermistor
AUXTIN0: +90.0°C (high = +80.0°C, hyst = +75.0°C) ALARM
(crit = +125.0°C) sensor = thermistor
AUXTIN1: +34.0°C (high = +80.0°C, hyst = +75.0°C)
(crit = +125.0°C) sensor = thermistor
AUXTIN2: +33.0°C (high = +80.0°C, hyst = +75.0°C)
(crit = +100.0°C) sensor = thermistor
AUXTIN3: +95.0°C (high = +80.0°C, hyst = +75.0°C) ALARM
(crit = +100.0°C) sensor = thermistor
AUXTIN4: +34.0°C (high = +80.0°C, hyst = +75.0°C)
(crit = +100.0°C)
PECI Agent 0 Calibration: +36.0°C (high = +80.0°C, hyst = +75.0°C)
PCH_CHIP_CPU_MAX_TEMP: +0.0°C
PCH_CHIP_TEMP: +0.0°C
PCH_CPU_TEMP: +0.0°C
PCH_MCH_TEMP: +0.0°C
TSI0_TEMP: +44.6°C
TSI1_TEMP: +60.0°C
intrusion0: ALARM
intrusion1: ALARM
beep_enable: disabled
nvme-pci-0400
Adapter: PCI adapter
Composite: +45.9°C (low = -273.1°C, high = +69.8°C)
(crit = +79.8°C)
k10temp-pci-00c3
Adapter: PCI adapter
Tctl: +44.9°C
Tccd1: +41.0°C
Tccd2: +38.5°C
''';
void main() {
test('parse sensors1', () {
final sensors = SensorItem.parse(_sensorsRaw);
expect(sensors.map((e) => e.device), [
'coretemp-isa-0000',
'acpitz-acpi-0',
'iwlwifi_1-virtual-0',
'nvme-pci-0400',
]);
expect(sensors.map((e) => e.adapter), [
SensorAdaptor.isa,
SensorAdaptor.acpi,
SensorAdaptor.virtual,
SensorAdaptor.pci,
]);
expect(sensors.map((e) => e.summary), [
'+56.0°C (high = +105.0°C, crit = +105.0°C)',
'+27.8°C (crit = +119.0°C)',
'+56.0°C',
'+45.9°C (low = -273.1°C, high = +83.8°C)',
]);
});
test('parse sensors2', () {
final sensors = SensorItem.parse(_sensorsRaw2);
expect(sensors.map((e) => e.device), [
'asusec-isa-0000',
'nct6798-isa-0290',
'nvme-pci-0400',
'k10temp-pci-00c3',
]);
expect(sensors.map((e) => e.adapter), [
SensorAdaptor.isa,
SensorAdaptor.isa,
SensorAdaptor.pci,
SensorAdaptor.pci,
]);
expect(sensors.map((e) => e.summary), [
'1.26 V',
'1.19 V (min = +0.00 V, max = +1.74 V)',
'+45.9°C (low = -273.1°C, high = +69.8°C)',
'+44.9°C',
]);
});
}