7a04f298d2
- 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
123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package tdjson_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/go-faster/jx"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tdapi"
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
|
|
"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
|
|
)
|
|
|
|
func TestEncodeDecode(t *testing.T) {
|
|
type obj interface {
|
|
tdjson.TDLibDecoder
|
|
tdjson.TDLibEncoder
|
|
TypeInfo() tdp.Type
|
|
}
|
|
|
|
test := func(create func() obj) func(t *testing.T) {
|
|
return func(t *testing.T) {
|
|
a := require.New(t)
|
|
req := create()
|
|
|
|
enc := tdjson.Encoder{
|
|
Writer: &jx.Writer{},
|
|
}
|
|
a.NoError(req.EncodeTDLibJSON(enc))
|
|
a.True(json.Valid(enc.Buf))
|
|
|
|
dec := tdjson.Decoder{
|
|
Decoder: jx.DecodeBytes(enc.Buf),
|
|
}
|
|
a.NoError(req.DecodeTDLibJSON(dec))
|
|
}
|
|
}
|
|
|
|
types := []obj{
|
|
&tdapi.SetTdlibParametersRequest{
|
|
UseTestDC: true,
|
|
DatabaseDirectory: "database",
|
|
FilesDirectory: "files",
|
|
UseFileDatabase: true,
|
|
UseChatInfoDatabase: true,
|
|
UseMessageDatabase: true,
|
|
UseSecretChats: true,
|
|
APIID: 10,
|
|
APIHash: "russcox",
|
|
SystemLanguageCode: "ru",
|
|
DeviceModel: "gotd",
|
|
SystemVersion: "10",
|
|
ApplicationVersion: "10",
|
|
},
|
|
&tdapi.ProfilePhoto{
|
|
ID: 1,
|
|
},
|
|
&tdapi.ReplyMarkupInlineKeyboard{
|
|
Rows: [][]tdapi.InlineKeyboardButton{
|
|
{
|
|
{
|
|
Text: "text",
|
|
Type: &tdapi.InlineKeyboardButtonTypeCallback{
|
|
Data: []byte("a"),
|
|
},
|
|
},
|
|
{
|
|
Text: "text2",
|
|
Type: &tdapi.InlineKeyboardButtonTypeCallback{
|
|
Data: []byte("b"),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
{
|
|
Text: "text3",
|
|
Type: &tdapi.InlineKeyboardButtonTypeCallback{
|
|
Data: []byte("c"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
// Test empty array.
|
|
&tdapi.ReplyMarkupInlineKeyboard{
|
|
Rows: [][]tdapi.InlineKeyboardButton{},
|
|
},
|
|
}
|
|
|
|
for _, typ := range types {
|
|
t.Run(typ.TypeInfo().Name, test(func() obj {
|
|
return typ
|
|
}))
|
|
}
|
|
}
|
|
|
|
func TestEncoder_PutLong(t *testing.T) {
|
|
for _, tt := range []int64{
|
|
-1,
|
|
0,
|
|
1,
|
|
10,
|
|
math.MaxInt64,
|
|
math.MinInt64,
|
|
} {
|
|
t.Run(strconv.FormatInt(tt, 10), func(t *testing.T) {
|
|
a := require.New(t)
|
|
e := tdjson.Encoder{Writer: &jx.Writer{}}
|
|
e.PutLong(tt)
|
|
data := e.Buf
|
|
a.Equal(strconv.Quote(strconv.FormatInt(tt, 10)), string(data))
|
|
|
|
d := tdjson.Decoder{Decoder: jx.DecodeBytes(data)}
|
|
v, err := d.Long()
|
|
a.NoError(err)
|
|
a.Equal(tt, v)
|
|
})
|
|
}
|
|
}
|