SendTracked returns a string id for a send message, and Update can be used to change that message

This commit is contained in:
bel
2022-01-17 20:05:02 -07:00
parent f2c9602d70
commit d4c1e20230
3 changed files with 89 additions and 34 deletions

View File

@@ -1,57 +1,63 @@
package message
import (
"encoding/json"
"io/ioutil"
"local/truckstop/config"
"os"
"path"
"testing"
"github.com/google/uuid"
)
func TestMatrixSend(t *testing.T) {
if len(os.Getenv("INTEGRATION")) == 0 {
t.Skip("$INTEGRATION not set")
}
var c config.Config
b, err := ioutil.ReadFile("../config.json")
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &c); err != nil {
t.Fatal(err)
}
var sender Sender = &Matrix{
homeserver: c.Message.Matrix.Homeserver,
username: c.Message.Matrix.Username,
token: c.Message.Matrix.Token,
room: c.Message.Matrix.Room,
}
sender := testMatrix(t)
if err := sender.Send("hello world from unittest"); err != nil {
t.Fatal(err)
}
}
func TestMatrixReceive(t *testing.T) {
if len(os.Getenv("INTEGRATION")) == 0 {
t.Skip("$INTEGRATION not set")
}
var c config.Config
b, err := ioutil.ReadFile("../config.json")
func TestMatrixUpdate(t *testing.T) {
// 19:32:34: VER: matrix event: {StateKey:<nil> Sender:@bot:m.bltrucks.top Type:m.room.message Timestamp:1642471594729 ID:$n0ln9TFZrko_cBNFXeh8YyICZ3fFm17Jhz3bmZcqig4 RoomID:!OYZqtInrBCn1cyz90D:m.bltrucks.top Redacts: Unsigned:map[transaction_id:go1642471594571852423] Content:map[body:hello world from unittest format: formatted_body: msgtype:m.text] PrevContent:map[]}
// func (m Matrix) Update(id, text string) error {
m := testMatrix(t)
uid := uuid.New().String()[:3]
id, err := m.SendTracked("hello, heheheh from test matrix update " + uid)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &c); err != nil {
err = m.Update(id, "heheheh i updated from test matrix update "+uid)
if err != nil {
t.Fatal(err)
}
var sender Sender = &Matrix{
homeserver: c.Message.Matrix.Homeserver,
username: c.Message.Matrix.Username,
token: c.Message.Matrix.Token,
room: c.Message.Matrix.Room,
}
m.Receive()
}
func TestMatrixReceive(t *testing.T) {
sender := testMatrix(t)
if msgs, err := sender.Receive(); err != nil {
t.Fatal(err)
} else {
t.Logf("%+v", msgs)
}
}
func testMatrix(t *testing.T) Matrix {
if len(os.Getenv("INTEGRATION")) == 0 {
t.Skip("$INTEGRATION not set")
}
d := t.TempDir()
f := path.Join(d, "config.test.json")
b, err := ioutil.ReadFile("../config.json")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(f, b, os.ModePerm); err != nil {
t.Fatal(err)
}
os.Setenv("CONFIG", f)
if err := config.Refresh(nil); err != nil {
t.Fatal(err)
}
return NewMatrix()
}