mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 21:07:15 +01:00
firebase_auth_rest_test.dart 525 -> 153 + _google 158 + _tokens 206 + _uid 141
sync_revisions_test.dart 451 -> 223 + _peers 228 + _backends 158
+ _order 165 + _persistence 215
sync_settings_screen_test 457 -> 207 + _backup 143 + _google 145
mirror_store_test.dart 452 -> 209 + _reads 172 + _fallback 196 + _revisions 244
firebase_client_test.dart 374 -> 198 + _writes 227
firebase_config_test.dart 360 -> 138 + _client 148 + _client_google 164
device_auth_test.dart 307 -> 128 + _poll 201
github_client_test.dart 291 -> 143 + _writes 201
sync_test.dart 269 -> 160 + _skips 175
Two traps this hit, both invisible to a line count:
- _FakePersistence is declared *after* main(), so a naive cut at the last
brace dropped it; sync_revisions_persistence_test.dart failed to load
until it was carried across. Audited every other split file for trailing
top-level declarations — this was the only one.
- Copying the preamble into each sibling left helpers the sibling never
uses. `dart analyze` flagged 12; all removed rather than left as dead
code, and the analyzer is now clean.
crdt_sync_dart 280 passed, sync_settings_ui 29 passed, github_device_auth
analyze clean — all matching a clean HEAD worktree.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
154 lines
4.5 KiB
Dart
154 lines
4.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart' as http_testing;
|
|
import 'package:test/test.dart';
|
|
|
|
/// A fixed "now" so token expiry is deterministic rather than wall-clock.
|
|
final _now = DateTime.utc(2026, 8, 3, 12);
|
|
|
|
http.Response _json(int statusCode, Object body) =>
|
|
http.Response(jsonEncode(body), statusCode);
|
|
|
|
/// Builds a provider whose POSTs return [responses] in call order (each
|
|
/// either an [http.Response] or a thrown [http.ClientException]).
|
|
FirebaseTokenProvider _provider(
|
|
List<Object> responses, {
|
|
FirebaseCredentialStore? store,
|
|
DateTime? now,
|
|
}) {
|
|
var index = 0;
|
|
final mock = http_testing.MockClient((request) async {
|
|
final response = responses[index];
|
|
index++;
|
|
if (response is http.ClientException) throw response;
|
|
return response as http.Response;
|
|
});
|
|
return FirebaseTokenProvider(
|
|
apiKey: 'fake-api-key',
|
|
store: store ?? InMemoryCredentialStore(),
|
|
httpClient: mock,
|
|
clock: () => now ?? _now,
|
|
);
|
|
}
|
|
|
|
FirebaseCredentials _credentials({
|
|
String idToken = 'id-1',
|
|
String refreshToken = 'refresh-1',
|
|
Duration validFor = const Duration(hours: 1),
|
|
}) => FirebaseCredentials(
|
|
idToken: idToken,
|
|
refreshToken: refreshToken,
|
|
expiresAt: _now.add(validFor),
|
|
);
|
|
|
|
void main() {
|
|
group('FirebaseCredentials', () {
|
|
test('round-trips through JSON', () {
|
|
final original = _credentials();
|
|
final restored = FirebaseCredentials.fromJson(original.toJson());
|
|
expect(restored.idToken, original.idToken);
|
|
expect(restored.refreshToken, original.refreshToken);
|
|
expect(restored.expiresAt, original.expiresAt);
|
|
});
|
|
|
|
test('is not expired while comfortably inside its lifetime', () {
|
|
expect(_credentials().isExpiredAt(_now), isFalse);
|
|
});
|
|
|
|
test('is expired once past expiry', () {
|
|
expect(
|
|
_credentials(validFor: const Duration(minutes: -1)).isExpiredAt(_now),
|
|
isTrue,
|
|
);
|
|
});
|
|
|
|
test('is expired inside the refresh skew, before real expiry', () {
|
|
// A tick starting now would outlive a token with 2 minutes left, so it
|
|
// must be treated as already expired.
|
|
expect(
|
|
_credentials(validFor: const Duration(minutes: 2)).isExpiredAt(_now),
|
|
isTrue,
|
|
);
|
|
});
|
|
});
|
|
|
|
group('signIn', () {
|
|
test('stores the session returned by identitytoolkit', () async {
|
|
final store = InMemoryCredentialStore();
|
|
final provider = _provider([
|
|
_json(200, {
|
|
'idToken': 'id-1',
|
|
'refreshToken': 'refresh-1',
|
|
'expiresIn': '3600',
|
|
}),
|
|
], store: store);
|
|
|
|
await provider.signIn(email: 'me@example.com', password: 'hunter2');
|
|
|
|
final saved = await store.load();
|
|
expect(saved!.idToken, 'id-1');
|
|
expect(saved.refreshToken, 'refresh-1');
|
|
expect(saved.expiresAt, _now.add(const Duration(hours: 1)));
|
|
});
|
|
|
|
test('reports Google\'s reason on a rejected password', () async {
|
|
final provider = _provider([
|
|
_json(400, {
|
|
'error': {'message': 'INVALID_LOGIN_CREDENTIALS'},
|
|
}),
|
|
]);
|
|
expect(
|
|
() => provider.signIn(email: 'me@example.com', password: 'wrong'),
|
|
throwsA(
|
|
isA<FirebaseAuthError>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('INVALID_LOGIN_CREDENTIALS'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('reports a string-shaped error body', () async {
|
|
final provider = _provider([
|
|
_json(400, {'error': 'BAD_REQUEST'}),
|
|
]);
|
|
expect(
|
|
() => provider.signIn(email: 'me@example.com', password: 'x'),
|
|
throwsA(
|
|
isA<FirebaseAuthError>().having(
|
|
(e) => e.message,
|
|
'message',
|
|
contains('BAD_REQUEST'),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
test('survives a non-JSON error body', () async {
|
|
final provider = FirebaseTokenProvider(
|
|
apiKey: 'k',
|
|
store: InMemoryCredentialStore(),
|
|
httpClient: http_testing.MockClient(
|
|
(_) async => http.Response('<html>502</html>', 502),
|
|
),
|
|
clock: () => _now,
|
|
);
|
|
await expectLater(
|
|
() => provider.signIn(email: 'a@b.c', password: 'x'),
|
|
throwsA(isA<FirebaseAuthError>()),
|
|
);
|
|
});
|
|
|
|
test('turns a network failure into FirebaseAuthError', () async {
|
|
final provider = _provider([http.ClientException('offline')]);
|
|
expect(
|
|
() => provider.signIn(email: 'a@b.c', password: 'x'),
|
|
throwsA(isA<FirebaseAuthError>()),
|
|
);
|
|
});
|
|
});
|
|
}
|