Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1// eslint-disable-next-line import/no-extraneous-dependencies
2import { computeUserScore } from './computeUserScore.js';
3
4describe('computeUserScore', () => {
5 const userSubmissionStatistics = [
6 { itemTypeId: 'abc', numSubmissions: 92 },
7 { itemTypeId: 'def', numSubmissions: 8 },
8 ];
9
10 const userActionStatistics = [
11 // This action, policy pair was applied twice by user rules,
12 // hence no item submission ids.
13 {
14 actionId: '1',
15 policyId: '1',
16 count: 2,
17 itemSubmissionIds: [],
18 },
19 ];
20
21 test('should count actions triggered by user rules', async () => {
22 const sut = computeUserScore.bind(
23 null,
24 userSubmissionStatistics,
25 userActionStatistics,
26 );
27
28 // should have two actions, with an effective 4 penalty points, out of 100
29 // submissions, which is a 4.
30 expect(
31 await sut([{ policyId: '1', actionId: '1', penalties: [1, 3] }]),
32 ).toBe(4);
33
34 // ditto, but now 6 penalty points, which is a 3.
35 expect(
36 await sut([{ policyId: '1', actionId: '1', penalties: [2, 4] }]),
37 ).toBe(3);
38 });
39
40 test('should apply only the most-severe penalty per item submission', async () => {
41 const sut = computeUserScore.bind(null, userSubmissionStatistics, [
42 { actionId: '1', policyId: '1', count: 1, itemSubmissionIds: ['a'] },
43 { actionId: '2', policyId: '1', count: 1, itemSubmissionIds: ['a'] },
44 ]);
45
46 // should use penalty 4 for submission `a`, because that's the highest
47 // first-strike penalty among the (action, policy) pairs that `a` matched.
48 expect(
49 await sut([
50 { actionId: '1', policyId: '1', penalties: [2, 12] },
51 { actionId: '2', policyId: '1', penalties: [4] },
52 ]),
53 ).toBe(4);
54 });
55
56 test('should return expected results', async () => {
57 const sut = computeUserScore.bind(
58 null,
59 [
60 { itemTypeId: 'abc', numSubmissions: 92 },
61 { itemTypeId: 'def', numSubmissions: 8 },
62 ],
63 [
64 {
65 actionId: '1',
66 policyId: '1',
67 count: 2,
68 itemSubmissionIds: ['a', 'b'],
69 },
70 { actionId: '2', policyId: '1', count: 1, itemSubmissionIds: ['c'] },
71 { actionId: '1', policyId: '2', count: 1, itemSubmissionIds: ['d'] },
72 {
73 actionId: '2',
74 policyId: '2',
75 count: 6,
76 itemSubmissionIds: ['e', 'f', 'g', 'h', 'i', 'j'],
77 },
78 // doesn't blow up on null policy id. and the penalty from (action,
79 // policy) = (2, 2) above should apply to these repeat submission ids.
80 {
81 actionId: '2',
82 policyId: null,
83 count: 2,
84 itemSubmissionIds: ['i', 'j'],
85 },
86 ],
87 );
88
89 // If no penalties are defined, the user should get a score of 5.
90 expect(await sut([])).toBe(5);
91
92 // if the penalties are all 1 (which should apply to all subsequent
93 // "strikes"), then we have 10 violations out of 100 submissions, which
94 // should be a 3.
95 expect(
96 await sut([
97 { actionId: '1', policyId: '1', penalties: [1] },
98 { actionId: '1', policyId: '2', penalties: [1] },
99 { actionId: '2', policyId: '1', penalties: [1] },
100 { actionId: '2', policyId: '2', penalties: [1] },
101 ]),
102 ).toBe(3);
103
104 // If one penalty is 3, then we have 13 'effective' violations out of 100
105 // submissions, which would be a 2.
106 expect(
107 await sut([
108 { actionId: '1', policyId: '1', penalties: [1] },
109 { actionId: '1', policyId: '2', penalties: [1] },
110 { actionId: '2', policyId: '1', penalties: [3] },
111 { actionId: '2', policyId: '2', penalties: [1] },
112 ]),
113 ).toBe(2);
114
115 // One penalty at 3 for an one (action, penalty) pair that occurred twice,
116 // plus one penatly at 9, means we have 28 effective violations out of 100,
117 // which should be a 2.
118 expect(
119 await sut([
120 { actionId: '1', policyId: '1', penalties: [3] },
121 { actionId: '1', policyId: '2', penalties: [1] },
122 { actionId: '2', policyId: '1', penalties: [9] },
123 { actionId: '2', policyId: '2', penalties: [1] },
124 ]),
125 ).toBe(2);
126
127 // it should support different penalties for the first, second, ..., nth
128 // violation. (action 2, policy 2) has six occurrences, so this should lead
129 // to 1 + 3 + 9 + 27 + 27 + 27 = 94 effective violations out of 100.
130 expect(
131 await sut([{ actionId: '2', policyId: '2', penalties: [1, 3, 9, 27] }]),
132 ).toBe(1);
133 });
134});