mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-11 23:19:31 +00:00
Initial release of go-telegram
A fully-generated, strongly-typed Go client for the Telegram Bot API. * 176 methods + 301 types generated from Bot API v10.0 * 1408 auto-generated tests (8 scenarios per method) * Typed unions throughout — no 'any' in the public surface * Pluggable HTTP transport and JSON codec (default goccy/go-json) * Built-in retry middleware honouring Telegram's retry_after * Generic dispatcher with filters and conversation handlers * Self-verifying codegen pipeline (regen → audit → emit → run tests) * 14 example bots covering common patterns
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/client"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMeCache_FetchesOnce(t *testing.T) {
|
||||
m := &mockDoer{}
|
||||
var calls atomic.Int32
|
||||
m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
|
||||
if strings.HasSuffix(r.URL.Path, "/getMe") {
|
||||
calls.Add(1)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})).Return(newJSONResp(200, `{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"echo","username":"echo_bot"}}`), nil)
|
||||
|
||||
bot := client.New("t", client.WithHTTPClient(m))
|
||||
var cache MeCache
|
||||
|
||||
me1, err := cache.Get(context.Background(), bot)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "echo_bot", me1.Username)
|
||||
|
||||
me2, err := cache.Get(context.Background(), bot)
|
||||
require.NoError(t, err)
|
||||
require.Same(t, me1, me2)
|
||||
require.Equal(t, int32(1), calls.Load(), "should fetch only once")
|
||||
}
|
||||
|
||||
func TestMeCache_Reset(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
m := &mockDoer{}
|
||||
m.On("Do", mock.Anything).Run(func(args mock.Arguments) {
|
||||
calls.Add(1)
|
||||
}).Return(newJSONResp(200, `{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"echo","username":"echo_bot"}}`), nil).Once()
|
||||
m.On("Do", mock.Anything).Run(func(args mock.Arguments) {
|
||||
calls.Add(1)
|
||||
}).Return(newJSONResp(200, `{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"echo","username":"echo_bot"}}`), nil).Once()
|
||||
|
||||
bot := client.New("t", client.WithHTTPClient(m))
|
||||
var cache MeCache
|
||||
|
||||
_, err := cache.Get(context.Background(), bot)
|
||||
require.NoError(t, err)
|
||||
cache.Reset()
|
||||
_, err = cache.Get(context.Background(), bot)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, int32(2), calls.Load())
|
||||
}
|
||||
Reference in New Issue
Block a user