Cotton code
71 lines · 3.3 KB · GoRaw
package mainimport (	"path/filepath"	"testing"	"go.mau.fi/whatsmeow/proto/waCommon"	"go.mau.fi/whatsmeow/proto/waE2E"	"go.mau.fi/whatsmeow/proto/waWeb"	"go.mau.fi/whatsmeow/types"	"go.mau.fi/whatsmeow/types/events"	"google.golang.org/protobuf/proto")// RULE (owner, FEEDBACK-3): the census names every kind WhatsApp sends, the ones Convert drops too (poll votes,// calls, live locations, view-once), and keeps counts only.func TestTheCensusCountsEveryKindAndNothingElse(t *testing.T) {	cases := map[string]*waE2E.Message{		"text":                {Conversation: proto.String("invented")},		"gif":                 {VideoMessage: &waE2E.VideoMessage{GifPlayback: proto.Bool(true)}},		"voice note":          {AudioMessage: &waE2E.AudioMessage{PTT: proto.Bool(true)}},		"poll":                {PollCreationMessageV3: &waE2E.PollCreationMessage{Name: proto.String("invented")}},		"poll vote":           {PollUpdateMessage: &waE2E.PollUpdateMessage{}},		"live location":       {LiveLocationMessage: &waE2E.LiveLocationMessage{}},		"delete for everyone": {ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_REVOKE.Enum()}},		"edit":                {ProtocolMessage: &waE2E.ProtocolMessage{Type: waE2E.ProtocolMessage_MESSAGE_EDIT.Enum()}},	}	for want, msg := range cases {		if got := kindOf(msg); got != want {			t.Errorf("kindOf = %q, want %q", got, want)		}	}	o, err := OpenOutbox(filepath.Join(t.TempDir(), "outbox.db"))	if err != nil {		t.Fatal(err)	}	defer o.Close()	h := NewHub(nil, o, nil)	once := &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{Chat: jid("15550002222@s.whatsapp.net")}}, Message: cases["text"], IsViewOnce: true}	h.census("live", once, jid("15550002222@s.whatsapp.net"))	h.census("live", &events.Message{Info: types.MessageInfo{MessageSource: types.MessageSource{Chat: jid("123@lid")}}, Message: cases["poll vote"]}, jid("123@lid"))	got, err := o.Census()	if err != nil {		t.Fatal(err)	}	want := map[string]int64{"text": 1, "(view once)": 1, "poll vote": 1, "(chat known by no number)": 1}	if len(got["live"]) != len(want) {		t.Fatalf("census = %v", got)	}	for kind, n := range want {		if got["live"][kind] != n {			t.Fatalf("census[%q] = %d, want %d (%v)", kind, got["live"][kind], n, got)		}	}}// RULE (CONTRACT "Deleted for everyone in WhatsApp", the ask of mac-import): a history entry WhatsApp keeps for a// message deleted for everyone (a REVOKE stub, no content) goes as a revoke of that message's own id; any other// entry, and a message with content, goes as nothing of the kind.func TestAHistoryDeletedMarkerIsARevokeOfItsMessage(t *testing.T) {	chat := jid("15550002222@s.whatsapp.net")	stub := &waWeb.WebMessageInfo{Key: &waCommon.MessageKey{ID: proto.String("3EB0GONE"), FromMe: proto.Bool(false)}, MessageTimestamp: proto.Uint64(1_760_000_000), MessageStubType: waWeb.WebMessageInfo_REVOKE.Enum()}	ev := historyRevoke(stub, chat, nil)	if ev == nil || ev.Event != "revoke" || ev.Target != "3EB0GONE" || ev.Chat != "15550002222@s.whatsapp.net" || ev.Message.Timestamp != 1_760_000_000 || ev.Message.Origin != "history" {		t.Fatalf("stub became %+v", ev)	}	join := &waWeb.WebMessageInfo{Key: &waCommon.MessageKey{ID: proto.String("x")}, MessageStubType: waWeb.WebMessageInfo_GROUP_PARTICIPANT_ADD.Enum()}	if historyRevoke(join, chat, nil) != nil {		t.Fatal("a join became a revoke")	}}