Refactor as necessary to avoid adding tech debt during day-to-day. Add tests and rearchitect during the normal development cycle when adding to a preexisting codebase.
Taking the time to do things right makes everything take a little longer than it strictly needs to, but it allows you to avoid unpredictable …