140 lines
3.7 KiB
Dart
140 lines
3.7 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:motorcycle_dash/services/clubs/club_service.dart';
|
|
|
|
void main() {
|
|
group('ClubService Validation & Invariant Contracts', () {
|
|
test('reports availability correctly when offline', () {
|
|
final service = ClubService();
|
|
expect(service.isAvailable, isFalse);
|
|
});
|
|
|
|
test('rejects club creation when unauthenticated or caller UID missing', () async {
|
|
final service = ClubService();
|
|
|
|
expect(
|
|
() => service.createClub(
|
|
name: 'Bay Area Riders',
|
|
slug: 'bay-area-riders',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('unauthenticated'),
|
|
)),
|
|
);
|
|
});
|
|
|
|
test('rejects club creation with invalid name length', () async {
|
|
final service = ClubService();
|
|
|
|
// Too short (< 3)
|
|
expect(
|
|
() => service.createClub(
|
|
name: 'AB',
|
|
slug: 'valid-slug',
|
|
callerUid: 'alice',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('invalid-name'),
|
|
)),
|
|
);
|
|
|
|
// Too long (> 50)
|
|
expect(
|
|
() => service.createClub(
|
|
name: 'A' * 51,
|
|
slug: 'valid-slug',
|
|
callerUid: 'alice',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('invalid-name'),
|
|
)),
|
|
);
|
|
});
|
|
|
|
test('rejects club creation with invalid slug format or reserved keywords', () async {
|
|
final service = ClubService();
|
|
|
|
// Invalid slug format (underscores)
|
|
expect(
|
|
() => service.createClub(
|
|
name: 'Valid Name',
|
|
slug: 'invalid_slug_with_underscores',
|
|
callerUid: 'alice',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('invalid-slug'),
|
|
)),
|
|
);
|
|
|
|
// Reserved slug
|
|
expect(
|
|
() => service.createClub(
|
|
name: 'Valid Name',
|
|
slug: 'admin',
|
|
callerUid: 'alice',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('invalid-slug'),
|
|
)),
|
|
);
|
|
});
|
|
|
|
test('rejects ownership transfer to self', () async {
|
|
final service = ClubService();
|
|
|
|
expect(
|
|
() => service.transferOwnership(
|
|
'club_1',
|
|
'alice',
|
|
callerUid: 'alice',
|
|
),
|
|
throwsA(isA<ClubOperationException>().having(
|
|
(e) => e.code,
|
|
'code',
|
|
equals('invalid-target'),
|
|
)),
|
|
);
|
|
});
|
|
|
|
test('gracefully returns null/empty for streams when offline', () async {
|
|
final service = ClubService();
|
|
|
|
final club = await service.getClub('club_1');
|
|
expect(club, isNull);
|
|
|
|
final member = await service.getMember('club_1', 'alice');
|
|
expect(member, isNull);
|
|
|
|
final clubStream = service.clubStream('club_1');
|
|
expect(await clubStream.first, isNull);
|
|
|
|
final memberStream = service.memberStream('club_1', 'alice');
|
|
expect(await memberStream.first, isNull);
|
|
|
|
final activeStream = service.activeMembersStream('club_1');
|
|
expect(await activeStream.first, isEmpty);
|
|
|
|
final pendingStream = service.pendingRequestsStream('club_1');
|
|
expect(await pendingStream.first, isEmpty);
|
|
|
|
final myClubsStream = service.myActiveClubsStream('alice');
|
|
expect(await myClubsStream.first, isEmpty);
|
|
|
|
final myInvitesStream = service.myInvitationsStream('alice');
|
|
expect(await myInvitesStream.first, isEmpty);
|
|
|
|
final discovery = await service.discoverPublicClubs();
|
|
expect(discovery, isEmpty);
|
|
});
|
|
});
|
|
}
|