Cotton code
137 lines · 6.0 KB · GoRaw
package mainimport (	"testing"	"time"	waBinary "go.mau.fi/whatsmeow/binary"	"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")// Every number and id here is invented.var (	asha  = types.NewJID("15550000001", types.DefaultUserServer)	owner = types.NewJID("15550000009", types.DefaultUserServer)	club  = types.NewJID("120363000000000001", types.GroupServer)	t0    = time.Unix(1_790_000_000, 0))func meta(id string, from, creator types.JID, at time.Time) types.BasicCallMeta {	return types.BasicCallMeta{From: from, CallCreator: creator, CallID: id, Timestamp: at}}func offer(id string, creator types.JID, video bool) *events.CallOffer {	node := &waBinary.Node{Tag: "offer", Content: []waBinary.Node{{Tag: "audio"}}}	if video {		node.Content = append(node.Content.([]waBinary.Node), waBinary.Node{Tag: "video"})	}	return &events.CallOffer{BasicCallMeta: meta(id, creator, creator, t0), Data: node}}func ended(t *testing.T, ev *Event) *Call {	t.Helper()	if ev == nil || ev.Message == nil || ev.Message.Call == nil {		t.Fatalf("no call line: %+v", ev)	}	if ev.Message.Kind != "call" || ev.Event != "message" {		t.Fatalf("a call goes as a message of kind call: %+v", ev.Message)	}	return ev.Message.Call}// RULE (chat CONTRACT, WhatsApp calls): a call the link sees goes, once it ends, as one message in its chat carrying// the call's facts in CallFacts' shape; nothing is said while it rings.func TestALiveCallEndsAsOneLineWithItsFacts(t *testing.T) {	c := NewCalls()	c.clock = func() time.Time { return t0 }	// Asha rings; he answers on his phone; 3:12 later it ends.	if ev := c.Event("acct", owner.User, offer("A1", asha, false), nil); ev != nil {		t.Fatalf("nothing while it rings: %+v", ev)	}	c.Event("acct", owner.User, &events.CallAccept{BasicCallMeta: meta("A1", owner, asha, t0.Add(4*time.Second))}, nil)	ev := c.Event("acct", owner.User, &events.CallTerminate{BasicCallMeta: meta("A1", asha, asha, t0.Add(196*time.Second)), Reason: "hangup"}, nil)	call := ended(t, ev)	if *call != (Call{Room: "A1", Direction: "in", Outcome: "answered", Reason: "answered", DurationS: 192}) {		t.Fatalf("answered: %+v", call)	}	if ev.Message.ID != "call:A1" || ev.Message.Chat != asha.String() || ev.Message.FromMe || ev.Message.Timestamp != t0.Unix() || ev.Message.Origin != "live" {		t.Fatalf("the line: %+v", ev.Message)	}	// A video call nobody here answered: missed.	c.Event("acct", owner.User, offer("A2", asha, true), nil)	call = ended(t, c.Event("acct", owner.User, &events.CallTerminate{BasicCallMeta: meta("A2", asha, asha, t0.Add(30*time.Second)), Reason: "timeout"}, nil))	if *call != (Call{Room: "A2", Direction: "in", Video: true, Outcome: "missed", Reason: "missed", Missed: true}) {		t.Fatalf("missed: %+v", call)	}	// Declined on his phone: not missed.	c.Event("acct", owner.User, offer("A3", asha, false), nil)	c.Event("acct", owner.User, &events.CallReject{BasicCallMeta: meta("A3", owner, asha, t0)}, nil)	call = ended(t, c.Event("acct", owner.User, &events.CallTerminate{BasicCallMeta: meta("A3", asha, asha, t0)}, nil))	if call.Reason != "declined" || call.Missed {		t.Fatalf("declined: %+v", call)	}	// A group's call notice: the line goes in the group.	notice := &events.CallOfferNotice{BasicCallMeta: types.BasicCallMeta{From: asha, CallCreator: asha, CallID: "G1", GroupJID: club, Timestamp: t0}, Media: "video", Type: "group"}	c.Event("acct", owner.User, notice, nil)	ev = c.Event("acct", owner.User, &events.CallTerminate{BasicCallMeta: meta("G1", asha, asha, t0)}, nil)	call = ended(t, ev)	if !call.Group || !call.Video || ev.Message.Chat != club.String() || !ev.Message.IsGroup {		t.Fatalf("group: %+v %+v", call, ev.Message)	}	// The end of a call never seen ringing says nothing; a call whose end never comes is let go.	if ev := c.Event("acct", owner.User, &events.CallTerminate{BasicCallMeta: meta("unknown", asha, asha, t0)}, nil); ev != nil {		t.Fatalf("nothing for an unseen call: %+v", ev)	}	c.Event("acct", owner.User, offer("A4", asha, false), nil)	c.clock = func() time.Time { return t0.Add(3 * time.Hour) }	c.Event("acct", owner.User, offer("A5", asha, false), nil)	if _, open := c.open["A4"]; open {		t.Fatal("a call with no end is let go")	}}// RULE: WhatsApp's history keeps calls too: a missed call's stub and a call log message each go as a line.func TestHistoryCallsGoAsLines(t *testing.T) {	stub := &waWeb.WebMessageInfo{		Key:              &waCommon.MessageKey{ID: proto.String("H1"), RemoteJID: proto.String(asha.String())},		MessageTimestamp: proto.Uint64(uint64(t0.Unix())),		MessageStubType:  waWeb.WebMessageInfo_CALL_MISSED_VIDEO.Enum(),	}	ev := historyMissedCall(stub, asha, nil)	call := ended(t, ev)	if *call != (Call{Room: "H1", Direction: "in", Video: true, Outcome: "missed", Reason: "missed", Missed: true}) || ev.Message.Origin != "history" || ev.Message.ID != "H1" {		t.Fatalf("stub: %+v %+v", call, ev.Message)	}	other := &waWeb.WebMessageInfo{Key: &waCommon.MessageKey{ID: proto.String("H2")}, MessageStubType: waWeb.WebMessageInfo_GROUP_CHANGE_SUBJECT.Enum()}	if historyMissedCall(other, asha, nil) != nil {		t.Fatal("only call stubs")	}	outcome := waE2E.CallLogMessage_CONNECTED	log := &waE2E.CallLogMessage{IsVideo: proto.Bool(false), CallOutcome: &outcome, DurationSecs: proto.Int64(75)}	if got := callLog(log, "H3", true, false); *got != (Call{Room: "H3", Direction: "out", Outcome: "answered", Reason: "answered", DurationS: 75}) {		t.Fatalf("log answered: %+v", got)	}	missed := waE2E.CallLogMessage_MISSED	log.CallOutcome = &missed	if got := callLog(log, "H4", true, false); got.Reason != "no_answer" || got.Missed {		t.Fatalf("his own call nobody took: %+v", got)	}	if got := callLog(log, "H5", false, false); !got.Missed {		t.Fatalf("a call to him nobody took: %+v", got)	}	ongoing := waE2E.CallLogMessage_ONGOING	log.CallOutcome = &ongoing	if callLog(log, "H6", false, false) != nil {		t.Fatal("a call still going on is no line yet")	}}