Cotton code
134 lines · 5.0 KB · GoRaw
package mainimport (	"path/filepath"	"testing"	"time")// RULE: The helper asks WhatsApp only about the chats Cotton last named, a newly named one first, each again only after pictureRecheck; a chat Cotton stops naming is forgotten.func TestPictureQueueAsksOnlyWantedChatsInTurn(t *testing.T) {	o, err := OpenOutbox(filepath.Join(t.TempDir(), "outbox.db"))	if err != nil {		t.Fatal(err)	}	defer o.Close()	now := time.Unix(1_760_000_000, 0)	due := now.Add(-pictureRecheck).Unix()	if err := o.SetWantedPictures([]string{"15550001111@s.whatsapp.net", "120363000000000001@g.us"}); err != nil {		t.Fatal(err)	}	chat, _, ok, err := o.NextPicture(due)	if err != nil || !ok || chat != "120363000000000001@g.us" {		t.Fatalf("first: %q %v %v", chat, ok, err)	}	if err := o.MarkPicture(chat, "7", now.Unix()); err != nil {		t.Fatal(err)	}	next, _, _, _ := o.NextPicture(due)	if next != "15550001111@s.whatsapp.net" {		t.Fatalf("second: %q", next)	}	_ = o.MarkPicture(next, "", now.Unix())	if chat, _, ok, _ := o.NextPicture(due); ok {		t.Fatalf("nothing is due before the recheck, got %q", chat)	}	// A new chat is asked about at once; the group keeps the id it was announced with.	_ = o.SetWantedPictures([]string{"120363000000000001@g.us", "15550004444@s.whatsapp.net"})	chat, id, ok, _ := o.NextPicture(due)	if !ok || chat != "15550004444@s.whatsapp.net" || id != "" {		t.Fatalf("the new chat first: %q %q", chat, id)	}	later := now.Add(pictureRecheck).Unix()	_ = o.MarkPicture(chat, "", now.Unix())	chat, id, _, _ = o.NextPicture(later)	if chat != "120363000000000001@g.us" || id != "7" {		t.Fatalf("the group again after the recheck, with its id: %q %q", chat, id)	}	wanted, _ := o.WantedPictures()	if len(wanted) != 2 {		t.Fatalf("the dropped chat is forgotten: %v", wanted)	}}// RULE: The wanted list's digest is the one the Rust reader computes (whatsapp_link.rs pictures_digest), so an unchanged list is never sent again.func TestPicturesDigestMatchesTheReader(t *testing.T) {	got := picturesDigest([]string{"15550001111@s.whatsapp.net", "120363000000000001@g.us"})	if got != "f0a1d2427c30841c3f109eefc292730f6b73c00fbf79f415b247bf31af936e50" {		t.Fatalf("digest %s", got)	}}// RULE: The helper asks WhatsApp about profile pictures no faster than one every two seconds and backs off for minutes when told to; a burst of questions reads as a bot and WhatsApp bans numbers for it.func TestPictureQuestionsArePaced(t *testing.T) {	if pictureEvery < 2*time.Second || pictureBackoff < 5*time.Minute || pictureRecheck < 24*time.Hour {		t.Fatalf("pacing too fast: every %s, backoff %s, recheck %s", pictureEvery, pictureBackoff, pictureRecheck)	}}// RULE: An outbox that announced pictures under 0.29.27 — which refused them and still acknowledged the page — asks about every wanted chat again, once: the next open forgets the announced ids, and the one after keeps what it learns.func TestAnOutboxForgetsThePicturesItAnnouncedOnceOnly(t *testing.T) {	path := filepath.Join(t.TempDir(), "outbox.db")	o, err := OpenOutbox(path)	if err != nil {		t.Fatal(err)	}	now := time.Unix(1_760_000_000, 0)	due := now.Add(-pictureRecheck).Unix()	_ = o.SetWantedPictures([]string{"15550001111@s.whatsapp.net"})	// As 0.29.27's helper left it: announced, and no version yet.	_ = o.MarkPicture("15550001111@s.whatsapp.net", "7", now.Unix())	if _, err := o.db.Exec(`PRAGMA user_version = 0`); err != nil {		t.Fatal(err)	}	o.Close()	o, err = OpenOutbox(path)	if err != nil {		t.Fatal(err)	}	chat, id, ok, _ := o.NextPicture(due)	if !ok || chat != "15550001111@s.whatsapp.net" || id != "" {		t.Fatalf("asked again with no id, so it is announced again: %q %q %v", chat, id, ok)	}	_ = o.MarkPicture(chat, "8", now.Unix())	o.Close()	o, err = OpenOutbox(path)	if err != nil {		t.Fatal(err)	}	defer o.Close()	if chat, _, ok, _ := o.NextPicture(due); ok {		t.Fatalf("forgotten once only, got %q", chat)	}}// RULE (owner via the coordinator, 24 Nov 2026: Comms showed old pictures for contacts whose WhatsApp picture// changed): WhatsApp's picture-changed notice makes that wanted chat due at once; a chat Cotton does not want is// never asked about because of one.func TestAPictureWhatsAppSaysChangedIsAskedAboutNext(t *testing.T) {	o, err := OpenOutbox(filepath.Join(t.TempDir(), "outbox.db"))	if err != nil {		t.Fatal(err)	}	defer o.Close()	now := time.Unix(1_760_000_000, 0)	due := now.Add(-pictureRecheck).Unix()	_ = o.SetWantedPictures([]string{"15550001111@s.whatsapp.net"})	_ = o.MarkPicture("15550001111@s.whatsapp.net", "7", now.Unix())	if _, _, ok, _ := o.NextPicture(due); ok {		t.Fatal("asked about just now: not due")	}	if err := o.PictureChanged("15550001111@s.whatsapp.net"); err != nil {		t.Fatal(err)	}	_ = o.PictureChanged("15559999999@s.whatsapp.net")	chat, id, ok, _ := o.NextPicture(due)	if !ok || chat != "15550001111@s.whatsapp.net" || id != "7" {		t.Fatalf("the changed picture is due, with the id it had: %q %q %v", chat, id, ok)	}	if wanted, _ := o.WantedPictures(); len(wanted) != 1 {		t.Fatalf("a notice wants no new chat: %v", wanted)	}}