]> Sergey Matveev's repositories - public-inbox.git/blob - t/view.t
line-wrap generated HTML source around attrs for readability
[public-inbox.git] / t / view.t
1 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use PublicInbox::View;
8
9 # plain text
10 {
11         my $body = <<EOF;
12 So and so wrote:
13 > keep this inline
14
15 OK
16
17 > Long and wordy reply goes here and it is split across multiple lines.
18 > We generate links to a separate full page where quoted-text is inline.
19 > This is
20 >
21 > Currently 12 lines
22 > See MAX_INLINE_QUOTED
23 > See MAX_INLINE_QUOTED
24 > See MAX_INLINE_QUOTED
25 > See MAX_INLINE_QUOTED
26 > See MAX_INLINE_QUOTED
27 > See MAX_INLINE_QUOTED
28 > See MAX_INLINE_QUOTED
29 > See MAX_INLINE_QUOTED
30
31 hello world
32 EOF
33         my $s = Email::Simple->create(
34                 header => [
35                         From => 'a@example.com',
36                         To => 'b@example.com',
37                         'Content-Type' => 'text/plain',
38                         'Message-ID' => '<hello@example.com>',
39                         Subject => 'this is a subject',
40                 ],
41                 body => $body,
42         );
43         $s = Email::MIME->new($s->as_string);
44         my $html = PublicInbox::View->msg_html($s);
45
46         # ghetto tests
47         like($html, qr!<a\nhref="\.\./m/hello%40!s, "MID link present");
48         like($html, qr/hello world\b/, "body present");
49         like($html, qr/&gt; keep this inline/, "short quoted text is inline");
50         like($html, qr/<a\nname=[^>]+>&gt; Long and wordy/,
51                 "long quoted text is anchored");
52
53         # short page
54         my $pfx = "http://example.com/test/f";
55         my $short = PublicInbox::View->msg_html($s, $pfx);
56         like($short, qr!<a\nhref="hello%40!s, "MID link present");
57         like($short, qr/\n&gt; keep this inline/,
58                 "short quoted text is inline");
59         like($short, qr/<a\nhref="\Q$pfx\E#[^>]+>Long and wordy/,
60                 "long quoted text is made into a link");
61         ok(length($short) < length($html), "short page is shorter");
62 }
63
64 # multipart crap
65 {
66         my $parts = [
67                 Email::MIME->create(
68                         attributes => { content_type => 'text/plain', },
69                         body => 'hi',
70                 ),
71                 Email::MIME->create(
72                         attributes => { content_type => 'text/plain', },
73                         body => 'bye',
74                 )
75         ];
76         my $mime = Email::MIME->create(
77                 header_str => [
78                         From => 'a@example.com',
79                         Subject => 'blargh',
80                         'Message-ID' => '<blah@example.com>',
81                         'In-Reply-To' => '<irp@example.com>',
82                         ],
83                 parts => $parts,
84         );
85
86         my $html = PublicInbox::View->msg_html($mime);
87         like($html, qr/hi\n-+ part #2 -+\nbye\n/, "multipart split");
88 }
89
90 # multipart email with attached patch
91 {
92         my $parts = [
93                 Email::MIME->create(
94                         attributes => { content_type => 'text/plain', },
95                         body => 'hi, see attached patch',
96                 ),
97                 Email::MIME->create(
98                         attributes => {
99                                 content_type => 'text/plain',
100                                 filename => "foo.patch",
101                         },
102                         body => "--- a/file\n+++ b/file\n" .
103                                 "@@ -49, 7 +49,34 @@\n",
104                 ),
105         ];
106         my $mime = Email::MIME->create(
107                 header_str => [
108                         From => 'a@example.com',
109                         Subject => '[PATCH] asdf',
110                         'Message-ID' => '<patch@example.com>',
111                         ],
112                 parts => $parts,
113         );
114
115         my $html = PublicInbox::View->msg_html($mime);
116         like($html, qr!see attached patch\n-+ foo\.patch -+\n--- a/file\n!,
117                 "parts split with filename");
118 }
119
120 # multipart collapsed to single quoted-printable text/plain
121 {
122         my $parts = [
123                 Email::MIME->create(
124                         attributes => {
125                                 content_type => 'text/plain',
126                                 encoding => 'quoted-printable',
127                         },
128                         body => 'hi = bye',
129                 )
130         ];
131         my $mime = Email::MIME->create(
132                 header_str => [
133                         From => 'qp@example.com',
134                         Subject => 'QP',
135                         'Message-ID' => '<qp@example.com>',
136                         ],
137                 parts => $parts,
138         );
139
140         my $html = PublicInbox::View->msg_html($mime);
141         like($mime->body_raw, qr/hi =3D bye=/, "our test used QP correctly");
142         like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
143 }
144
145 done_testing();