Playwright.t 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "_build_classes" => sub {
  24. local $Playwright::spec = {
  25. Fake => {
  26. members => {
  27. tickle => {
  28. args => {
  29. chase => { type => { name => 'boolean' }, order => 1 },
  30. tickleOptions => {
  31. order => 0,
  32. type => {
  33. name => 'Object',
  34. properties => {
  35. intense => { name => 'intense', type => { name => 'boolean' } },
  36. tickler => { name => 'tickler', type => { name => 'string' } },
  37. optional => { name => 'optional', type => { name => 'boolean' } }, # Optional, shouldn't show up in output
  38. },
  39. },
  40. },
  41. who => { type => { name => 'string' }, order => 2 },
  42. hug => { type => { name => 'boolean' }, order => 3 }, # Optional bool arg, make sure we dont choke
  43. },
  44. },
  45. }
  46. },
  47. };
  48. #Very light testing here, example.pl is really what tests this
  49. Playwright::_build_classes();
  50. ok(defined &Playwright::Fake::new, "Constructors set up correctly");
  51. ok(defined &Playwright::Fake::tickle, "Class methods set up correctly");
  52. };
  53. subtest "_check_node" => sub {
  54. my $which = Test::MockModule->new('File::Which');
  55. my %to_return = (
  56. node => '/bogus',
  57. npm => '/hokum',
  58. playwright_server => "$path2here/../bin/playwright_server",
  59. );
  60. $which->redefine('which', sub { my $to = shift; $to_return{$to} } );
  61. my $node = Test::MockFile->file('/bogus', undef, { mode => 0777 } );
  62. my $npm = Test::MockFile->file('/hokum', undef, { mode => 0777 } );
  63. like( dies { Playwright::_check_node() }, qr/node must exist/i, "node not existing throws");
  64. undef $node;
  65. $node = Test::MockFile->file('/bogus', '', { mode => 0777 } );
  66. my $bin = Test::MockFile->file("$path2here/../bin/playwright_server");
  67. like( dies { Playwright::_check_node() }, qr/server in/i, "Server not existing throws");
  68. undef $bin;
  69. $bin = Test::MockFile->file("$path2here/../bin/playwright_server",'');
  70. like( dies { Playwright::_check_node() }, qr/npm must exist/i, "npm not existing throws");
  71. undef $npm;
  72. $npm = Test::MockFile->file('/hokum', '', { mode => 0777 } );
  73. my $fakecapture = Test::MockModule->new('Capture::Tiny');
  74. $fakecapture->redefine('capture_stderr', sub { 'oh no' });
  75. my $pmock = Test::MockModule->new('File::pushd');
  76. $pmock->redefine('pushd', sub {shift});
  77. #XXX doesn't look like we can mock $? correctly
  78. #like( dies { Playwright::_check_node($path2here, $decoder) }, qr/installing node/i, "npm failure throws");
  79. $fakecapture->redefine('capture_stderr', sub { 'package-lock' });
  80. $qxcode = 0;
  81. ok( lives { Playwright::_check_node() }, "Can run all the way thru") or note $@;
  82. };
  83. subtest "new" => sub {
  84. my $portmock = Test::MockModule->new('Net::EmptyPort');
  85. $portmock->redefine('empty_port', sub { 420 });
  86. my $lwpmock = Test::MockModule->new('LWP::UserAgent');
  87. $lwpmock->redefine('new', sub { bless({},'LWP::UserAgent') });
  88. $lwpmock->redefine('request', sub {});
  89. my $selfmock = Test::MockModule->new('Playwright');
  90. $selfmock->redefine('_start_server', sub { 666 });
  91. $selfmock->redefine('_check_and_build_spec', sub {});
  92. $selfmock->redefine('_build_classes',sub {});
  93. $selfmock->redefine('DESTROY', sub {});
  94. my $expected = bless({
  95. ua => 'whee',
  96. debug => 1,
  97. parent => $$,
  98. pid => 666,
  99. port => 420,
  100. timeout => 5,
  101. }, 'Playwright');
  102. is(Playwright->new( timeout => 5, ua => 'whee', debug => 1), $expected, "Constructor functions as expected");
  103. $expected = bless({
  104. ua => bless({},'LWP::UserAgent'),
  105. debug => undef,
  106. parent => $$,
  107. pid => 666,
  108. port => 420,
  109. timeout => 30,
  110. }, 'Playwright');
  111. is(Playwright->new(), $expected, "Constructor defaults expected");
  112. };
  113. subtest "launch" => sub {
  114. my $basemock = Test::MockModule->new('Playwright::Base');
  115. $basemock->redefine('_coerce', sub {});
  116. my $utilmock = Test::MockModule->new('Playwright::Util');
  117. $utilmock->redefine('request', sub { 'eee' });
  118. my $selfmock = Test::MockModule->new('Playwright');
  119. $selfmock->redefine('DESTROY', sub {});
  120. my $obj = bless({}, 'Playwright');
  121. is($obj->launch( type => 'eee' ), 'eee' ,"launch passthru works");
  122. #XXX Don't feel like mocking the objectification right now
  123. };
  124. subtest "await" => sub {
  125. my $selfmock = Test::MockModule->new('Playwright');
  126. $selfmock->redefine('DESTROY', sub {});
  127. my $res = {};
  128. my $utilmock = Test::MockModule->new('Playwright::Util');
  129. $utilmock->redefine('await', sub { $res } );
  130. my $promise = { file => 'foo.out', pid => 666 };
  131. my $obj = bless({ ua => 'eee', 'port' => 1 }, 'Playwright');
  132. no warnings qw{redefine once};
  133. 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') };
  134. use warnings;
  135. is($obj->await($promise), {},"await passthru works");
  136. $res = { _guid => 'abc123', _type => 'Bogus' };
  137. my $expected = bless({ spec => 'whee', ua => 'eee', port => 1, guid => 'abc123', type => 'Bogus' }, 'Bogus');
  138. is($obj->await($promise),$expected,"await objectification works");
  139. };
  140. #XXX Omitting destructor and server startup testing for now
  141. done_testing();