classifiers.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { Option, Text, u32 } from '@polkadot/types';
  2. import {
  3. AcceptingApplications,
  4. ActiveOpeningStage,
  5. ApplicationRationingPolicy,
  6. StakingPolicy,
  7. Opening, OpeningStage,
  8. ReviewPeriod,
  9. } from "@joystream/types/lib/hiring"
  10. import {
  11. OpeningState,
  12. IBlockQueryer,
  13. classifyOpeningStage, OpeningStageClassification,
  14. } from './classifiers'
  15. class mockBlockQueryer {
  16. hash: string
  17. timestamp: Date
  18. constructor(
  19. hash: string = "somehash",
  20. timestamp: Date = new Date(),
  21. ) {
  22. this.hash = hash
  23. this.timestamp = timestamp
  24. }
  25. async blockHash(height: number): Promise<string> {
  26. return this.hash
  27. }
  28. async blockTimestamp(height: number): Promise<Date> {
  29. return this.timestamp
  30. }
  31. async expectedBlockTime(): Promise<number> {
  32. return 6
  33. }
  34. }
  35. type Test = {
  36. description: string
  37. input: {
  38. queryer: IBlockQueryer,
  39. opening: Opening,
  40. }
  41. output: OpeningStageClassification
  42. }
  43. describe('hiring.Opening-> OpeningStageClassification', (): void => {
  44. const now = new Date("2020-01-23T11:47:04.433Z")
  45. const cases: Test[] = [
  46. {
  47. description: "WaitingToBegin",
  48. input: {
  49. opening: new Opening({
  50. created: new u32(100),
  51. stage: new OpeningStage({
  52. 'WaitingToBegin': {
  53. begins_at_block: new u32(100),
  54. }
  55. }),
  56. max_review_period_length: new u32(100),
  57. application_rationing_policy: new Option(ApplicationRationingPolicy),
  58. application_staking_policy: new Option(StakingPolicy),
  59. role_staking_policy: new Option(StakingPolicy),
  60. human_readable_text: new Text(),
  61. }),
  62. queryer: new mockBlockQueryer("somehash", now),
  63. },
  64. output: {
  65. state: OpeningState.WaitingToBegin,
  66. starting_block: 100,
  67. starting_block_hash: "somehash",
  68. starting_time: now,
  69. },
  70. },
  71. {
  72. description: "Accepting applications",
  73. input: {
  74. opening: new Opening({
  75. created: new u32(100),
  76. stage: new OpeningStage({
  77. 'Active': {
  78. stage: new ActiveOpeningStage({
  79. acceptingApplications: new AcceptingApplications({
  80. started_accepting_applicants_at_block: new u32(100),
  81. })
  82. })
  83. }
  84. }),
  85. max_review_period_length: new u32(100),
  86. application_rationing_policy: new Option(ApplicationRationingPolicy),
  87. application_staking_policy: new Option(StakingPolicy),
  88. role_staking_policy: new Option(StakingPolicy),
  89. human_readable_text: new Text(),
  90. }),
  91. queryer: new mockBlockQueryer("somehash", now),
  92. },
  93. output: {
  94. state: OpeningState.AcceptingApplications,
  95. starting_block: 100,
  96. starting_block_hash: "somehash",
  97. starting_time: now,
  98. },
  99. },
  100. {
  101. description: "In review period",
  102. input: {
  103. opening: new Opening({
  104. created: new u32(100),
  105. stage: new OpeningStage({
  106. 'Active': {
  107. stage: new ActiveOpeningStage({
  108. reviewPeriod: new ReviewPeriod({
  109. started_accepting_applicants_at_block: new u32(100),
  110. started_review_period_at_block: new u32(100),
  111. })
  112. })
  113. }
  114. }),
  115. max_review_period_length: new u32(14400),
  116. application_rationing_policy: new Option(ApplicationRationingPolicy),
  117. application_staking_policy: new Option(StakingPolicy),
  118. role_staking_policy: new Option(StakingPolicy),
  119. human_readable_text: new Text(),
  120. }),
  121. queryer: new mockBlockQueryer("somehash", now),
  122. },
  123. output: {
  124. state: OpeningState.InReview,
  125. starting_block: 100,
  126. starting_block_hash: "somehash",
  127. starting_time: now,
  128. review_end_block: 14500,
  129. review_end_time: new Date("2020-01-24T11:47:04.433Z"),
  130. },
  131. },
  132. /*
  133. * jest is having trouble with the enum type
  134. {
  135. description: "Deactivated: cancelled",
  136. input: {
  137. opening: new Opening({
  138. created: new u32(100),
  139. stage: new OpeningStage({
  140. 'Active': {
  141. stage: new ActiveOpeningStage({
  142. reviewPeriod: new Deactivated({
  143. cause: new OpeningDeactivationCause(
  144. OpeningDeactivationCauseKeys.CancelledBeforeActivation,
  145. ),
  146. deactivated_at_block: new u32(123),
  147. started_accepting_applicants_at_block: new u32(100),
  148. started_review_period_at_block: new Option(u32, 100),
  149. })
  150. })
  151. }
  152. }),
  153. max_review_period_length: new u32(100),
  154. application_rationing_policy: new Option(ApplicationRationingPolicy),
  155. application_staking_policy: new Option(StakingPolicy),
  156. role_staking_policy: new Option(StakingPolicy),
  157. human_readable_text: new Text(),
  158. }),
  159. queryer: new mockBlockQueryer("somehash", now),
  160. },
  161. output: {
  162. state: OpeningState.Cancelled,
  163. starting_block: 123,
  164. starting_block_hash: "somehash",
  165. starting_time: now,
  166. },
  167. },
  168. */
  169. ]
  170. cases.forEach((test: Test) => {
  171. it(test.description, async () => {
  172. expect(
  173. await classifyOpeningStage(
  174. test.input.queryer,
  175. test.input.opening,
  176. )
  177. ).toEqual(
  178. test.output
  179. )
  180. });
  181. })
  182. })