Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RCV Copeland voting system #1072

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions src/voting/__snapshots__/copeland.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Partial ranking 1`] = `
[
4,
6,
2,
0,
]
`;

exports[`getScores 1`] = `
[
5,
5,
2,
0,
]
`;

exports[`getScores 2`] = `
[
5,
5,
2,
0,
]
`;

exports[`getScores 3`] = `
[
5,
5,
2,
0,
]
`;

exports[`getScores 4`] = `
[
5,
5,
2,
0,
]
`;

exports[`getScoresByStrategy 1`] = `
[
[
5,
],
[
5,
],
[
2,
],
[
0,
],
]
`;

exports[`getScoresByStrategy 2`] = `
[
[
5,
],
[
5,
],
[
2,
],
[
0,
],
]
`;

exports[`getScoresByStrategy 3`] = `
[
[
5,
5,
5,
],
[
5,
5,
5,
],
[
2,
2,
2,
],
[
0,
0,
0,
],
]
`;

exports[`getScoresByStrategy 4`] = `
[
[
5,
5,
5,
],
[
5,
5,
5,
],
[
2,
2,
2,
],
[
0,
0,
0,
],
]
`;

exports[`getScoresTotal 1`] = `4`;

exports[`getScoresTotal 2`] = `12`;
129 changes: 129 additions & 0 deletions src/voting/copeland.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { test, expect } from 'vitest';
import CopelandVoting from './copeland';
import example from './examples/copeland.json';

// Helper function to create a more complex example with multiple strategies
const example2 = () => {
const proposal = {
choices: ['Alice', 'Bob', 'Carol', 'David']
};
const strategies = [
{ name: 'ticket', network: '1', params: {} },
{ name: 'ticket', network: '1', params: {} },
{ name: 'ticket', network: '1', params: {} }
];
const votes = example.votes.map((vote) => ({
choice: vote.choice,
balance: 3,
scores: [1, 1, 1]
}));

return {
proposal,
strategies,
votes
};
};

// Generate a set of votes including some invalid choices
const votesWithInvalidChoices = () => {
const invalidVotes = [
{ choice: [0, 1], balance: 1, scores: [1] },
{ choice: [1, 5], balance: 1, scores: [1] },
{ choice: [1, 1], balance: 1, scores: [1] },
{ choice: [], balance: 1, scores: [1] },
{ choice: [1, 2, 3, 4, 5], balance: 1, scores: [1] }
];
return [...invalidVotes, ...example.votes];
};

// Generate a set of votes including some invalid choices for the multi-strategy example
const votesWithInvalidChoices2 = () => {
const invalidVotes = [
{ choice: [0, 1], balance: 3, scores: [1, 1, 1] },
{ choice: [1, 5], balance: 3, scores: [1, 1, 1] },
{ choice: [1, 1], balance: 3, scores: [1, 1, 1] },
{ choice: [], balance: 3, scores: [1, 1, 1] },
{ choice: [1, 2, 3, 4, 5], balance: 3, scores: [1, 1, 1] }
];
return [...invalidVotes, ...example2().votes];
};

// Test cases for getScores method
test.each([
[example.proposal, example.votes, example.strategies],
[example.proposal, votesWithInvalidChoices(), example.strategies],
[example2().proposal, example2().votes, example2().strategies],
[example2().proposal, votesWithInvalidChoices2(), example2().strategies]
])('getScores', (proposal, votes, strategies) => {
const copeland = new CopelandVoting(
proposal,
votes,
strategies,
example.selectedChoice
);
expect(copeland.getScores()).toMatchSnapshot();
});

// Test cases for getScoresByStrategy method
test.each([
[example.proposal, example.votes, example.strategies],
[example.proposal, votesWithInvalidChoices(), example.strategies],
[example2().proposal, example2().votes, example2().strategies],
[example2().proposal, votesWithInvalidChoices2(), example2().strategies]
])('getScoresByStrategy', (proposal, votes, strategies) => {
const copeland = new CopelandVoting(
proposal,
votes,
strategies,
example.selectedChoice
);
expect(copeland.getScoresByStrategy()).toMatchSnapshot();
});

// Test cases for getScoresTotal method
test.each([
[example.proposal, example.votes, example.strategies],
[example2().proposal, example2().votes, example2().strategies]
])('getScoresTotal', (proposal, votes, strategies) => {
const copeland = new CopelandVoting(
proposal,
votes,
strategies,
example.selectedChoice
);
expect(copeland.getScoresTotal()).toMatchSnapshot();
});

// Test cases for getChoiceString method
test.each([
[[1, 2], 'Alice, Bob'],
[[4, 2, 3, 1], 'David, Bob, Carol, Alice']
])('getChoiceString %s', (selected, expected) => {
const copeland = new CopelandVoting(
example.proposal,
example.votes,
example.strategies,
selected
);
expect(copeland.getChoiceString()).toBe(expected);
});

// Test case for partial ranking
test('Partial ranking', () => {
const partialVotes = [
...example.votes,
{
choice: [2, 1],
balance: 1,
scores: [1]
}
];
const copeland = new CopelandVoting(
example.proposal,
partialVotes,
example.strategies,
example.selectedChoice
);
expect(copeland.getScores()).toMatchSnapshot();
});
Loading