main
dart 140 lines 4.04 KB
Raw
1 import 'package:flutter/material.dart';
2 import 'package:flutter_localizations/flutter_localizations.dart';
3 import 'package:flutter_test/flutter_test.dart';
4 import 'package:localmind/core/models/enums.dart';
5 import 'package:localmind/features/servers/data/models/server.dart';
6 import 'package:localmind/features/servers/views/components/server_card.dart';
7 import 'package:localmind/l10n/app_localizations.dart';
8
9 Widget _wrapWithApp(Widget child) {
10 return MaterialApp(
11 locale: const Locale('en'),
12 localizationsDelegates: const [
13 AppLocalizations.delegate,
14 GlobalMaterialLocalizations.delegate,
15 GlobalWidgetsLocalizations.delegate,
16 GlobalCupertinoLocalizations.delegate,
17 ],
18 supportedLocales: AppLocalizations.supportedLocales,
19 home: Scaffold(body: child),
20 );
21 }
22
23 Server _buildServer({
24 String name = 'llama home',
25 ServerType type = ServerType.ollama,
26 String host = '192.168.0.210',
27 int port = 11434,
28 bool isDefault = false,
29 ConnectionStatus status = ConnectionStatus.disconnected,
30 }) {
31 return Server(
32 id: 'test-id',
33 name: name,
34 type: type,
35 host: host,
36 port: port,
37 isDefault: isDefault,
38 createdAt: DateTime(2026, 1, 1),
39 lastConnectedAt: DateTime(2026, 1, 1),
40 status: status,
41 );
42 }
43
44 void main() {
45 group('ServerCard', () {
46 testWidgets('renders server name, type chip and address', (tester) async {
47 await tester.pumpWidget(_wrapWithApp(ServerCard(server: _buildServer())));
48 await tester.pump();
49
50 expect(find.text('llama home'), findsOneWidget);
51 // Type chip should render the localized ollama display name.
52 expect(find.text('Ollama'), findsOneWidget);
53 // Address row should render the IP:port.
54 expect(find.text('192.168.0.210:11434'), findsOneWidget);
55 });
56
57 testWidgets('shows the Offline status pill when disconnected', (
58 tester,
59 ) async {
60 await tester.pumpWidget(_wrapWithApp(ServerCard(server: _buildServer())));
61 await tester.pump();
62
63 expect(find.text('Offline'), findsOneWidget);
64 });
65
66 testWidgets('shows the Connected status pill when connected', (
67 tester,
68 ) async {
69 await tester.pumpWidget(
70 _wrapWithApp(
71 ServerCard(server: _buildServer(status: ConnectionStatus.connected)),
72 ),
73 );
74 await tester.pump();
75
76 expect(find.text('Connected'), findsOneWidget);
77 });
78
79 testWidgets('shows the Checking status pill while checking', (
80 tester,
81 ) async {
82 await tester.pumpWidget(
83 _wrapWithApp(
84 ServerCard(server: _buildServer(status: ConnectionStatus.checking)),
85 ),
86 );
87 await tester.pump();
88
89 expect(find.text('Checking'), findsOneWidget);
90 });
91
92 testWidgets('shows the default star indicator when server is default', (
93 tester,
94 ) async {
95 await tester.pumpWidget(
96 _wrapWithApp(ServerCard(server: _buildServer(isDefault: true))),
97 );
98 await tester.pump();
99
100 // The star icon (Tooltip wraps the icon) should be present.
101 expect(find.byTooltip('Default'), findsOneWidget);
102 });
103
104 testWidgets('invokes onTap when the card is tapped', (tester) async {
105 var taps = 0;
106 await tester.pumpWidget(
107 _wrapWithApp(ServerCard(server: _buildServer(), onTap: () => taps++)),
108 );
109 await tester.pump();
110
111 await tester.tap(find.byType(ServerCard));
112 await tester.pump();
113
114 expect(taps, 1);
115 });
116
117 testWidgets('renders different content for on-device servers', (
118 tester,
119 ) async {
120 await tester.pumpWidget(
121 _wrapWithApp(
122 ServerCard(
123 server: _buildServer(
124 name: 'On-Device',
125 type: ServerType.onDevice,
126 host: '',
127 port: 0,
128 ),
129 ),
130 ),
131 );
132 await tester.pump();
133
134 // The name and the on-device type chip both render "On-Device".
135 expect(find.text('On-Device'), findsWidgets);
136 // On-device uses a localized placeholder for the address.
137 expect(find.text('Local inference'), findsOneWidget);
138 });
139 });
140 }