Skip to content

Commit

Permalink
fix: skip webhooks for flagged proposals (#147)
Browse files Browse the repository at this point in the history
* fix: skip webhooks for flagged proposals

* chore: setup tests

* chore: add tests

* chore: update CI to use node 18
  • Loading branch information
wa0x6e authored Nov 28, 2023
1 parent 3db0115 commit b649796
Show file tree
Hide file tree
Showing 7 changed files with 1,881 additions and 17 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Test

on: [push]

jobs:
test:
strategy:
matrix:
target: ['18']
uses: snapshot-labs/actions/.github/workflows/test.yml@main
secrets: inherit
with:
target: ${{ matrix.target }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_modules
dist
build
.env
coverage

# Remove some common IDE working directories
.idea
Expand Down
18 changes: 18 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

export default {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['./src/**'],
coverageDirectory: 'coverage',
coverageProvider: 'v8',
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/dist/', '<rootDir>/test/fixtures/'],
preset: 'ts-jest',
testEnvironment: 'jest-environment-node-single-context',
setupFiles: ['dotenv/config'],
moduleFileExtensions: ['js', 'ts'],
verbose: true
};
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"typecheck": "tsc --noEmit",
"build": "tsc",
"dev": "nodemon src/index.ts",
"start": "node dist/src/index.js"
"start": "node dist/src/index.js",
"test": "yarn jest"
},
"eslintConfig": {
"extends": "@snapshot-labs"
Expand Down Expand Up @@ -43,9 +44,13 @@
"@snapshot-labs/eslint-config": "^0.1.0-beta.15",
"@snapshot-labs/prettier-config": "^0.1.0-beta.11",
"@types/express": "^4.17.11",
"@types/jest": "^29.5.10",
"@types/node": "^18.0.0",
"eslint": "^8.47.0",
"prettier": "^3.0.3"
"jest": "^29.7.0",
"jest-environment-node-single-context": "^29.1.0",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1"
},
"engines": {
"node": "^18.0.0"
Expand Down
9 changes: 4 additions & 5 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export async function getSubscribers(space) {
return subscriptions.map(subscription => subscription.address);
}

export async function getProposal(id) {
let proposal: { [key: string]: any } | null = null;
export async function getProposal(id: string): Promise<Record<string, any> | null> {
const query = {
proposal: {
__args: {
Expand All @@ -52,7 +51,8 @@ export async function getProposal(id) {
start: true,
end: true,
link: true,
snapshot: true
snapshot: true,
flagged: true
}
};

Expand All @@ -61,8 +61,7 @@ export async function getProposal(id) {
if (result.errors) {
console.error(`[events] Errors in subgraph request for proposal id: ${id}`);
}
proposal = result.proposal || null;
return proposal;
return result.proposal?.flagged ? null : result.proposal || null;
} catch (e: any) {
capture(e, { contexts: { input: { query, id } } });
return null;
Expand Down
42 changes: 42 additions & 0 deletions test/unit/helpers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getProposal } from '../../../src/helpers/utils';

const PROPOSAL = { id: '0x0', flagged: false };

const mockSnapshotUtilsSubgraphRequest = jest.fn((): any => {
return {};
});
jest.mock('@snapshot-labs/snapshot.js', () => {
const originalModule = jest.requireActual('@snapshot-labs/snapshot.js');

return {
...originalModule,
utils: {
...originalModule.utils,
subgraphRequest: () => mockSnapshotUtilsSubgraphRequest()
}
};
});

describe('getProposal()', () => {
it('returns null when the proposal is flagged', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({
proposal: { ...PROPOSAL, flagged: true }
});
expect(getProposal('')).resolves.toBeNull();
});

it('returns null when the proposal does not exist', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({ proposal: null });
expect(getProposal('')).resolves.toBeNull();
});

it('returns null on missing response', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({});
expect(getProposal('')).resolves.toBeNull();
});

it('returns the proposal', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({ proposal: PROPOSAL });
expect(getProposal('')).resolves.toEqual(PROPOSAL);
});
});
Loading

0 comments on commit b649796

Please sign in to comment.