test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const { AdvancedBase } = require("@heyputer/puter-js-common");
  2. const useapi = require("useapi");
  3. const { BaseService } = require("../exports");
  4. const CoreModule = require("../src/CoreModule");
  5. const { Context } = require("../src/util/context");
  6. class TestLogger {
  7. constructor () {
  8. console.log(
  9. `\x1B[36;1mBoot logger started :)\x1B[0m`,
  10. );
  11. }
  12. info (...args) {
  13. console.log(
  14. '\x1B[36;1m[TESTKERNEL/INFO]\x1B[0m',
  15. ...args,
  16. );
  17. }
  18. error (...args) {
  19. console.log(
  20. '\x1B[31;1m[TESTKERNEL/ERROR]\x1B[0m',
  21. ...args,
  22. );
  23. }
  24. }
  25. class TestKernel extends AdvancedBase {
  26. constructor () {
  27. super();
  28. this.modules = [];
  29. this.useapi = useapi();
  30. this.useapi.withuse(() => {
  31. def('Module', AdvancedBase)
  32. def('Service', BaseService)
  33. });
  34. this.logfn_ = (...a) => a;
  35. }
  36. add_module (module) {
  37. this.modules.push(module);
  38. }
  39. boot () {
  40. const { consoleLogManager } = require('../src/util/consolelog');
  41. consoleLogManager.initialize_proxy_methods();
  42. consoleLogManager.decorate_all(({ manager, replace }, ...a) => {
  43. replace(...this.logfn_(...a));
  44. });
  45. const { Container } = require('../src/services/Container');
  46. this.testLogger = new TestLogger();
  47. const services = new Container({ logger: this.testLogger });
  48. this.services = services;
  49. // app.set('services', services);
  50. const root_context = Context.create({
  51. services,
  52. useapi: this.useapi,
  53. }, 'app');
  54. globalThis.root_context = root_context;
  55. root_context.arun(async () => {
  56. await this._install_modules();
  57. // await this._boot_services();
  58. });
  59. // Error.stackTraceLimit = Infinity;
  60. Error.stackTraceLimit = 200;
  61. }
  62. async _install_modules () {
  63. const { services } = this;
  64. for ( const module of this.modules ) {
  65. await module.install(Context.get());
  66. }
  67. // Real kernel initializes services here, but in this test kernel
  68. // we don't initialize any services.
  69. // Real kernel adds legacy services here but these will break
  70. // the test kernel.
  71. services.ready.resolve();
  72. // provide services to helpers
  73. // const { tmp_provide_services } = require('../src/helpers');
  74. // tmp_provide_services(services);
  75. }
  76. }
  77. const k = new TestKernel();
  78. k.add_module(new CoreModule());
  79. k.boot();
  80. const do_after_tests_ = [];
  81. // const do_after_tests = (fn) => {
  82. // do_after_tests_.push(fn);
  83. // };
  84. const repeat_after = (fn) => {
  85. fn();
  86. do_after_tests_.push(fn);
  87. };
  88. let total_passed = 0;
  89. let total_failed = 0;
  90. for ( const name in k.services.instances_ ) {
  91. console.log('name', name)
  92. const ins = k.services.instances_[name];
  93. ins.construct();
  94. if ( ! ins._test || typeof ins._test !== 'function' ) {
  95. continue;
  96. }
  97. let passed = 0;
  98. let failed = 0;
  99. repeat_after(() => {
  100. console.log(`\x1B[33;1m=== [ Service :: ${name} ] ===\x1B[0m`);
  101. });
  102. const testapi = {
  103. assert: (condition, name) => {
  104. name = name || condition.toString();
  105. if ( condition() ) {
  106. passed++;
  107. repeat_after(() => console.log(`\x1B[32;1m ✔ ${name}\x1B[0m`));
  108. } else {
  109. failed++;
  110. repeat_after(() => console.log(`\x1B[31;1m ✘ ${name}\x1B[0m`));
  111. }
  112. }
  113. };
  114. ins._test(testapi);
  115. total_passed += passed;
  116. total_failed += failed;
  117. }
  118. console.log(`\x1B[36;1m<===\x1B[0m ` +
  119. 'ASSERTION OUTPUTS ARE REPEATED BELOW' +
  120. ` \x1B[36;1m===>\x1B[0m`);
  121. for ( const fn of do_after_tests_ ) {
  122. fn();
  123. }
  124. console.log(`\x1B[36;1m=== [ Summary ] ===\x1B[0m`);
  125. console.log(`Passed: ${total_passed}`);
  126. console.log(`Failed: ${total_failed}`);