Use sandboxed sinon and chai for testing
Extract state machine into separate module

Add test coverage in the process