Files
clawbot 16649e0f2f
check / check (push) Successful in 54s
A SimpleX Chat bot that answers arithmetic (closes #1)
Remove the template's HTTP service, database and fx wiring. Add exact
arithmetic on go/parser and go/constant, a client that runs simplex-chat
as a child process and drives its WebSocket API, and the bot, which keeps
an auto-accepting address and replies to each message. The image adds the
checksum-pinned simplex-chat v7.0.2 on Ubuntu 22.04.

Model: opus-5-5
2026-09-26 22:01:51 +00:00

58 lines
2.0 KiB
Go

package simplex_test
import (
"encoding/json"
"testing"
"sneak.berlin/go/simplexcalc/internal/simplex"
)
// newChatItems holds one message the bot must answer and four it must
// not, each shaped as the chat client sends it.
const newChatItems = `{"type":"newChatItems","user":{"userId":1},"chatItems":[
{"chatInfo":{"type":"direct","contact":{"contactId":3,"localDisplayName":"alice"}},
"chatItem":{"chatDir":{"type":"directRcv"},
"meta":{"itemId":41,"itemText":"2 + 2","itemEdited":false},
"content":{"type":"rcvMsgContent","msgContent":{"type":"text","text":"2 + 2"}},
"mentions":{},"reactions":[]}},
{"chatInfo":{"type":"direct","contact":{"contactId":3}},
"chatItem":{"chatDir":{"type":"directSnd"},"meta":{"itemId":42},
"content":{"type":"sndMsgContent","msgContent":{"type":"text","text":"4"}}}},
{"chatInfo":{"type":"group","groupInfo":{"groupId":9}},
"chatItem":{"chatDir":{"type":"groupRcv","groupMember":{"groupMemberId":5}},
"meta":{"itemId":43},
"content":{"type":"rcvMsgContent","msgContent":{"type":"text","text":"1 + 1"}}}},
{"chatInfo":{"type":"direct","contact":{"contactId":3}},
"chatItem":{"chatDir":{"type":"directRcv"},"meta":{"itemId":44},
"content":{"type":"rcvMsgContent","msgContent":{"type":"file","text":"3 * 3"}}}},
{"chatInfo":{"type":"direct","contact":{"contactId":3}},
"chatItem":{"chatDir":{"type":"directRcv"},"meta":{"itemId":45},
"content":{"type":"rcvDirectEvent","rcvDirectEvent":{"type":"contactDeleted"}}}}
]}`
// TestMessage: only a text message a contact sent in a direct chat is a
// message to answer. Answering the bot's own messages would loop.
func TestMessage(t *testing.T) {
t.Parallel()
var r simplex.NewChatItems
err := json.Unmarshal([]byte(newChatItems), &r)
if err != nil {
t.Fatalf("decoding: %v", err)
}
var got []simplex.Message
for _, item := range r.ChatItems {
if msg, ok := item.Message(); ok {
got = append(got, msg)
}
}
want := simplex.Message{ContactID: 3, ItemID: 41, Text: "2 + 2"}
if len(got) != 1 || got[0] != want {
t.Errorf("messages = %+v, want only %+v", got, want)
}
}