1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main

import (
	"fmt"
	"testing"
)

// seedDatedMessage is seedMessage with the date given rather than taken from
// the clock, because what is under test here is what happens when two messages
// carry the same one.
func seedDatedMessage(t *testing.T, s *server, uid, date int64, from, to string) {
	t.Helper()
	_, err := s.db.Write.Exec(
		`INSERT INTO messages (mailbox, uid, uidvalidity, subject, from_addr, date, to_addrs)
		 VALUES ('INBOX', ?, 1, 'a subject', ?, ?, ?)`,
		uid, from, date, to,
	)
	if err != nil {
		t.Fatalf("seed message: %v", err)
	}
}

// TestContactsOrderIsStable pins the contacts listing to an order the mail
// decides, over the two ways it used to be decided by nothing of the sort.
//
// Contacts are folded into a map keyed by address and the slice is built by
// ranging over it, so the input to the sort arrives in Go's randomised
// iteration order. Every address on a message is stamped with that message's
// date, so a mail to several people leaves them tied exactly, and a sort that
// stops at the last-interaction time leaves such rows wherever the map put
// them: the listing reshuffled on every request, and in the paged text
// rendering a contact crossed a page boundary while the mail stood still.
//
// Repetition is the test. One call cannot distinguish a stable order from a
// lucky one, so each case asks the same question of a fresh read often enough
// that a random order would almost certainly have shown itself.
func TestContactsOrderIsStable(t *testing.T) {
	setTestAccount(t)
	s := newTestServer(t)

	const rounds = 30

	// Ten correspondents on one mail, hence ten contacts whose last
	// interaction is the same second. uid order is deliberately not address
	// order, so a listing that merely echoes the table is not mistaken for a
	// sorted one.
	to := `[`
	for i := range 10 {
		if i > 0 {
			to += `,`
		}
		to += fmt.Sprintf(`{"name":"P %d","address":"p%d@example.org"}`, i, (i*7)%10)
	}
	to += `]`
	seedDatedMessage(t, s, 1, 1000, "me@example.org", to)

	// An older contact, and one marked important. Important sorts above
	// everything, so a listing that only got recency right fails here too.
	seedDatedMessage(t, s, 2, 500, "Old <old@example.org>", `[{"address":"me@example.org"}]`)
	seedDatedMessage(t, s, 3, 400, "Vip <vip@example.org>", `[{"address":"me@example.org"}]`)
	if _, err := s.db.Write.Exec(
		`INSERT INTO contact_flags (address, flag, marked_at) VALUES ('vip@example.org', ?, 1)`,
		flagImportant,
	); err != nil {
		t.Fatalf("mark important: %v", err)
	}

	addresses := func() []string {
		t.Helper()
		cs, err := collectContacts(s.db.Read, false)
		if err != nil {
			t.Fatalf("collect contacts: %v", err)
		}
		out := make([]string, len(cs))
		for i, c := range cs {
			out[i] = c.Address
		}
		return out
	}

	t.Run("the order is the one the mail states", func(t *testing.T) {
		want := []string{
			// Important first, despite being the oldest mail here.
			"vip@example.org",
			// Then the ten tied recipients, by address.
			"p0@example.org", "p1@example.org", "p2@example.org", "p3@example.org",
			"p4@example.org", "p5@example.org", "p6@example.org", "p7@example.org",
			"p8@example.org", "p9@example.org",
			// Then the older correspondent.
			"old@example.org",
		}
		for round := range rounds {
			got := addresses()
			if len(got) != len(want) {
				t.Fatalf("round %d: %d contacts, want %d: %v", round, len(got), len(want), got)
			}
			for i := range want {
				if got[i] != want[i] {
					t.Fatalf("round %d: position %d = %s, want %s (order: %v)",
						round, i, got[i], want[i], got)
				}
			}
		}
	})

	t.Run("a name is not decided by the query plan", func(t *testing.T) {
		// Two messages from one address, same second, different display
		// names. The name shown is the one on the most recent message, and
		// with the timestamps equal "most recent" can only mean the one read
		// last — which is settled by the scan's ORDER BY and not by whichever
		// row SQLite cared to produce first.
		seedDatedMessage(t, s, 4, 2000, "First Claim <two@example.org>", `[{"address":"me@example.org"}]`)
		seedDatedMessage(t, s, 5, 2000, "Second Claim <two@example.org>", `[{"address":"me@example.org"}]`)

		for round := range rounds {
			cs, err := collectContacts(s.db.Read, false)
			if err != nil {
				t.Fatalf("collect contacts: %v", err)
			}
			var name string
			for _, c := range cs {
				if c.Address == "two@example.org" {
					name = c.Name.Claimed
				}
			}
			if name != "Second Claim" {
				t.Fatalf("round %d: name = %q, want %q", round, name, "Second Claim")
			}
		}
	})
}