|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.23; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {ISemaphore} from "@semaphore/contracts/interfaces/ISemaphore.sol"; |
| 6 | +import {ISemaphoreGroups} from "@semaphore/contracts/interfaces/ISemaphoreGroups.sol"; |
| 7 | +import {Feedback} from "../src/Feedback.sol"; |
| 8 | +import {DeployFeedback} from "../script/DeployFeedback.s.sol"; |
| 9 | + |
| 10 | +contract FeedbackTest is Test { |
| 11 | + event MemberAdded(uint256 indexed groupId, uint256 index, uint256 identityCommitment, uint256 merkleTreeRoot); |
| 12 | + |
| 13 | + Feedback internal feedbackContract; |
| 14 | + ISemaphore internal semaphoreContract; |
| 15 | + ISemaphoreGroups internal semaphoreGroups; |
| 16 | + uint256 internal groupId; |
| 17 | + |
| 18 | + function setUp() external { |
| 19 | + DeployFeedback deployFeedback = new DeployFeedback(); |
| 20 | + (address feedbackAddress, address semaphoreAddress, ) = deployFeedback.run(); |
| 21 | + feedbackContract = Feedback(feedbackAddress); |
| 22 | + semaphoreContract = ISemaphore(semaphoreAddress); |
| 23 | + semaphoreGroups = ISemaphoreGroups(semaphoreAddress); |
| 24 | + groupId = feedbackContract.groupId(); |
| 25 | + } |
| 26 | + |
| 27 | + function testGroupCreatedInConstructor() public view { |
| 28 | + uint256 groupCount = semaphoreContract.groupCounter(); |
| 29 | + assertEq(groupCount, 1); |
| 30 | + } |
| 31 | + |
| 32 | + function testJoinGroup() public { |
| 33 | + // The commitment below is generated with private key of the first account in Anvil |
| 34 | + uint256 identityCommitment = 15072455385723004728391568434269917452175057560864330595979104241296826134229; |
| 35 | + |
| 36 | + // Test: expect an event emitted. Check for all event topics and data |
| 37 | + vm.expectEmit(true, true, true, true); |
| 38 | + emit MemberAdded(groupId, 0, identityCommitment, identityCommitment); |
| 39 | + |
| 40 | + feedbackContract.joinGroup(identityCommitment); |
| 41 | + } |
| 42 | + |
| 43 | + function testSendFeedback() public { |
| 44 | + uint256[] memory commitments = new uint256[](2); |
| 45 | + commitments[0] = uint256(11005642493773047649202648265396872197147567800455247120861783398111750817516); |
| 46 | + commitments[1] = uint256(14473821761500463903284857947161896352613497175238126022206384102438097355186); |
| 47 | + |
| 48 | + for (uint256 i = 0; i < commitments.length; ++i) { |
| 49 | + feedbackContract.joinGroup(commitments[i]); |
| 50 | + } |
| 51 | + |
| 52 | + uint256 merkleTreeDepth = 1; |
| 53 | + uint256 merkleTreeRoot = semaphoreGroups.getMerkleTreeRoot(groupId); |
| 54 | + uint256 feedback = uint256(bytes32("Hello World")); |
| 55 | + |
| 56 | + // These values are computed by running through @semaphore-protocol/circuits |
| 57 | + uint256 nullifier = 14622092170088252518938850323258916742048811914834592843410744760450844885096; |
| 58 | + uint256[8] memory points = [ |
| 59 | + 2004484873491928515306456072357737929124240734208600886081152392890959117520, |
| 60 | + 21291026142870585364296731900941597996672838511394659364623185352043543529323, |
| 61 | + 4657264777014371046112557309523098953851041383509685591373847255581509612788, |
| 62 | + 6904165961903336246592681066375875983213983935764940579845010085396463328555, |
| 63 | + 1952750241178995674697344628236393389729638396609772141225880353616301956443, |
| 64 | + 106937615136633409337870509099767689510837462832227699340906789167349502398, |
| 65 | + 13080722838047436988558418790480431472161933638137155324683844808531903905810, |
| 66 | + 2547578906197450986657523555784319153413167960139250957065929818900731634820 |
| 67 | + ]; |
| 68 | + |
| 69 | + vm.expectEmit(true, true, true, true); |
| 70 | + emit ISemaphore.ProofValidated(groupId, merkleTreeDepth, merkleTreeRoot, nullifier, feedback, groupId, points); |
| 71 | + |
| 72 | + feedbackContract.sendFeedback(merkleTreeDepth, merkleTreeRoot, nullifier, feedback, points); |
| 73 | + } |
| 74 | +} |
0 commit comments