|
| 1 | +// Set environment before importing app |
| 2 | +process.env['NODE_ENV'] = 'test'; |
| 3 | + |
| 4 | +import request from 'supertest'; |
| 5 | +import { app } from '../src/index'; |
| 6 | +import { PrismaClient } from '@prisma/client'; |
| 7 | +import { setupTestDatabase, clearTestData, createTestUser, createTestQuest, createTestToken, getTestPrisma, resetUserCounter } from './setup'; |
| 8 | + |
| 9 | +const prisma = new PrismaClient(); |
| 10 | + |
| 11 | +jest.setTimeout(30000); |
| 12 | + |
| 13 | +describe('Repeatable Quest Functionality', () => { |
| 14 | + beforeAll(async () => { |
| 15 | + await setupTestDatabase(); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + resetUserCounter(); // Reset counter FIRST to ensure unique emails |
| 20 | + await clearTestData(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(async () => { |
| 24 | + await prisma.$disconnect(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('Cooldown based on submission date', () => { |
| 28 | + it('should set cooldown based on completion date, not approval date', async () => { |
| 29 | + const admin = await createTestUser({ role: 'ADMIN', email: 'admin@test.com' }); |
| 30 | + const questGiver = await createTestUser({ role: 'EDITOR', email: 'questgiver@test.com' }); |
| 31 | + const player = await createTestUser({ role: 'PLAYER', email: 'player@test.com' }); |
| 32 | + |
| 33 | + // Create a repeatable quest |
| 34 | + const quest = await createTestQuest(questGiver.id, { |
| 35 | + bounty: 100, |
| 36 | + isRepeatable: true, |
| 37 | + cooldownDays: 7 |
| 38 | + }); |
| 39 | + |
| 40 | + const playerToken = createTestToken(player.id, player.email, player.role); |
| 41 | + const adminToken = createTestToken(admin.id, admin.email, admin.role); |
| 42 | + |
| 43 | + // Player claims the quest |
| 44 | + await request(app) |
| 45 | + .put(`/quests/${quest.id}/claim`) |
| 46 | + .set('Authorization', `Bearer ${playerToken}`); |
| 47 | + |
| 48 | + // Player completes the quest (this sets completedAt) |
| 49 | + const completionTime = new Date('2024-01-01T10:00:00Z'); |
| 50 | + await prisma.quest.update({ |
| 51 | + where: { id: quest.id }, |
| 52 | + data: { |
| 53 | + status: 'PENDING_APPROVAL', |
| 54 | + completedAt: completionTime |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // Admin approves the quest |
| 59 | + const response = await request(app) |
| 60 | + .put(`/quests/${quest.id}/approve`) |
| 61 | + .set('Authorization', `Bearer ${adminToken}`); |
| 62 | + |
| 63 | + expect(response.status).toBe(200); |
| 64 | + expect(response.body.data.quest.status).toBe('COOLDOWN'); |
| 65 | + |
| 66 | + // Check that lastCompletedAt is set to the completion date, not approval date |
| 67 | + const updatedQuest = await prisma.quest.findUnique({ where: { id: quest.id } }); |
| 68 | + expect(updatedQuest?.lastCompletedAt).toEqual(completionTime); |
| 69 | + |
| 70 | + // Calculate when cooldown should end (based on completion date) |
| 71 | + const expectedCooldownEnd = new Date(completionTime.getTime() + 7 * 24 * 60 * 60 * 1000); |
| 72 | + expect(expectedCooldownEnd).toEqual(new Date('2024-01-08T10:00:00Z')); // 7 days from completion |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('Admin reset functionality', () => { |
| 77 | + it('should allow admin to reset repeatable quest from cooldown to available', async () => { |
| 78 | + const admin = await createTestUser({ role: 'ADMIN', email: 'admin@test.com' }); |
| 79 | + const questGiver = await createTestUser({ role: 'EDITOR', email: 'questgiver@test.com' }); |
| 80 | + const player = await createTestUser({ role: 'PLAYER', email: 'player@test.com' }); |
| 81 | + |
| 82 | + // Create a repeatable quest in cooldown status |
| 83 | + const quest = await createTestQuest(questGiver.id, { |
| 84 | + bounty: 100, |
| 85 | + isRepeatable: true, |
| 86 | + cooldownDays: 7, |
| 87 | + status: 'COOLDOWN', |
| 88 | + lastCompletedAt: new Date('2024-01-01T10:00:00Z') |
| 89 | + }); |
| 90 | + |
| 91 | + const adminToken = createTestToken(admin.id, admin.email, admin.role); |
| 92 | + |
| 93 | + // Admin resets the quest |
| 94 | + const response = await request(app) |
| 95 | + .put(`/quests/${quest.id}/reset`) |
| 96 | + .set('Authorization', `Bearer ${adminToken}`); |
| 97 | + |
| 98 | + expect(response.status).toBe(200); |
| 99 | + expect(response.body.success).toBe(true); |
| 100 | + expect(response.body.data.quest.status).toBe('AVAILABLE'); |
| 101 | + expect(response.body.data.quest.lastCompletedAt).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not allow non-admin users to reset quests', async () => { |
| 105 | + const editor = await createTestUser({ role: 'EDITOR', email: 'editor@test.com' }); |
| 106 | + const questGiver = await createTestUser({ role: 'EDITOR', email: 'questgiver@test.com' }); |
| 107 | + |
| 108 | + // Create a repeatable quest in cooldown status |
| 109 | + const quest = await createTestQuest(questGiver.id, { |
| 110 | + bounty: 100, |
| 111 | + isRepeatable: true, |
| 112 | + cooldownDays: 7, |
| 113 | + status: 'COOLDOWN', |
| 114 | + lastCompletedAt: new Date('2024-01-01T10:00:00Z') |
| 115 | + }); |
| 116 | + |
| 117 | + const editorToken = createTestToken(editor.id, editor.email, editor.role); |
| 118 | + |
| 119 | + // Editor tries to reset the quest |
| 120 | + const response = await request(app) |
| 121 | + .put(`/quests/${quest.id}/reset`) |
| 122 | + .set('Authorization', `Bearer ${editorToken}`); |
| 123 | + |
| 124 | + expect(response.status).toBe(403); |
| 125 | + expect(response.body.success).toBe(false); |
| 126 | + expect(response.body.error.message).toBe('Access denied. Admin role required.'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should not allow resetting non-repeatable quests', async () => { |
| 130 | + const admin = await createTestUser({ role: 'ADMIN', email: 'admin@test.com' }); |
| 131 | + const questGiver = await createTestUser({ role: 'EDITOR', email: 'questgiver@test.com' }); |
| 132 | + |
| 133 | + // Create a non-repeatable quest in cooldown status |
| 134 | + const quest = await createTestQuest(questGiver.id, { |
| 135 | + bounty: 100, |
| 136 | + isRepeatable: false, |
| 137 | + status: 'COOLDOWN', |
| 138 | + lastCompletedAt: new Date('2024-01-01T10:00:00Z') |
| 139 | + }); |
| 140 | + |
| 141 | + const adminToken = createTestToken(admin.id, admin.email, admin.role); |
| 142 | + |
| 143 | + // Admin tries to reset the quest |
| 144 | + const response = await request(app) |
| 145 | + .put(`/quests/${quest.id}/reset`) |
| 146 | + .set('Authorization', `Bearer ${adminToken}`); |
| 147 | + |
| 148 | + expect(response.status).toBe(400); |
| 149 | + expect(response.body.success).toBe(false); |
| 150 | + expect(response.body.error.message).toBe('Only repeatable quests can be reset'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should not allow resetting quests that are not in cooldown', async () => { |
| 154 | + const admin = await createTestUser({ role: 'ADMIN', email: 'admin@test.com' }); |
| 155 | + const questGiver = await createTestUser({ role: 'EDITOR', email: 'questgiver@test.com' }); |
| 156 | + |
| 157 | + // Create a repeatable quest in available status |
| 158 | + const quest = await createTestQuest(questGiver.id, { |
| 159 | + bounty: 100, |
| 160 | + isRepeatable: true, |
| 161 | + cooldownDays: 7, |
| 162 | + status: 'AVAILABLE' |
| 163 | + }); |
| 164 | + |
| 165 | + const adminToken = createTestToken(admin.id, admin.email, admin.role); |
| 166 | + |
| 167 | + // Admin tries to reset the quest |
| 168 | + const response = await request(app) |
| 169 | + .put(`/quests/${quest.id}/reset`) |
| 170 | + .set('Authorization', `Bearer ${adminToken}`); |
| 171 | + |
| 172 | + expect(response.status).toBe(400); |
| 173 | + expect(response.body.success).toBe(false); |
| 174 | + expect(response.body.error.message).toBe('Quest is not in cooldown status'); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments