|
1 | 1 | import { describe, it, expect, beforeEach } from 'vitest' |
2 | 2 | import { setup, $fetch } from '@nuxt/test-utils/e2e' |
3 | 3 | import { |
4 | | - resetDb, |
5 | 4 | createAuthTestData, |
| 5 | + createTestReward, |
| 6 | + createTestTask, |
6 | 7 | getSessionCookie, |
7 | 8 | getUserByUsername, |
8 | | - TEST_PARENT_USER, |
| 9 | + resetDb, |
| 10 | + setTestUserPoints, |
9 | 11 | TEST_CHILD_USER, |
| 12 | + TEST_PARENT_USER, |
10 | 13 | } from '../utils/index' |
11 | 14 | import { LogsResponse } from '../../shared/types' |
12 | 15 |
|
@@ -149,6 +152,110 @@ describe('Logs API', async () => { |
149 | 152 |
|
150 | 153 | expect(response).toBeDefined() |
151 | 154 | expect(response.logs).toBeDefined() |
152 | | - // Logs might be empty, but the structure should be correct |
| 155 | + }) |
| 156 | + |
| 157 | + it('should correctly log task completions with added points', async () => { |
| 158 | + // Login as parent to get session cookie |
| 159 | + const cookie = await getSessionCookie( |
| 160 | + TEST_PARENT_USER.username, |
| 161 | + TEST_PARENT_USER.password |
| 162 | + ) |
| 163 | + |
| 164 | + const parentUser = await getUserByUsername(TEST_PARENT_USER.username) |
| 165 | + const childUser = await getUserByUsername(TEST_CHILD_USER.username) |
| 166 | + |
| 167 | + await setTestUserPoints(TEST_CHILD_USER.username, 100) |
| 168 | + |
| 169 | + const taskId = await createTestTask( |
| 170 | + parentUser.id, |
| 171 | + childUser.id, |
| 172 | + 'Test Task', |
| 173 | + 50, |
| 174 | + 'single-use' |
| 175 | + ) |
| 176 | + |
| 177 | + // Approve the task completion |
| 178 | + const response = await $fetch(`/api/tasks/${taskId}/approve_complete`, { |
| 179 | + method: 'POST', |
| 180 | + headers: { |
| 181 | + cookie: cookie, |
| 182 | + }, |
| 183 | + }) |
| 184 | + |
| 185 | + expect(response).toBeDefined() |
| 186 | + expect((response as any).statusCode).toBe(200) |
| 187 | + |
| 188 | + const logsResponse = await $fetch<LogsResponse>('/api/logs', { |
| 189 | + method: 'GET', |
| 190 | + headers: { |
| 191 | + cookie: cookie, |
| 192 | + }, |
| 193 | + }) |
| 194 | + |
| 195 | + expect(logsResponse).toBeDefined() |
| 196 | + expect(logsResponse.logs).toBeDefined() |
| 197 | + |
| 198 | + // Find the task completion log entry |
| 199 | + const completionLog = logsResponse.logs.find( |
| 200 | + (log) => log.action_type === 'approve_task_complete' |
| 201 | + ) |
| 202 | + |
| 203 | + expect(completionLog).toBeDefined() |
| 204 | + expect(completionLog?.points_before).toBe(100) |
| 205 | + expect(completionLog?.points_after).toBe(150) |
| 206 | + }) |
| 207 | + |
| 208 | + it('should correctly log reward redemptions with deducted points', async () => { |
| 209 | + // Login as parent to get session cookie |
| 210 | + const cookie = await getSessionCookie( |
| 211 | + TEST_PARENT_USER.username, |
| 212 | + TEST_PARENT_USER.password |
| 213 | + ) |
| 214 | + |
| 215 | + const parentUser = await getUserByUsername(TEST_PARENT_USER.username) |
| 216 | + const childUser = await getUserByUsername(TEST_CHILD_USER.username) |
| 217 | + |
| 218 | + await setTestUserPoints(TEST_CHILD_USER.username, 100) |
| 219 | + |
| 220 | + const rewardId = await createTestReward( |
| 221 | + parentUser.id, |
| 222 | + childUser.id, |
| 223 | + 'Test Reward', |
| 224 | + 50, |
| 225 | + 'single-use' |
| 226 | + ) |
| 227 | + |
| 228 | + // Approve the redemption |
| 229 | + const response = await $fetch( |
| 230 | + `/api/rewards/${rewardId}/approve_redemption`, |
| 231 | + { |
| 232 | + method: 'POST', |
| 233 | + headers: { |
| 234 | + cookie: cookie, |
| 235 | + }, |
| 236 | + } |
| 237 | + ) |
| 238 | + |
| 239 | + expect(response).toBeDefined() |
| 240 | + expect((response as any).statusCode).toBe(200) |
| 241 | + |
| 242 | + const logsResponse = await $fetch<LogsResponse>('/api/logs', { |
| 243 | + method: 'GET', |
| 244 | + headers: { |
| 245 | + cookie: cookie, |
| 246 | + }, |
| 247 | + }) |
| 248 | + |
| 249 | + expect(logsResponse).toBeDefined() |
| 250 | + expect(logsResponse.logs).toBeDefined() |
| 251 | + |
| 252 | + // Find the reward redemption log entry |
| 253 | + const redemptionLog = logsResponse.logs.find( |
| 254 | + (log) => log.action_type === 'approve_redemption' |
| 255 | + ) |
| 256 | + |
| 257 | + expect(redemptionLog).toBeDefined() |
| 258 | + expect(redemptionLog?.points_before).toBe(100) |
| 259 | + expect(redemptionLog?.points_after).toBe(50) |
153 | 260 | }) |
154 | 261 | }) |
0 commit comments