public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 16ceee71f25e0c1bc46f0450729a22e625a96ae7 2655 bytes (raw)
$ git show repobrowse:t/thread-cycle.t	# shows this blob on the CLI

 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
 
# Copyright (C) 2016 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict;
use warnings;
use Test::More;
use_ok('PublicInbox::SearchThread');
use Email::Simple;
my $mt = eval {
	require Mail::Thread;
	no warnings 'once';
	$Mail::Thread::nosubject = 1;
	$Mail::Thread::noprune = 1;
};
my @check;
my @msgs = map {
	my $msg = $_;
	$msg->{references} =~ s/\s+/ /sg if $msg->{references};
	my $simple = Email::Simple->create(header => [
		'Message-Id' => "<$msg->{mid}>",
		'References' => $msg->{references},
	]);
	push @check, $simple;
	bless $msg, 'PublicInbox::SearchMsg'
} (

# data from t/testbox-6 in Mail::Thread 2.55:
	{ mid => '20021124145312.GA1759@nlin.net' },
	{ mid => 'slrnau448m.7l4.markj+0111@cloaked.freeserve.co.uk',
	  references => '<20021124145312.GA1759@nlin.net>',
	},
	{ mid => '15842.10677.577458.656565@jupiter.akutech-local.de',
	  references => '<20021124145312.GA1759@nlin.net>
			<slrnau448m.7l4.markj+0111@cloaked.freeserve.co.uk>',
	},
	{ mid => '20021125171807.GK8236@somanetworks.com',
	  references => '<20021124145312.GA1759@nlin.net>
			<slrnau448m.7l4.markj+0111@cloaked.freeserve.co.uk>
			<15842.10677.577458.656565@jupiter.akutech-local.de>',
	},
	{ mid => '15843.12163.554914.469248@jupiter.akutech-local.de',
	  references => '<20021124145312.GA1759@nlin.net>
			<slrnau448m.7l4.markj+0111@cloaked.freeserve.co.uk>
			<15842.10677.577458.656565@jupiter.akutech-local.de>
			<E18GPHf-0000zp-00@cloaked.freeserve.co.uk>',
	},
	{ mid => 'E18GPHf-0000zp-00@cloaked.freeserve.co.uk',
	  references => '<20021124145312.GA1759@nlin.net>
			<slrnau448m.7l4.markj+0111@cloaked.freeserve.co.uk>
			<15842.10677.577458.656565@jupiter.akutech-local.de>'
	}
);

my $st = thread_to_s(\@msgs);

SKIP: {
	skip 'Mail::Thread missing', 1 unless $mt;
	$mt = Mail::Thread->new(@check);
	$mt->thread;
	$mt->order(sub { sort { $a->messageid cmp $b->messageid } @_ });
	my $check = '';

	my @q = map { (0, $_) } $mt->rootset;
	while (@q) {
		my $level = shift @q;
		my $node = shift @q or next;
		$check .= (" "x$level) . $node->messageid . "\n";
		unshift @q, $level + 1, $node->child, $level, $node->next;
	}
	is($check, $st, 'Mail::Thread output matches');
}

done_testing();

sub thread_to_s {
	my ($msgs) = @_;
	my $rootset = PublicInbox::SearchThread::thread($msgs, sub {
		[ sort { $a->{id} cmp $b->{id} } @{$_[0]} ] });
	my $st = '';
	my @q = map { (0, $_) } @$rootset;
	while (@q) {
		my $level = shift @q;
		my $node = shift @q or next;
		$st .= (" "x$level). "$node->{id}\n";
		my $cl = $level + 1;
		unshift @q, map { ($cl, $_) } @{$node->{children}};
	}
	$st;
}

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git