58 lines · 2.8 KB · GoRaw
package mainimport ( "crypto/sha256" "encoding/hex" "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")func optionHash(words string) []byte { h := sha256.Sum256([]byte(words)); return h[:] }// RULE (owner, FEEDBACK-3): a poll comes with its options in WhatsApp's order and how many one may pick, and the// question as its text.func TestAPollComesWithItsOptions(t *testing.T) { evt := &events.Message{ Info: types.MessageInfo{ID: "3EB0POLL", MessageSource: types.MessageSource{Chat: jid("120363000000000001@g.us"), Sender: jid("15550002222@s.whatsapp.net"), IsGroup: true}}, Message: &waE2E.Message{PollCreationMessageV3: &waE2E.PollCreationMessage{Name: proto.String("Which day?"), SelectableOptionsCount: proto.Uint32(1), Options: []*waE2E.PollCreationMessage_Option{{OptionName: proto.String("Thursday")}, {OptionName: proto.String("Friday")}}}}, } ev := Convert(evt, "live", nil) if ev == nil || ev.Message.Kind != "poll" || ev.Message.Text != "Which day?" || ev.Message.Poll == nil { t.Fatalf("event = %+v", ev) } if p := ev.Message.Poll; len(p.Options) != 2 || p.Options[1] != "Friday" || p.Selectable != 1 { t.Fatalf("poll = %+v", p) }}// RULE: the votes a poll carries in WhatsApp's history come as one poll_vote each, naming the poll, the voter (the// owner as from_me) and the options picked as hashes of their words; a vote taken back picks none.func TestHistoryVotesNameThePollTheVoterAndTheChoice(t *testing.T) { web := &waWeb.WebMessageInfo{ Key: &waCommon.MessageKey{ID: proto.String("3EB0POLL"), RemoteJID: proto.String("120363000000000001@g.us")}, PollUpdates: []*waWeb.PollUpdate{ {PollUpdateMessageKey: &waCommon.MessageKey{ID: proto.String("V1"), Participant: proto.String("15550002222@s.whatsapp.net")}, Vote: &waE2E.PollVoteMessage{SelectedOptions: [][]byte{optionHash("Friday")}}, SenderTimestampMS: proto.Int64(1_760_000_000_000)}, {PollUpdateMessageKey: &waCommon.MessageKey{ID: proto.String("V2"), FromMe: proto.Bool(true)}, Vote: &waE2E.PollVoteMessage{}}, }, } votes := historyVotes(web, jid("120363000000000001@g.us"), nil) if len(votes) != 2 { t.Fatalf("votes = %d", len(votes)) } first := votes[0] if first.Event != "poll_vote" || first.Target != "3EB0POLL" || first.Message.Sender != "15550002222@s.whatsapp.net" || first.Message.Timestamp != 1_760_000_000 { t.Fatalf("first = %+v %+v", first, first.Message) } if len(first.Votes) != 1 || first.Votes[0] != hex.EncodeToString(optionHash("Friday")) { t.Fatalf("first votes = %v", first.Votes) } if mine := votes[1]; !mine.Message.FromMe || mine.Message.Sender != "" || len(mine.Votes) != 0 { t.Fatalf("the owner's taken-back vote = %+v %+v", mine, mine.Message) }}