consumer: add unit test

also fix a bunch of other bugs found by the test