inventory_model_test.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:flutter_test/flutter_test.dart';
  2. import 'package:rackpeek_mobile/models/inventory.dart';
  3. const _document = {
  4. 'version': 5,
  5. 'resources': [
  6. {
  7. 'kind': 'Server',
  8. 'name': 'srv01',
  9. 'tags': ['homelab'],
  10. 'labels': {'env': 'prod'},
  11. 'ram': {'size': 64, 'mts': 4800},
  12. 'ipmi': true,
  13. 'ports': [
  14. {'type': 'rj45', 'speed': 1, 'count': 2}
  15. ],
  16. },
  17. {'kind': 'AccessPoint', 'name': 'ap01', 'model': 'U6 Lite', 'speed': 4.8},
  18. {
  19. 'kind': 'System',
  20. 'name': 'pve01',
  21. 'runsOn': ['srv01'],
  22. 'type': 'hypervisor',
  23. 'os': 'Proxmox VE 9',
  24. 'cores': 16,
  25. 'ram': 128,
  26. 'ip': '10.0.0.10'
  27. },
  28. {
  29. 'kind': 'Service',
  30. 'name': 'jellyfin',
  31. 'runsOn': ['pve01'],
  32. 'network': {'ip': '10.0.0.11', 'port': 8096, 'protocol': 'TCP'},
  33. 'endpoints': [
  34. {'ip': '10.0.0.11', 'port': 8096, 'protocol': 'TCP'},
  35. {'ip': '10.0.0.12', 'port': 8920, 'protocol': 'TCP'}
  36. ]
  37. },
  38. ],
  39. 'connections': [
  40. {
  41. 'a': {'resource': 'srv01', 'portGroup': 0, 'portIndex': 0},
  42. 'b': {'resource': 'ap01', 'portGroup': 0, 'portIndex': 0},
  43. 'label': 'uplink'
  44. }
  45. ],
  46. };
  47. void main() {
  48. group('InventoryDocument', () {
  49. final doc = InventoryDocument.fromJson(_document);
  50. test('parses version, resources and connections', () {
  51. expect(doc.version, 5);
  52. expect(doc.resources.length, 4);
  53. expect(doc.connections.length, 1);
  54. });
  55. test('hardware ram is an object, system ram is a number', () {
  56. final server = doc.byName('srv01')!;
  57. expect(server.ram?.size, 64);
  58. expect(server.ram?.mts, 4800);
  59. final system = doc.byName('pve01')!;
  60. expect(system.ramGb, 128);
  61. expect(system.ram, isNull);
  62. });
  63. test('AccessPoint discriminator parses and carries its fields', () {
  64. final ap = doc.byName('ap01')!;
  65. expect(ap.kind, 'AccessPoint');
  66. expect(ap.model, 'U6 Lite');
  67. expect(ap.speed, 4.8);
  68. });
  69. test('name lookup is case-insensitive', () {
  70. expect(doc.byName('SRV01')?.name, 'srv01');
  71. });
  72. test('dependants follow runsOn', () {
  73. expect(doc.dependantsOf('srv01').map((r) => r.name), ['pve01']);
  74. expect(doc.dependantsOf('pve01').map((r) => r.name), ['jellyfin']);
  75. });
  76. test('connections touch both endpoints', () {
  77. expect(doc.connectionsFor('srv01').length, 1);
  78. expect(doc.connectionsFor('pve01'), isEmpty);
  79. expect(doc.connections.first.label, 'uplink');
  80. });
  81. test('service subtitle shows address', () {
  82. final service = doc.byName('jellyfin')!;
  83. expect(service.subtitle, '10.0.0.11:8096');
  84. expect(service.endpoints.map((e) => e.describe()), [
  85. '10.0.0.11:8096',
  86. '10.0.0.12:8920',
  87. ]);
  88. });
  89. test('missing optional fields default safely', () {
  90. final sparse = Resource.fromJson({'kind': 'Other', 'name': 'x'});
  91. expect(sparse.tags, isEmpty);
  92. expect(sparse.labels, isEmpty);
  93. expect(sparse.runsOn, isEmpty);
  94. expect(sparse.notes, isNull);
  95. expect(sparse.subtitle, 'Other');
  96. });
  97. });
  98. }