1#!/usr/bin/env bash
2test_description="API tests for notmuch_thread_*"
3
4. $(dirname "$0")/test-lib.sh || exit 1
5
6add_email_corpus
7
8test_begin_subtest "building database"
9test_expect_success "NOTMUCH_NEW"
10
11test_begin_subtest "finding thread"
12THREAD=$(notmuch search --output=threads id:20091117190054.GU3165@dottiness.seas.harvard.edu)
13count=$(notmuch count $THREAD)
14test_expect_equal "$count" "7"
15
16cat <<'EOF' > c_tail
17   if (stat) {
18       const char *stat_str = notmuch_database_status_string (db);
19       if (stat_str)
20           fputs (stat_str, stderr);
21    }
22
23}
24EOF
25
26cat <<EOF > c_head
27#include <stdio.h>
28#include <notmuch.h>
29#include <notmuch-test.h>
30int main (int argc, char** argv)
31{
32   notmuch_database_t *db;
33   notmuch_status_t stat;
34   char *msg = NULL;
35   notmuch_thread_t *thread = NULL;
36   notmuch_threads_t *threads = NULL;
37   notmuch_query_t *query = NULL;
38   const char *id = "${THREAD}";
39
40   stat = notmuch_database_open_verbose (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db, &msg);
41   if (stat != NOTMUCH_STATUS_SUCCESS) {
42     fprintf (stderr, "error opening database: %d %s\n", stat, msg ? msg : "");
43     exit (1);
44   }
45
46   query = notmuch_query_create (db, id);
47   EXPECT0(notmuch_query_search_threads (query, &threads));
48   thread = notmuch_threads_get (threads);
49   EXPECT0(notmuch_database_close (db));
50EOF
51
52test_begin_subtest "get thread-id from closed database"
53cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
54    {
55        const char *id2;
56        id2 = notmuch_thread_get_thread_id (thread);
57        printf("%d\n%s\n", thread != NULL, id2);
58    }
59EOF
60thread_num=${THREAD#thread:}
61cat <<EOF > EXPECTED
62== stdout ==
631
64${thread_num}
65== stderr ==
66EOF
67test_expect_equal_file EXPECTED OUTPUT
68
69test_begin_subtest "get total messages with closed database"
70cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
71    {
72        int count;
73        count = notmuch_thread_get_total_messages (thread);
74        printf("%d\n%d\n", thread != NULL, count);
75    }
76EOF
77cat <<EOF > EXPECTED
78== stdout ==
791
807
81== stderr ==
82EOF
83test_expect_equal_file EXPECTED OUTPUT
84
85test_begin_subtest "get total files with closed database"
86cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
87    {
88        int count;
89        count = notmuch_thread_get_total_files (thread);
90        printf("%d\n%d\n", thread != NULL, count);
91    }
92EOF
93cat <<EOF > EXPECTED
94== stdout ==
951
967
97== stderr ==
98EOF
99test_expect_equal_file EXPECTED OUTPUT
100
101test_begin_subtest "get top level messages with closed database"
102cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
103    {
104        notmuch_messages_t *messages;
105        messages = notmuch_thread_get_toplevel_messages (thread);
106        printf("%d\n%d\n", thread != NULL, messages != NULL);
107    }
108EOF
109cat <<EOF > EXPECTED
110== stdout ==
1111
1121
113== stderr ==
114EOF
115test_expect_equal_file EXPECTED OUTPUT
116
117test_begin_subtest "iterate over level messages with closed database"
118cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
119    {
120      notmuch_messages_t *messages;
121      for (messages = notmuch_thread_get_toplevel_messages (thread);
122           notmuch_messages_valid (messages);
123           notmuch_messages_move_to_next (messages)) {
124        notmuch_message_t *message = notmuch_messages_get (messages);
125        const char *mid = notmuch_message_get_message_id (message);
126        printf("%s\n", mid);
127      }
128    }
129EOF
130cat <<EOF > EXPECTED
131== stdout ==
13220091117190054.GU3165@dottiness.seas.harvard.edu
133== stderr ==
134EOF
135test_expect_equal_file EXPECTED OUTPUT
136
137test_begin_subtest "iterate over level messages with closed database"
138cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
139    {
140      notmuch_messages_t *messages;
141      for (messages = notmuch_thread_get_toplevel_messages (thread);
142           notmuch_messages_valid (messages);
143           notmuch_messages_move_to_next (messages)) {
144        notmuch_message_t *message = notmuch_messages_get (messages);
145        const char *mid = notmuch_message_get_message_id (message);
146        printf("%s\n", mid);
147      }
148    }
149EOF
150cat <<EOF > EXPECTED
151== stdout ==
15220091117190054.GU3165@dottiness.seas.harvard.edu
153== stderr ==
154EOF
155test_expect_equal_file EXPECTED OUTPUT
156
157test_begin_subtest "iterate over replies with closed database"
158cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
159    {
160      notmuch_messages_t *messages = notmuch_thread_get_toplevel_messages (thread);
161      notmuch_message_t *message = notmuch_messages_get (messages);
162      notmuch_messages_t *replies;
163      for (replies = notmuch_message_get_replies (message);
164           notmuch_messages_valid (replies);
165           notmuch_messages_move_to_next (replies)) {
166        notmuch_message_t *message = notmuch_messages_get (replies);
167        const char *mid = notmuch_message_get_message_id (message);
168
169        printf("%s\n", mid);
170      }
171    }
172EOF
173cat <<EOF > EXPECTED
174== stdout ==
17587iqd9rn3l.fsf@vertex.dottedmag
17687ocn0qh6d.fsf@yoom.home.cworth.org
177== stderr ==
178EOF
179test_expect_equal_file EXPECTED OUTPUT
180
181test_begin_subtest "iterate over all messages with closed database"
182cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
183    {
184      notmuch_messages_t *messages;
185      for (messages = notmuch_thread_get_messages (thread);
186           notmuch_messages_valid (messages);
187           notmuch_messages_move_to_next (messages)) {
188        notmuch_message_t *message = notmuch_messages_get (messages);
189        const char *mid = notmuch_message_get_message_id (message);
190        printf("%s\n", mid);
191      }
192    }
193EOF
194cat <<EOF > EXPECTED
195== stdout ==
19620091117190054.GU3165@dottiness.seas.harvard.edu
19787iqd9rn3l.fsf@vertex.dottedmag
19820091117203301.GV3165@dottiness.seas.harvard.edu
19987fx8can9z.fsf@vertex.dottedmag
200yunaayketfm.fsf@aiko.keithp.com
20120091118005040.GA25380@dottiness.seas.harvard.edu
20287ocn0qh6d.fsf@yoom.home.cworth.org
203== stderr ==
204EOF
205test_expect_equal_file EXPECTED OUTPUT
206
207test_begin_subtest "get authors from closed database"
208cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
209    {
210        const char *authors;
211        authors = notmuch_thread_get_authors (thread);
212        printf("%d\n%s\n", thread != NULL, authors);
213    }
214EOF
215cat <<EOF > EXPECTED
216== stdout ==
2171
218Lars Kellogg-Stedman, Mikhail Gusarov, Keith Packard, Carl Worth
219== stderr ==
220EOF
221test_expect_equal_file EXPECTED OUTPUT
222
223test_begin_subtest "get subject from closed database"
224cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
225    {
226        const char *subject;
227        subject = notmuch_thread_get_subject (thread);
228        printf("%d\n%s\n", thread != NULL, subject);
229    }
230EOF
231cat <<EOF > EXPECTED
232== stdout ==
2331
234[notmuch] Working with Maildir storage?
235== stderr ==
236EOF
237test_expect_equal_file EXPECTED OUTPUT
238
239test_begin_subtest "oldest date from closed database"
240cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
241    {
242        time_t stamp;
243        stamp = notmuch_thread_get_oldest_date (thread);
244        printf("%d\n%d\n", thread != NULL, stamp > 0);
245    }
246EOF
247cat <<EOF > EXPECTED
248== stdout ==
2491
2501
251== stderr ==
252EOF
253test_expect_equal_file EXPECTED OUTPUT
254
255test_begin_subtest "newest date from closed database"
256cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
257    {
258        time_t stamp;
259        stamp = notmuch_thread_get_newest_date (thread);
260        printf("%d\n%d\n", thread != NULL, stamp > 0);
261    }
262EOF
263cat <<EOF > EXPECTED
264== stdout ==
2651
2661
267== stderr ==
268EOF
269test_expect_equal_file EXPECTED OUTPUT
270
271test_begin_subtest "iterate tags from closed database"
272cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
273    {
274      notmuch_tags_t *tags;
275      const char *tag;
276      for (tags = notmuch_thread_get_tags (thread);
277           notmuch_tags_valid (tags);
278           notmuch_tags_move_to_next (tags))
279        {
280          tag = notmuch_tags_get (tags);
281          printf ("%s\n", tag);
282        }
283    }
284EOF
285cat <<EOF > EXPECTED
286== stdout ==
287inbox
288signed
289unread
290== stderr ==
291EOF
292test_expect_equal_file EXPECTED OUTPUT
293
294test_begin_subtest "collect tags with closed database"
295cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
296    {
297      notmuch_messages_t *messages = notmuch_thread_get_messages (thread);
298
299      notmuch_tags_t *tags = notmuch_messages_collect_tags (messages);
300
301      const char *tag;
302      for (tags = notmuch_thread_get_tags (thread);
303           notmuch_tags_valid (tags);
304           notmuch_tags_move_to_next (tags))
305        {
306          tag = notmuch_tags_get (tags);
307          printf ("%s\n", tag);
308        }
309      notmuch_tags_destroy (tags);
310      notmuch_messages_destroy (messages);
311
312      printf("SUCCESS\n");
313    }
314EOF
315cat <<EOF > EXPECTED
316== stdout ==
317inbox
318signed
319unread
320SUCCESS
321== stderr ==
322EOF
323test_expect_equal_file EXPECTED OUTPUT
324
325test_begin_subtest "destroy thread with closed database"
326cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
327    {
328        time_t stamp;
329        notmuch_thread_destroy (thread);
330        printf("SUCCESS\n");
331    }
332EOF
333cat <<EOF > EXPECTED
334== stdout ==
335SUCCESS
336== stderr ==
337EOF
338test_expect_equal_file EXPECTED OUTPUT
339
340test_done
341