2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::TestCommon;
10 require_mods(qw(DBD::SQLite));
11 use_ok 'PublicInbox::LeiDedupe';
12 my $eml = eml_load('t/plack-qp.eml');
13 my $sameish = eml_load('t/plack-qp.eml');
14 $sameish->header_set('Message-ID', '<cuepee@example.com>');
15 my $mid = $eml->header_raw('Message-ID');
16 my $different = eml_load('t/msg_iter-order.eml');
17 $different->header_set('Message-ID', $mid);
18 my $smsg = bless { ds => time }, 'PublicInbox::Smsg';
19 $smsg->populate($eml);
20 $smsg->{$_} //= '' for (qw(to cc references)) ;
22 my $check_storable = sub {
25 require_mods('Storable', 1);
26 my $dup = Storable::thaw(Storable::freeze($x));
27 is_deeply($dup, $x, "$x->[3] round-trips through storable");
31 my $lei = { opt => { dedupe => 'none' } };
32 my $dd = PublicInbox::LeiDedupe->new($lei);
33 $check_storable->($dd);
35 ok(!$dd->is_dup($eml), '1st is_dup w/o dedupe');
36 ok(!$dd->is_dup($eml), '2nd is_dup w/o dedupe');
37 ok(!$dd->is_dup($different), 'different is_dup w/o dedupe');
38 ok(!$dd->is_smsg_dup($smsg), 'smsg dedupe none 1');
39 ok(!$dd->is_smsg_dup($smsg), 'smsg dedupe none 2');
41 for my $strat (undef, 'content') {
42 $lei->{opt}->{dedupe} = $strat;
43 $dd = PublicInbox::LeiDedupe->new($lei);
44 $check_storable->($dd);
46 my $desc = $strat // 'default';
47 ok(!$dd->is_dup($eml), "1st is_dup with $desc dedupe");
48 ok($dd->is_dup($eml), "2nd seen with $desc dedupe");
49 ok(!$dd->is_dup($different), "different is_dup with $desc dedupe");
50 ok(!$dd->is_smsg_dup($smsg), "is_smsg_dup pass w/ $desc dedupe");
51 ok($dd->is_smsg_dup($smsg), "is_smsg_dup reject w/ $desc dedupe");
52 ok(!$dd->is_dup($sameish),
53 "Message-ID accounted for w/ same content otherwise");
55 $lei->{opt}->{dedupe} = 'bogus';
56 eval { PublicInbox::LeiDedupe->new($lei) };
57 like($@, qr/unsupported.*bogus/, 'died on bogus strategy');
59 $lei->{opt}->{dedupe} = 'mid';
60 $dd = PublicInbox::LeiDedupe->new($lei);
61 $check_storable->($dd);
63 ok(!$dd->is_dup($eml), '1st is_dup with mid dedupe');
64 ok($dd->is_dup($eml), '2nd seen with mid dedupe');
65 ok($dd->is_dup($different), 'different seen with mid dedupe');
66 ok(!$dd->is_smsg_dup($smsg), 'smsg mid dedupe pass');
67 ok($dd->is_smsg_dup($smsg), 'smsg mid dedupe reject');
69 $lei->{opt}->{dedupe} = 'oid';
70 $dd = PublicInbox::LeiDedupe->new($lei);
71 $check_storable->($dd);
74 # --augment won't have OIDs:
75 ok(!$dd->is_dup($eml), '1st is_dup with oid dedupe (augment)');
76 ok($dd->is_dup($eml), '2nd seen with oid dedupe (augment)');
77 ok(!$dd->is_dup($different), 'different is_dup with mid dedupe (augment)');
78 $different->header_set('Status', 'RO');
79 ok($dd->is_dup($different), 'different seen with oid dedupe Status removed');
81 $smsg = { blob => '01d' };
82 ok(!$dd->is_dup($eml, $smsg), '1st is_dup with oid dedupe');
83 ok($dd->is_dup($different, $smsg), 'different content ignored if oid matches');
84 $smsg->{blob} = uc($smsg->{blob});
85 ok($dd->is_dup($eml, $smsg), 'case insensitive oid comparison :P');
86 $smsg->{blob} = '01dbad';
87 ok(!$dd->is_dup($eml, $smsg), 'case insensitive oid comparison :P');
89 $smsg->{blob} = 'dead';
90 ok(!$dd->is_smsg_dup($smsg), 'smsg dedupe pass');
91 ok($dd->is_smsg_dup($smsg), 'smsg dedupe reject');