When AI generates a complex function, isolate it for testing. Create a minimal test file:
Copy just the suspicious function
Add hardcoded test inputs
Run it standalone
Compare actual vs expected output
Isolation removes variables. If it works in isolation but fails in context, the bug is in how it's being called, not the function itself.