move gotd fork into repo. (#111)

- update to latest telegram layer
- remove some references to fields in tg.Entities that don't exist in
the schema
- originally added here:
https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e
  - referenced here
-
https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3
-
https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
Adam Van Ymeren
2025-06-27 20:03:37 -07:00
committed by GitHub
parent 0952df0244
commit 7a04f298d2
19264 changed files with 1539697 additions and 84 deletions
+2
View File
@@ -0,0 +1,2 @@
// Package tgmock implements mock for tg.Invoker.
package tgmock
+16
View File
@@ -0,0 +1,16 @@
package tgmock
import "go.mau.fi/mautrix-telegram/pkg/gotd/bin"
// Handler is a RPC call handler.
type Handler interface {
Handle(id int64, body bin.Encoder) (bin.Encoder, error)
}
// HandlerFunc is a function adapter for Handler.
type HandlerFunc func(id int64, body bin.Encoder) (bin.Encoder, error)
// Handle implements Handler.
func (h HandlerFunc) Handle(id int64, body bin.Encoder) (bin.Encoder, error) {
return h(id, body)
}
+35
View File
@@ -0,0 +1,35 @@
package tgmock
import (
"context"
"crypto/rand"
"github.com/go-faster/errors"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/crypto"
)
// Invoke implements tg.Invoker.
func (i *Mock) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
h := i.Handler()
id, err := crypto.RandInt64(rand.Reader)
if err != nil {
return errors.Wrap(err, "generate id")
}
body, err := h(id, input)
if err != nil {
return errors.Wrap(err, "mock invoke")
}
buf := new(bin.Buffer)
if err := body.Encode(buf); err != nil {
return errors.Wrap(err, "encode")
}
if err := output.Decode(buf); err != nil {
return errors.Wrap(err, "decode")
}
return nil
}
+23
View File
@@ -0,0 +1,23 @@
package tgmock
import (
"context"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
)
// Invoker implements tg.Invoker as function.
type Invoker func(request bin.Encoder) (bin.Encoder, error)
// Invoke implements tg.Invoker.
func (f Invoker) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
resp, err := f(input)
if err != nil {
return err
}
b := &bin.Buffer{}
if err := resp.Encode(b); err != nil {
return err
}
return output.Decode(b)
}
+114
View File
@@ -0,0 +1,114 @@
package tgmock
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
)
// TestingT is simplified *testing.T interface.
type TestingT interface {
require.TestingT
assert.TestingT
Helper()
Cleanup(cb func())
}
// Mock is a mock for tg.Invoker with testify/require support.
type Mock struct {
calls []Handler
assert assertions
}
// Option configures Mock.
type Option interface {
apply(t TestingT, m *Mock)
}
type assertions interface {
Truef(value bool, msg string, args ...interface{})
Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{})
}
type assertAssertions struct {
assert *assert.Assertions
}
func (a assertAssertions) Equal(expected, actual interface{}, msgAndArgs ...interface{}) {
a.assert.Equal(expected, actual, msgAndArgs...)
}
func (a assertAssertions) Truef(value bool, msg string, args ...interface{}) {
a.assert.Truef(value, msg, args...)
}
type optionFunc func(t TestingT, m *Mock)
func (o optionFunc) apply(t TestingT, m *Mock) { o(t, m) }
// WithRequire configures mock to use "require" assertions.
func WithRequire() Option {
return optionFunc(func(t TestingT, m *Mock) {
m.assert = require.New(t)
})
}
// NewRequire creates new Mock with "require" assertions.
func NewRequire(t TestingT) *Mock {
return New(t, WithRequire())
}
// New creates new Mock.
func New(t TestingT, options ...Option) *Mock {
m := &Mock{
assert: &assertAssertions{
assert: assert.New(t),
},
}
for _, o := range options {
o.apply(t, m)
}
t.Cleanup(func() {
m.assert.Truef(
m.AllWereMet(),
"not all expected calls happen (expected yet %d)",
len(m.calls),
)
})
return m
}
// AllWereMet returns true if all expected calls happened.
func (i *Mock) AllWereMet() bool {
return len(i.calls) == 0
}
func (i *Mock) add(h Handler) *Mock {
i.calls = append(i.calls, h)
return i
}
func (i *Mock) pop() (Handler, bool) {
if len(i.calls) < 1 {
return nil, false
}
h := i.calls[0]
// Delete from SliceTricks.
copy(i.calls, i.calls[1:])
i.calls[len(i.calls)-1] = nil
i.calls = i.calls[:len(i.calls)-1]
return h, true
}
// Handler returns HandlerFunc of Mock.
func (i *Mock) Handler() HandlerFunc {
return func(id int64, body bin.Encoder) (bin.Encoder, error) {
h, ok := i.pop()
i.assert.Truef(ok, "unexpected call")
return h.Handle(id, body)
}
}
+116
View File
@@ -0,0 +1,116 @@
package tgmock
import (
"strconv"
"strings"
"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)
func (i *Mock) request(fn func(b bin.Encoder)) *RequestBuilder {
return &RequestBuilder{
mock: i,
expectFn: fn,
times: 1,
}
}
// Expect creates builder of new expected call.
func (i *Mock) Expect() *RequestBuilder {
return i.request(nil)
}
// ExpectFunc creates builder of new expected call with given input checker.
func (i *Mock) ExpectFunc(fn func(b bin.Encoder)) *RequestBuilder {
return i.request(fn)
}
// ExpectCall creates builder of new expected call with given input.
func (i *Mock) ExpectCall(body bin.Encoder) *RequestBuilder {
return i.request(func(got bin.Encoder) {
i.assert.Equal(body, got)
})
}
// RequestBuilder is builder of expected RPC request.
type RequestBuilder struct {
mock *Mock
expectFn func(b bin.Encoder)
times int
}
// N sets count of expected calls.
func (b *RequestBuilder) N(times int) *RequestBuilder {
b.times = times
return b
}
// ThenResult adds call result to the end of call stack.
func (b *RequestBuilder) ThenResult(body bin.Encoder) *Mock {
return b.result(body, nil)
}
// ThenTrue adds call tg.BoolTrue result to the end of call stack.
func (b *RequestBuilder) ThenTrue() *Mock {
return b.result(&tg.BoolTrue{}, nil)
}
// ThenFalse adds call tg.BoolFalse result to the end of call stack.
func (b *RequestBuilder) ThenFalse() *Mock {
return b.result(&tg.BoolFalse{}, nil)
}
// ThenErr adds call result to the end of call stack.
func (b *RequestBuilder) ThenErr(err error) *Mock {
return b.result(nil, err)
}
// ThenRPCErr adds call result to the end of call stack.
func (b *RequestBuilder) ThenRPCErr(err *tgerr.Error) *Mock {
return b.ThenErr(err)
}
// ThenMigrate adds call result to the end of call stack.
func (b *RequestBuilder) ThenMigrate(typ string, arg int) *Mock {
t := strings.ToUpper(typ) + "_MIGRATE"
return b.ThenRPCErr(&tgerr.Error{
Code: 303,
Message: t + "_" + strconv.Itoa(arg),
Type: t,
Argument: arg,
})
}
// ThenFlood adds call result to the end of call stack.
func (b *RequestBuilder) ThenFlood(arg int) *Mock {
return b.ThenRPCErr(&tgerr.Error{
Code: 420,
Message: "FLOOD_WAIT_" + strconv.Itoa(arg),
Type: "FLOOD_WAIT",
Argument: arg,
})
}
// ThenUnregistered adds call result to the end of call stack.
func (b *RequestBuilder) ThenUnregistered() *Mock {
return b.ThenRPCErr(&tgerr.Error{
Code: 401,
Message: "AUTH_KEY_UNREGISTERED",
Type: "AUTH_KEY_UNREGISTERED",
})
}
func (b *RequestBuilder) result(r bin.Encoder, err error) *Mock {
for i := 0; i < b.times; i++ {
b.mock.add(HandlerFunc(func(id int64, body bin.Encoder) (bin.Encoder, error) {
if b.expectFn != nil {
b.expectFn(body)
}
return r, err
}))
}
return b.mock
}