--- /dev/null
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 9EBBA431FBD\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Apr 2014 12:58:32 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id MLONvpHhLFhR for <notmuch@notmuchmail.org>;\r
+ Mon, 21 Apr 2014 12:58:26 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu\r
+ [18.9.25.12])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 88E8B431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 21 Apr 2014 12:58:26 -0700 (PDT)\r
+X-AuditID: 1209190c-f794a6d000000c27-fa-53557861ec90\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 2C.8E.03111.16875535; Mon, 21 Apr 2014 15:58:25 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s3LJwKeu029665; \r
+ Mon, 21 Apr 2014 15:58:21 -0400\r
+Received: from drake.dyndns.org\r
+ (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s3LJwIfg023311\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Mon, 21 Apr 2014 15:58:20 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1WcKM6-0004AH-LS; Mon, 21 Apr 2014 15:58:18 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2] test: Test thread linking in all possible delivery orders\r
+Date: Mon, 21 Apr 2014 15:58:11 -0400\r
+Message-Id: <1398110291-15832-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.9.1\r
+In-Reply-To: <87y4zhfmrn.fsf@maritornes.cs.unb.ca>\r
+References: <87y4zhfmrn.fsf@maritornes.cs.unb.ca>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrOIsWRmVeSWpSXmKPExsUixG6nrptYERps8PiWssWN1m5Gi9VzeSyu\r
+ 35zJ7MDssXPWXXaPZ6tuMXtsOfSeOYA5issmJTUnsyy1SN8ugSvj5e4upoIrShXfzn9kaWB8\r
+ LtXFyMkhIWAicXL7d3YIW0ziwr31bF2MXBxCArOZJGY2/GeCcDYySlzoncQM4dxhkjj+/BFU\r
+ Zi6jRP/jm8wg/WwCGhLb9i9nBLFFBKQldt6dzQpiMwvESjS+XQZWIyzgK/FlYScbiM0ioCqx\r
+ 7OgXMJtXwEFi782LTBB3yEmcPDYZrJdTwEhiz5LrYL1CAoYS314dZpvAyL+AkWEVo2xKbpVu\r
+ bmJmTnFqsm5xcmJeXmqRrqFebmaJXmpK6SZGUHhxSvLsYHxzUOkQowAHoxIPr4RBaLAQa2JZ\r
+ cWXuIUZJDiYlUV6BEqAQX1J+SmVGYnFGfFFpTmrxIUYJDmYlEd61aUA53pTEyqrUonyYlDQH\r
+ i5I471trq2AhgfTEktTs1NSC1CKYrAwHh5IEL3M5UKNgUWp6akVaZk4JQpqJgxNkOA/Q8Moy\r
+ kOHFBYm5xZnpEPlTjIpS4rwXQRICIImM0jy4Xlj8v2IUB3pFmPc2SBUPMHXAdb8CGswENPjJ\r
+ lhCQwSWJCCmpBkYW3V2/Vk/79Mzdeql+bf41d4HVHrIrMjKC3xcezTzGyNg9d7LghZ02Gjy/\r
+ ZzfxH7I6/mCFeUPgk/i0SS7bN3m357953OAkulHa/E7Fvg1f9Nq00q04H7gbPPvwq1V7+7ss\r
+ l8JA9dnNQtOnfdscqfbu9qLMUnE33Zh60+2Bx/Zv3d+qHL8k7oQSS3FGoqEWc1FxIgD4fHqu\r
+ 2gIAAA==\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 21 Apr 2014 19:58:32 -0000\r
+\r
+These tests deliver all possible (single-root) four-message threads in\r
+all possible orders and check that notmuch successfully links them\r
+into threads.\r
+\r
+There are two variants of the test: one delivers messages that\r
+reference only their immediate parent and the other delivers messages\r
+that reference all of their parents. The latter test is currently\r
+known-broken.\r
+\r
+This is introduced as a new test (rather than just adding it to\r
+T050-new) because it's much easier for this to start with an empty\r
+database.\r
+---\r
+\r
+This version hopefully addresses David's comments in\r
+id:87y4zhfmrn.fsf@maritornes.cs.unb.ca and adds a second test that\r
+demonstrates the bug Mark in figured out in\r
+id:8738h7kv2q.fsf@qmul.ac.uk.\r
+\r
+test/T051-new-linking.sh | 91 ++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 91 insertions(+)\r
+ create mode 100755 test/T051-new-linking.sh\r
+\r
+diff --git a/test/T051-new-linking.sh b/test/T051-new-linking.sh\r
+new file mode 100755\r
+index 0000000..9ccbc52\r
+--- /dev/null\r
++++ b/test/T051-new-linking.sh\r
+@@ -0,0 +1,91 @@\r
++#!/usr/bin/env bash\r
++test_description='"notmuch new" thread linking'\r
++\r
++. ./test-lib.sh\r
++\r
++# Generate all possible single-root four message thread structures.\r
++# Each line in THREADS is a thread structure, where the n'th field is\r
++# the parent of message n. We'll use this for multiple tests below.\r
++THREADS=$(python -c '\r
++def mkTrees(free, tree={}):\r
++ if free == set():\r
++ print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())])))\r
++ return\r
++ # Attach each free message to each message in the tree (if there is\r
++ # no tree, make the free message the root), backtracking after each\r
++ for msg in sorted(free):\r
++ parents = sorted(tree.keys()) if tree else ["none"]\r
++ for parent in parents:\r
++ ntree = tree.copy()\r
++ ntree[msg] = parent\r
++ mkTrees(free - set([msg]), ntree)\r
++mkTrees(set(range(4)))')\r
++nthreads=$(wc -l <<< "$THREADS")\r
++\r
++test_begin_subtest "All four-message threads get linked in all delivery orders (one parent)"\r
++# In the first variant, this delivers messages that reference only\r
++# their immediate parent. Hence, we should only expect threads to be\r
++# fully joined at the end.\r
++for ((n = 0; n < 4; n++)); do\r
++ # Deliver the n'th message of every thread\r
++ thread=0\r
++ while read -a parents; do\r
++ parent=${parents[$n]}\r
++ generate_message \\r
++ [id]=m$n@t$thread [in-reply-to]="\<m$parent@t$thread\>" \\r
++ [subject]=p$thread [from]=m$n\r
++ thread=$((thread + 1))\r
++ done <<< "$THREADS"\r
++ notmuch new > /dev/null\r
++done\r
++output=$(notmuch search --sort=newest-first '*' | notmuch_search_sanitize)\r
++expected=$(for ((i = 0; i < $nthreads; i++)); do\r
++ echo "thread:XXX 2001-01-05 [4/4] m3, m2, m1, m0; p$i (inbox unread)"\r
++ done)\r
++test_expect_equal "$output" "$expected"\r
++\r
++test_begin_subtest "The same (full parent linkage)"\r
++test_subtest_known_broken\r
++# Here we do the same thing as the previous test, but each message\r
++# references all of its parents. Since every message references the\r
++# root of the thread, each thread should always be fully joined. This\r
++# is currently broken because of the bug detailed in\r
++# id:8738h7kv2q.fsf@qmul.ac.uk.\r
++rm ${MAIL_DIR}/*\r
++notmuch new\r
++output=""\r
++expected=""\r
++for ((n = 0; n < 4; n++)); do\r
++ # Deliver the n'th message of every thread\r
++ thread=0\r
++ while read -a parents; do\r
++ references=""\r
++ parent=${parents[$n]}\r
++ while [[ $parent != none ]]; do\r
++ references="<m$parent@t$thread> $references"\r
++ parent=${parents[$parent]}\r
++ done\r
++\r
++ generate_message \\r
++ [id]=m$n@t$thread [references]="'$references'" \\r
++ [subject]=p$thread [from]=m$n\r
++ thread=$((thread + 1))\r
++ done <<< "$THREADS"\r
++ notmuch new > /dev/null\r
++\r
++ output="$output\r
++$(notmuch search --sort=newest-first '*' | notmuch_search_sanitize)"\r
++\r
++ # Construct expected output\r
++ template="thread:XXX 2001-01-05 [$((n+1))/$((n+1))]"\r
++ for ((m = n; m > 0; m--)); do\r
++ template="$template m$m,"\r
++ done\r
++ expected="$expected\r
++$(for ((i = 0; i < $nthreads; i++)); do\r
++ echo "$template m0; p$i (inbox unread)"\r
++ done)"\r
++done\r
++test_expect_equal "$output" "$expected"\r
++\r
++test_done\r
+-- \r
+1.9.1\r
+\r