Cotton code
139 lines · 6.4 KB · GoRaw
package mainimport (	"context"	"encoding/json"	"errors"	"path/filepath"	"strings"	"testing"	"go.mau.fi/whatsmeow/types")// RULE (CONTRACT "Replies and reactions"): a plain text is a Conversation; a reply is an ExtendedTextMessage quoting// the message by its id, its writer, and what WhatsApp draws in the quote by its kind (words, a picture's caption, a// voice note, a document's name).func TestBuildText(t *testing.T) {	plain := buildText("hi", Quote{})	if plain.GetConversation() != "hi" || plain.ExtendedTextMessage != nil {		t.Fatalf("plain = %v", plain)	}	reply := buildText("yes", Quote{ID: "3EB0AA", Sender: "15550001111@s.whatsapp.net", Kind: "text", Text: "Dinner on Friday?"})	ext := reply.GetExtendedTextMessage()	if reply.Conversation != nil || ext.GetText() != "yes" {		t.Fatalf("reply = %v", reply)	}	ci := ext.GetContextInfo()	if ci.GetStanzaID() != "3EB0AA" || ci.GetParticipant() != "15550001111@s.whatsapp.net" || ci.GetQuotedMessage().GetConversation() != "Dinner on Friday?" {		t.Fatalf("context = %v", ci)	}	if buildText("yes", Quote{ID: "3EB0AA"}).GetExtendedTextMessage().GetContextInfo().Participant != nil {		t.Fatal("participant set without a quoted sender")	}	if q := (Quote{ID: "x", Kind: "image", Text: "the view"}).context().GetQuotedMessage(); q.GetImageMessage().GetCaption() != "the view" {		t.Fatalf("picture quote = %v", q)	}	if q := (Quote{ID: "x", Kind: "voice"}).context().GetQuotedMessage(); !q.GetAudioMessage().GetPTT() {		t.Fatalf("voice quote = %v", q)	}	if q := (Quote{ID: "x", Kind: "document", Text: "plan.pdf"}).context().GetQuotedMessage(); q.GetDocumentMessage().GetTitle() != "plan.pdf" {		t.Fatalf("document quote = %v", q)	}}// RULE: a send without text, without a message id, or for an unknown account is refused, not sent.func TestDispatchSendRefuses(t *testing.T) {	h := NewHub(nil, nil, nil)	cases := []Request{		{ID: 1, Op: "send", Account: "15550001111@s.whatsapp.net", Chat: "15550002222@s.whatsapp.net", Text: "  ", MessageID: "3EB0AA"},		{ID: 2, Op: "send", Account: "15550001111@s.whatsapp.net", Chat: "15550002222@s.whatsapp.net", Text: "hi"},		{ID: 3, Op: "send", Account: "15550001111@s.whatsapp.net", Chat: "15550002222@s.whatsapp.net", Text: "hi", MessageID: "3EB0AA"},		{ID: 4, Op: "send", Account: "15550001111@s.whatsapp.net", Chat: "", Text: "hi", MessageID: "3EB0AA"},	}	for _, c := range cases {		if _, err := dispatch(context.Background(), h, &c); err == nil {			t.Errorf("request %d was not refused", c.ID)		}	}	h.held["15550001111@s.whatsapp.net"] = true	if _, err := dispatch(context.Background(), h, &cases[2]); err == nil {		t.Error("a held account sent")	}}// RULE: WhatsApp never echoes this device's own sends, so a text the helper sent is kept in its outbox as the// owner's message in that chat, under the id it went out with: Cotton lands it once, like any message.func TestASentTextIsKeptAsTheOwnersMessage(t *testing.T) {	o, err := OpenOutbox(filepath.Join(t.TempDir(), "outbox.db"))	if err != nil {		t.Fatal(err)	}	defer o.Close()	h := NewHub(nil, o, nil)	h.durable(sentEvent("15550001111@s.whatsapp.net", jid("15550002222@s.whatsapp.net"), "on my way", "3EB0SENT", 1_760_000_000, ""))	seqs, raws, err := o.After(0, 10)	if err != nil || len(seqs) != 1 {		t.Fatalf("outbox holds %d events (%v)", len(seqs), err)	}	var ev Event	if err := json.Unmarshal([]byte(raws[0]), &ev); err != nil {		t.Fatal(err)	}	m := ev.Message	if ev.Event != "message" || ev.Account != "15550001111@s.whatsapp.net" || m == nil {		t.Fatalf("event = %+v", ev)	}	if m.ID != "3EB0SENT" || !m.FromMe || m.Chat != "15550002222@s.whatsapp.net" || m.Text != "on my way" || m.Kind != "text" || m.Timestamp != 1_760_000_000 || m.IsGroup {		t.Fatalf("message = %+v", m)	}	if g := sentEvent("a", jid("120363000000000001@g.us"), "hi all", "3EB0G", 1, "").Message; !g.IsGroup {		t.Fatal("a group send is not marked a group")	}}// RULE: a text to a person's number asks WhatsApp first. A number with no chat yet is sent to (WhatsApp starts the// chat), under the number WhatsApp keeps; a number WhatsApp does not know is refused as not_on_whatsapp before// anything is sent; a group goes as given, never asked.func TestRecipientAsksWhatsAppAboutANumber(t *testing.T) {	var asked []string	lookup := func(answers ...types.IsOnWhatsAppResponse) func(context.Context, []string) ([]types.IsOnWhatsAppResponse, error) {		return func(_ context.Context, phones []string) ([]types.IsOnWhatsAppResponse, error) {			asked = append(asked, phones...)			return answers, nil		}	}	known := types.IsOnWhatsAppResponse{Query: "+15550002222", IsIn: true, JID: jid("123456789@lid"), PhoneNumber: jid("15550002222@s.whatsapp.net")}	got, err := recipient(context.Background(), jid("15550002222@s.whatsapp.net"), lookup(known))	if err != nil || got.String() != "15550002222@s.whatsapp.net" {		t.Fatalf("a known number with no chat = %v, %v", got, err)	}	if len(asked) != 1 || asked[0] != "+15550002222" {		t.Fatalf("asked %v", asked)	}	// WhatsApp keeps some numbers in another form (a country's extra digit): the text goes where WhatsApp keeps it.	moved := types.IsOnWhatsAppResponse{IsIn: true, JID: jid("5215550003333@s.whatsapp.net")}	if got, _ := recipient(context.Background(), jid("525550003333@s.whatsapp.net"), lookup(moved)); got.String() != "5215550003333@s.whatsapp.net" {		t.Fatalf("a number WhatsApp writes otherwise = %v", got)	}	for name, answers := range map[string][]types.IsOnWhatsAppResponse{		"answered not in": {{Query: "+15550009999", IsIn: false, JID: jid("15550009999@s.whatsapp.net")}},		"not answered":    nil,	} {		_, err := recipient(context.Background(), jid("15550009999@s.whatsapp.net"), lookup(answers...))		if err == nil || !strings.HasPrefix(err.Error(), NotOnWhatsApp+": +15550009999") {			t.Fatalf("%s: err = %v", name, err)		}	}	asked = nil	group := jid("120363000000000001@g.us")	if got, err := recipient(context.Background(), group, lookup()); err != nil || got != group || len(asked) != 0 {		t.Fatalf("a group = %v, %v, asked %v", got, err, asked)	}	failing := func(context.Context, []string) ([]types.IsOnWhatsAppResponse, error) {		return nil, errors.New("timed out")	}	if _, err := recipient(context.Background(), jid("15550002222@s.whatsapp.net"), failing); err == nil || strings.HasPrefix(err.Error(), NotOnWhatsApp) {		t.Fatalf("a failed question is not a number WhatsApp does not know: %v", err)	}}