X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/80e348724041a694002fc38f57e8868b9b9a2be9..448c931a776c3e3f65ca2eab11da0c062d597818:/telegram_fake_helper_test.go?ds=sidebyside diff --git a/telegram_fake_helper_test.go b/telegram_fake_helper_test.go index 6082d7d..5ed4e2d 100644 --- a/telegram_fake_helper_test.go +++ b/telegram_fake_helper_test.go @@ -79,6 +79,7 @@ func newTelegramLocalFake(t *testing.T) *TelegramFake { added: make(chan struct{}, 1), stopped: make(chan struct{}), } + t.Cleanup(func() { close(f.stopped) }) @@ -86,69 +87,6 @@ func newTelegramLocalFake(t *testing.T) *TelegramFake { return f } -// Add makes update available to getUpdates. -func (f *TelegramFake) Add(update TelegramUpdate) { - f.mutex.Lock() - - f.updates = append(f.updates, update) - f.mutex.Unlock() - - select { - case f.added <- struct{}{}: - default: - } -} - -// Calls returns the calls so far. -func (f *TelegramFake) Calls() []TelegramFakeCall { - f.mutex.Lock() - defer f.mutex.Unlock() - - return append([]TelegramFakeCall(nil), f.calls...) -} - -// Sent returns the texts of the sendMessage calls and the Markdown of the -// sendRichMessage calls that succeeded, in order. -func (f *TelegramFake) Sent() []string { - var texts []string - - for _, call := range f.Calls() { - switch { - case !call.ok: - case call.method == "sendMessage": - texts = append(texts, call.params["text"].(string)) - case call.method == "sendRichMessage": - texts = append(texts, call.params["rich_message"].(map[string]any)["markdown"].(string)) - } - } - - return texts -} - -func (f *TelegramFake) Image(id string, data []byte) { - f.mutex.Lock() - defer f.mutex.Unlock() - - if f.images == nil { - f.images = make(map[string][]byte) - } - - f.images[id] = data -} - -// RoundTrip handles a request in memory, so synctest can control its timers. -func (f *TelegramFake) RoundTrip(request *http.Request) (*http.Response, error) { - response := httptest.NewRecorder() - f.serve(response, request) - - ctx := request.Context() - if ctx.Err() != nil { - return nil, ctx.Err() - } - - return response.Result(), nil -} - func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { id, file := strings.CutPrefix(r.URL.Path, "/file/bot"+telegramTestToken+"/") if file { @@ -163,7 +101,9 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { return } - _, _ = w.Write(data) + // The client can cancel a media request before this reply is + // written. + w.Write(data) // nolint: errcheck return } @@ -250,7 +190,9 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { wait = 1 } - _, _ = fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":%d}}`, status, description, wait) + // A test can cancel the request after the handler selects its + // reply. + fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":%d}}`, status, description, wait) // nolint: errcheck return } @@ -259,7 +201,8 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { f.record(call) if method == "getFile" { - _ = json.NewEncoder(w).Encode(map[string]any{ + // A cancelled client does not need a response body. + json.NewEncoder(w).Encode(map[string]any{ // nolint: errcheck "ok": true, "result": map[string]any{"file_path": params["file_id"]}, }) @@ -268,7 +211,9 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { } if method != "getUpdates" { - _, _ = io.WriteString(w, `{"ok":true,"result":true}`) + // A cancelled client does not need a response body. + io.WriteString(w, `{"ok":true,"result":true}`) // nolint: errcheck + return } @@ -284,7 +229,8 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { updates = f.from(offset) } - _ = json.NewEncoder(w).Encode(map[string]any{ + // A cancelled client does not need a response body. + json.NewEncoder(w).Encode(map[string]any{ // nolint: errcheck "ok": true, "result": updates, }) @@ -319,6 +265,69 @@ func (f *TelegramFake) from(offset int64) []TelegramUpdate { return updates } +// Add makes update available to getUpdates. +func (f *TelegramFake) Add(update TelegramUpdate) { + f.mutex.Lock() + + f.updates = append(f.updates, update) + f.mutex.Unlock() + + select { + case f.added <- struct{}{}: + default: + } +} + +// Sent returns the texts of the sendMessage calls and the Markdown of the +// sendRichMessage calls that succeeded, in order. +func (f *TelegramFake) Sent() []string { + var texts []string + + for _, call := range f.Calls() { + switch { + case !call.ok: + case call.method == "sendMessage": + texts = append(texts, call.params["text"].(string)) + case call.method == "sendRichMessage": + texts = append(texts, call.params["rich_message"].(map[string]any)["markdown"].(string)) + } + } + + return texts +} + +// Calls returns the calls so far. +func (f *TelegramFake) Calls() []TelegramFakeCall { + f.mutex.Lock() + defer f.mutex.Unlock() + + return append([]TelegramFakeCall(nil), f.calls...) +} + +func (f *TelegramFake) Image(id string, data []byte) { + f.mutex.Lock() + defer f.mutex.Unlock() + + if f.images == nil { + f.images = make(map[string][]byte) + } + + f.images[id] = data +} + +// RoundTrip handles a request in memory, so synctest can control its timers. +func (f *TelegramFake) RoundTrip(request *http.Request) (*http.Response, error) { + response := httptest.NewRecorder() + f.serve(response, request) + + ctx := request.Context() + if ctx.Err() != nil { + return nil, ctx.Err() + } + + return response.Result(), nil +} + // telegramOwnerMessage returns a text message of the owner in the private chat. func telegramOwnerMessage(text string) *TelegramMessage { return &TelegramMessage{