Playwright.t 5.0 KB

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