AuthorityUtils.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const { ethers } = require('hardhat');
  2. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  3. async function fixture() {
  4. const [user, other] = await ethers.getSigners();
  5. const mock = await ethers.deployContract('$AuthorityUtils');
  6. const notAuthorityMock = await ethers.deployContract('NotAuthorityMock');
  7. const authorityNoDelayMock = await ethers.deployContract('AuthorityNoDelayMock');
  8. const authorityDelayMock = await ethers.deployContract('AuthorityDelayMock');
  9. const authorityNoResponse = await ethers.deployContract('AuthorityNoResponse');
  10. return {
  11. user,
  12. other,
  13. mock,
  14. notAuthorityMock,
  15. authorityNoDelayMock,
  16. authorityDelayMock,
  17. authorityNoResponse,
  18. };
  19. }
  20. describe('AuthorityUtils', function () {
  21. beforeEach(async function () {
  22. Object.assign(this, await loadFixture(fixture));
  23. });
  24. describe('canCallWithDelay', function () {
  25. describe('when authority does not have a canCall function', function () {
  26. beforeEach(async function () {
  27. this.authority = this.notAuthorityMock;
  28. });
  29. it('returns (immediate = 0, delay = 0)', async function () {
  30. const { immediate, delay } = await this.mock.$canCallWithDelay(
  31. this.authority,
  32. this.user,
  33. this.other,
  34. '0x12345678',
  35. );
  36. expect(immediate).to.equal(false);
  37. expect(delay).to.be.equal(0n);
  38. });
  39. });
  40. describe('when authority has no delay', function () {
  41. beforeEach(async function () {
  42. this.authority = this.authorityNoDelayMock;
  43. this.immediate = true;
  44. await this.authority._setImmediate(this.immediate);
  45. });
  46. it('returns (immediate, delay = 0)', async function () {
  47. const { immediate, delay } = await this.mock.$canCallWithDelay(
  48. this.authority,
  49. this.user,
  50. this.other,
  51. '0x12345678',
  52. );
  53. expect(immediate).to.equal(this.immediate);
  54. expect(delay).to.be.equal(0n);
  55. });
  56. });
  57. describe('when authority replies with a delay', function () {
  58. beforeEach(async function () {
  59. this.authority = this.authorityDelayMock;
  60. });
  61. for (const immediate of [true, false]) {
  62. for (const delay of [0n, 42n]) {
  63. it(`returns (immediate=${immediate}, delay=${delay})`, async function () {
  64. await this.authority._setImmediate(immediate);
  65. await this.authority._setDelay(delay);
  66. const result = await this.mock.$canCallWithDelay(this.authority, this.user, this.other, '0x12345678');
  67. expect(result.immediate).to.equal(immediate);
  68. expect(result.delay).to.be.equal(delay);
  69. });
  70. }
  71. }
  72. });
  73. describe('when authority replies with empty data', function () {
  74. beforeEach(async function () {
  75. this.authority = this.authorityNoResponse;
  76. });
  77. it('returns (immediate = 0, delay = 0)', async function () {
  78. const { immediate, delay } = await this.mock.$canCallWithDelay(
  79. this.authority,
  80. this.user,
  81. this.other,
  82. '0x12345678',
  83. );
  84. expect(immediate).to.equal(false);
  85. expect(delay).to.be.equal(0n);
  86. });
  87. });
  88. });
  89. });