Trog-DataSource.t 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #!/usr/bin/env perl
  2. # A datasource's posts are built fresh on every view and have never been near
  3. # the datastore, so the search the reader typed -- which get() applied to the
  4. # datastore and then threw away along with the posts it filtered -- has to be
  5. # applied to them by somebody. This is that somebody.
  6. use strict;
  7. use warnings;
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use Test::More;
  11. use Test::Fatal qw{exception};
  12. require_ok('Trog::DataSource') or BAIL_OUT("Can't find SUT");
  13. my $t = 1700000000;
  14. sub guest {
  15. my ( $title, %o ) = @_;
  16. return {
  17. id => "id-$title",
  18. title => $title,
  19. data => '',
  20. created => $t++,
  21. user => 'bob',
  22. version => 0,
  23. %o,
  24. };
  25. }
  26. my @posts = (
  27. guest( 'webserver', state => 'running', hypervisor_title => 'Rack One' ),
  28. guest( 'database', state => 'shut off', hypervisor_title => 'Rack One' ),
  29. guest( 'mailhost', state => 'running', hypervisor_title => 'Rack Two', user => 'ann' ),
  30. );
  31. sub titles {
  32. my (@got) = @_;
  33. return [ sort map { $_->{title} } @got ];
  34. }
  35. subtest 'the default filter searches title and body' => sub {
  36. is_deeply( titles( Trog::DataSource::filter( {}, @posts ) ), titles(@posts), 'an empty query filters nothing' );
  37. is_deeply( titles( Trog::DataSource::filter( undef, @posts ) ), titles(@posts), 'and neither does no query at all' );
  38. is_deeply( titles( Trog::DataSource::filter( { like => 'web' }, @posts ) ), ['webserver'], 'a substring of the title matches' );
  39. is_deeply( titles( Trog::DataSource::filter( { like => 'SERVER' }, @posts ) ), ['webserver'], 'case insensitively' );
  40. is_deeply( titles( Trog::DataSource::filter( { like => 'erve' }, @posts ) ), ['webserver'], 'inside a word' );
  41. is_deeply( titles( Trog::DataSource::filter( { like => 'nonesuch' }, @posts ) ), [], 'and nothing matches nothing' );
  42. # The body counts too, for a source whose posts have one.
  43. my @bodied = ( guest( 'notes', data => 'the quick brown fox' ) );
  44. is_deeply( titles( Trog::DataSource::filter( { like => 'quick' }, @bodied ) ), ['notes'], 'the body is searched as well' );
  45. # The default does NOT know about a source's own fields -- that is what a
  46. # source implementing filter() is for.
  47. is_deeply( titles( Trog::DataSource::filter( { like => 'Rack One' }, @posts ) ), [], 'but a field it has never heard of is not' );
  48. };
  49. subtest 'the rest of the query still means what it means' => sub {
  50. is_deeply( titles( Trog::DataSource::filter( { author => 'ann' }, @posts ) ), ['mailhost'], 'author filters on the post user' );
  51. is_deeply( titles( Trog::DataSource::filter( { author => 'nobody' }, @posts ) ), [], 'and an author with nothing gets nothing' );
  52. is_deeply( titles( Trog::DataSource::filter( { older => $posts[1]{created} }, @posts ) ), ['webserver'], 'older is older' );
  53. is_deeply( titles( Trog::DataSource::filter( { newer => $posts[1]{created} }, @posts ) ), ['mailhost'], 'and newer is newer' );
  54. # Straight off a query string, so it is coerced rather than trusted.
  55. is_deeply(
  56. titles( Trog::DataSource::filter( { older => "$posts[1]{created}abc" }, @posts ) ),
  57. ['webserver'],
  58. 'a non-numeric older is coerced rather than dying'
  59. );
  60. # Combined, the way a search on a filtered page arrives.
  61. is_deeply( titles( Trog::DataSource::filter( { like => 'host', author => 'ann' }, @posts ) ), ['mailhost'], 'filters compose' );
  62. is_deeply( titles( Trog::DataSource::filter( { like => 'host', author => 'bob' }, @posts ) ), [], 'and disagree when they should' );
  63. };
  64. subtest 'what the default deliberately does not do' => sub {
  65. # Tags and acls are answers about the datastore, and they are settled before
  66. # a datasource is reached -- the reader got to the series page by holding
  67. # its acls, and every synthesized post belongs to that series. Applying
  68. # them again would blank the page for a source whose posts carry no tags,
  69. # which nothing requires them to.
  70. is_deeply( titles( Trog::DataSource::filter( { acls => [] }, @posts ) ), titles(@posts), 'an empty acl list does not blank the page' );
  71. is_deeply( titles( Trog::DataSource::filter( { tags => ['nonesuch'] }, @posts ) ), titles(@posts), 'nor does a tag nothing carries' );
  72. # An id query would otherwise match the series, not its children, and leave
  73. # nothing at all.
  74. is_deeply( titles( Trog::DataSource::filter( { id => 'the-series' }, @posts ) ), titles(@posts), 'and neither does the series id' );
  75. };
  76. subtest 'searchable_match' => sub {
  77. ok( Trog::DataSource::searchable_match( 'ell', 'hello' ), 'a substring matches' );
  78. ok( Trog::DataSource::searchable_match( 'ELL', 'hello' ), 'case insensitively' );
  79. ok( Trog::DataSource::searchable_match( 'x', 'a', undef, 'x' ), 'across several values' );
  80. ok( !Trog::DataSource::searchable_match( 'zz', 'a', 'b' ), 'and misses when it should' );
  81. ok( !Trog::DataSource::searchable_match( '', 'anything' ), 'an empty search matches nothing' );
  82. ok( !Trog::DataSource::searchable_match( undef, 'anything' ), 'and neither does no search' );
  83. # Quotemeta'd, so a search for punctuation is a search for punctuation.
  84. ok( !Trog::DataSource::searchable_match( '.*', 'anything' ), 'a regex is text, not a pattern' );
  85. ok( Trog::DataSource::searchable_match( '.*', 'a .* b' ), 'and matches where that text is' );
  86. is( exception { Trog::DataSource::searchable_match( '(unclosed', 'x' ) }, undef, 'even when it would not compile' );
  87. # A body stored as an array would otherwise be matched against its address.
  88. ok( !Trog::DataSource::searchable_match( 'ARRAY', ['slide one'] ), 'a reference is skipped, not stringified' );
  89. };
  90. subtest 'for_type' => sub {
  91. is( Trog::DataSource::for_type(), undef, 'no type names no source' );
  92. is( Trog::DataSource::for_type(''), undef, 'and neither does an empty one' );
  93. is( Trog::DataSource::for_type('blog.tx'), undef, 'a type with no datasource names none' );
  94. };
  95. subtest 'the defaults are advertised' => sub {
  96. ok( length Trog::DataSource::lang(), 'there is a language to advertise' );
  97. like( Trog::DataSource::help(), qr{^https?://}, 'and somewhere to read about it' );
  98. };
  99. subtest 'Trog::DataSource::Virt searches what its posts carry' => sub {
  100. require_ok('Trog::DataSource::Virt') or return;
  101. # A guest post's body is empty; everything about it is in named fields.
  102. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'web' }, @posts ) ), ['webserver'], 'the guest name is searched' );
  103. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'shut' }, @posts ) ), ['database'], 'and so is its state' );
  104. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'Rack One' }, @posts ) ), [ sort qw{database webserver} ], 'and the hypervisor it is on' );
  105. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'nonesuch' }, @posts ) ), [], 'and nothing matches nothing' );
  106. # The rest of the query is still the default's job.
  107. is_deeply( titles( Trog::DataSource::Virt::filter( { author => 'ann' }, @posts ) ), ['mailhost'], 'author still filters' );
  108. is_deeply( titles( Trog::DataSource::Virt::filter( { like => 'running', author => 'ann' }, @posts ) ), ['mailhost'], 'alongside the search' );
  109. # It says so, rather than letting the page advertise the data model's.
  110. isnt( Trog::DataSource::Virt::lang(), Trog::DataSource::lang(), 'and it describes its own search' );
  111. like( Trog::DataSource::Virt::lang(), qr/guest/i, 'in terms of what is on the page' );
  112. };
  113. #--------------------------------------------------------------------------
  114. # The wiring: a route has to actually reach for all this.
  115. #--------------------------------------------------------------------------
  116. BEGIN {
  117. $INC{'Trog/DataSource/TestPlain.pm'} = 1;
  118. $INC{'Trog/DataSource/TestPicky.pm'} = 1;
  119. $INC{'Trog/DataSource/TestBroken.pm'} = 1;
  120. }
  121. {
  122. # A source with no filter() of its own: the default has to be applied for it.
  123. package Trog::DataSource::TestPlain;
  124. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  125. # One with opinions, including its own search language.
  126. package Trog::DataSource::TestPicky;
  127. our @FILTERED;
  128. sub posts { return ( main::guest('alpha'), main::guest('beta') ) }
  129. sub filter {
  130. my ( $q, @p ) = @_;
  131. push @FILTERED, $q->{like};
  132. return grep { $_->{title} eq 'beta' } @p;
  133. }
  134. sub lang { 'Picky search' }
  135. sub help { 'https://example.com/picky' }
  136. # One whose filter blows up.
  137. package Trog::DataSource::TestBroken;
  138. sub posts { return ( main::guest('alpha') ) }
  139. sub filter { die "no\n" }
  140. }
  141. subtest 'the route applies the search to synthesized posts' => sub {
  142. require Trog::Routes::HTML;
  143. require Test::MockModule;
  144. my $meta = Test::MockModule->new('Trog::DataModule');
  145. my $source;
  146. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  147. my $query = sub { return { primary_post => { child_form => 'fake.tx' }, @_ } };
  148. # No datasource at all: the stored posts are handed straight back.
  149. $source = undef;
  150. my @stored = ( guest('stored') );
  151. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), ['stored'], 'an ordinary type is left alone' );
  152. $source = 'Trog::DataSource::TestPlain';
  153. is_deeply( titles( Trog::Routes::HTML::_datasource_posts( $query->(), \@stored ) ), [ sort qw{alpha beta} ], 'a datasource replaces them' );
  154. # The point of the exercise.
  155. is_deeply(
  156. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  157. ['alpha'],
  158. 'and the search reaches them, rather than being dropped'
  159. );
  160. is_deeply(
  161. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'nonesuch' ), \@stored ) ),
  162. [],
  163. 'including when it matches nothing'
  164. );
  165. # A source with its own filter gets to use it.
  166. $source = 'Trog::DataSource::TestPicky';
  167. @Trog::DataSource::TestPicky::FILTERED = ();
  168. is_deeply(
  169. titles( Trog::Routes::HTML::_datasource_posts( $query->( like => 'alph' ), \@stored ) ),
  170. ['beta'],
  171. "a source's own filter wins over the default"
  172. );
  173. is_deeply( \@Trog::DataSource::TestPicky::FILTERED, ['alph'], 'and it is handed the query' );
  174. # A filter that dies must not take the page with it.
  175. $source = 'Trog::DataSource::TestBroken';
  176. my @survived;
  177. {
  178. my $log = Test::MockModule->new('Trog::Routes::HTML');
  179. $log->redefine( WARN => sub { note(shift) } );
  180. @survived = Trog::Routes::HTML::_datasource_posts( $query->( like => 'x' ), \@stored );
  181. }
  182. is_deeply( titles(@survived), ['alpha'], 'a filter that dies falls back to the unfiltered posts' );
  183. # A sidecar cannot make us load whatever it likes.
  184. $source = 'Evil::Module';
  185. my @refused;
  186. {
  187. my $log = Test::MockModule->new('Trog::DataSource');
  188. $log->redefine( WARN => sub { note(shift) } );
  189. @refused = Trog::Routes::HTML::_datasource_posts( $query->(), \@stored );
  190. }
  191. is_deeply( titles(@refused), ['stored'], 'a datasource outside the namespace is refused' );
  192. };
  193. subtest 'the search box says what it is searching' => sub {
  194. require Trog::Routes::HTML;
  195. require Test::MockModule;
  196. # Stubbed rather than built: constructing a real model here would create a
  197. # datastore in the repo, and none of this is about which model is configured.
  198. my $data = Test::MockModule->new('Trog::Data');
  199. $data->redefine( new => sub { return bless {}, 'FakeModel' } );
  200. {
  201. no warnings qw{once};
  202. *FakeModel::lang = sub { 'Model language' };
  203. *FakeModel::help = sub { 'https://example.com/model' };
  204. }
  205. my $meta = Test::MockModule->new('Trog::DataModule');
  206. my $source;
  207. $meta->redefine( type_meta_for => sub { return $source ? { 'x-tcms-datasource' => $source } : {} } );
  208. $source = undef;
  209. is_deeply(
  210. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'blog.tx' } } ) ],
  211. [ 'Model language', 'https://example.com/model' ],
  212. 'an ordinary page advertises the data model'
  213. );
  214. is_deeply(
  215. [ Trog::Routes::HTML::_search_language( {} ) ],
  216. [ 'Model language', 'https://example.com/model' ],
  217. 'and so does a page with no series at all'
  218. );
  219. # A datasource page is not being served out of the datastore, so the model's
  220. # query language is the wrong thing to advertise on it.
  221. $source = 'Trog::DataSource::TestPlain';
  222. is_deeply(
  223. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  224. [ Trog::DataSource::lang(), Trog::DataSource::help() ],
  225. 'a datasource page advertises the datasource default'
  226. );
  227. $source = 'Trog::DataSource::TestPicky';
  228. is_deeply(
  229. [ Trog::Routes::HTML::_search_language( { primary_post => { child_form => 'fake.tx' } } ) ],
  230. [ 'Picky search', 'https://example.com/picky' ],
  231. 'or its own, when it has one'
  232. );
  233. };
  234. subtest 'ordering is total, so page 2 means something' => sub {
  235. my @same = (
  236. guest( 'charlie', created => 100 ),
  237. guest( 'alpha', created => 100 ),
  238. guest( 'bravo', created => 100 ),
  239. guest( 'delta', created => 200 ),
  240. );
  241. my @ordered = Trog::DataSource::order( {}, @same );
  242. is_deeply( [ map { $_->{title} } @ordered ], [qw{delta alpha bravo charlie}], 'newest first, then by title' );
  243. # The property that actually matters: same input, same order, every time.
  244. is_deeply(
  245. [ map { $_->{title} } Trog::DataSource::order( {}, reverse @same ) ],
  246. [ map { $_->{title} } @ordered ],
  247. 'and it does not depend on what order they arrived in'
  248. );
  249. # A source that stamps everything with the time it built them -- which is
  250. # every libvirt guest -- falls back to being sorted by name.
  251. my @built = map { guest( $_, created => 1700 ) } qw{zulu alpha mike};
  252. is_deeply( [ map { $_->{title} } Trog::DataSource::order( {}, @built ) ], [qw{alpha mike zulu}], 'identical timestamps sort by name' );
  253. # Missing fields must not make it die or shuffle.
  254. my @sparse = ( { id => 'b' }, { id => 'a' }, guest('titled') );
  255. is( exception { Trog::DataSource::order( {}, @sparse ) }, undef, 'posts missing created and title are still orderable' );
  256. };
  257. subtest 'offset pagination' => sub {
  258. require Trog::Routes::HTML;
  259. my @ten = map { guest( sprintf( 'g%02d', $_ ), created => 100 + $_ ) } 1 .. 10;
  260. my ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 4, @ten );
  261. is( $page, 1, 'page one is page one' );
  262. is( $total, 3, 'ten posts at four a page is three pages' );
  263. is( scalar(@slice), 4, 'and a full page' );
  264. is( $slice[0]{title}, 'g01', 'starting at the beginning' );
  265. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 2 }, 4, @ten );
  266. is( $slice[0]{title}, 'g05', 'page two carries on where page one stopped' );
  267. is( scalar(@slice), 4, 'and is also full' );
  268. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 3 }, 4, @ten );
  269. is( scalar(@slice), 2, 'the last page holds the remainder' );
  270. is( $slice[0]{title}, 'g09', 'from where page two stopped' );
  271. # Every post appears exactly once across the pages, which is the whole point.
  272. my @walked;
  273. push( @walked, ( Trog::Routes::HTML::_paginate_offset( { page => $_ }, 4, @ten ) )[ 2 .. 5 ] ) for 1 .. 3;
  274. @walked = grep { defined } @walked;
  275. is_deeply( [ sort map { $_->{title} } @walked ], [ sort map { $_->{title} } @ten ], 'walking the pages visits everything, once' );
  276. # Straight off a query string.
  277. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 99 }, 4, @ten );
  278. is( $page, 3, 'a page past the end shows the last one' );
  279. ok( scalar(@slice), 'rather than nothing at all' );
  280. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 0 }, 4, @ten );
  281. is( $page, 1, 'and page zero is page one' );
  282. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 'nonsense' }, 4, @ten );
  283. is( $page, 1, 'as is a page that is not a number' );
  284. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( {}, 4, @ten );
  285. is( $page, 1, 'and no page at all' );
  286. # A limit that would divide by zero.
  287. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 0, @ten );
  288. is( $total, 1, 'a limit of zero falls back to the default rather than dividing by it' );
  289. is( scalar(@slice), 10, 'and shows everything' );
  290. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 4 );
  291. is( $total, 1, 'no posts is one empty page' );
  292. is( scalar(@slice), 0, 'holding nothing' );
  293. ( $page, $total, @slice ) = Trog::Routes::HTML::_paginate_offset( { page => 1 }, 25, @ten );
  294. is( $total, 1, 'fewer posts than a page is one page' );
  295. is( scalar(@slice), 10, 'holding all of them' );
  296. };
  297. subtest 'the paginator renders page links for a datasource' => sub {
  298. require Trog::Renderer::Base;
  299. my $render = sub {
  300. my (%vars) = @_;
  301. return Trog::Renderer::Base::render(
  302. template => 'paginator.tx',
  303. contenttype => 'text/html',
  304. component => 1,
  305. data => { limit => 25, sizes => [ 25, 50, 100 ], years => [2024], months => [ 0 .. 11 ], older => 0, newer => 0, %vars },
  306. );
  307. };
  308. my $offset = $render->( paginate_offset => 1, pages => 1, page => 2, pages_total => 5, like => 'web' );
  309. like( $offset, qr/Page 2 of 5/, 'it says where you are' );
  310. like( $offset, qr/href="\?page=1&limit=25/, 'Prev goes back a page' );
  311. like( $offset, qr/href="\?page=3&limit=25/, 'Next goes on a page' );
  312. # & because Xslate escapes what an expression emits, which is what an
  313. # ampersand in an attribute is supposed to be. The cursor branch is the same.
  314. like( $offset, qr/like=web/, 'and the search survives the trip' );
  315. unlike( $offset, qr/older=|newer=/, 'no cursor, which could not move on such a page anyway' );
  316. unlike( $offset, qr/Jump to/, 'and no date jump, since these posts share a date' );
  317. my $first = $render->( paginate_offset => 1, pages => 1, page => 1, pages_total => 3 );
  318. unlike( $first, qr/rel="prev"/, 'the first page offers no Prev' );
  319. like( $first, qr/rel="next"/, 'but does offer Next' );
  320. my $last = $render->( paginate_offset => 1, pages => 1, page => 3, pages_total => 3 );
  321. like( $last, qr/rel="prev"/, 'the last page offers Prev' );
  322. unlike( $last, qr/rel="next"/, 'and no Next' );
  323. my $only = $render->( paginate_offset => 1, pages => 0, page => 1, pages_total => 1 );
  324. unlike( $only, qr/rel="prev"|rel="next"/, 'a single page offers neither' );
  325. like( $only, qr/id="paginator" class="disabled"/, 'and says so' );
  326. # Stored posts are untouched by any of this.
  327. my $cursor = $render->( paginate_offset => 0, pages => 1, older => 1700, newer => 1800, like => 'x' );
  328. like( $cursor, qr/href="\?older=1700&limit=25/, 'the cursor paginator still pages by cursor' );
  329. like( $cursor, qr/href="\?newer=1800&limit=25/, 'both ways' );
  330. like( $cursor, qr/Jump to/, 'and keeps its date jump' );
  331. unlike( $cursor, qr/\?page=/, 'with no page numbers' );
  332. };
  333. done_testing();