diff --git a/asks/faketelegram_test.go b/asks/faketelegram_test.go index 15fadd9..b00f9fd 100644 --- a/asks/faketelegram_test.go +++ b/asks/faketelegram_test.go @@ -3,8 +3,10 @@ package asks import ( "encoding/json" "fmt" + "html" "net/http" "net/http/httptest" + "regexp" "strings" "sync" "testing" @@ -100,6 +102,11 @@ func (f *fakeTelegram) serve(w http.ResponseWriter, r *http.Request) { case "sendMessage", "editMessageText": chat := int64(num(body["chat_id"])) text, _ := body["text"].(string) + if mode, _ := body["parse_mode"].(string); mode == "HTML" { + // What a message shows, and what a tap on it carries back, is its text with the markup read: tags gone, + // entities unescaped — as the service gives it (novox/hq issue 692). + text = shownOfHTML(text) + } m := tgMessage{Chat: chat, Text: text, Buttons: buttonsOf(body["reply_markup"]), At: time.Now()} f.mu.Lock() if method == "sendMessage" { @@ -324,3 +331,21 @@ func firstLine(s string) string { } return s } + +var htmlTag = regexp.MustCompile(`]*>`) + +// shownOfHTML is a text sent in Telegram's HTML as the service shows it and gives it back: its tags read and taken +// out, its entities written as the characters they stand for. +func shownOfHTML(text string) string { + return html.UnescapeString(htmlTag.ReplaceAllString(text, "")) +} + +// The fake phone shows a message sent in HTML as Telegram does (novox/hq issue 692): the words without their +// markup, so a tap carries back what the channel digests, as on the real service. +func TestTheFakePhoneShowsHTMLAsTelegramDoes(t *testing.T) { + in := "Flip the lab's switch?\nA <b> & c\n+ k: v\n\n
Asked by lab-asker.
" + want := "Flip the lab's switch?\nA & c\n+ k: v\n\nAsked by lab-asker." + if got := shownOfHTML(in); got != want { + t.Errorf("%q, want %q", got, want) + } +} diff --git a/asks/merge-check.sh b/asks/merge-check.sh index 11070dd..0ecb971 100644 --- a/asks/merge-check.sh +++ b/asks/merge-check.sh @@ -26,7 +26,7 @@ sh -n live-acceptance.sh go vet ./... out=$(mktemp) status=0 -go test -count=1 -timeout 25m -v -run '^TestTheOperatorsAnswerEndToEnd$' . >"$out" 2>&1 || status=$? +go test -count=1 -timeout 25m -v -run '^(TestTheOperatorsAnswerEndToEnd|TestTheFakePhoneShowsHTMLAsTelegramDoes)$' . >"$out" 2>&1 || status=$? grep -E '^(--- | --- |ok|FAIL|PASS)' "$out" || true if grep -q 'NOT RUN' "$out"; then echo "NOT RUN: THE PROOF OF THE OPERATOR'S ANSWERS DID NOT RUN HERE:"