using System.Net;
using System.Net.Http.Json;
using BigRegister.Api.Contracts;
namespace BigRegister.Tests.Acceptance;
///
/// Contract test for the FE/BE seam on phone-number stripping (WP-75). Both sides share
/// the same format regex (^0\d{9}$) but, until this test, diverged on what they
/// strip before checking it: the FE's parseTelefoonnummer
/// (registratie/domain/value-objects/telefoonnummer.ts) also drops parentheses and maps a
/// leading +31 to a leading 0;
/// used to strip only spaces and dashes. This was latent, not live — the FE always sends
/// its already-normalised value over the wire — but a crafted/future caller posting a raw,
/// FE-valid number would hit a backend that disagrees with the FE about what's valid. The
/// backend is the authority (ADR-0001), so it must agree with the FE on every FE-valid input.
///
public class PhoneFormatContractTests(TestWebApplicationFactory factory) : IClassFixture
{
private readonly HttpClient _client = factory.CreateClient();
private Task PostChangeRequest(string telefoon) =>
_client.PostAsJsonAsync("/api/v1/change-requests", new ChangeRequestRequest(telefoon));
[Fact]
public async Task A_leading_plus31_is_accepted_like_the_frontends_normalised_form()
{
// Given a phone number in international form — parseTelefoonnummer maps a leading
// "+31" to "0" and accepts it (it becomes "0612345678", 10 digits starting 0).
// When it is posted to the backend exactly as the user typed it (not FE-normalised)...
var res = await PostChangeRequest("+31612345678");
// Then the backend must agree it's valid, not reject it as malformed.
res.EnsureSuccessStatusCode();
var body = (await res.Content.ReadFromJsonAsync())!;
Assert.NotNull(body.Referentie);
}
[Fact]
public async Task Parentheses_around_the_area_code_are_accepted_like_the_frontend()
{
// Given a phone number with the area code in parentheses — parseTelefoonnummer strips
// "()" along with spaces/dashes and accepts it (it becomes "0612345678").
// When it is posted to the backend exactly as the user typed it...
var res = await PostChangeRequest("(06) 12345678");
// Then the backend must agree it's valid, not reject it as malformed.
res.EnsureSuccessStatusCode();
var body = (await res.Content.ReadFromJsonAsync())!;
Assert.NotNull(body.Referentie);
}
}