Job.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import "@/tests/support/setup";
  2. import { faker } from "@faker-js/faker";
  3. import sinon from "sinon";
  4. import Job, { JobOptions, JobStatus } from "@/Job";
  5. import { TestModule } from "@/tests/support/TestModule";
  6. describe("Job", function () {
  7. class TestJob extends Job {
  8. public constructor(options?: JobOptions) {
  9. super(new TestModule(), undefined, options);
  10. }
  11. protected async _execute() {}
  12. }
  13. describe("getName", function () {
  14. it("should return camelcase name of class", function () {
  15. TestJob.getName().should.be.equal("testJob");
  16. new TestJob().getName().should.be.equal("testJob");
  17. });
  18. });
  19. describe("getPath", function () {
  20. it("should return joined module and job name", function () {
  21. new TestJob().getPath().should.be.equal("test.testJob");
  22. });
  23. });
  24. describe("getPriority", function () {
  25. it("should return configured priority", function () {
  26. const priority = faker.number.int();
  27. new TestJob({ priority }).getPriority().should.be.equal(priority);
  28. });
  29. });
  30. describe("getUuid", function () {
  31. it("should return generated uuid", function () {
  32. const job = new TestJob();
  33. const uuid = faker.string.uuid();
  34. Reflect.set(job, "_uuid", uuid);
  35. job.getUuid().should.be.equal(uuid);
  36. });
  37. });
  38. describe("getStatus", function () {
  39. it("should return current status", function () {
  40. new TestJob().getStatus().should.be.equal(JobStatus.QUEUED);
  41. });
  42. });
  43. describe("getModule", function () {
  44. it("should return configured module", function () {
  45. const module = new TestModule();
  46. const job = new TestJob();
  47. Reflect.set(job, "_module", module);
  48. job.getModule().should.be.equal(module);
  49. });
  50. });
  51. describe("isApiEnabled", function () {
  52. it("should return configured value", function () {
  53. class EnabledJob extends TestJob {
  54. protected static _apiEnabled = true;
  55. }
  56. EnabledJob.isApiEnabled().should.be.true;
  57. new EnabledJob().isApiEnabled().should.be.true;
  58. class DisabledJob extends TestJob {
  59. protected static _apiEnabled = false;
  60. }
  61. DisabledJob.isApiEnabled().should.be.false;
  62. new DisabledJob().isApiEnabled().should.be.false;
  63. });
  64. });
  65. describe("execute", function () {
  66. it("should prevent multiple executions", async function () {
  67. const job = new TestJob();
  68. Reflect.set(job, "_authorize", sinon.stub());
  69. await job.execute();
  70. await job
  71. .execute()
  72. .should.eventually.be.rejectedWith(
  73. "Job has already been executed."
  74. );
  75. });
  76. it("should prevent execution if module can not run jobs", async function () {
  77. const module = new TestModule();
  78. Reflect.set(
  79. module,
  80. "canRunJobs",
  81. sinon.fake(() => false)
  82. );
  83. const job = new TestJob();
  84. Reflect.set(job, "_module", module);
  85. Reflect.set(job, "_authorize", sinon.stub());
  86. await job
  87. .execute()
  88. .should.eventually.be.rejectedWith(
  89. "Module can not currently run jobs."
  90. );
  91. });
  92. it("should update status to active", async function () {
  93. class ActiveJob extends TestJob {
  94. public callback?: (value?: unknown) => void;
  95. protected async _authorize() {}
  96. protected async _execute() {
  97. this.getStatus().should.be.equal(JobStatus.ACTIVE);
  98. }
  99. }
  100. await new ActiveJob().execute();
  101. });
  102. it("should call validation method", async function () {
  103. const job = new TestJob();
  104. const stub = sinon.stub();
  105. Reflect.set(job, "_validate", stub);
  106. Reflect.set(job, "_validated", true);
  107. Reflect.set(job, "_authorize", sinon.stub());
  108. await job.execute();
  109. stub.calledOnce.should.be.true;
  110. });
  111. it("should call authorize method", async function () {
  112. const job = new TestJob();
  113. const stub = sinon.stub();
  114. Reflect.set(job, "_authorize", stub);
  115. await job.execute();
  116. stub.calledOnce.should.be.true;
  117. });
  118. it("should publish callback event if ref configured on success");
  119. it("should add log on success");
  120. it("should update stats on success");
  121. it("should return data from private execute method on success", async function () {
  122. const job = new TestJob();
  123. const data = faker.word.words();
  124. Reflect.set(job, "_authorize", sinon.stub());
  125. Reflect.set(
  126. job,
  127. "_execute",
  128. sinon.fake(async () => data)
  129. );
  130. await job.execute().should.eventually.be.equal(data);
  131. });
  132. it("should publish callback event if ref configured on error");
  133. it("should add log on error");
  134. it("should update stats on error");
  135. it("should rethrow error");
  136. it("should update stats on completion", async function () {
  137. const job = new TestJob();
  138. Reflect.set(job, "_authorize", sinon.stub());
  139. await job.execute();
  140. job.getStatus().should.be.equal(JobStatus.COMPLETED);
  141. });
  142. });
  143. describe("log", function () {
  144. it("should adds log to logbook");
  145. it("should add path as category");
  146. it("should add job json to log data");
  147. });
  148. describe("toJSON", function () {
  149. it("should return job data as json object");
  150. });
  151. });