100 lines · 3.8 KB · GoRaw
package mainimport ( "context" "errors" "testing" "time" "go.mau.fi/whatsmeow/appstate" "go.mau.fi/whatsmeow/types" "go.mau.fi/whatsmeow/types/events")// fakeActor stands in for a linked client: it keeps what it was asked and changes nothing anywhere.type fakeActor struct { blocked []string patches []appstate.PatchInfo refuse error}func (f *fakeActor) UpdateBlocklist(_ context.Context, jid types.JID, action events.BlocklistChangeAction) (*types.Blocklist, error) { if f.refuse != nil { return nil, f.refuse } f.blocked = append(f.blocked, string(action)+" "+jid.String()) return &types.Blocklist{}, nil}func (f *fakeActor) SendAppState(_ context.Context, patch appstate.PatchInfo) error { if f.refuse != nil { return f.refuse } f.patches = append(f.patches, patch) return nil}const person = "15550002222@s.whatsapp.net"// RULE (owner, 24 Nov 2026): block and unblock go to WhatsApp's own block list for that person, and nothing else.func TestBlockAndUnblockAPerson(t *testing.T) { f := &fakeActor{} for _, kind := range []string{"block", "unblock"} { if _, err := sourceAct(t.Context(), f, &Request{Kind: kind, Chat: person, MessageID: "3EB0A"}, time.Unix(100, 0)); err != nil { t.Fatalf("%s: %v", kind, err) } } if want := []string{"block " + person, "unblock " + person}; len(f.blocked) != 2 || f.blocked[0] != want[0] || f.blocked[1] != want[1] { t.Fatalf("blocked %v, want %v", f.blocked, want) } if len(f.patches) != 0 { t.Fatalf("a block changed app state: %d patches", len(f.patches)) }}// RULE (owner, 24 Nov 2026): a chat is deleted for him alone, up to its newest message, keeping its media on his// phone; a group, and a delete naming no newest message, are refused before anything goes.func TestDeleteAChatForHimOnly(t *testing.T) { f := &fakeActor{} req := &Request{Kind: "delete_chat", Chat: person, MessageID: "3EB0B", Target: "3EB0LAST", TargetFromMe: true, TargetTS: 1_763_900_000} if _, err := sourceAct(t.Context(), f, req, time.Unix(100, 0)); err != nil { t.Fatal(err) } if len(f.patches) != 1 || len(f.blocked) != 0 { t.Fatalf("patches %d, blocks %d", len(f.patches), len(f.blocked)) } got := f.patches[0] want := appstate.BuildDeleteChat(types.NewJID("15550002222", types.DefaultUserServer), time.Unix(1_763_900_000, 0), nil, false) if got.Type != want.Type || len(got.Mutations) != 1 || got.Mutations[0].Index[0] != want.Mutations[0].Index[0] || got.Mutations[0].Index[1] != person { t.Fatalf("patch %+v is not a delete-chat of %s", got, person) } // deleteMedia false: the media he has on his phone stays. if idx := got.Mutations[0].Index; len(idx) < 3 || idx[2] != "0" { t.Fatalf("delete-chat index %v deletes his media too", idx) } range_ := got.Mutations[0].Value.GetDeleteChatAction().GetMessageRange() if range_.GetLastMessageTimestamp() != 1_763_900_000 || range_.GetMessages()[0].GetKey().GetID() != "3EB0LAST" || !range_.GetMessages()[0].GetKey().GetFromMe() { t.Fatalf("message range %+v", range_) } for _, bad := range []*Request{ {Kind: "delete_chat", Chat: "120363000000000000@g.us", Target: "3EB0LAST", TargetTS: 1}, {Kind: "delete_chat", Chat: person}, {Kind: "block", Chat: "120363000000000000@g.us"}, {Kind: "archive", Chat: person}, } { if _, err := sourceAct(t.Context(), f, bad, time.Unix(100, 0)); err == nil { t.Errorf("%+v was not refused", bad) } } if len(f.patches) != 1 || len(f.blocked) != 0 { t.Fatalf("a refused act still went: patches %d, blocks %d", len(f.patches), len(f.blocked)) }}// RULE: WhatsApp's refusal is said, never taken as done.func TestARefusedActIsSaid(t *testing.T) { f := &fakeActor{refuse: errors.New("not-authorized")} if _, err := sourceAct(t.Context(), f, &Request{Kind: "block", Chat: person}, time.Unix(1, 0)); err == nil { t.Fatal("a refused block was taken as done") }}