Merge pull request 'The lab's phone shows an HTML message as Telegram does, so a tap carries back the words shown (issue 692)' (#104) from fix/692-the-fake-phone-reads-html into main
This commit was merged in pull request #104.
This commit is contained in:
@@ -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(`</?[a-zA-Z][^>]*>`)
|
||||
|
||||
// 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 := "<b>Flip the lab's switch?</b>\nA <b> & c\n<code>+ k: v</code>\n\n<blockquote expandable>Asked by lab-asker.</blockquote>"
|
||||
want := "Flip the lab's switch?\nA <b> & c\n+ k: v\n\nAsked by lab-asker."
|
||||
if got := shownOfHTML(in); got != want {
|
||||
t.Errorf("%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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:"
|
||||
|
||||
Reference in New Issue
Block a user