X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/b2fc2d6a737dd5bab25e57ba822dd07258de93e6..refs/heads/master:/telegram_fake_helper_test.go diff --git a/telegram_fake_helper_test.go b/telegram_fake_helper_test.go index 6a89054..f6ccd84 100644 --- a/telegram_fake_helper_test.go +++ b/telegram_fake_helper_test.go @@ -98,6 +98,19 @@ func (f *TelegramFake) Image(id string, data []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 { @@ -256,6 +269,23 @@ func newTelegramFake(t *testing.T) *TelegramFake { return f } +// newTelegramLocalFake returns the same fake API without network goroutines. +// Its RoundTrip handles requests directly for tests with fake time. +func newTelegramLocalFake(t *testing.T) *TelegramFake { + t.Helper() + + f := &TelegramFake{ + Server: &httptest.Server{URL: "https://example.com"}, + added: make(chan struct{}, 1), + stopped: make(chan struct{}), + } + t.Cleanup(func() { + close(f.stopped) + }) + + return f +} + // telegramOwnerMessage returns a text message of the owner in the private chat. func telegramOwnerMessage(text string) *TelegramMessage { return &TelegramMessage{