Playwright.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. use Test2::V0;
  2. use Test2::Tools::Explain;
  3. use JSON::MaybeXS;
  4. use Test::MockModule qw{strict};
  5. use Test::MockFile;
  6. use Test::Fatal qw{exception};
  7. my ($qxret,$qxcode) = ('',255);
  8. use Test::Mock::Cmd qx => sub { $? = $qxcode; return $qxret }, system => sub { print $qxret };
  9. #De-Fang our BEGIN block so we can test safely
  10. no warnings qw{redefine once};
  11. $Playwright::SKIP_BEGIN = 1;
  12. use warnings;
  13. require Playwright;
  14. my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
  15. subtest "_check_and_build_spec" => sub {
  16. local $Playwright::spec = {};
  17. is(Playwright::_check_and_build_spec({}),{},"Already defined spec short-circuits");
  18. my $utilmock = Test::MockModule->new('Playwright::Util');
  19. $utilmock->redefine('request', sub { 'eee' });
  20. undef $Playwright::spec;
  21. like(exception { Playwright::_check_and_build_spec({ ua => 'eeep', port => 666} ) },qr/Could not retrieve/,"Fetch explodes when playwright_server doesn't have spec");
  22. };
  23. subtest "_check_node" => sub {
  24. my $which = Test::MockModule->new('File::Which');
  25. my %to_return = (
  26. node => '/bogus',
  27. npm => '/hokum',
  28. playwright_server => "$path2here/../bin/playwright_server",
  29. );
  30. $which->redefine('which', sub { my $to = shift; $to_return{$to} } );
  31. my $node = Test::MockFile->file('/bogus', undef, { mode => 0777 } );
  32. my $npm = Test::MockFile->file('/hokum', undef, { mode => 0777 } );
  33. like( dies { Playwright::_check_node() }, qr/node must exist/i, "node not existing throws");
  34. undef $node;
  35. $node = Test::MockFile->file('/bogus', '', { mode => 0777 } );
  36. my $bin = Test::MockFile->file("$path2here/../bin/playwright_server");
  37. like( dies { Playwright::_check_node() }, qr/locate playwright_server/i, "Server not existing throws");
  38. undef $bin;
  39. $bin = Test::MockFile->file("$path2here/../bin/playwright_server",'', { mode => 0777 } );
  40. like( dies { Playwright::_check_node() }, qr/could not run/i, "Server exploding throws");
  41. #XXX for some reason I can't redefine this correctly
  42. #my $fakecapture = Test::MockModule->new('Capture::Tiny');
  43. #$fakecapture->redefine('capture_merged', sub { 'OK' });
  44. #ok( lives { Playwright::_check_node() }, "Can run all the way thru") or note $@;
  45. };
  46. subtest "new" => sub {
  47. my $portmock = Test::MockModule->new('Net::EmptyPort');
  48. $portmock->redefine('empty_port', sub { 420 });
  49. my $lwpmock = Test::MockModule->new('LWP::UserAgent');
  50. $lwpmock->redefine('new', sub { bless({},'LWP::UserAgent') });
  51. $lwpmock->redefine('request', sub {});
  52. my $selfmock = Test::MockModule->new('Playwright');
  53. $selfmock->redefine('_start_server', sub { 666 });
  54. $selfmock->redefine('_check_and_build_spec', sub {});
  55. $selfmock->redefine('_build_classes',sub {});
  56. $selfmock->redefine('DESTROY', sub {});
  57. my $expected = bless({
  58. ua => 'whee',
  59. debug => 1,
  60. parent => $$,
  61. pid => 666,
  62. port => 420,
  63. timeout => 5,
  64. cleanup => 1,
  65. }, 'Playwright');
  66. is(Playwright->new( timeout => 5, ua => 'whee', debug => 1), $expected, "Constructor functions as expected");
  67. $expected = bless({
  68. ua => bless({},'LWP::UserAgent'),
  69. debug => undef,
  70. parent => $$,
  71. pid => 666,
  72. port => 420,
  73. timeout => 30,
  74. cleanup => 1,
  75. }, 'Playwright');
  76. is(Playwright->new(), $expected, "Constructor defaults expected");
  77. };
  78. subtest "launch" => sub {
  79. my $basemock = Test::MockModule->new('Playwright::Base');
  80. $basemock->redefine('_coerce', sub {});
  81. my $utilmock = Test::MockModule->new('Playwright::Util');
  82. $utilmock->redefine('request', sub { 'eee' });
  83. my $selfmock = Test::MockModule->new('Playwright');
  84. $selfmock->redefine('DESTROY', sub {});
  85. my $obj = bless({}, 'Playwright');
  86. is($obj->launch( type => 'eee' ), 'eee' ,"launch passthru works");
  87. #XXX Don't feel like mocking the objectification right now
  88. };
  89. subtest "await" => sub {
  90. my $selfmock = Test::MockModule->new('Playwright');
  91. $selfmock->redefine('DESTROY', sub {});
  92. my $res = {};
  93. my $utilmock = Test::MockModule->new('Playwright::Util');
  94. $utilmock->redefine('await', sub { $res } );
  95. my $promise = { file => 'foo.out', pid => 666 };
  96. my $obj = bless({ ua => 'eee', 'port' => 1 }, 'Playwright');
  97. no warnings qw{redefine once};
  98. local *Playwright::Bogus::new = sub { my ($class, %input) = @_; return bless({ spec => 'whee', ua => $input{handle}{ua}, port => $input{handle}{port}, type => $input{type}, guid => $input{id} }, 'Playwright::Bogus') };
  99. use warnings;
  100. is($obj->await($promise), {},"await passthru works");
  101. $res = { _guid => 'abc123', _type => 'Bogus' };
  102. my $expected = bless({ spec => 'whee', ua => 'eee', port => 1, guid => 'abc123', type => 'Bogus' }, 'Bogus');
  103. is($obj->await($promise),$expected,"await objectification works");
  104. };
  105. #XXX Omitting destructor and server startup testing for now
  106. done_testing();