musora-content-services 2.167.2 → 2.167.4

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 (140) hide show
  1. package/.agent/decisions/2026-07-20-mu2-1724-songs-lessons-counts.md +33 -0
  2. package/.agent/decisions/2026-07-20-query-builder-tostring.md +37 -0
  3. package/.idea/codeStyles/Project.xml +61 -0
  4. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  5. package/.idea/modules.xml +8 -0
  6. package/.idea/musora-content-services.iml +8 -0
  7. package/.idea/php.xml +19 -0
  8. package/.idea/prettier.xml +6 -0
  9. package/.idea/vcs.xml +7 -0
  10. package/.yarn/install-state.gz +0 -0
  11. package/CHANGELOG.md +10 -0
  12. package/package.json +2 -1
  13. package/src/contentTypeConfig.js +1 -0
  14. package/src/lib/sanity/query.ts +5 -0
  15. package/src/services/config.js +15 -0
  16. package/src/services/content/artist.ts +1 -3
  17. package/src/services/content/counts.ts +47 -0
  18. package/src/services/content/genre.ts +1 -3
  19. package/src/services/content/instructor.ts +1 -3
  20. package/src/services/multi-user-accounts/multi-user-accounts.ts +1 -0
  21. package/src/services/userActivity.js +2 -0
  22. package/src/version-info.js +1 -0
  23. package/.claude/settings.local.json +0 -53
  24. package/test/initializeTests.js +0 -60
  25. package/test/integration/awards/award-exclusion-handling.test.ts +0 -275
  26. package/test/integration/awards/internal/image-utils.test.ts +0 -86
  27. package/test/integration/content-progress/__mocks__/mocks.ts +0 -104
  28. package/test/integration/content-progress/contentProgress.test.ts +0 -335
  29. package/test/integration/content-progress/e2eOfflineProgress.test.ts +0 -352
  30. package/test/integration/content-progress/e2eProgress.test.ts +0 -612
  31. package/test/integration/content-progress/getters.test.ts +0 -334
  32. package/test/integration/content-progress/helpers.test.ts +0 -263
  33. package/test/integration/content-progress/offlineContentProgress.test.ts +0 -226
  34. package/test/integration/forums.test.ts +0 -209
  35. package/test/integration/infrastructure/FetchRequestExecutor.test.ts +0 -88
  36. package/test/integration/initializeTestDB.ts +0 -80
  37. package/test/integration/learning-paths.test.ts +0 -854
  38. package/test/integration/notifications.test.ts +0 -178
  39. package/test/integration/progress-row/content-card.test.ts +0 -283
  40. package/test/integration/sync/adapters/idb-errors.test.ts +0 -144
  41. package/test/integration/sync/adapters/sqlite-errors.test.ts +0 -173
  42. package/test/integration/sync/fetch.test.ts +0 -224
  43. package/test/integration/sync/repositories/content-likes.test.ts +0 -99
  44. package/test/integration/sync/repositories/practices.test.ts +0 -179
  45. package/test/integration/sync/repositories/progress.test.ts +0 -245
  46. package/test/integration/sync/repositories/user-award-progress.static.test.ts +0 -68
  47. package/test/integration/sync/repositories/user-award-progress.test.ts +0 -387
  48. package/test/integration/sync/store/cross-user-protection.test.ts +0 -217
  49. package/test/integration/sync/store/store-idb.test.ts +0 -180
  50. package/test/integration/sync/store/store.test.ts +0 -361
  51. package/test/integration/user/permissions.test.js +0 -20
  52. package/test/integration/userActivity.test.ts +0 -102
  53. package/test/live/README.md +0 -29
  54. package/test/live/content.test.js +0 -116
  55. package/test/localStorageMock.js +0 -21
  56. package/test/log.js +0 -5
  57. package/test/mockData/award-definitions.js +0 -171
  58. package/test/mockData/mockData_fetchByRailContentIds_one_content.json +0 -35
  59. package/test/mockData/mockData_progress_content.json +0 -182
  60. package/test/mockData/mockData_sanity_progress_content.json +0 -1451
  61. package/test/mockData/mockData_user_practices.json +0 -9
  62. package/test/setupConsole.js +0 -6
  63. package/test/setupNetworkGuard.js +0 -3
  64. package/test/setupTimers.js +0 -13
  65. package/test/unit/HttpClient.test.ts +0 -267
  66. package/test/unit/awards/award-alacarte-observer.test.ts +0 -199
  67. package/test/unit/awards/award-auto-refresh.test.ts +0 -84
  68. package/test/unit/awards/award-calculations.test.ts +0 -34
  69. package/test/unit/awards/award-callbacks.test.ts +0 -144
  70. package/test/unit/awards/award-certificate-display.test.ts +0 -330
  71. package/test/unit/awards/award-collection-edge-cases.test.ts +0 -212
  72. package/test/unit/awards/award-collection-filtering.test.ts +0 -287
  73. package/test/unit/awards/award-completion-flow.test.ts +0 -306
  74. package/test/unit/awards/award-multi-lesson.test.ts +0 -243
  75. package/test/unit/awards/award-observer-integration.test.ts +0 -327
  76. package/test/unit/awards/award-query-messages.test.ts +0 -448
  77. package/test/unit/awards/award-user-collection.test.ts +0 -416
  78. package/test/unit/awards/duplicate-prevention.test.ts +0 -120
  79. package/test/unit/awards/helpers/completion-mock.ts +0 -56
  80. package/test/unit/awards/helpers/index.ts +0 -3
  81. package/test/unit/awards/helpers/mock-setup.ts +0 -79
  82. package/test/unit/awards/helpers/progress-emitter.ts +0 -45
  83. package/test/unit/awards/message-generator.test.ts +0 -162
  84. package/test/unit/content-progress/bubbleTrickle.test.ts +0 -322
  85. package/test/unit/content-progress/helpers.test.ts +0 -329
  86. package/test/unit/content-progress/navigateTo.test.ts +0 -392
  87. package/test/unit/contentLikes.test.ts +0 -62
  88. package/test/unit/contentMetaData.test.ts +0 -58
  89. package/test/unit/dataContext.test.ts +0 -38
  90. package/test/unit/dateUtils.test.ts +0 -188
  91. package/test/unit/endScreen.test.js +0 -712
  92. package/test/unit/eventsAPI.test.ts +0 -159
  93. package/test/unit/imageSRCBuilder.test.ts +0 -37
  94. package/test/unit/imageSRCVerify.test.ts +0 -155
  95. package/test/unit/infrastructure/DefaultHeaderProvider.test.ts +0 -39
  96. package/test/unit/infrastructure/sanity/ContentClient.test.ts +0 -168
  97. package/test/unit/infrastructure/sanity/DefaultConfigProvider.test.ts +0 -93
  98. package/test/unit/infrastructure/sanity/FetchQueryExecutor.test.ts +0 -174
  99. package/test/unit/infrastructure/sanity/SanityClient.test.ts +0 -140
  100. package/test/unit/lib/__snapshots__/filter.test.ts.snap +0 -5
  101. package/test/unit/lib/filter.test.ts +0 -1154
  102. package/test/unit/lib/lastUpdated.test.ts +0 -21
  103. package/test/unit/lib/query.test.ts +0 -1469
  104. package/test/unit/lib/sanity/decorators/base.test.ts +0 -368
  105. package/test/unit/lib/sanity/decorators/navigate-to.test.ts +0 -361
  106. package/test/unit/lib/sanity/decorators/need-access.test.ts +0 -89
  107. package/test/unit/lib/sanity/decorators/need-lifetime-upgrade.test.ts +0 -85
  108. package/test/unit/lib/sanity/decorators/page-type.test.ts +0 -81
  109. package/test/unit/playerSettings.test.ts +0 -73
  110. package/test/unit/progress-row/content-card.test.ts +0 -314
  111. package/test/unit/progress-row/playlist-card.test.ts +0 -104
  112. package/test/unit/progressRows.test.ts +0 -243
  113. package/test/unit/recommendations.test.js +0 -47
  114. package/test/unit/sanityQueryService.test.ts +0 -200
  115. package/test/unit/search.test.ts +0 -44
  116. package/test/unit/sentry.test.ts +0 -62
  117. package/test/unit/services/progress-internal/bubble.test.ts +0 -279
  118. package/test/unit/services/progress.test.ts +0 -396
  119. package/test/unit/streakMessage.test.ts +0 -251
  120. package/test/unit/sync/context.test.ts +0 -51
  121. package/test/unit/sync/effects/logout-warning.test.ts +0 -158
  122. package/test/unit/sync/effects/push-failure-notification.test.ts +0 -196
  123. package/test/unit/sync/errors/sync-errors.test.ts +0 -106
  124. package/test/unit/sync/errors/validators.test.ts +0 -61
  125. package/test/unit/sync/helpers/TestModel.ts +0 -44
  126. package/test/unit/sync/helpers/index.ts +0 -172
  127. package/test/unit/sync/manager.test.ts +0 -303
  128. package/test/unit/sync/models/user-award-progress.test.ts +0 -82
  129. package/test/unit/sync/resolver.test.ts +0 -229
  130. package/test/unit/sync/retry.test.ts +0 -314
  131. package/test/unit/sync/run-scope.test.ts +0 -23
  132. package/test/unit/sync/stale-record-cleanup.test.ts +0 -107
  133. package/test/unit/sync/store/push-coalescer.test.ts +0 -156
  134. package/test/unit/sync/store-configs.test.ts +0 -37
  135. package/test/unit/sync/telemetry/sync-telemetry.test.ts +0 -118
  136. package/test/unit/sync/utils/event-emitter.test.ts +0 -64
  137. package/test/unit/sync/utils/throttle.test.ts +0 -245
  138. package/test/unit/url-builder.test.ts +0 -72
  139. package/test/unit/user/account.test.ts +0 -66
  140. package/test/unit/user/permissions.test.js +0 -191
@@ -1,287 +0,0 @@
1
- import { contentProgressObserver } from '../../../src/services/awards/internal/content-progress-observer.js'
2
- import { awardEvents } from '../../../src/services/awards/internal/award-events.js'
3
- import { mockAwardDefinitions } from '../../mockData/award-definitions.js'
4
- import { setupDefaultMocks, setupAwardEventListeners } from './helpers/index'
5
- import { COLLECTION_TYPE, emitProgress, waitForDebounce } from './helpers/progress-emitter'
6
-
7
- jest.mock('../../../src/services/sanity.js', () => ({
8
- ...jest.requireActual('../../../src/services/sanity'),
9
- default: { fetch: jest.fn() },
10
- fetchSanity: jest.fn()
11
- }))
12
-
13
- jest.mock('../../../src/services/sync/repository-proxy.ts', () => {
14
- const mockFns = {
15
- contentProgress: {
16
- getOneProgressByContentId: jest.fn(),
17
- getSomeProgressByContentIds: jest.fn(),
18
- queryOne: jest.fn(),
19
- queryAll: jest.fn()
20
- },
21
- practices: {
22
- sumPracticeMinutesForContent: jest.fn()
23
- },
24
- userAwardProgress: {
25
- hasCompletedAward: jest.fn(),
26
- recordAwardProgress: jest.fn(),
27
- getByAwardId: jest.fn()
28
- }
29
- }
30
- return { default: mockFns, ...mockFns }
31
- })
32
-
33
- import sanityClient, { fetchSanity } from '../../../src/services/sanity.js'
34
- import db_ from '../../../src/services/sync/repository-proxy'
35
- const db = db_ as any
36
- import { awardDefinitions } from '../../../src/services/awards/internal/award-definitions.js'
37
-
38
- describe('Award Collection Filtering', () => {
39
- let listeners
40
-
41
- beforeEach(async () => {
42
- jest.clearAllMocks()
43
- awardEvents.removeAllListeners()
44
-
45
- sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
46
- setupDefaultMocks(db, fetchSanity)
47
-
48
- await awardDefinitions.refresh()
49
-
50
- listeners = setupAwardEventListeners(awardEvents)
51
-
52
- await contentProgressObserver.start()
53
- })
54
-
55
- afterEach(() => {
56
- awardEvents.removeAllListeners()
57
- awardDefinitions.clear()
58
- contentProgressObserver.stop()
59
- })
60
-
61
- describe('Collection type matching', () => {
62
- test('child in learning-path-v2 triggers only learning-path-v2 award', async () => {
63
- emitProgress({
64
- contentId: 418004,
65
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
66
- collectionId: 418010
67
- })
68
- await waitForDebounce()
69
-
70
- expect(listeners.granted).toHaveBeenCalledWith(
71
- expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
72
- )
73
- })
74
-
75
- test('child in skill-pack triggers only skill-pack award', async () => {
76
- emitProgress({
77
- contentId: 418001,
78
- collectionType: 'skill-pack',
79
- collectionId: 418000
80
- })
81
- await waitForDebounce()
82
-
83
- expect(listeners.granted).toHaveBeenCalledWith(
84
- expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
85
- )
86
- })
87
-
88
- test('child in course triggers course award (a la carte)', async () => {
89
- emitProgress({
90
- contentId: 416448
91
- })
92
- await waitForDebounce()
93
-
94
- expect(listeners.granted).toHaveBeenCalledWith(
95
- expect.objectContaining({ awardId: '0238b1e5-ebee-42b3-9390-91467d113575' })
96
- )
97
- })
98
- })
99
-
100
- describe('Collection ID matching', () => {
101
- test('same content_type but different content_id does not trigger', async () => {
102
- emitProgress({
103
- contentId: 418004,
104
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
105
- collectionId: 999999
106
- })
107
- await waitForDebounce()
108
-
109
- expect(listeners.granted).not.toHaveBeenCalled()
110
- })
111
-
112
- test('both type and ID must match to trigger award', async () => {
113
- emitProgress({
114
- contentId: 418001,
115
- collectionType: 'skill-pack',
116
- collectionId: 418000
117
- })
118
- await waitForDebounce()
119
-
120
- const call = listeners.granted.mock.calls[0]
121
- expect(call).toBeDefined()
122
- expect(call[0].definition.content_type).toBe('skill-pack')
123
- expect(call[0].definition.content_id).toBe(418000)
124
- })
125
- })
126
-
127
- describe('Overlapping children', () => {
128
- test('shared child in non-LP context triggers all matching awards (a la carte)', async () => {
129
- emitProgress({
130
- contentId: 418003,
131
- collectionType: 'skill-pack',
132
- collectionId: 418000
133
- })
134
- await waitForDebounce()
135
-
136
- expect(listeners.granted).toHaveBeenCalledWith(
137
- expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
138
- )
139
- expect(listeners.granted).toHaveBeenCalledWith(
140
- expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
141
- )
142
- })
143
-
144
- test('shared child in learning-path-v2 context triggers only learning-path award', async () => {
145
- emitProgress({
146
- contentId: 418003,
147
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
148
- collectionId: 418010
149
- })
150
- await waitForDebounce()
151
-
152
- expect(listeners.granted).toHaveBeenCalledWith(
153
- expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
154
- )
155
- expect(listeners.granted).not.toHaveBeenCalledWith(
156
- expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
157
- )
158
- })
159
-
160
- test('non-LP context triggers all awards, LP context triggers only matching LP award', async () => {
161
- emitProgress({
162
- contentId: 418003,
163
- collectionType: 'skill-pack',
164
- collectionId: 418000
165
- })
166
- await waitForDebounce()
167
-
168
- expect(listeners.granted).toHaveBeenCalledTimes(2)
169
-
170
- listeners.granted.mockClear()
171
- db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
172
-
173
- emitProgress({
174
- contentId: 418003,
175
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
176
- collectionId: 418010
177
- })
178
- await waitForDebounce()
179
-
180
- expect(listeners.granted).not.toHaveBeenCalled()
181
- })
182
- })
183
-
184
- describe('A la carte collection context', () => {
185
- test('collectionType null and collectionId null triggers awards (a la carte)', async () => {
186
- emitProgress({ contentId: 418001 })
187
- await waitForDebounce()
188
-
189
- expect(listeners.granted).toHaveBeenCalled()
190
- })
191
-
192
- test('collectionType present but collectionId null triggers awards (a la carte)', async () => {
193
- emitProgress({
194
- contentId: 418001,
195
- collectionType: 'skill-pack',
196
- collectionId: null
197
- })
198
- await waitForDebounce()
199
-
200
- expect(listeners.granted).toHaveBeenCalled()
201
- })
202
-
203
- test('collectionType null but collectionId present triggers awards (a la carte)', async () => {
204
- emitProgress({
205
- contentId: 418001,
206
- collectionType: null,
207
- collectionId: 418000
208
- })
209
- await waitForDebounce()
210
-
211
- expect(listeners.granted).toHaveBeenCalled()
212
- })
213
- })
214
-
215
- describe('Direct string comparison', () => {
216
- test('learning-path-v2 matches learning-path-v2 exactly', async () => {
217
- emitProgress({
218
- contentId: 418004,
219
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
220
- collectionId: 418010
221
- })
222
- await waitForDebounce()
223
-
224
- expect(listeners.granted).toHaveBeenCalledWith(
225
- expect.objectContaining({
226
- definition: expect.objectContaining({
227
- content_type: 'learning-path-v2'
228
- })
229
- })
230
- )
231
- })
232
-
233
- test('skill-pack matches skill-pack exactly', async () => {
234
- emitProgress({
235
- contentId: 418001,
236
- collectionType: 'skill-pack',
237
- collectionId: 418000
238
- })
239
- await waitForDebounce()
240
-
241
- expect(listeners.granted).toHaveBeenCalledWith(
242
- expect.objectContaining({
243
- definition: expect.objectContaining({
244
- content_type: 'skill-pack'
245
- })
246
- })
247
- )
248
- })
249
- })
250
-
251
- describe('Non-matching collection context', () => {
252
- test('LP child in wrong LP collection ID ignores award', async () => {
253
- emitProgress({
254
- contentId: 418004,
255
- collectionType: COLLECTION_TYPE.LEARNING_PATH,
256
- collectionId: 999999
257
- })
258
- await waitForDebounce()
259
-
260
- expect(listeners.granted).not.toHaveBeenCalled()
261
- expect(listeners.progress).not.toHaveBeenCalled()
262
- })
263
-
264
- test('non-LP child with wrong collection still triggers (a la carte)', async () => {
265
- emitProgress({
266
- contentId: 418001,
267
- collectionType: 'skill-pack',
268
- collectionId: 418010
269
- })
270
- await waitForDebounce()
271
-
272
- expect(listeners.granted).toHaveBeenCalled()
273
- })
274
-
275
- test('child not in any award ignores completely', async () => {
276
- emitProgress({
277
- contentId: 999999,
278
- collectionType: 'skill-pack',
279
- collectionId: 418000
280
- })
281
- await waitForDebounce()
282
-
283
- expect(listeners.granted).not.toHaveBeenCalled()
284
- expect(listeners.progress).not.toHaveBeenCalled()
285
- })
286
- })
287
- })
@@ -1,306 +0,0 @@
1
- import { awardManager } from '../../../src/services/awards/internal/award-manager.js'
2
- import { awardEvents } from '../../../src/services/awards/internal/award-events.js'
3
- import { mockAwardDefinitions, getAwardByContentId } from '../../mockData/award-definitions.js'
4
- import { setupDefaultMocks, setupAwardEventListeners } from './helpers/index'
5
- import { mockCompletionStates, mockAllCompleted } from './helpers/completion-mock'
6
-
7
- jest.mock('../../../src/services/sanity.js', () => ({
8
- ...jest.requireActual('../../../src/services/sanity'),
9
- default: { fetch: jest.fn() },
10
- fetchSanity: jest.fn()
11
- }))
12
-
13
- jest.mock('../../../src/services/sync/repository-proxy.ts', () => {
14
- const mockFns = {
15
- contentProgress: {
16
- getOneProgressByContentId: jest.fn(),
17
- getSomeProgressByContentIds: jest.fn(),
18
- queryOne: jest.fn(),
19
- queryAll: jest.fn()
20
- },
21
- practices: {
22
- sumPracticeMinutesForContent: jest.fn()
23
- },
24
- userAwardProgress: {
25
- hasCompletedAward: jest.fn(),
26
- recordAwardProgress: jest.fn(),
27
- getByAwardId: jest.fn()
28
- }
29
- }
30
- return { default: mockFns, ...mockFns }
31
- })
32
-
33
- import sanityClient, { fetchSanity } from '../../../src/services/sanity.js'
34
- import db_ from '../../../src/services/sync/repository-proxy'
35
- const db = db_ as any
36
- import { awardDefinitions } from '../../../src/services/awards/internal/award-definitions.js'
37
-
38
- describe('Award Completion Flow - E2E Scenarios', () => {
39
- let listeners
40
- const testAward = getAwardByContentId(416446)
41
- const courseId = 416446
42
-
43
- beforeEach(async () => {
44
- jest.clearAllMocks()
45
- awardEvents.removeAllListeners()
46
-
47
- sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
48
- setupDefaultMocks(db, fetchSanity, { practiceMinutes: 180 })
49
-
50
- await awardDefinitions.refresh()
51
-
52
- listeners = setupAwardEventListeners(awardEvents)
53
- })
54
-
55
- afterEach(() => {
56
- awardEvents.removeAllListeners()
57
- awardDefinitions.clear()
58
- })
59
-
60
- describe('Scenario: User completes all lessons in course', () => {
61
- beforeEach(() => {
62
- mockAllCompleted(db)
63
- })
64
-
65
- test('award is granted with 100% progress', async () => {
66
- await awardManager.onContentCompleted(courseId)
67
-
68
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
69
- testAward._id,
70
- 100,
71
- expect.objectContaining({
72
- completedAt: expect.any(String),
73
- completionData: expect.objectContaining({
74
- content_title: expect.any(String),
75
- days_user_practiced: expect.any(Number),
76
- practice_minutes: 180
77
- }),
78
- progressData: expect.any(Object),
79
- immediate: true
80
- })
81
- )
82
-
83
- expect(listeners.granted).toHaveBeenCalledTimes(1)
84
- })
85
-
86
- test('awardGranted event contains complete payload', async () => {
87
- await awardManager.onContentCompleted(courseId)
88
-
89
- const payload = listeners.granted.mock.calls[0][0]
90
-
91
- expect(payload).toMatchObject({
92
- awardId: testAward._id,
93
- definition: expect.objectContaining({
94
- name: testAward.name,
95
- badge: testAward.badge,
96
- award: testAward.award,
97
- content_type: expect.any(String)
98
- }),
99
- completionData: expect.objectContaining({
100
- content_title: expect.any(String),
101
- days_user_practiced: expect.any(Number),
102
- practice_minutes: 180
103
- }),
104
- popupMessage: expect.stringContaining('Adrian Guided Course Test'),
105
- timestamp: expect.any(Number)
106
- })
107
- })
108
-
109
- test('popup message contains correct practice data', async () => {
110
- await awardManager.onContentCompleted(courseId)
111
-
112
- const payload = listeners.granted.mock.calls[0][0]
113
-
114
- expect(payload.popupMessage).toContain('180 minutes')
115
- expect(payload.popupMessage).toContain('Adrian Guided Course Test')
116
- })
117
-
118
- test('award is immediately synced to backend', async () => {
119
- await awardManager.onContentCompleted(courseId)
120
-
121
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
122
- testAward._id,
123
- 100,
124
- expect.objectContaining({
125
- completedAt: expect.any(String),
126
- immediate: true
127
- })
128
- )
129
- })
130
-
131
- test('multiple event listeners all receive notification', async () => {
132
- const listener1 = jest.fn()
133
- const listener2 = jest.fn()
134
- const listener3 = jest.fn()
135
-
136
- awardEvents.on('awardGranted', listener1)
137
- awardEvents.on('awardGranted', listener2)
138
- awardEvents.on('awardGranted', listener3)
139
-
140
- await awardManager.onContentCompleted(courseId)
141
-
142
- expect(listener1).toHaveBeenCalledTimes(1)
143
- expect(listener2).toHaveBeenCalledTimes(1)
144
- expect(listener3).toHaveBeenCalledTimes(1)
145
-
146
- const payload1 = listener1.mock.calls[0][0]
147
- const payload2 = listener2.mock.calls[0][0]
148
- expect(payload1.awardId).toBe(payload2.awardId)
149
- })
150
- })
151
-
152
- describe('Scenario: Parent course completed but children incomplete', () => {
153
- const multiLessonAward = getAwardByContentId(417049)
154
- const parentCourseId = 417049
155
-
156
- test('does not grant award when parent completed but only 2 of 4 children completed', async () => {
157
- mockCompletionStates(db, [417045, 417046])
158
-
159
- await awardManager.onContentCompleted(parentCourseId)
160
-
161
- expect(listeners.granted).not.toHaveBeenCalled()
162
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
163
- multiLessonAward._id,
164
- 50,
165
- expect.objectContaining({
166
- progressData: expect.any(Object)
167
- })
168
- )
169
- })
170
-
171
- test('emits awardProgress event with partial progress when parent completed', async () => {
172
- mockCompletionStates(db, [417045, 417046])
173
-
174
- await awardManager.onContentCompleted(parentCourseId)
175
-
176
- expect(listeners.progress).toHaveBeenCalledWith(
177
- expect.objectContaining({
178
- awardId: multiLessonAward._id,
179
- progressPercentage: 50,
180
- timestamp: expect.any(Number)
181
- })
182
- )
183
- })
184
- })
185
-
186
- describe('Scenario: Content has no associated award', () => {
187
- test('completes gracefully without errors', async () => {
188
- const nonExistentCourseId = 999999
189
-
190
- await expect(
191
- awardManager.onContentCompleted(nonExistentCourseId)
192
- ).resolves.not.toThrow()
193
-
194
- expect(listeners.granted).not.toHaveBeenCalled()
195
- })
196
- })
197
-
198
- describe('Scenario: User already earned the award', () => {
199
- beforeEach(() => {
200
- db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
201
- mockAllCompleted(db)
202
- })
203
-
204
- test('does not grant award again', async () => {
205
- await awardManager.onContentCompleted(courseId)
206
-
207
- expect(listeners.granted).not.toHaveBeenCalled()
208
- })
209
-
210
- test('does not update progress', async () => {
211
- await awardManager.onContentCompleted(courseId)
212
-
213
- expect(db.userAwardProgress.recordAwardProgress).not.toHaveBeenCalled()
214
- })
215
- })
216
-
217
- describe('Scenario: Race condition - eligibility check fails but progress reaches 100%', () => {
218
- const multiLessonAward = getAwardByContentId(417049)
219
- const parentCourseId = 417049
220
-
221
- test('grants award with completedAt when updateAwardProgress calculates 100%', async () => {
222
- let callCount = 0
223
-
224
- db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds) => {
225
- callCount++
226
-
227
- if (callCount === 1) {
228
- const partialRecords = contentIds
229
- .filter(id => [417045, 417046, 417047].includes(id))
230
- .map(id => ({
231
- content_id: id,
232
- state: 'completed',
233
- created_at: Math.floor(Date.now() / 1000)
234
- }))
235
- return Promise.resolve({ data: partialRecords })
236
- }
237
-
238
- const allRecords = contentIds.map(id => ({
239
- content_id: id,
240
- state: 'completed',
241
- created_at: Math.floor(Date.now() / 1000)
242
- }))
243
- return Promise.resolve({ data: allRecords })
244
- })
245
-
246
- await awardManager.onContentCompleted(parentCourseId)
247
-
248
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
249
- multiLessonAward._id,
250
- 100,
251
- expect.objectContaining({
252
- completedAt: expect.any(String),
253
- completionData: expect.objectContaining({
254
- completed_at: expect.any(String)
255
- }),
256
- immediate: true
257
- })
258
- )
259
-
260
- expect(listeners.granted).toHaveBeenCalledTimes(1)
261
- expect(listeners.progress).not.toHaveBeenCalled()
262
- })
263
-
264
- test('emits awardGranted event when race condition triggers completion', async () => {
265
- let callCount = 0
266
-
267
- db.contentProgress.getSomeProgressByContentIds.mockImplementation((contentIds) => {
268
- callCount++
269
-
270
- if (callCount === 1) {
271
- const partialRecords = contentIds
272
- .filter(id => [417045, 417046].includes(id))
273
- .map(id => ({
274
- content_id: id,
275
- state: 'completed',
276
- created_at: Math.floor(Date.now() / 1000)
277
- }))
278
- return Promise.resolve({ data: partialRecords })
279
- }
280
-
281
- const allRecords = contentIds.map(id => ({
282
- content_id: id,
283
- state: 'completed',
284
- created_at: Math.floor(Date.now() / 1000)
285
- }))
286
- return Promise.resolve({ data: allRecords })
287
- })
288
-
289
- await awardManager.onContentCompleted(parentCourseId)
290
-
291
- expect(listeners.granted).toHaveBeenCalledTimes(1)
292
- expect(listeners.granted).toHaveBeenCalledWith(
293
- expect.objectContaining({
294
- awardId: multiLessonAward._id,
295
- definition: expect.objectContaining({
296
- name: multiLessonAward.name
297
- }),
298
- completionData: expect.objectContaining({
299
- completed_at: expect.any(String)
300
- }),
301
- timestamp: expect.any(Number)
302
- })
303
- )
304
- })
305
- })
306
- })