Add cli tests, fix related bugs