musora-content-services 2.94.8 → 2.95.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/.claude/settings.local.json +18 -0
  2. package/CHANGELOG.md +18 -0
  3. package/CLAUDE.md +408 -0
  4. package/babel.config.cjs +10 -0
  5. package/jsdoc.json +2 -1
  6. package/package.json +2 -2
  7. package/src/constants/award-assets.js +35 -0
  8. package/src/filterBuilder.js +7 -2
  9. package/src/index.d.ts +26 -5
  10. package/src/index.js +26 -5
  11. package/src/services/awards/award-callbacks.js +165 -0
  12. package/src/services/awards/award-query.js +495 -0
  13. package/src/services/awards/internal/.indexignore +1 -0
  14. package/src/services/awards/internal/award-definitions.js +239 -0
  15. package/src/services/awards/internal/award-events.js +102 -0
  16. package/src/services/awards/internal/award-manager.js +162 -0
  17. package/src/services/awards/internal/certificate-builder.js +66 -0
  18. package/src/services/awards/internal/completion-data-generator.js +84 -0
  19. package/src/services/awards/internal/content-progress-observer.js +137 -0
  20. package/src/services/awards/internal/image-utils.js +62 -0
  21. package/src/services/awards/internal/message-generator.js +17 -0
  22. package/src/services/awards/internal/types.js +5 -0
  23. package/src/services/awards/types.d.ts +79 -0
  24. package/src/services/awards/types.js +101 -0
  25. package/src/services/config.js +24 -4
  26. package/src/services/content-org/learning-paths.ts +19 -15
  27. package/src/services/gamification/awards.ts +114 -83
  28. package/src/services/progress-events.js +58 -0
  29. package/src/services/progress-row/method-card.js +20 -5
  30. package/src/services/sanity.js +1 -1
  31. package/src/services/sync/fetch.ts +10 -2
  32. package/src/services/sync/manager.ts +6 -0
  33. package/src/services/sync/models/ContentProgress.ts +5 -6
  34. package/src/services/sync/models/UserAwardProgress.ts +55 -0
  35. package/src/services/sync/models/index.ts +1 -0
  36. package/src/services/sync/repositories/content-progress.ts +47 -25
  37. package/src/services/sync/repositories/index.ts +1 -0
  38. package/src/services/sync/repositories/practices.ts +16 -1
  39. package/src/services/sync/repositories/user-award-progress.ts +133 -0
  40. package/src/services/sync/repository-proxy.ts +6 -0
  41. package/src/services/sync/retry.ts +12 -11
  42. package/src/services/sync/schema/index.ts +18 -3
  43. package/src/services/sync/store/index.ts +53 -8
  44. package/src/services/sync/store/push-coalescer.ts +3 -3
  45. package/src/services/sync/store-configs.ts +7 -1
  46. package/src/services/userActivity.js +0 -1
  47. package/test/HttpClient.test.js +6 -6
  48. package/test/awards/award-alacarte-observer.test.js +196 -0
  49. package/test/awards/award-auto-refresh.test.js +83 -0
  50. package/test/awards/award-calculations.test.js +33 -0
  51. package/test/awards/award-certificate-display.test.js +328 -0
  52. package/test/awards/award-collection-edge-cases.test.js +210 -0
  53. package/test/awards/award-collection-filtering.test.js +285 -0
  54. package/test/awards/award-completion-flow.test.js +213 -0
  55. package/test/awards/award-exclusion-handling.test.js +273 -0
  56. package/test/awards/award-multi-lesson.test.js +241 -0
  57. package/test/awards/award-observer-integration.test.js +325 -0
  58. package/test/awards/award-query-messages.test.js +438 -0
  59. package/test/awards/award-user-collection.test.js +412 -0
  60. package/test/awards/duplicate-prevention.test.js +118 -0
  61. package/test/awards/helpers/completion-mock.js +54 -0
  62. package/test/awards/helpers/index.js +3 -0
  63. package/test/awards/helpers/mock-setup.js +69 -0
  64. package/test/awards/helpers/progress-emitter.js +39 -0
  65. package/test/awards/message-generator.test.js +162 -0
  66. package/test/initializeTests.js +6 -0
  67. package/test/mockData/award-definitions.js +171 -0
  68. package/test/sync/models/award-database-integration.test.js +519 -0
  69. package/tools/generate-index.cjs +9 -0
@@ -0,0 +1,412 @@
1
+ import { mockAwardDefinitions } from '../mockData/award-definitions'
2
+
3
+ jest.mock('../../src/services/sanity', () => ({
4
+ default: {
5
+ fetch: jest.fn()
6
+ },
7
+ fetchSanity: jest.fn()
8
+ }))
9
+
10
+ jest.mock('../../src/services/sync/repository-proxy', () => {
11
+ const mockFns = {
12
+ userAwardProgress: {
13
+ getAll: jest.fn(),
14
+ getByAwardId: jest.fn()
15
+ }
16
+ }
17
+ return { default: mockFns, ...mockFns }
18
+ })
19
+
20
+ import sanityClient, { fetchSanity } from '../../src/services/sanity'
21
+ import db from '../../src/services/sync/repository-proxy'
22
+ import { awardDefinitions } from '../../src/services/awards/internal/award-definitions'
23
+ import { getCompletedAwards, getInProgressAwards, getAwardStatistics } from '../../src/services/awards/award-query'
24
+
25
+ describe('Award User Collection - E2E Scenarios', () => {
26
+ beforeEach(async () => {
27
+ jest.clearAllMocks()
28
+
29
+ sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
30
+ fetchSanity.mockResolvedValue(mockAwardDefinitions)
31
+ db.userAwardProgress.getAll = jest.fn()
32
+ db.userAwardProgress.getByAwardId = jest.fn()
33
+
34
+ await awardDefinitions.refresh()
35
+ })
36
+
37
+ afterEach(() => {
38
+ awardDefinitions.clear()
39
+ })
40
+
41
+ describe('Scenario: User with multiple completed awards', () => {
42
+ beforeEach(() => {
43
+ db.userAwardProgress.getAll.mockResolvedValue({
44
+ data: [
45
+ {
46
+ award_id: '0238b1e5-ebee-42b3-9390-91467d113575',
47
+ progress_percentage: 100,
48
+ completed_at: Math.floor(Date.now() / 1000) - 86400 * 5,
49
+ completion_data: {
50
+ content_title: 'Adrian Guided Course Test',
51
+ completed_at: new Date(Date.now() - 86400 * 5 * 1000).toISOString(),
52
+ days_user_practiced: 14,
53
+ practice_minutes: 180
54
+ }
55
+ },
56
+ {
57
+ award_id: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
58
+ progress_percentage: 100,
59
+ completed_at: Math.floor(Date.now() / 1000) - 86400 * 2,
60
+ completion_data: {
61
+ content_title: 'Enrolling w/ Kickoff, has product GC (EC)',
62
+ completed_at: new Date(Date.now() - 86400 * 2 * 1000).toISOString(),
63
+ days_user_practiced: 10,
64
+ practice_minutes: 200
65
+ }
66
+ },
67
+ {
68
+ award_id: '361f3034-c6c9-45f7-bbfb-0d58dbe14411',
69
+ progress_percentage: 100,
70
+ completed_at: Math.floor(Date.now() / 1000) - 86400 * 30,
71
+ completion_data: {
72
+ content_title: 'Learn To Play The Drums',
73
+ completed_at: new Date(Date.now() - 86400 * 30 * 1000).toISOString(),
74
+ days_user_practiced: 60,
75
+ practice_minutes: 1200
76
+ }
77
+ }
78
+ ]
79
+ })
80
+ })
81
+
82
+ test('returns all completed awards with completion data', async () => {
83
+ const completed = await getCompletedAwards()
84
+
85
+ expect(completed).toHaveLength(3)
86
+ expect(completed[0]).toMatchObject({
87
+ awardId: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
88
+ awardTitle: 'Enrolling w/ Kickoff, has product GC (EC)',
89
+ progressPercentage: 100,
90
+ completedAt: expect.any(String),
91
+ brand: 'pianote'
92
+ })
93
+ })
94
+
95
+ test('completed awards include award definition details', async () => {
96
+ const completed = await getCompletedAwards()
97
+
98
+ expect(completed[0]).toMatchObject({
99
+ badge: expect.stringContaining('cdn.sanity.io'),
100
+ award: expect.stringContaining('cdn.sanity.io'),
101
+ instructorName: 'Lisa Witt'
102
+ })
103
+
104
+ expect(completed[1]).toMatchObject({
105
+ badge: expect.stringContaining('cdn.sanity.io'),
106
+ award: expect.stringContaining('cdn.sanity.io'),
107
+ instructorName: 'Aaron Graham'
108
+ })
109
+ })
110
+
111
+ test('completed awards are sorted by completion date (most recent first)', async () => {
112
+ const completed = await getCompletedAwards()
113
+
114
+ const dates = completed.map(a => new Date(a.completedAt).getTime())
115
+ expect(dates[0]).toBeGreaterThan(dates[1])
116
+ expect(dates[1]).toBeGreaterThan(dates[2])
117
+ })
118
+ })
119
+
120
+ describe('Scenario: User with in-progress awards', () => {
121
+ beforeEach(() => {
122
+ db.userAwardProgress.getAll.mockResolvedValue({
123
+ data: [
124
+ {
125
+ award_id: '0238b1e5-ebee-42b3-9390-91467d113575',
126
+ progress_percentage: 50,
127
+ completed_at: null,
128
+ completion_data: null
129
+ },
130
+ {
131
+ award_id: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
132
+ progress_percentage: 75,
133
+ completed_at: null,
134
+ completion_data: null
135
+ },
136
+ {
137
+ award_id: '361f3034-c6c9-45f7-bbfb-0d58dbe14411',
138
+ progress_percentage: 20,
139
+ completed_at: null,
140
+ completion_data: null
141
+ }
142
+ ]
143
+ })
144
+ })
145
+
146
+ test('returns all in-progress awards with progress percentages', async () => {
147
+ const inProgress = await getInProgressAwards()
148
+
149
+ expect(inProgress).toHaveLength(3)
150
+ expect(inProgress[0]).toMatchObject({
151
+ awardId: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
152
+ progressPercentage: 75,
153
+ completedAt: null
154
+ })
155
+ })
156
+
157
+ test('in-progress awards include award definition details', async () => {
158
+ const inProgress = await getInProgressAwards()
159
+
160
+ expect(inProgress[0]).toMatchObject({
161
+ awardTitle: 'Enrolling w/ Kickoff, has product GC (EC)',
162
+ badge: expect.stringContaining('cdn.sanity.io'),
163
+ brand: 'pianote'
164
+ })
165
+ })
166
+
167
+ test('in-progress awards are sorted by progress percentage (highest first)', async () => {
168
+ const inProgress = await getInProgressAwards()
169
+
170
+ expect(inProgress[0].progressPercentage).toBe(75)
171
+ expect(inProgress[1].progressPercentage).toBe(50)
172
+ expect(inProgress[2].progressPercentage).toBe(20)
173
+ })
174
+ })
175
+
176
+ describe('Scenario: User with mixed completed and in-progress awards', () => {
177
+ beforeEach(() => {
178
+ db.userAwardProgress.getAll.mockResolvedValue({
179
+ data: [
180
+ {
181
+ award_id: '0238b1e5-ebee-42b3-9390-91467d113575',
182
+ progress_percentage: 100,
183
+ completed_at: Math.floor(Date.now() / 1000) - 86400,
184
+ completion_data: {
185
+ content_title: 'Adrian Guided Course Test',
186
+ completed_at: new Date(Date.now() - 86400 * 1000).toISOString(),
187
+ days_user_practiced: 14,
188
+ practice_minutes: 180
189
+ }
190
+ },
191
+ {
192
+ award_id: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
193
+ progress_percentage: 60,
194
+ completed_at: null,
195
+ completion_data: null
196
+ },
197
+ {
198
+ award_id: '361f3034-c6c9-45f7-bbfb-0d58dbe14411',
199
+ progress_percentage: 100,
200
+ completed_at: Math.floor(Date.now() / 1000) - 86400 * 10,
201
+ completion_data: {
202
+ content_title: 'Learn To Play The Drums',
203
+ completed_at: new Date(Date.now() - 86400 * 10 * 1000).toISOString(),
204
+ days_user_practiced: 60,
205
+ practice_minutes: 1200
206
+ }
207
+ }
208
+ ]
209
+ })
210
+ })
211
+
212
+ test('getCompletedAwards only returns completed awards', async () => {
213
+ const completed = await getCompletedAwards()
214
+
215
+ expect(completed).toHaveLength(2)
216
+ expect(completed.every(a => a.progressPercentage === 100)).toBe(true)
217
+ expect(completed.every(a => a.completedAt !== null)).toBe(true)
218
+ })
219
+
220
+ test('getInProgressAwards only returns in-progress awards', async () => {
221
+ const inProgress = await getInProgressAwards()
222
+
223
+ expect(inProgress).toHaveLength(1)
224
+ expect(inProgress[0].progressPercentage).toBe(60)
225
+ expect(inProgress[0].completedAt).toBeNull()
226
+ })
227
+
228
+ test('calculates correct statistics', async () => {
229
+ const stats = await getAwardStatistics()
230
+
231
+ expect(stats).toMatchObject({
232
+ totalAvailable: mockAwardDefinitions.length,
233
+ completed: 2,
234
+ inProgress: 1,
235
+ notStarted: mockAwardDefinitions.length - 3,
236
+ completionPercentage: expect.any(Number)
237
+ })
238
+
239
+ expect(stats.completionPercentage).toBeCloseTo((2 / mockAwardDefinitions.length) * 100, 1)
240
+ })
241
+ })
242
+
243
+ describe('Scenario: Brand-specific award filtering', () => {
244
+ beforeEach(() => {
245
+ db.userAwardProgress.getAll.mockResolvedValue({
246
+ data: [
247
+ {
248
+ award_id: '0238b1e5-ebee-42b3-9390-91467d113575',
249
+ progress_percentage: 100,
250
+ completed_at: Math.floor(Date.now() / 1000),
251
+ completion_data: {
252
+ content_title: 'Adrian Guided Course Test',
253
+ completed_at: new Date().toISOString(),
254
+ days_user_practiced: 14,
255
+ practice_minutes: 180
256
+ }
257
+ },
258
+ {
259
+ award_id: '0f49cb6a-1b23-4628-968e-15df02ffad7f',
260
+ progress_percentage: 100,
261
+ completed_at: Math.floor(Date.now() / 1000),
262
+ completion_data: {
263
+ content_title: 'Enrolling w/ Kickoff, has product GC (EC)',
264
+ completed_at: new Date().toISOString(),
265
+ days_user_practiced: 60,
266
+ practice_minutes: 1200
267
+ }
268
+ }
269
+ ]
270
+ })
271
+ })
272
+
273
+ test('filters completed awards by brand', async () => {
274
+ const drumeoAwards = await getCompletedAwards('drumeo')
275
+
276
+ expect(drumeoAwards).toHaveLength(1)
277
+ expect(drumeoAwards.every(a => a.brand === 'drumeo')).toBe(true)
278
+ })
279
+
280
+ test('returns empty array for brand with no awards', async () => {
281
+ const singeoAwards = await getCompletedAwards('singeo')
282
+
283
+ expect(singeoAwards).toHaveLength(0)
284
+ })
285
+
286
+ test('calculates brand-specific statistics including completed count', async () => {
287
+ const drumeoStats = await getAwardStatistics('drumeo')
288
+ const drumeoAwardsCount = mockAwardDefinitions.filter(a => a.brand === 'drumeo').length
289
+
290
+ expect(drumeoStats.totalAvailable).toBe(drumeoAwardsCount)
291
+ expect(drumeoStats.completed).toBe(1)
292
+
293
+ const pianoteStats = await getAwardStatistics('pianote')
294
+ const pianoteAwardsCount = mockAwardDefinitions.filter(a => a.brand === 'pianote').length
295
+
296
+ expect(pianoteStats.totalAvailable).toBe(pianoteAwardsCount)
297
+ expect(pianoteStats.completed).toBe(1)
298
+ })
299
+ })
300
+
301
+ describe('Scenario: New user with no awards', () => {
302
+ beforeEach(() => {
303
+ db.userAwardProgress.getAll.mockResolvedValue({
304
+ data: []
305
+ })
306
+ })
307
+
308
+ test('getCompletedAwards returns empty array', async () => {
309
+ const completed = await getCompletedAwards()
310
+
311
+ expect(completed).toHaveLength(0)
312
+ })
313
+
314
+ test('getInProgressAwards returns empty array', async () => {
315
+ const inProgress = await getInProgressAwards()
316
+
317
+ expect(inProgress).toHaveLength(0)
318
+ })
319
+
320
+ test('statistics show all awards as not started', async () => {
321
+ const stats = await getAwardStatistics()
322
+
323
+ expect(stats).toMatchObject({
324
+ totalAvailable: mockAwardDefinitions.length,
325
+ completed: 0,
326
+ inProgress: 0,
327
+ notStarted: mockAwardDefinitions.length,
328
+ completionPercentage: 0
329
+ })
330
+ })
331
+ })
332
+
333
+ describe('Scenario: Award collection pagination', () => {
334
+ beforeEach(() => {
335
+ const availableAwardIds = mockAwardDefinitions.map(a => a._id)
336
+ const manyAwards = availableAwardIds.slice(0, Math.min(20, availableAwardIds.length)).map((id, i) => ({
337
+ award_id: id,
338
+ progress_percentage: 100,
339
+ completed_at: Math.floor(Date.now() / 1000) - 86400 * i,
340
+ completion_data: {
341
+ content_title: `Course ${i}`,
342
+ completed_at: new Date(Date.now() - 86400 * i * 1000).toISOString(),
343
+ days_user_practiced: 10,
344
+ practice_minutes: 100
345
+ }
346
+ }))
347
+
348
+ db.userAwardProgress.getAll.mockResolvedValue({
349
+ data: manyAwards
350
+ })
351
+ })
352
+
353
+ test('supports limit parameter for completed awards', async () => {
354
+ const completed = await getCompletedAwards(null, { limit: 5 })
355
+
356
+ expect(completed.length).toBeLessThanOrEqual(5)
357
+ })
358
+
359
+ test('supports offset parameter for completed awards', async () => {
360
+ const firstPage = await getCompletedAwards(null, { limit: 3, offset: 0 })
361
+ const secondPage = await getCompletedAwards(null, { limit: 3, offset: 3 })
362
+
363
+ expect(firstPage).toHaveLength(3)
364
+ expect(secondPage).toHaveLength(3)
365
+ expect(firstPage[0].awardId).not.toBe(secondPage[0].awardId)
366
+ })
367
+ })
368
+
369
+ describe('Scenario: Award with missing definition', () => {
370
+ beforeEach(() => {
371
+ db.userAwardProgress.getAll.mockResolvedValue({
372
+ data: [
373
+ {
374
+ award_id: '0238b1e5-ebee-42b3-9390-91467d113575',
375
+ progress_percentage: 100,
376
+ completed_at: Math.floor(Date.now() / 1000),
377
+ completion_data: {
378
+ content_title: 'Adrian Guided Course Test',
379
+ completed_at: new Date().toISOString(),
380
+ days_user_practiced: 14,
381
+ practice_minutes: 180
382
+ }
383
+ },
384
+ {
385
+ award_id: 'non-existent-award-id',
386
+ progress_percentage: 100,
387
+ completed_at: Math.floor(Date.now() / 1000),
388
+ completion_data: {
389
+ content_title: 'Deleted Course',
390
+ completed_at: new Date().toISOString(),
391
+ days_user_practiced: 5,
392
+ practice_minutes: 50
393
+ }
394
+ }
395
+ ]
396
+ })
397
+ })
398
+
399
+ test('filters out awards with missing definitions', async () => {
400
+ const completed = await getCompletedAwards()
401
+
402
+ expect(completed).toHaveLength(1)
403
+ expect(completed[0].awardId).toBe('0238b1e5-ebee-42b3-9390-91467d113575')
404
+ })
405
+
406
+ test('statistics only count awards with valid definitions', async () => {
407
+ const stats = await getAwardStatistics()
408
+
409
+ expect(stats.completed).toBe(1)
410
+ })
411
+ })
412
+ })
@@ -0,0 +1,118 @@
1
+ import { mockAwardDefinitions, getAwardByContentId } from '../mockData/award-definitions'
2
+ import { setupDefaultMocks, setupAwardEventListeners } from './helpers'
3
+ import { COLLECTION_TYPE, emitLearningPathProgress, emitAlaCarteProgress, waitForDebounce } from './helpers/progress-emitter'
4
+ import { mockCollectionAwareCompletion } from './helpers/completion-mock'
5
+
6
+ jest.mock('../../src/services/sanity', () => ({
7
+ default: { fetch: jest.fn() },
8
+ fetchSanity: jest.fn()
9
+ }))
10
+
11
+ jest.mock('../../src/services/sync/repository-proxy', () => {
12
+ const mockFns = {
13
+ contentProgress: {
14
+ getOneProgressByContentId: jest.fn(),
15
+ getSomeProgressByContentIds: jest.fn(),
16
+ queryOne: jest.fn(),
17
+ queryAll: jest.fn()
18
+ },
19
+ practices: {
20
+ sumPracticeMinutesForContent: jest.fn()
21
+ },
22
+ userAwardProgress: {
23
+ hasCompletedAward: jest.fn(),
24
+ recordAwardProgress: jest.fn(),
25
+ getByAwardId: jest.fn()
26
+ }
27
+ }
28
+ return { default: mockFns, ...mockFns }
29
+ })
30
+
31
+ import sanityClient, { fetchSanity } from '../../src/services/sanity'
32
+ import db from '../../src/services/sync/repository-proxy'
33
+ import { awardDefinitions } from '../../src/services/awards/internal/award-definitions'
34
+ import { awardEvents } from '../../src/services/awards/internal/award-events'
35
+ import { contentProgressObserver } from '../../src/services/awards/internal/content-progress-observer'
36
+
37
+ describe('Duplicate Award Prevention', () => {
38
+ let listeners
39
+
40
+ beforeEach(async () => {
41
+ jest.clearAllMocks()
42
+ awardEvents.removeAllListeners()
43
+
44
+ sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
45
+ setupDefaultMocks(db, fetchSanity)
46
+
47
+ await awardDefinitions.refresh()
48
+
49
+ listeners = setupAwardEventListeners(awardEvents)
50
+
51
+ await contentProgressObserver.start()
52
+ })
53
+
54
+ afterEach(() => {
55
+ awardEvents.removeAllListeners()
56
+ awardDefinitions.clear()
57
+ contentProgressObserver.stop()
58
+ })
59
+
60
+ // Shared child 418003 exists in both:
61
+ // - Skill pack (418000): child_ids [418001, 418002, 418003]
62
+ // - Learning path (418010): child_ids [418003, 418004, 418005]
63
+ const lpAward = getAwardByContentId(418010)
64
+ const skillPackAward = getAwardByContentId(418000)
65
+ const sharedChild = 418003
66
+
67
+ test('completing shared lesson within LP context only grants LP award', async () => {
68
+ mockCollectionAwareCompletion(db, {
69
+ [`${sharedChild}:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true,
70
+ [`418004:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true,
71
+ [`418005:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true
72
+ })
73
+
74
+ emitLearningPathProgress(sharedChild, lpAward.content_id)
75
+ await waitForDebounce()
76
+
77
+ const grantedAwardIds = listeners.granted.mock.calls.map(call => call[0].awardId)
78
+
79
+ expect(grantedAwardIds).toContain(lpAward._id)
80
+ expect(grantedAwardIds).not.toContain(skillPackAward._id)
81
+ })
82
+
83
+ test('completing shared lesson a la carte triggers both awards', async () => {
84
+ mockCollectionAwareCompletion(db, {
85
+ '418001': true,
86
+ '418002': true,
87
+ '418003': true,
88
+ '418004': true,
89
+ '418005': true,
90
+ [`418003:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true,
91
+ [`418004:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true,
92
+ [`418005:${COLLECTION_TYPE.LEARNING_PATH}:${lpAward.content_id}`]: true
93
+ })
94
+
95
+ emitAlaCarteProgress(sharedChild)
96
+ await waitForDebounce()
97
+
98
+ const grantedAwardIds = listeners.granted.mock.calls.map(call => call[0].awardId)
99
+
100
+ expect(grantedAwardIds).toContain(lpAward._id)
101
+ expect(grantedAwardIds).toContain(skillPackAward._id)
102
+ })
103
+
104
+ test('completing lesson in wrong LP collection does not grant award', async () => {
105
+ const wrongCollectionId = 999999
106
+
107
+ mockCollectionAwareCompletion(db, {
108
+ [`418003:${COLLECTION_TYPE.LEARNING_PATH}:${wrongCollectionId}`]: true,
109
+ [`418004:${COLLECTION_TYPE.LEARNING_PATH}:${wrongCollectionId}`]: true,
110
+ [`418005:${COLLECTION_TYPE.LEARNING_PATH}:${wrongCollectionId}`]: true
111
+ })
112
+
113
+ emitLearningPathProgress(418003, wrongCollectionId)
114
+ await waitForDebounce()
115
+
116
+ expect(listeners.granted).not.toHaveBeenCalled()
117
+ })
118
+ })
@@ -0,0 +1,54 @@
1
+ export const mockCompletionStates = (db, completedIds = [], collection = null) => {
2
+ db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds, requestedCollection) => {
3
+ const collectionMatches = !collection || (
4
+ requestedCollection?.type === collection.type &&
5
+ requestedCollection?.id === collection.id
6
+ )
7
+
8
+ const completedRecords = contentIds
9
+ .filter(id => completedIds.includes(id) && collectionMatches)
10
+ .map(id => ({
11
+ content_id: id,
12
+ state: 'completed',
13
+ created_at: Math.floor(Date.now() / 1000)
14
+ }))
15
+
16
+ return Promise.resolve({ data: completedRecords })
17
+ })
18
+ }
19
+
20
+ export const mockAllCompleted = (db) => {
21
+ db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds) => {
22
+ const completedRecords = contentIds.map(id => ({
23
+ content_id: id,
24
+ state: 'completed',
25
+ created_at: Math.floor(Date.now() / 1000)
26
+ }))
27
+
28
+ return Promise.resolve({ data: completedRecords })
29
+ })
30
+ }
31
+
32
+ export const mockNoneCompleted = (db) => {
33
+ db.contentProgress.getSomeProgressByContentIds.mockResolvedValue({ data: [] })
34
+ }
35
+
36
+ export const mockCollectionAwareCompletion = (db, completionMap) => {
37
+ db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds, collection) => {
38
+ const completedRecords = contentIds
39
+ .filter(id => {
40
+ const key = collection
41
+ ? `${id}:${collection.type}:${collection.id}`
42
+ : `${id}`
43
+ const nullKey = `${id}`
44
+ return completionMap[key] || (!collection && completionMap[nullKey])
45
+ })
46
+ .map(id => ({
47
+ content_id: id,
48
+ state: 'completed',
49
+ created_at: Math.floor(Date.now() / 1000)
50
+ }))
51
+
52
+ return Promise.resolve({ data: completedRecords })
53
+ })
54
+ }
@@ -0,0 +1,3 @@
1
+ export * from './mock-setup'
2
+ export * from './progress-emitter'
3
+ export * from './completion-mock'
@@ -0,0 +1,69 @@
1
+ import { mockAwardDefinitions } from '../../mockData/award-definitions'
2
+
3
+ export const createRepositoryProxyMock = () => ({
4
+ contentProgress: {
5
+ getOneProgressByContentId: jest.fn(),
6
+ getSomeProgressByContentIds: jest.fn(),
7
+ queryOne: jest.fn(),
8
+ queryAll: jest.fn()
9
+ },
10
+ practices: {
11
+ sumPracticeMinutesForContent: jest.fn()
12
+ },
13
+ userAwardProgress: {
14
+ hasCompletedAward: jest.fn(),
15
+ recordAwardProgress: jest.fn(),
16
+ getByAwardId: jest.fn(),
17
+ getAll: jest.fn(),
18
+ getAwardsForContent: jest.fn()
19
+ }
20
+ })
21
+
22
+ export const setupDefaultMocks = (db, fetchSanity, options = {}) => {
23
+ const {
24
+ definitions = mockAwardDefinitions,
25
+ practiceMinutes = 200,
26
+ hasCompleted = false
27
+ } = options
28
+
29
+ fetchSanity.mockResolvedValue(definitions)
30
+
31
+ db.practices.sumPracticeMinutesForContent.mockResolvedValue(practiceMinutes)
32
+ db.userAwardProgress.hasCompletedAward.mockResolvedValue(hasCompleted)
33
+ db.userAwardProgress.recordAwardProgress.mockResolvedValue({ data: {}, status: 'synced' })
34
+
35
+ const defaultTimestamp = Math.floor(Date.now() / 1000)
36
+
37
+ db.contentProgress.getOneProgressByContentId.mockResolvedValue({
38
+ data: { state: 'completed', created_at: defaultTimestamp }
39
+ })
40
+
41
+ db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds) => {
42
+ const records = contentIds.map(id => ({
43
+ content_id: id,
44
+ state: 'completed',
45
+ created_at: defaultTimestamp - 86400 * 10
46
+ }))
47
+ return Promise.resolve({ data: records })
48
+ })
49
+
50
+ db.contentProgress.queryOne.mockResolvedValue({
51
+ data: { state: 'completed', created_at: defaultTimestamp }
52
+ })
53
+
54
+ db.contentProgress.queryAll.mockResolvedValue({
55
+ data: [{ created_at: defaultTimestamp - 86400 * 10 }]
56
+ })
57
+ }
58
+
59
+ export const setupAwardEventListeners = (awardEvents) => {
60
+ const listeners = {
61
+ progress: jest.fn(),
62
+ granted: jest.fn()
63
+ }
64
+
65
+ awardEvents.on('awardProgress', listeners.progress)
66
+ awardEvents.on('awardGranted', listeners.granted)
67
+
68
+ return listeners
69
+ }
@@ -0,0 +1,39 @@
1
+ import { emitProgressSaved } from '../../../src/services/progress-events'
2
+ import { COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
3
+
4
+ export { COLLECTION_TYPE }
5
+
6
+ export const emitProgress = ({
7
+ contentId,
8
+ collectionType = null,
9
+ collectionId = null,
10
+ progressPercent = 100,
11
+ userId = 123
12
+ }) => {
13
+ emitProgressSaved({
14
+ userId,
15
+ contentId,
16
+ progressPercent,
17
+ progressStatus: progressPercent === 100 ? 'completed' : 'started',
18
+ bubble: true,
19
+ collectionType,
20
+ collectionId,
21
+ resumeTimeSeconds: null,
22
+ timestamp: Date.now()
23
+ })
24
+ }
25
+
26
+ export const emitLearningPathProgress = (contentId, learningPathId, progressPercent = 100) => {
27
+ emitProgress({
28
+ contentId,
29
+ collectionType: COLLECTION_TYPE.LEARNING_PATH,
30
+ collectionId: learningPathId,
31
+ progressPercent
32
+ })
33
+ }
34
+
35
+ export const emitAlaCarteProgress = (contentId, progressPercent = 100) => {
36
+ emitProgress({ contentId, progressPercent })
37
+ }
38
+
39
+ export const waitForDebounce = (ms = 100) => new Promise(resolve => setTimeout(resolve, ms))