How? There’s no understanding, just output of highly probable text suggestions which sometimes coincides with correct text suggestions.
Correctness exists only in the understanding of humans.
In the case of writing to tests there are infinite ways to have green tests and break things anyway.