forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcampaignRepository.test.js
More file actions
124 lines (106 loc) · 3.63 KB
/
Copy pathcampaignRepository.test.js
File metadata and controls
124 lines (106 loc) · 3.63 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
// Feature: nova-rewards, Property 3: Campaign validation rejects invalid inputs
// Validates: Requirements 7.3
jest.mock('../db/index', () => ({ query: jest.fn() }));
const fc = require('fast-check');
const { validateCampaign } = require('../db/campaignRepository');
const { CampaignFactory } = require('./fixtures');
const baseDateArb = fc.date({
min: new Date('2020-01-01'),
max: new Date('2030-12-31'),
});
function toIsoDate(date) {
return date.toISOString().slice(0, 10);
}
describe('validateCampaign (Property 3)', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test('rejects reward rate <= 0 for any date range', () => {
fc.assert(
fc.property(
fc.oneof(
fc.constant(0),
fc.float({ max: 0, noNaN: true }),
fc.integer({ max: -1 }),
),
baseDateArb,
fc.integer({ min: 1, max: 365 }),
(rewardRate, startDateBase, durationDays) => {
const startDate = toIsoDate(startDateBase);
const endDate = toIsoDate(
new Date(startDateBase.getTime() + durationDays * 24 * 60 * 60 * 1000)
);
const { valid, errors } = validateCampaign({ rewardRate, startDate, endDate });
expect(valid).toBe(false);
expect(errors).toContain('rewardRate must be greater than 0');
return true;
}
),
{ numRuns: 100 }
);
});
test('rejects end date that is not strictly after start date', () => {
fc.assert(
fc.property(
fc.integer({ min: 1, max: 1000 }),
baseDateArb,
fc.integer({ min: 0, max: 30 }),
(rewardRate, endDateBase, offsetDays) => {
const endDate = toIsoDate(endDateBase);
const startDate = toIsoDate(
new Date(endDateBase.getTime() + offsetDays * 24 * 60 * 60 * 1000)
);
const { valid, errors } = validateCampaign({
rewardRate,
startDate,
endDate,
});
expect(valid).toBe(false);
expect(errors).toContain('endDate must be strictly after startDate');
return true;
}
),
{ numRuns: 100 }
);
});
test('accepts valid campaign inputs', () => {
fc.assert(
fc.property(
fc.integer({ min: 1, max: 1000 }),
baseDateArb,
fc.integer({ min: 1, max: 365 }),
(rewardRate, startDateBase, durationDays) => {
const startDate = toIsoDate(startDateBase);
const endDate = toIsoDate(
new Date(startDateBase.getTime() + durationDays * 24 * 60 * 60 * 1000)
);
const { valid, errors } = validateCampaign({ rewardRate, startDate, endDate });
expect(valid).toBe(true);
expect(errors).toHaveLength(0);
return true;
}
),
{ numRuns: 100 }
);
});
test('factory-built campaign passes validation', () => {
const campaign = CampaignFactory.build();
const { valid, errors } = validateCampaign({
rewardRate: parseFloat(campaign.reward_rate),
startDate: campaign.start_date,
endDate: campaign.end_date,
});
expect(valid).toBe(true);
expect(errors).toHaveLength(0);
});
test('factory-built expired campaign fails validation (end before start)', () => {
const campaign = CampaignFactory.expired();
// expired campaign has end_date in the past and start_date even further back — still valid dates
const { valid } = validateCampaign({
rewardRate: 1,
startDate: campaign.start_date,
endDate: campaign.end_date,
});
expect(valid).toBe(true); // dates are still ordered correctly; expiry is a runtime concern
});
});