Fix crash when terminal support is ambiguous

Fix crash when TerminalSupported returns true (because TERM is a
supported terminal), but TerminalMode returns an error (because Stdin
has been redirected).
Add test for concurrent WriteHistory

Only activated when run with `go test -race`