Playwright.t 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 Async;
  7. my ($qxret,$qxcode) = ('',255);
  8. use Test::Mock::Cmd qx => sub { $? = $qxcode; return $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. is(Playwright::_check_and_build_spec({ ua => 'eeep', port => 666}),'eee',"Fetch works when spec undef");
  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. $which->redefine('which', sub { "$path2here/../bin/playwright_server" });
  56. my $bin = Test::MockFile->file("$path2here/../bin/playwright_server");
  57. like( dies { Playwright::_check_node() }, qr/server in/i, "Server not existing throws");
  58. undef $bin;
  59. $bin = Test::MockFile->file("$path2here/../bin/playwright_server",'');
  60. $which->redefine('which', sub { shift eq 'node' ? '/bogus' : '/hokum' });
  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. like( dies { Playwright::_check_node() }, qr/npm must exist/i, "npm not existing throws");
  67. undef $npm;
  68. $npm = Test::MockFile->file('/hokum', '', { mode => 0777 } );
  69. my $fakecapture = Test::MockModule->new('Capture::Tiny');
  70. $fakecapture->redefine('capture_stderr', sub { 'oh no' });
  71. $qxret = '';
  72. like( dies { Playwright::_check_node() }, qr/could not list/i, "package.json not existing throws");
  73. $qxret = '{
  74. "name": "playwright-server-perl",
  75. "version": "1.0.0",
  76. "problems": [
  77. "missing: express@^4.17, required by playwright-server-perl@1.0.0",
  78. "missing: playwright@^1.5, required by playwright-server-perl@1.0.0",
  79. "missing: yargs@^16.1, required by playwright-server-perl@1.0.0",
  80. "missing: uuid@^8.3, required by playwright-server-perl@1.0.0"
  81. ],
  82. "dependencies": {
  83. "express": {
  84. "required": "^4.17",
  85. "missing": true
  86. },
  87. "playwright": {
  88. "required": "^1.5",
  89. "missing": true
  90. },
  91. "yargs": {
  92. "required": "^16.1",
  93. "missing": true
  94. },
  95. "uuid": {
  96. "required": "^8.3",
  97. "missing": true
  98. }
  99. }
  100. }';
  101. #XXX doesn't look like we can mock $? correctly
  102. #like( dies { Playwright::_check_node($path2here, $decoder) }, qr/installing node/i, "npm failure throws");
  103. $fakecapture->redefine('capture_stderr', sub { 'package-lock' });
  104. $qxcode = 0;
  105. ok( lives { Playwright::_check_node() }, "Can run all the way thru") or note $@;
  106. };
  107. subtest "new" => sub {
  108. my $portmock = Test::MockModule->new('Net::EmptyPort');
  109. $portmock->redefine('empty_port', sub { 420 });
  110. my $lwpmock = Test::MockModule->new('LWP::UserAgent');
  111. $lwpmock->redefine('new', sub { bless({},'LWP::UserAgent') });
  112. $lwpmock->redefine('request', sub {});
  113. my $selfmock = Test::MockModule->new('Playwright');
  114. $selfmock->redefine('_start_server', sub { 666 });
  115. $selfmock->redefine('_check_and_build_spec', sub {});
  116. $selfmock->redefine('_build_classes',sub {});
  117. $selfmock->redefine('DESTROY', sub {});
  118. my $expected = bless({
  119. ua => 'whee',
  120. debug => 1,
  121. parent => $$,
  122. pid => 666,
  123. port => 420,
  124. }, 'Playwright');
  125. is(Playwright->new( ua => 'whee', debug => 1), $expected, "Constructor functions as expected");
  126. $expected = bless({
  127. ua => bless({},'LWP::UserAgent'),
  128. debug => undef,
  129. parent => $$,
  130. pid => 666,
  131. port => 420,
  132. }, 'Playwright');
  133. is(Playwright->new(), $expected, "Constructor defaults expected");
  134. };
  135. subtest "launch" => sub {
  136. my $basemock = Test::MockModule->new('Playwright::Base');
  137. $basemock->redefine('_coerce', sub {});
  138. my $utilmock = Test::MockModule->new('Playwright::Util');
  139. $utilmock->redefine('request', sub { 'eee' });
  140. my $selfmock = Test::MockModule->new('Playwright');
  141. $selfmock->redefine('DESTROY', sub {});
  142. my $obj = bless({}, 'Playwright');
  143. is($obj->launch( type => 'eee' ), 'eee' ,"launch passthru works");
  144. #XXX Don't feel like mocking the objectification right now
  145. };
  146. subtest "await" => sub {
  147. my $selfmock = Test::MockModule->new('Playwright');
  148. $selfmock->redefine('DESTROY', sub {});
  149. my $res = {};
  150. no warnings qw{redefine once};
  151. local *AsyncData::result = sub { $res };
  152. use warnings;
  153. my $promise = bless({},'AsyncData');
  154. my $obj = bless({ ua => 'eee', 'port' => 1 }, 'Playwright');
  155. no warnings qw{redefine once};
  156. 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') };
  157. use warnings;
  158. is($obj->await($promise),{},"await passthru works");
  159. $res = { _guid => 'abc123', _type => 'Bogus' };
  160. my $expected = bless({ spec => 'whee', ua => 'eee', port => 1, guid => 'abc123', type => 'Bogus' }, 'Bogus');
  161. is($obj->await($promise),$expected,"await objectification works");
  162. };
  163. #XXX Omitting destructor and server startup testing for now
  164. done_testing();