← Contracts

Tests · MIT license

EventRegistry.t.sol

Registry tests: listing rules, cancelling, moderation, ownership, paging.

Download .sol
1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.28;
3
4import {Test} from "forge-std/Test.sol";
5import {EventRegistry} from "../src/EventRegistry.sol";
6
7contract EventRegistryTest is Test {
8 EventRegistry reg;
9
10 address owner = makeAddr("owner");
11 address alice = makeAddr("alice");
12 address bob = makeAddr("bob");
13
14 function setUp() public {
15 vm.warp(1_790_000_000);
16 reg = new EventRegistry(owner);
17 }
18
19 function _params() internal view returns (EventRegistry.ListingParams memory) {
20 return EventRegistry.ListingParams({
21 title: "Super Bowl LXI",
22 description: "The NFL championship, first ever on Valentine's Day.",
23 category: "sports",
24 location: "SoFi Stadium, Inglewood",
25 url: "https://www.nfl.com/super-bowl",
26 imageURI: "",
27 startsAt: uint64(block.timestamp + 30 days),
28 endsAt: uint64(block.timestamp + 30 days + 4 hours),
29 latE6: 33_953_500,
30 lngE6: -118_339_200
31 });
32 }
33
34 function test_list_storesListing() public {
35 vm.prank(alice);
36 uint256 id = reg.list(_params());
37 assertEq(id, 0);
38 assertEq(reg.listingCount(), 1);
39
40 EventRegistry.Listing memory l = reg.getListing(0);
41 assertEq(l.lister, alice);
42 assertEq(l.title, "Super Bowl LXI");
43 assertEq(l.location, "SoFi Stadium, Inglewood");
44 assertEq(l.listedAt, block.timestamp);
45 assertEq(l.startsAt, block.timestamp + 30 days);
46 assertEq(l.latE6, 33_953_500);
47 assertFalse(l.cancelled);
48 assertFalse(l.hidden);
49 }
50
51 function test_list_emits() public {
52 EventRegistry.ListingParams memory p = _params();
53 vm.expectEmit(true, true, false, true);
54 emit EventRegistry.Listed(0, alice, p.startsAt, p.title);
55 vm.prank(alice);
56 reg.list(p);
57 }
58
59 function test_list_validatesText() public {
60 EventRegistry.ListingParams memory p = _params();
61 p.title = "";
62 vm.expectRevert(EventRegistry.BadTitle.selector);
63 reg.list(p);
64
65 p = _params();
66 p.title = string(new bytes(81));
67 vm.expectRevert(EventRegistry.BadTitle.selector);
68 reg.list(p);
69
70 p = _params();
71 p.category = "";
72 vm.expectRevert(EventRegistry.BadCategory.selector);
73 reg.list(p);
74
75 p = _params();
76 p.description = string(new bytes(281));
77 vm.expectRevert(EventRegistry.TooLong.selector);
78 reg.list(p);
79
80 p = _params();
81 p.url = string(new bytes(257));
82 vm.expectRevert(EventRegistry.TooLong.selector);
83 reg.list(p);
84 }
85
86 function test_list_validatesTime() public {
87 EventRegistry.ListingParams memory p = _params();
88 p.startsAt = uint64(block.timestamp);
89 vm.expectRevert(EventRegistry.BadTime.selector);
90 reg.list(p);
91
92 p = _params();
93 p.startsAt = uint64(block.timestamp + 731 days);
94 p.endsAt = 0;
95 vm.expectRevert(EventRegistry.BadTime.selector);
96 reg.list(p);
97
98 p = _params();
99 p.endsAt = p.startsAt - 1;
100 vm.expectRevert(EventRegistry.BadTime.selector);
101 reg.list(p);
102
103 // An unknown end time is fine.
104 p = _params();
105 p.endsAt = 0;
106 reg.list(p);
107 }
108
109 function test_list_validatesCoordinates() public {
110 EventRegistry.ListingParams memory p = _params();
111 p.latE6 = 90_000_001;
112 vm.expectRevert(EventRegistry.BadCoordinates.selector);
113 reg.list(p);
114
115 p = _params();
116 p.lngE6 = -180_000_001;
117 vm.expectRevert(EventRegistry.BadCoordinates.selector);
118 reg.list(p);
119 }
120
121 function test_cancel_onlyLister() public {
122 vm.prank(alice);
123 reg.list(_params());
124
125 vm.prank(bob);
126 vm.expectRevert(EventRegistry.NotLister.selector);
127 reg.cancel(0);
128
129 vm.prank(alice);
130 reg.cancel(0);
131 assertTrue(reg.getListing(0).cancelled);
132 }
133
134 function test_setHidden_onlyOwner() public {
135 vm.prank(alice);
136 reg.list(_params());
137
138 vm.prank(alice);
139 vm.expectRevert(EventRegistry.NotOwner.selector);
140 reg.setHidden(0, true);
141
142 vm.prank(owner);
143 reg.setHidden(0, true);
144 assertTrue(reg.getListing(0).hidden);
145
146 vm.prank(owner);
147 reg.setHidden(0, false);
148 assertFalse(reg.getListing(0).hidden);
149 }
150
151 function test_unknownListing() public {
152 vm.expectRevert(EventRegistry.UnknownListing.selector);
153 reg.getListing(0);
154 vm.expectRevert(EventRegistry.UnknownListing.selector);
155 reg.cancel(3);
156 }
157
158 function test_transferOwnership() public {
159 vm.prank(owner);
160 reg.transferOwnership(bob);
161 assertEq(reg.owner(), bob);
162
163 vm.prank(owner);
164 vm.expectRevert(EventRegistry.NotOwner.selector);
165 reg.setHidden(0, true);
166
167 vm.prank(bob);
168 vm.expectRevert(EventRegistry.ZeroAddress.selector);
169 reg.transferOwnership(address(0));
170 }
171
172 function test_getListings_pages() public {
173 for (uint256 i = 0; i < 5; i++) {
174 vm.prank(alice);
175 reg.list(_params());
176 }
177 assertEq(reg.getListings(0, 2).length, 2);
178 assertEq(reg.getListings(3, 10).length, 2);
179 assertEq(reg.getListings(3, 10)[1].id, 4);
180 assertEq(reg.getListings(5, 10).length, 0);
181 }
182}