Cotton code
56 lines · 2.1 KB · GoRaw
package mainimport (	"testing"	"time"	"go.mau.fi/whatsmeow/types"	"go.mau.fi/whatsmeow/types/events")// RULE (owner, FEEDBACK-3, 23 Nov 2026): a file's pointer names the message it came in, as WhatsApp addressed it, so// a file WhatsApp's servers dropped can be asked of his phone again.func TestAPointerNamesItsMessage(t *testing.T) {	info := types.MessageInfo{ID: "3EB0PIC", MessageSource: types.MessageSource{Chat: jid("120363000000000001@g.us"), Sender: jid("123456789@lid"), IsGroup: true}}	p := &MediaPointer{Type: "image"}	reuploadable(p, info)	if p.MessageID != "3EB0PIC" || p.Chat != "120363000000000001@g.us" || p.Sender != "123456789@lid" || !p.IsGroup || p.FromMe {		t.Fatalf("pointer = %+v", p)	}	reuploadable(nil, info) // a message with no file: nothing to name}// RULE: the phone's answer reaches the download waiting for that message alone; an answer that says the phone no// longer has the file is "gone", as before; nobody waiting drops the answer.func TestThePhonesAnswerReachesItsDownload(t *testing.T) {	var r retries	mine, done := r.wait("3EB0MINE")	defer done()	r.answered(&events.MediaRetry{MessageID: "3EB0OTHER"})	select {	case <-mine:		t.Fatal("another message's answer reached this download")	case <-time.After(10 * time.Millisecond):	}	r.answered(&events.MediaRetry{MessageID: "3EB0MINE", Error: &events.MediaRetryError{Code: 2}})	evt := <-mine	if _, err := retryPath(evt, make([]byte, 32)); err != ErrGone {		t.Fatalf("a phone without the file = %v, want gone", err)	}	done()	r.answered(&events.MediaRetry{MessageID: "3EB0MINE"}) // nobody waits: dropped, never blocks}// RULE: once the phone leaves an ask unanswered (off, away), the next files are not asked for ten minutes, so a page// of expired files never waits a minute and a half each.func TestASilentPhoneIsNotAskedAgainAtOnce(t *testing.T) {	var r retries	now := time.Unix(1_760_000_000, 0)	if r.silent(now) {		t.Fatal("silent before any ask")	}	r.unanswered(now)	if !r.silent(now.Add(9*time.Minute)) || r.silent(now.Add(11*time.Minute)) {		t.Fatal("quiet for ten minutes, then asked again")	}}