import 'dart:convert';
import 'package:consumer_app/core/networking/api_client.dart';
import 'package:consumer_app/core/networking/public_read_cache.dart';
import 'package:consumer_app/services/bootstrap_service.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
void main() {
test('revalidates a cached bootstrap missing Places profiles', () async {
final store = _MemoryPublicReadCacheStore();
var requests = 0;
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
publicReadCache: PublicReadCache(store: store),
client: MockClient((_) async {
requests++;
if (requests == 1) {
return http.Response(
jsonEncode(_bootstrapJson(withPlacesCategories: false)),
200,
headers: {'etag': 'W/"old-bootstrap"'},
);
}
if (requests == 2) return http.Response('', 304);
return http.Response(
jsonEncode(_bootstrapJson(withPlacesCategories: true)),
200,
headers: {'etag': 'W/"new-bootstrap"'},
);
}),
);
await api.getPublicJson('/bootstrap');
final snapshot = await BootstrapService(api).load();
expect(requests, 3);
expect(
snapshot.config.contentCategories['places']?.profiles.keys,
contains('gastronomy'),
);
});
}
Map<String, dynamic> _bootstrapJson({required bool withPlacesCategories}) {
return {
'config': {
if (withPlacesCategories)
'content_categories': {
'places': {
'taxonomy': 'mw_place_category',
'profiles': {
'gastronomy': {
'slugs': ['restaurants'],
'labels': {'restaurants': 'Restaurants'},
'fields': [],
'required': [],
},
},
},
},
},
'resources': [
{'id': 'places', 'label': 'Places', 'endpoint': '', 'geo': true},
],
'features': {},
};
}
class _MemoryPublicReadCacheStore implements PublicReadCacheStore {
final Map<String, String> values = {};
@override
Future<String?> read(String key) async => values[key];
@override
Future<void> write(String key, String value) async => values[key] = value;
@override
Future<void> remove(String key) async => values.remove(key);
@override
Future<Set<String>> keys() async => values.keys.toSet();
}
