utils/crdt_sync_dart/test/firebase_config_test.dart
Krzysztof kuhy Rudnicki 200432a2b4 dart: split ten oversized test files by group
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
2026-08-22 14:54:35 +02:00

118 lines
3.2 KiB
Dart

/// Tests for the shared Firebase configuration on the Dart side.
///
/// The failure modes asserted here are the ones that would otherwise surface
/// as an authentication error long after the real mistake, so each is checked
/// to name the field at fault rather than merely to throw.
library;
import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:test/test.dart';
const _valid = {
'apiKey': 'AIzaSyExample',
'databaseUrl':
'https://kuhy-syncs-default-rtdb.europe-west1.firebasedatabase.app',
'projectId': 'kuhy-syncs',
'uid': 'OvA2REQyLIhAHOEjzwS1o877rgG3',
'email': 'sync@example.com',
};
void main() {
group('FirebaseConfig', () {
test('parses a valid config', () {
final config = FirebaseConfig.parse(jsonEncode(_valid));
expect(config.apiKey, 'AIzaSyExample');
expect(config.projectId, 'kuhy-syncs');
expect(config.uid, 'OvA2REQyLIhAHOEjzwS1o877rgG3');
expect(config.email, 'sync@example.com');
expect(config.databaseUrl, endsWith('europe-west1.firebasedatabase.app'));
});
test('ignores the scaffold comment keys', () {
final annotated = {..._valid, '_comment_apiKey': 'where to find it'};
expect(
FirebaseConfig.parse(jsonEncode(annotated)).apiKey,
'AIzaSyExample',
);
});
test('agrees with the Python side on the field names', () {
// Both languages read the same file; a rename on one side only would
// break the other silently.
final config = FirebaseConfig.parse(jsonEncode(_valid));
expect(config.databaseUrl, _valid['databaseUrl']);
expect(config.uid, _valid['uid']);
});
for (final field in _valid.keys) {
test('names $field when it is missing', () {
final incomplete = {..._valid}..remove(field);
expect(
() => FirebaseConfig.parse(jsonEncode(incomplete)),
throwsA(
isA<ConfigException>().having(
(e) => e.message,
'message',
contains(field),
),
),
);
});
}
test('names an empty field', () {
final blank = {..._valid, 'uid': ' '};
expect(
() => FirebaseConfig.parse(jsonEncode(blank)),
throwsA(
isA<ConfigException>().having(
(e) => e.message,
'message',
contains('uid'),
),
),
);
});
test('names an unfilled placeholder', () {
final unfilled = {..._valid, 'apiKey': 'PASTE_WEB_API_KEY_HERE'};
expect(
() => FirebaseConfig.parse(jsonEncode(unfilled)),
throwsA(
isA<ConfigException>().having(
(e) => e.message,
'message',
contains('placeholder for: apiKey'),
),
),
);
});
test('rejects unparsable JSON', () {
expect(
() => FirebaseConfig.parse('{not json'),
throwsA(isA<ConfigException>()),
);
});
test('rejects a non-object document', () {
expect(
() => FirebaseConfig.parse('["a list"]'),
throwsA(isA<ConfigException>()),
);
});
test('has a readable toString on the exception', () {
expect(ConfigException('boom').toString(), contains('boom'));
});
});
}