]> git.giacomolli.uk - onechat/blobdiff - telegram_fake_helper_test.go
Name timeouts for the operations they limit
[onechat] / telegram_fake_helper_test.go
index 6a89054a8b0e10702b8b2d0ba05de3768fda7ffa..f6ccd8425d41e36372fc51fb8e8a5afc7f2b2216 100644 (file)
@@ -98,6 +98,19 @@ func (f *TelegramFake) Image(id string, data []byte) {
        f.images[id] = data
 }
 
        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 {
 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
 }
 
        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{
 // telegramOwnerMessage returns a text message of the owner in the private chat.
 func telegramOwnerMessage(text string) *TelegramMessage {
        return &TelegramMessage{