The answer is actually not as complicated as you might think. A trick that I have found very useful in situations similar to this is to remove the <cr> from the end of your mapping and see what comes up. If I do that and run 30<C-x>b, this pops up in the command line:
This is because it's basically like you typed 30:...
From :h insertcharpre:
It is not allowed to change the text |textlock|.
You need to delay the change with a one-shot autocmd, for example until the next TextChangedI is fired:
fu! Test() abort
au TextChangedI * ++once call setline(1, getline('.'))
au InsertCharPre * call Test()
The ++once flag is provided ...