Playwright.t 7.5 KB

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