Repository navigation
Expand file tree
/
Copy pathrep.test.ts
More file actions
189 lines (166 loc) · 5.84 KB
/
Copy pathrep.test.ts
File metadata and controls
189 lines (166 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
import {
computeLedger,
emptyLedger,
meetsRequirement,
repOf,
requiredRep,
type RepActivity,
type RepConfig
} from '../src/lib/rep/engine';
const cfg = (over: Partial<RepConfig> = {}): RepConfig => ({
enabled: true,
gains: { post: 5, comment: 2, answerAccepted: 15 },
dailyCaps: { post: 25, comment: 10 },
topics: { showcase: 50 },
onViolation: 'move',
fallbackTopic: 'general',
exemptMaintainers: true,
dataBranch: 'rep-data',
...over
});
const act = (
login: string,
action: RepActivity['action'],
createdAt: string,
discussionId = 'D1'
): RepActivity => ({ login, action, createdAt, discussionId });
describe('emptyLedger', () => {
it('creates a versioned ledger with no users', () => {
expect(emptyLedger('2026-01-01T00:00:00Z')).toEqual({
version: 1,
updatedAt: '2026-01-01T00:00:00Z',
users: {}
});
});
it('defaults updatedAt to the epoch', () => {
expect(emptyLedger().updatedAt).toBe(new Date(0).toISOString());
});
});
describe('computeLedger', () => {
it('sums gains per action type', () => {
const ledger = computeLedger(
[
act('alice', 'post', '2026-01-01T10:00:00Z'),
act('alice', 'comment', '2026-01-01T11:00:00Z'),
act('alice', 'answerAccepted', '2026-01-01T12:00:00Z'),
act('bob', 'comment', '2026-01-01T13:00:00Z')
],
cfg(),
'2026-01-02T00:00:00Z'
);
expect(ledger.users).toEqual({ alice: 22, bob: 2 });
expect(ledger.updatedAt).toBe('2026-01-02T00:00:00Z');
});
it('defaults updatedAt to now', () => {
expect(Date.parse(computeLedger([], cfg()).updatedAt)).not.toBeNaN();
});
it('keys users by lowercased login', () => {
const ledger = computeLedger(
[act('Alice', 'post', '2026-01-01T10:00:00Z'), act('ALICE', 'post', '2026-01-01T11:00:00Z')],
cfg()
);
expect(ledger.users).toEqual({ alice: 10 });
});
it('caps per-day gains per action type, resetting on a new UTC day', () => {
// comment gain 2, daily cap 10 → 5 comments/day count, the rest earn 0
const day1 = Array.from({ length: 8 }, (_, i) =>
act('alice', 'comment', `2026-01-01T0${i}:00:00Z`)
);
const day2 = [act('alice', 'comment', '2026-01-02T00:00:00Z')];
const ledger = computeLedger([...day1, ...day2], cfg());
expect(ledger.users.alice).toBe(10 + 2);
});
it('caps partially when a gain straddles the cap boundary', () => {
// post gain 5, cap 12 → 5 + 5 + 2 (partial) + 0
const posts = Array.from({ length: 4 }, (_, i) =>
act('alice', 'post', `2026-01-01T0${i}:00:00Z`)
);
const ledger = computeLedger(posts, cfg({ dailyCaps: { post: 12, comment: 10 } }));
expect(ledger.users.alice).toBe(12);
});
it('never caps accepted answers', () => {
const answers = Array.from({ length: 10 }, (_, i) =>
act('alice', 'answerAccepted', `2026-01-01T0${i}:00:00Z`)
);
expect(computeLedger(answers, cfg()).users.alice).toBe(150);
});
it('treats a zero cap as uncapped', () => {
const posts = Array.from({ length: 9 }, (_, i) =>
act('alice', 'post', `2026-01-01T0${i}:00:00Z`)
);
expect(computeLedger(posts, cfg({ dailyCaps: { post: 0, comment: 10 } })).users.alice).toBe(45);
});
it('is independent of input ordering', () => {
const activity = [
act('alice', 'comment', '2026-01-01T06:00:00Z'),
act('alice', 'comment', '2026-01-01T01:00:00Z'),
act('alice', 'comment', '2026-01-01T03:00:00Z')
];
const shuffled = [activity[2], activity[0], activity[1]];
expect(computeLedger(activity, cfg()).users).toEqual(computeLedger(shuffled, cfg()).users);
});
});
describe('repOf', () => {
it('reads rep case-insensitively and defaults to 0', () => {
const ledger = computeLedger([act('Alice', 'post', '2026-01-01T00:00:00Z')], cfg());
expect(repOf(ledger, 'ALICE')).toBe(5);
expect(repOf(ledger, 'nobody')).toBe(0);
expect(repOf(ledger, null)).toBe(0);
expect(repOf(ledger, undefined)).toBe(0);
expect(repOf(null, 'alice')).toBe(0);
});
});
describe('requiredRep', () => {
it('reads the topic threshold, 0 for ungated topics', () => {
expect(requiredRep(cfg(), 'showcase')).toBe(50);
expect(requiredRep(cfg(), 'general')).toBe(0);
});
it('is always 0 when the feature is disabled', () => {
expect(requiredRep(cfg({ enabled: false }), 'showcase')).toBe(0);
});
});
describe('meetsRequirement', () => {
it('compares rep against the threshold', () => {
expect(meetsRequirement(cfg(), 'showcase', 50, false)).toBe(true);
expect(meetsRequirement(cfg(), 'showcase', 49, false)).toBe(false);
expect(meetsRequirement(cfg(), 'general', 0, false)).toBe(true);
});
it('always passes exempt users', () => {
expect(meetsRequirement(cfg(), 'showcase', 0, true)).toBe(true);
});
});
describe('fetchLedger', () => {
const { mockConfig } = vi.hoisted(() => ({
mockConfig: {
repo: { owner: 'o', name: 'r' },
rep: { enabled: true, dataBranch: 'data' }
}
}));
vi.mock('$lib/config', () => ({ forumConfig: mockConfig }));
let fetchLedger: typeof import('$lib/rep/ledger')['fetchLedger'];
let fetchMock: Mock;
beforeEach(async () => {
vi.resetModules();
fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
({ fetchLedger } = await import('$lib/rep/ledger'));
});
it('fetches the rep index from the data branch raw URL', async () => {
const ledger = { version: 1, updatedAt: 'T', users: { alice: 5 } };
fetchMock.mockResolvedValue({ ok: true, json: async () => ledger });
expect(await fetchLedger()).toEqual(ledger);
expect(fetchMock).toHaveBeenCalledWith(
'https://raw.githubusercontent.com/o/r/data/profiles/index.json',
{ cache: 'no-cache' }
);
});
it('returns null when the ledger does not exist yet', async () => {
fetchMock.mockResolvedValue({ ok: false, status: 404 });
expect(await fetchLedger()).toBeNull();
});
it('returns null on network failure', async () => {
fetchMock.mockRejectedValue(new TypeError('offline'));
expect(await fetchLedger()).toBeNull();
});
});