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,243 +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, mockNoneCompleted } 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 Progress Calculation', () => {
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, { practiceMinutes: 120 })
47
-
48
- db.contentProgress.getSomeProgressByContentIds.mockResolvedValue({
49
- data: [{ created_at: Math.floor(Date.now() / 1000) - 86400 * 5 }]
50
- })
51
-
52
- await awardDefinitions.refresh()
53
-
54
- listeners = setupAwardEventListeners(awardEvents)
55
- })
56
-
57
- afterEach(() => {
58
- awardEvents.removeAllListeners()
59
- awardDefinitions.clear()
60
- })
61
-
62
- describe('Single lesson course (content_id: 416446, child_ids: [416448])', () => {
63
- const award = getAwardByContentId(416446)
64
- const childId = 416448
65
-
66
- test('grants award at 100% when the single lesson is completed', async () => {
67
- mockAllCompleted(db)
68
-
69
- await awardManager.onContentCompleted(childId)
70
-
71
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
72
- award._id,
73
- 100,
74
- expect.objectContaining({
75
- completedAt: expect.any(String),
76
- completionData: expect.objectContaining({
77
- content_title: award.content_title,
78
- days_user_practiced: expect.any(Number),
79
- practice_minutes: expect.any(Number)
80
- }),
81
- progressData: {
82
- completedLessonIds: [childId],
83
- totalLessons: 1,
84
- completedCount: 1
85
- },
86
- immediate: true
87
- })
88
- )
89
- expect(listeners.granted).toHaveBeenCalledTimes(1)
90
- })
91
-
92
- test('records 0% progress when the lesson is not completed', async () => {
93
- mockNoneCompleted(db)
94
-
95
- await awardManager.onContentCompleted(childId)
96
-
97
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
98
- award._id,
99
- 0,
100
- expect.objectContaining({
101
- progressData: {
102
- completedLessonIds: [],
103
- totalLessons: 1,
104
- completedCount: 0
105
- }
106
- })
107
- )
108
- expect(listeners.granted).not.toHaveBeenCalled()
109
- })
110
- })
111
-
112
- describe('10-lesson course (content_id: 416450, child_ids: 10 lessons)', () => {
113
- const award = getAwardByContentId(416450)
114
- const childIds = award.child_ids
115
-
116
- test('calculates 50% progress when 5 of 10 lessons are completed', async () => {
117
- mockCompletionStates(db, childIds.slice(0, 5))
118
-
119
- await awardManager.onContentCompleted(childIds[0])
120
-
121
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
122
- award._id,
123
- 50,
124
- expect.objectContaining({
125
- progressData: expect.objectContaining({
126
- totalLessons: 10,
127
- completedCount: 5
128
- })
129
- })
130
- )
131
- })
132
-
133
- test('calculates 90% progress when 9 of 10 lessons are completed', async () => {
134
- mockCompletionStates(db, childIds.slice(0, 9))
135
-
136
- await awardManager.onContentCompleted(childIds[0])
137
-
138
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
139
- award._id,
140
- 90,
141
- expect.objectContaining({
142
- progressData: expect.objectContaining({
143
- totalLessons: 10,
144
- completedCount: 9
145
- })
146
- })
147
- )
148
- expect(listeners.granted).not.toHaveBeenCalled()
149
- })
150
-
151
- test('grants award when all 10 lessons are completed', async () => {
152
- mockAllCompleted(db)
153
-
154
- await awardManager.onContentCompleted(childIds[0])
155
-
156
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
157
- award._id,
158
- 100,
159
- expect.objectContaining({
160
- completedAt: expect.any(String),
161
- progressData: expect.objectContaining({
162
- totalLessons: 10,
163
- completedCount: 10
164
- }),
165
- immediate: true
166
- })
167
- )
168
- expect(listeners.granted).toHaveBeenCalledTimes(1)
169
- })
170
- })
171
-
172
- describe('22-lesson learning path (content_id: 417140)', () => {
173
- const award = getAwardByContentId(417140)
174
- const childIds = award.child_ids
175
-
176
- test('calculates correct percentage for partial completion (11/22 = 50%)', async () => {
177
- mockCompletionStates(db, childIds.slice(0, 11))
178
-
179
- await awardManager.onContentCompleted(childIds[0])
180
-
181
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
182
- award._id,
183
- 50,
184
- expect.objectContaining({
185
- progressData: expect.objectContaining({
186
- totalLessons: 22,
187
- completedCount: 11
188
- })
189
- })
190
- )
191
- })
192
-
193
- test('does not grant award when 21 of 22 lessons completed (95%)', async () => {
194
- mockCompletionStates(db, childIds.slice(0, 21))
195
-
196
- await awardManager.onContentCompleted(childIds[0])
197
-
198
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
199
- award._id,
200
- 95,
201
- expect.objectContaining({
202
- progressData: expect.objectContaining({
203
- totalLessons: 22,
204
- completedCount: 21
205
- })
206
- })
207
- )
208
- expect(listeners.granted).not.toHaveBeenCalled()
209
- })
210
-
211
- test('grants award with correct popup message when all 22 lessons completed', async () => {
212
- mockAllCompleted(db)
213
-
214
- await awardManager.onContentCompleted(childIds[0])
215
-
216
- expect(listeners.granted).toHaveBeenCalledTimes(1)
217
-
218
- const payload = listeners.granted.mock.calls[0][0]
219
- expect(payload.awardId).toBe(award._id)
220
- expect(payload.popupMessage).toContain(award.content_title)
221
- expect(payload.popupMessage).toContain('120 minutes')
222
- expect(payload.completionData).toEqual(expect.objectContaining({
223
- content_title: award.content_title,
224
- practice_minutes: 120
225
- }))
226
- })
227
- })
228
-
229
- describe('Award already completed', () => {
230
- const award = getAwardByContentId(416446)
231
- const childId = 416448
232
-
233
- test('skips evaluation when award is already completed', async () => {
234
- db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
235
- mockAllCompleted(db)
236
-
237
- await awardManager.onContentCompleted(childId)
238
-
239
- expect(db.userAwardProgress.recordAwardProgress).not.toHaveBeenCalled()
240
- expect(listeners.granted).not.toHaveBeenCalled()
241
- })
242
- })
243
- })
@@ -1,327 +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 { emitProgressSaved } from '../../../src/services/progress-events.js'
4
- import { mockAwardDefinitions, getAwardByContentId } from '../../mockData/award-definitions.js'
5
- import { setupDefaultMocks, setupAwardEventListeners } from './helpers/index'
6
- import { mockCompletionStates } from './helpers/completion-mock'
7
- import { COLLECTION_TYPE, waitForDebounce } from './helpers/progress-emitter'
8
-
9
- jest.mock('../../../src/services/sanity.js', () => ({
10
- ...jest.requireActual('../../../src/services/sanity'),
11
- default: { fetch: jest.fn() },
12
- fetchSanity: jest.fn()
13
- }))
14
-
15
- jest.mock('../../../src/services/sync/repository-proxy.ts', () => {
16
- const mockFns = {
17
- contentProgress: {
18
- getOneProgressByContentId: jest.fn(),
19
- getSomeProgressByContentIds: jest.fn(),
20
- queryOne: jest.fn(),
21
- queryAll: jest.fn()
22
- },
23
- practices: {
24
- sumPracticeMinutesForContent: jest.fn()
25
- },
26
- userAwardProgress: {
27
- hasCompletedAward: jest.fn(),
28
- recordAwardProgress: jest.fn(),
29
- getByAwardId: jest.fn()
30
- }
31
- }
32
- return { default: mockFns, ...mockFns }
33
- })
34
-
35
- import sanityClient, { fetchSanity } from '../../../src/services/sanity.js'
36
- import db_ from '../../../src/services/sync/repository-proxy'
37
- const db = db_ as any
38
- import { awardDefinitions } from '../../../src/services/awards/internal/award-definitions.js'
39
-
40
- describe('Award Observer Integration - E2E Scenarios', () => {
41
- let listeners
42
-
43
- const emitProgressWithCollection = (contentId, collectionType, collectionId, progressPercent = 100) => {
44
- emitProgressSaved({
45
- userId: 123,
46
- contentId,
47
- progressPercent,
48
- progressStatus: progressPercent === 100 ? 'completed' : 'started',
49
- bubble: true,
50
- collectionType,
51
- collectionId,
52
- resumeTimeSeconds: null,
53
- timestamp: Date.now()
54
- })
55
- }
56
-
57
- const emitAlaCarteProgress = (contentId, progressPercent = 100) => {
58
- emitProgressWithCollection(contentId, null, null, progressPercent)
59
- }
60
-
61
- beforeEach(async () => {
62
- jest.clearAllMocks()
63
- awardEvents.removeAllListeners()
64
-
65
- sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
66
- setupDefaultMocks(db, fetchSanity)
67
-
68
- await awardDefinitions.refresh()
69
-
70
- listeners = setupAwardEventListeners(awardEvents)
71
-
72
- await contentProgressObserver.start()
73
- })
74
-
75
- afterEach(() => {
76
- awardEvents.removeAllListeners()
77
- awardDefinitions.clear()
78
- contentProgressObserver.stop()
79
- })
80
-
81
- describe('Scenario: Observer setup and initialization', () => {
82
- test('starts observing progress events', async () => {
83
- expect(contentProgressObserver.isObserving).toBe(true)
84
- })
85
-
86
- test('loads award definitions on start', async () => {
87
- expect(fetchSanity).toHaveBeenCalled()
88
- expect(contentProgressObserver.allChildIds.size).toBeGreaterThan(0)
89
- })
90
-
91
- test('includes content_id in allChildIds Set', async () => {
92
- const lpAward = getAwardByContentId(417140)
93
- const gcAward = getAwardByContentId(416446)
94
-
95
- expect(contentProgressObserver.allChildIds.has(lpAward.content_id)).toBe(true)
96
- expect(contentProgressObserver.allChildIds.has(gcAward.content_id)).toBe(true)
97
- })
98
-
99
- test('includes all child_ids in allChildIds Set', async () => {
100
- const award = getAwardByContentId(416446)
101
-
102
- award.child_ids.forEach(childId => {
103
- expect(contentProgressObserver.allChildIds.has(childId)).toBe(true)
104
- })
105
- })
106
-
107
- test('returns cleanup function', async () => {
108
- const cleanup = await contentProgressObserver.start()
109
- expect(typeof cleanup).toBe('function')
110
- })
111
-
112
- test('does not start twice', async () => {
113
- const firstCallCount = fetchSanity.mock.calls.length
114
- await contentProgressObserver.start()
115
- expect(fetchSanity.mock.calls.length).toBe(firstCallCount)
116
- })
117
- })
118
-
119
- describe('Scenario: Progress update triggers award progress event', () => {
120
- const testAward = getAwardByContentId(417049)
121
-
122
- beforeEach(() => {
123
- mockCompletionStates(db, [417045])
124
- })
125
-
126
- test('emits awardProgress event when lesson completed', async () => {
127
- emitAlaCarteProgress(417045)
128
- await waitForDebounce()
129
-
130
- expect(listeners.progress).toHaveBeenCalled()
131
- const payload = listeners.progress.mock.calls[0][0]
132
- expect(payload).toHaveProperty('awardId', testAward._id)
133
- expect(payload).toHaveProperty('progressPercentage')
134
- })
135
-
136
- test('calculates correct progress percentage', async () => {
137
- mockCompletionStates(db, [417045, 417046])
138
-
139
- emitAlaCarteProgress(417045)
140
- await waitForDebounce()
141
-
142
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
143
- testAward._id,
144
- 50,
145
- expect.objectContaining({
146
- progressData: expect.any(Object)
147
- })
148
- )
149
- })
150
- })
151
-
152
- describe('Scenario: Progress update triggers award granted event', () => {
153
- const testAward = getAwardByContentId(417049)
154
-
155
- test('emits awardGranted event when all lessons completed', async () => {
156
- emitAlaCarteProgress(417045)
157
- await waitForDebounce()
158
-
159
- expect(listeners.granted).toHaveBeenCalledTimes(1)
160
- const payload = listeners.granted.mock.calls[0][0]
161
- expect(payload).toHaveProperty('awardId', testAward._id)
162
- expect(payload).toHaveProperty('completionData')
163
- expect(payload).toHaveProperty('popupMessage')
164
- })
165
-
166
- test('includes completion data in granted event', async () => {
167
- emitAlaCarteProgress(417045)
168
- await waitForDebounce()
169
-
170
- const payload = listeners.granted.mock.calls[0][0]
171
- expect(payload.completionData).toMatchObject({
172
- content_title: expect.any(String),
173
- completed_at: expect.any(String),
174
- days_user_practiced: expect.any(Number),
175
- practice_minutes: 200
176
- })
177
- })
178
- })
179
-
180
- describe('Scenario: Multiple lessons update rapidly (debouncing)', () => {
181
- const testAward = getAwardByContentId(416464)
182
- const nonKickoffLessons = [
183
- 416467, 416468, 416469, 416470, 416471, 416472, 416473,
184
- 416474, 416475, 416476, 416477, 416478, 416479, 416480, 416481,
185
- 416482, 416483, 416484, 416485, 416486, 416487, 416488, 416489
186
- ]
187
-
188
- beforeEach(() => {
189
- mockCompletionStates(db, nonKickoffLessons.slice(0, 12))
190
- })
191
-
192
- test('debounces multiple rapid updates to same course', async () => {
193
- emitAlaCarteProgress(416467)
194
- emitAlaCarteProgress(416468)
195
- emitAlaCarteProgress(416469)
196
-
197
- await waitForDebounce()
198
-
199
- expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledTimes(1)
200
- })
201
- })
202
-
203
- describe('Scenario: Progress for lessons without awards', () => {
204
- test('ignores lessons not associated with awards', async () => {
205
- emitAlaCarteProgress(999999)
206
- await waitForDebounce()
207
-
208
- expect(listeners.progress).not.toHaveBeenCalled()
209
- expect(listeners.granted).not.toHaveBeenCalled()
210
- })
211
- })
212
-
213
- describe('Scenario: Non-completed progress status', () => {
214
- beforeEach(() => {
215
- mockCompletionStates(db, [])
216
- })
217
-
218
- test('does not trigger award granted when lessons not completed in DB', async () => {
219
- emitAlaCarteProgress(417045, 50)
220
- await waitForDebounce()
221
-
222
- expect(listeners.granted).not.toHaveBeenCalled()
223
- })
224
-
225
- test('started progress state in DB does not grant award', async () => {
226
- emitProgressSaved({
227
- userId: 123,
228
- contentId: 417045,
229
- progressPercent: 75,
230
- progressStatus: 'started',
231
- bubble: true,
232
- collectionType: null,
233
- collectionId: null,
234
- resumeTimeSeconds: null,
235
- timestamp: Date.now()
236
- })
237
- await waitForDebounce()
238
-
239
- expect(listeners.granted).not.toHaveBeenCalled()
240
- })
241
- })
242
-
243
- describe('Scenario: Excluded content progress', () => {
244
- test('excluded content (not in child_ids) does not trigger award progress', async () => {
245
- emitAlaCarteProgress(416447)
246
- await waitForDebounce()
247
-
248
- expect(db.userAwardProgress.recordAwardProgress).not.toHaveBeenCalled()
249
- })
250
- })
251
-
252
- describe('Scenario: Already completed award', () => {
253
- beforeEach(() => {
254
- db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
255
- })
256
-
257
- test('does not re-grant already completed award', async () => {
258
- emitAlaCarteProgress(417045)
259
- await waitForDebounce()
260
-
261
- expect(listeners.granted).not.toHaveBeenCalled()
262
- expect(db.userAwardProgress.recordAwardProgress).not.toHaveBeenCalledWith(
263
- expect.anything(),
264
- 100,
265
- expect.objectContaining({ immediate: true })
266
- )
267
- })
268
- })
269
-
270
- describe('Scenario: Observer cleanup', () => {
271
- test('removes event listeners on stop', () => {
272
- contentProgressObserver.stop()
273
- expect(contentProgressObserver.isObserving).toBe(false)
274
- })
275
-
276
- test('clears debounce timers on stop', async () => {
277
- emitAlaCarteProgress(417045)
278
- contentProgressObserver.stop()
279
-
280
- expect(contentProgressObserver.debounceTimers.size).toBe(0)
281
- })
282
-
283
- test('clears allChildIds on stop', async () => {
284
- contentProgressObserver.stop()
285
-
286
- expect(contentProgressObserver.allChildIds.size).toBe(0)
287
- })
288
- })
289
-
290
- describe('Scenario: Learning path award events', () => {
291
- const learningPathAward = getAwardByContentId(417140)
292
-
293
- test('learning path award has correct popup message', async () => {
294
- emitProgressWithCollection(417105, COLLECTION_TYPE.LEARNING_PATH, 417140)
295
- await waitForDebounce()
296
-
297
- const payload = listeners.granted.mock.calls[0][0]
298
- expect(payload.popupMessage).toContain('Learn To Play The Drums')
299
- expect(payload.popupMessage).toContain('minutes')
300
- expect(payload.popupMessage).toContain('days')
301
- })
302
- })
303
-
304
- describe('Scenario: Multiple courses progressing simultaneously', () => {
305
- const testAward1 = getAwardByContentId(416446)
306
- const testAward2 = getAwardByContentId(417049)
307
-
308
- beforeEach(() => {
309
- mockCompletionStates(db, [])
310
- })
311
-
312
- test('tracks progress for multiple courses independently', async () => {
313
- emitAlaCarteProgress(416448)
314
- await waitForDebounce()
315
-
316
- emitAlaCarteProgress(417045)
317
- await waitForDebounce()
318
-
319
- expect(listeners.progress).toHaveBeenCalledTimes(2)
320
-
321
- const calls = listeners.progress.mock.calls
322
- const awardIds = calls.map(call => call[0].awardId)
323
- expect(awardIds).toContain(testAward1._id)
324
- expect(awardIds).toContain(testAward2._id)
325
- })
326
- })
327
- })