Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

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