dap: api-ize and tidy up a bit, move test code to its own file