import 'package:consumer_app/core/errors/api_error_code.dart';
import 'package:consumer_app/core/networking/api_client.dart';
import 'package:consumer_app/repositories/http_account_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
void main() {
test('maps the authenticated favorites contract', () async {
Uri? requestedUri;
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async {
requestedUri = request.url;
return http.Response(
'{"items":[{"object_type":"places","object_id":4,"item":{"id":4,"resource":"places","title":"Saved"}}]}',
200,
);
}),
);
final response = await HttpAccountRepository(api).favorites();
expect(requestedUri?.path, '/wp-json/meinweimar/v1/favorites');
expect(response.items.single.item?.title, 'Saved');
});
test('reads the authenticated profile through /me', () async {
Uri? requestedUri;
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async {
requestedUri = request.url;
return http.Response(
'{"user_id":8,"visible":true,"nickname":"Lokal"}',
200,
);
}),
);
final profile = await HttpAccountRepository(api).me();
expect(requestedUri?.path, '/wp-json/meinweimar/v1/me');
expect(profile.userId, 8);
expect(profile.nickname, 'Lokal');
});
test('updates the authenticated profile through PUT /me', () async {
Uri? requestedUri;
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async {
requestedUri = request.url;
expect(request.method, 'PUT');
return http.Response(
'{"user_id":8,"visible":true,"nickname":"Neu"}',
200,
);
}),
);
final profile = await HttpAccountRepository(
api,
).updateMe(const {'nickname': 'Neu'});
expect(requestedUri?.path, '/wp-json/meinweimar/v1/me');
expect(profile.nickname, 'Neu');
});
test('adds and removes a favorite through the typed route', () async {
final methods = <String>[];
final paths = <String>[];
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async {
methods.add(request.method);
paths.add(request.url.path);
return http.Response(
request.method == 'POST'
? '{"object_type":"places","object_id":453,"saved":true}'
: '{"object_type":"places","object_id":453,"saved":false}',
request.method == 'POST' ? 201 : 200,
);
}),
);
final repository = HttpAccountRepository(api);
expect(
await repository.updateFavorite(
objectType: 'places',
objectId: 453,
saved: true,
),
isTrue,
);
expect(
await repository.updateFavorite(
objectType: 'places',
objectId: 453,
saved: false,
),
isFalse,
);
expect(methods, ['POST', 'DELETE']);
expect(paths, [
'/wp-json/meinweimar/v1/favorites/places/453',
'/wp-json/meinweimar/v1/favorites/places/453',
]);
});
test('keeps the login boundary as a typed permission error', () async {
final api = ApiClient(
baseUrl: Uri.parse('https://example.test/wp-json/meinweimar/v1'),
locale: () => 'de-DE',
client: MockClient((request) async => http.Response('{}', 401)),
);
expect(
() => HttpAccountRepository(api).favorites(),
throwsA(
isA<ApiException>().having(
(error) => error.code,
'code',
ApiErrorCode.permissionDenied,
),
),
);
});
}
