musora-content-services 2.167.1 → 2.167.3

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 (136) hide show
  1. package/.agent/decisions/2026-07-17-onboarding-statuses.md +21 -0
  2. package/.agent/decisions/2026-07-20-mu2-1724-songs-lessons-counts.md +33 -0
  3. package/.agent/decisions/2026-07-20-query-builder-tostring.md +37 -0
  4. package/CHANGELOG.md +15 -0
  5. package/package.json +2 -1
  6. package/src/contentTypeConfig.js +2 -0
  7. package/src/index.d.ts +2 -0
  8. package/src/index.js +2 -0
  9. package/src/lib/sanity/query.ts +5 -0
  10. package/src/services/config.js +15 -0
  11. package/src/services/content/artist.ts +1 -3
  12. package/src/services/content/counts.ts +47 -0
  13. package/src/services/content/genre.ts +1 -3
  14. package/src/services/content/instructor.ts +1 -3
  15. package/src/services/contentProgress.js +1 -1
  16. package/src/services/eventsAPI.js +49 -7
  17. package/src/services/user/onboarding.ts +22 -0
  18. package/src/services/userActivity.js +2 -0
  19. package/src/version-info.js +1 -0
  20. package/.claude/settings.local.json +0 -52
  21. package/test/initializeTests.js +0 -60
  22. package/test/integration/awards/award-exclusion-handling.test.ts +0 -275
  23. package/test/integration/awards/internal/image-utils.test.ts +0 -86
  24. package/test/integration/content-progress/__mocks__/mocks.ts +0 -104
  25. package/test/integration/content-progress/contentProgress.test.ts +0 -335
  26. package/test/integration/content-progress/e2eOfflineProgress.test.ts +0 -352
  27. package/test/integration/content-progress/e2eProgress.test.ts +0 -612
  28. package/test/integration/content-progress/getters.test.ts +0 -334
  29. package/test/integration/content-progress/helpers.test.ts +0 -263
  30. package/test/integration/content-progress/offlineContentProgress.test.ts +0 -226
  31. package/test/integration/forums.test.ts +0 -209
  32. package/test/integration/infrastructure/FetchRequestExecutor.test.ts +0 -88
  33. package/test/integration/initializeTestDB.ts +0 -80
  34. package/test/integration/learning-paths.test.ts +0 -854
  35. package/test/integration/notifications.test.ts +0 -178
  36. package/test/integration/progress-row/content-card.test.ts +0 -283
  37. package/test/integration/sync/adapters/idb-errors.test.ts +0 -144
  38. package/test/integration/sync/adapters/sqlite-errors.test.ts +0 -173
  39. package/test/integration/sync/fetch.test.ts +0 -224
  40. package/test/integration/sync/repositories/content-likes.test.ts +0 -99
  41. package/test/integration/sync/repositories/practices.test.ts +0 -179
  42. package/test/integration/sync/repositories/progress.test.ts +0 -245
  43. package/test/integration/sync/repositories/user-award-progress.static.test.ts +0 -68
  44. package/test/integration/sync/repositories/user-award-progress.test.ts +0 -387
  45. package/test/integration/sync/store/cross-user-protection.test.ts +0 -217
  46. package/test/integration/sync/store/store-idb.test.ts +0 -180
  47. package/test/integration/sync/store/store.test.ts +0 -361
  48. package/test/integration/user/permissions.test.js +0 -20
  49. package/test/integration/userActivity.test.ts +0 -102
  50. package/test/live/README.md +0 -29
  51. package/test/live/content.test.js +0 -116
  52. package/test/localStorageMock.js +0 -21
  53. package/test/log.js +0 -5
  54. package/test/mockData/award-definitions.js +0 -171
  55. package/test/mockData/mockData_fetchByRailContentIds_one_content.json +0 -35
  56. package/test/mockData/mockData_progress_content.json +0 -182
  57. package/test/mockData/mockData_sanity_progress_content.json +0 -1451
  58. package/test/mockData/mockData_user_practices.json +0 -9
  59. package/test/setupConsole.js +0 -6
  60. package/test/setupNetworkGuard.js +0 -3
  61. package/test/setupTimers.js +0 -13
  62. package/test/unit/HttpClient.test.ts +0 -267
  63. package/test/unit/awards/award-alacarte-observer.test.ts +0 -199
  64. package/test/unit/awards/award-auto-refresh.test.ts +0 -84
  65. package/test/unit/awards/award-calculations.test.ts +0 -34
  66. package/test/unit/awards/award-callbacks.test.ts +0 -144
  67. package/test/unit/awards/award-certificate-display.test.ts +0 -330
  68. package/test/unit/awards/award-collection-edge-cases.test.ts +0 -212
  69. package/test/unit/awards/award-collection-filtering.test.ts +0 -287
  70. package/test/unit/awards/award-completion-flow.test.ts +0 -306
  71. package/test/unit/awards/award-multi-lesson.test.ts +0 -243
  72. package/test/unit/awards/award-observer-integration.test.ts +0 -327
  73. package/test/unit/awards/award-query-messages.test.ts +0 -448
  74. package/test/unit/awards/award-user-collection.test.ts +0 -416
  75. package/test/unit/awards/duplicate-prevention.test.ts +0 -120
  76. package/test/unit/awards/helpers/completion-mock.ts +0 -56
  77. package/test/unit/awards/helpers/index.ts +0 -3
  78. package/test/unit/awards/helpers/mock-setup.ts +0 -79
  79. package/test/unit/awards/helpers/progress-emitter.ts +0 -45
  80. package/test/unit/awards/message-generator.test.ts +0 -162
  81. package/test/unit/content-progress/bubbleTrickle.test.ts +0 -322
  82. package/test/unit/content-progress/helpers.test.ts +0 -329
  83. package/test/unit/content-progress/navigateTo.test.ts +0 -381
  84. package/test/unit/contentLikes.test.ts +0 -62
  85. package/test/unit/contentMetaData.test.ts +0 -58
  86. package/test/unit/dataContext.test.ts +0 -38
  87. package/test/unit/dateUtils.test.ts +0 -188
  88. package/test/unit/endScreen.test.js +0 -712
  89. package/test/unit/imageSRCBuilder.test.ts +0 -37
  90. package/test/unit/imageSRCVerify.test.ts +0 -155
  91. package/test/unit/infrastructure/DefaultHeaderProvider.test.ts +0 -39
  92. package/test/unit/infrastructure/sanity/ContentClient.test.ts +0 -168
  93. package/test/unit/infrastructure/sanity/DefaultConfigProvider.test.ts +0 -93
  94. package/test/unit/infrastructure/sanity/FetchQueryExecutor.test.ts +0 -174
  95. package/test/unit/infrastructure/sanity/SanityClient.test.ts +0 -140
  96. package/test/unit/lib/__snapshots__/filter.test.ts.snap +0 -5
  97. package/test/unit/lib/filter.test.ts +0 -1154
  98. package/test/unit/lib/lastUpdated.test.ts +0 -21
  99. package/test/unit/lib/query.test.ts +0 -1469
  100. package/test/unit/lib/sanity/decorators/base.test.ts +0 -368
  101. package/test/unit/lib/sanity/decorators/navigate-to.test.ts +0 -361
  102. package/test/unit/lib/sanity/decorators/need-access.test.ts +0 -89
  103. package/test/unit/lib/sanity/decorators/need-lifetime-upgrade.test.ts +0 -85
  104. package/test/unit/lib/sanity/decorators/page-type.test.ts +0 -81
  105. package/test/unit/playerSettings.test.ts +0 -73
  106. package/test/unit/progress-row/content-card.test.ts +0 -314
  107. package/test/unit/progress-row/playlist-card.test.ts +0 -104
  108. package/test/unit/progressRows.test.ts +0 -243
  109. package/test/unit/recommendations.test.js +0 -47
  110. package/test/unit/sanityQueryService.test.ts +0 -200
  111. package/test/unit/search.test.ts +0 -44
  112. package/test/unit/sentry.test.ts +0 -62
  113. package/test/unit/services/progress-internal/bubble.test.ts +0 -279
  114. package/test/unit/services/progress.test.ts +0 -396
  115. package/test/unit/streakMessage.test.ts +0 -251
  116. package/test/unit/sync/context.test.ts +0 -51
  117. package/test/unit/sync/effects/logout-warning.test.ts +0 -158
  118. package/test/unit/sync/effects/push-failure-notification.test.ts +0 -196
  119. package/test/unit/sync/errors/sync-errors.test.ts +0 -106
  120. package/test/unit/sync/errors/validators.test.ts +0 -61
  121. package/test/unit/sync/helpers/TestModel.ts +0 -44
  122. package/test/unit/sync/helpers/index.ts +0 -172
  123. package/test/unit/sync/manager.test.ts +0 -303
  124. package/test/unit/sync/models/user-award-progress.test.ts +0 -82
  125. package/test/unit/sync/resolver.test.ts +0 -229
  126. package/test/unit/sync/retry.test.ts +0 -314
  127. package/test/unit/sync/run-scope.test.ts +0 -23
  128. package/test/unit/sync/stale-record-cleanup.test.ts +0 -107
  129. package/test/unit/sync/store/push-coalescer.test.ts +0 -156
  130. package/test/unit/sync/store-configs.test.ts +0 -37
  131. package/test/unit/sync/telemetry/sync-telemetry.test.ts +0 -118
  132. package/test/unit/sync/utils/event-emitter.test.ts +0 -64
  133. package/test/unit/sync/utils/throttle.test.ts +0 -245
  134. package/test/unit/url-builder.test.ts +0 -72
  135. package/test/unit/user/account.test.ts +0 -66
  136. package/test/unit/user/permissions.test.js +0 -191
@@ -1,335 +0,0 @@
1
- import { initializeTestDB } from '../initializeTestDB'
2
- import {
3
- _recordWatchSession,
4
- contentStatusCompleted,
5
- contentStatusCompletedMany,
6
- contentStatusReset,
7
- contentStatusStarted,
8
- flushWatchSession,
9
- getProgressState,
10
- recordWatchSession,
11
- resetStatus,
12
- saveContentProgress,
13
- setStartedOrCompletedStatus,
14
- setStartedOrCompletedStatusMany,
15
- trackProgress,
16
- } from '../../../src/services/contentProgress.js'
17
- import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
18
- import db from '../../../src/services/sync/repository-proxy'
19
-
20
- jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
21
- jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
22
- jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
23
- jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
24
-
25
- jest.mock('../../../src/services/userActivity', () => ({
26
- trackUserPractice: jest.fn().mockResolvedValue(undefined),
27
- }))
28
-
29
- const meta = { brand: 'drumeo', type: 'lesson', parent_id: 0 }
30
- const collectionSelf = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
31
-
32
- const ctx = initializeTestDB()
33
-
34
- // ─── contentStatusCompleted ───────────────────────────────────────────────────
35
-
36
- describe('contentStatusCompleted', () => {
37
- test('sets state to completed in DB', async () => {
38
- await contentStatusCompleted(500)
39
- expect(await getProgressState(500)).toBe('completed')
40
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
41
- })
42
-
43
- test('sets progress_percent to 100 in DB', async () => {
44
- await contentStatusCompleted(500)
45
- const record = await db.contentProgress.getOneProgressByContentId(500, null)
46
- expect(record.data?.progress_percent).toBe(100)
47
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
48
- })
49
-
50
- test('defaults to SELF collection when null is passed', async () => {
51
- await contentStatusCompleted(500, null)
52
- const record = await db.contentProgress.getOneProgressByContentId(500, null)
53
- expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
54
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
55
- })
56
- })
57
-
58
- // ─── contentStatusCompletedMany ───────────────────────────────────────────────
59
-
60
- describe('contentStatusCompletedMany', () => {
61
- test('sets all ids to completed state in DB', async () => {
62
- await contentStatusCompletedMany([501, 502, 503])
63
- expect(await getProgressState(501)).toBe('completed')
64
- expect(await getProgressState(502)).toBe('completed')
65
- expect(await getProgressState(503)).toBe('completed')
66
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
67
- })
68
-
69
- test('each record has progress_percent of 100', async () => {
70
- await contentStatusCompletedMany([501, 502])
71
- const r1 = await db.contentProgress.getOneProgressByContentId(501, null)
72
- const r2 = await db.contentProgress.getOneProgressByContentId(502, null)
73
- expect(r1.data?.progress_percent).toBe(100)
74
- expect(r2.data?.progress_percent).toBe(100)
75
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
76
- })
77
- })
78
-
79
- // ─── contentStatusStarted ─────────────────────────────────────────────────────
80
-
81
- describe('contentStatusStarted', () => {
82
- test('sets state to started in DB', async () => {
83
- await contentStatusStarted(600)
84
- expect(await getProgressState(600)).toBe('started')
85
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
86
- })
87
-
88
- test('sets progress_percent to 0 in DB', async () => {
89
- await contentStatusStarted(600)
90
- const record = await db.contentProgress.getOneProgressByContentId(600, null)
91
- expect(record.data?.progress_percent).toBe(0)
92
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
93
- })
94
- })
95
-
96
- // ─── contentStatusReset ───────────────────────────────────────────────────────
97
-
98
- describe('contentStatusReset', () => {
99
- test('removes record from DB so getProgressState returns empty string', async () => {
100
- await contentStatusCompleted(700)
101
- expect(await getProgressState(700)).toBe('completed')
102
- await contentStatusReset(700)
103
- expect(await getProgressState(700)).toBe('')
104
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
105
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
106
- })
107
- })
108
-
109
- // ─── saveContentProgress ──────────────────────────────────────────────────────
110
-
111
- describe('saveContentProgress', () => {
112
- test('progress higher than existing writes new value', async () => {
113
- await db.contentProgress.recordProgress(800, null, 30, meta, undefined, { skipPush: true })
114
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
115
- await saveContentProgress(800, collectionSelf, 60, null, { hierarchy, isOffline: true })
116
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
117
- expect(record.data?.progress_percent).toBe(60)
118
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
119
- })
120
-
121
- test('progress lower than existing is filtered — DB value unchanged', async () => {
122
- await db.contentProgress.recordProgress(800, null, 70, meta, undefined, { skipPush: true })
123
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
124
- await saveContentProgress(800, collectionSelf, 40, null, { hierarchy, isOffline: true })
125
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
126
- expect(record.data?.progress_percent).toBe(70)
127
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
128
- })
129
-
130
- test('progress equal to existing writes successfully', async () => {
131
- await db.contentProgress.recordProgress(800, null, 50, meta, undefined, { skipPush: true })
132
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
133
- await saveContentProgress(800, collectionSelf, 50, null, { hierarchy, isOffline: true })
134
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
135
- expect(record.data?.progress_percent).toBe(50)
136
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
137
- })
138
- })
139
-
140
- // ─── setStartedOrCompletedStatus ─────────────────────────────────────────────
141
-
142
- describe('setStartedOrCompletedStatus', () => {
143
- test('completed=true writes state=completed to DB', async () => {
144
- await setStartedOrCompletedStatus(900, collectionSelf, true)
145
- expect(await getProgressState(900)).toBe('completed')
146
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
147
- })
148
-
149
- test('completed=false writes state=started to DB', async () => {
150
- await setStartedOrCompletedStatus(900, collectionSelf, false)
151
- expect(await getProgressState(900)).toBe('started')
152
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
153
- })
154
-
155
- test('skipBubbleTrickle=true still writes the main record', async () => {
156
- await setStartedOrCompletedStatus(900, collectionSelf, true, { skipBubbleTrickle: true })
157
- expect(await getProgressState(900)).toBe('completed')
158
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
159
- })
160
- })
161
-
162
- // ─── setStartedOrCompletedStatusMany ─────────────────────────────────────────
163
-
164
- describe('setStartedOrCompletedStatusMany', () => {
165
- test('isCompleted=true sets all ids to completed in DB', async () => {
166
- await setStartedOrCompletedStatusMany([1001, 1002, 1003], collectionSelf, true)
167
- expect(await getProgressState(1001)).toBe('completed')
168
- expect(await getProgressState(1002)).toBe('completed')
169
- expect(await getProgressState(1003)).toBe('completed')
170
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
171
- })
172
-
173
- test('isCompleted=false sets all ids to started in DB', async () => {
174
- await setStartedOrCompletedStatusMany([1001, 1002], collectionSelf, false)
175
- expect(await getProgressState(1001)).toBe('started')
176
- expect(await getProgressState(1002)).toBe('started')
177
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
178
- })
179
- })
180
-
181
- // ─── resetStatus ─────────────────────────────────────────────────────────────
182
-
183
- describe('resetStatus', () => {
184
- test('removes record so getProgressState returns empty string', async () => {
185
- await db.contentProgress.recordProgress(1100, null, 80, meta, undefined, { skipPush: true })
186
- expect(await getProgressState(1100)).toBe('started')
187
- await resetStatus(1100, collectionSelf)
188
- expect(await getProgressState(1100)).toBe('')
189
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
190
- })
191
- })
192
-
193
- // ─── trackProgress ────────────────────────────────────────────────────────────
194
-
195
- describe('trackProgress', () => {
196
- test('50 of 200 seconds records progress ~25 to DB', async () => {
197
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
198
- await trackProgress(100, collectionSelf, 50, 200, false, true, hierarchy)
199
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
200
- expect(record.data?.progress_percent).toBe(25)
201
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
202
- })
203
-
204
- test('0 seconds clamps to progress 1 in DB', async () => {
205
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
206
- await trackProgress(100, collectionSelf, 0, 200, false, true, hierarchy)
207
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
208
- expect(record.data?.progress_percent).toBe(1)
209
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
210
- })
211
-
212
- test('full duration (200/200) clamps to 99 — never reaches 100 via track', async () => {
213
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
214
- await trackProgress(100, collectionSelf, 200, 200, false, true, hierarchy)
215
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
216
- expect(record.data?.progress_percent).toBe(99)
217
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
218
- })
219
- })
220
-
221
- // ─── recordWatchSession ───────────────────────────────────────────────────────
222
-
223
- describe('recordWatchSession', () => {
224
- const mockUserActivity = jest.requireMock('../../../src/services/userActivity')
225
-
226
- beforeEach(() => {
227
- jest.clearAllMocks()
228
- })
229
-
230
- test('writes progress to DB and calls trackUserPractice', async () => {
231
- await recordWatchSession(234, null, 200, 100, 30)
232
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
233
- expect(record.data?.progress_percent).toBe(50)
234
- expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(234, 30, expect.any(Object))
235
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
236
- })
237
-
238
- test('normalizes string contentId to number in DB', async () => {
239
- await recordWatchSession('234' as any, null, 200, 100, 30)
240
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
241
- expect(record.data?.content_id).toBe(234)
242
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
243
- })
244
- })
245
-
246
- // ─── _recordWatchSession ──────────────────────────────────────────────────────
247
-
248
- describe('_recordWatchSession', () => {
249
- const mockUserActivity = jest.requireMock('../../../src/services/userActivity')
250
-
251
- beforeEach(() => {
252
- jest.clearAllMocks()
253
- })
254
-
255
- test('writes progress to DB and calls trackUserPractice', async () => {
256
- const hierarchy = { metadata: { 234: meta }, parents: {}, children: {} }
257
- await _recordWatchSession(234, 200, 100, 30, { collection: null, isOffline: true, hierarchy })
258
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
259
- expect(record.data?.progress_percent).toBe(50)
260
- expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(234, 30, expect.any(Object))
261
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
262
- })
263
- })
264
-
265
- // ─── flushWatchSession ────────────────────────────────────────────────────────
266
-
267
- describe('flushWatchSession', () => {
268
- test('triggers push for both contentProgress and practices', async () => {
269
- await expect(flushWatchSession()).resolves.not.toThrow()
270
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('flush-watch-session')
271
- expect(ctx.pushSpies.practices).toHaveBeenCalledWith('flush-watch-session')
272
- })
273
- })
274
-
275
- // ─── E2E Scenarios ────────────────────────────────────────────────────────────
276
-
277
- describe('Scenario: User completes lesson a-la-carte', () => {
278
- test('state=completed and progress_percent=100 written to DB', async () => {
279
- await contentStatusCompleted(42001)
280
- const record = await db.contentProgress.getOneProgressByContentId(42001, null)
281
- expect(record.data?.state).toBe('completed')
282
- expect(record.data?.progress_percent).toBe(100)
283
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
284
- })
285
- })
286
-
287
- describe('Scenario: User starts lesson a-la-carte', () => {
288
- test('state=started and progress_percent=0 written to DB', async () => {
289
- await contentStatusStarted(42002)
290
- const record = await db.contentProgress.getOneProgressByContentId(42002, null)
291
- expect(record.data?.state).toBe('started')
292
- expect(record.data?.progress_percent).toBe(0)
293
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
294
- })
295
- })
296
-
297
- describe('Scenario: User resets lesson progress', () => {
298
- test('record removed from DB after reset', async () => {
299
- await contentStatusCompleted(42003)
300
- expect(await getProgressState(42003)).toBe('completed')
301
- await contentStatusReset(42003)
302
- expect(await getProgressState(42003)).toBe('')
303
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
304
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
305
- })
306
- })
307
-
308
- describe('Scenario: User watches video halfway through', () => {
309
- test('progress written and clamped correctly', async () => {
310
- await recordWatchSession(42004, null, 200, 100, 30)
311
- const record = await db.contentProgress.getOneProgressByContentId(42004, null)
312
- expect(record.data?.progress_percent).toBe(50)
313
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
314
- })
315
- })
316
-
317
- describe('Scenario: User watches to end — clamped at 99 (not 100)', () => {
318
- test('progress_percent is 99, not 100, after watching to the end', async () => {
319
- await recordWatchSession(42005, null, 200, 200, 60)
320
- const record = await db.contentProgress.getOneProgressByContentId(42005, null)
321
- expect(record.data?.progress_percent).toBe(99)
322
- expect(record.data?.state).toBe('started')
323
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
324
- })
325
- })
326
-
327
- describe('Scenario: Completing multiple lessons at once', () => {
328
- test('all lessons written as completed to DB', async () => {
329
- await contentStatusCompletedMany([50001, 50002, 50003])
330
- expect(await getProgressState(50001)).toBe('completed')
331
- expect(await getProgressState(50002)).toBe('completed')
332
- expect(await getProgressState(50003)).toBe('completed')
333
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
334
- })
335
- })
@@ -1,352 +0,0 @@
1
- import { initializeTestDB } from '../initializeTestDB'
2
- import {
3
- contentStatusCompletedManyOffline,
4
- contentStatusCompletedOffline,
5
- contentStatusResetOffline,
6
- contentStatusStartedOffline,
7
- recordWatchSessionOffline,
8
- } from '../../../src/services/offline/progress'
9
- import { flushWatchSession } from '../../../src/services/contentProgress.js'
10
- import {
11
- COLLECTION_ID_SELF,
12
- COLLECTION_TYPE,
13
- CollectionParameter,
14
- } from '../../../src/services/sync/models/ContentProgress'
15
- import db from '../../../src/services/sync/repository-proxy'
16
- import { clearHierarchies, HierarchyTreeNode } from './__mocks__/mocks'
17
-
18
- jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
19
- jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
20
- jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
21
- jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
22
-
23
- jest.mock('../../../src/services/userActivity', () => ({
24
- trackUserPractice: jest.fn().mockResolvedValue(undefined),
25
- }))
26
-
27
- const selfCollection = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
28
- const playlistCollection = { type: COLLECTION_TYPE.PLAYLIST, id: 123 }
29
- const lpCollection = { type: COLLECTION_TYPE.LEARNING_PATH, id: 999 }
30
-
31
- const testMetadata = { brand: 'test-brand', type: 'test-type', parent_id: 0 }
32
-
33
- const learningPathsMock = jest.requireMock('../../../src/services/content-org/learning-paths.ts')
34
- const sanityMock = jest.requireMock('../../../src/services/sanity.js')
35
-
36
- const ctx = initializeTestDB()
37
-
38
- beforeEach(() => {
39
- clearHierarchies()
40
- learningPathsMock.onLearningPathCompletedActions.mockClear()
41
- learningPathsMock.getDailySession.mockClear()
42
- sanityMock.getHierarchy.mockClear()
43
- sanityMock.getHierarchies.mockClear()
44
- sanityMock.getSanityDate.mockClear()
45
- })
46
-
47
- afterEach(() => {
48
- expect(sanityMock.getHierarchy).not.toHaveBeenCalled()
49
- expect(sanityMock.getHierarchies).not.toHaveBeenCalled()
50
- expect(sanityMock.getSanityDate).not.toHaveBeenCalled()
51
- })
52
-
53
- type ExpectedProgress = {
54
- state?: string
55
- percent?: number
56
- collection?: CollectionParameter
57
- }
58
-
59
- function expectProgress(data: any, expected: ExpectedProgress) {
60
- const expectedState = expected.state ?? (expected.percent === 100 ? 'completed' : 'started')
61
- expect(data).not.toBeNull()
62
- if (expected.state !== undefined) expect(data.state).toBe(expectedState)
63
- if (expected.percent !== undefined) expect(data.progress_percent).toBe(expected.percent)
64
- if (expected.collection !== undefined) expect(data.collection_type).toBe(expected.collection.type)
65
- if (expected.collection !== undefined) expect(data.collection_id).toBe(expected.collection.id)
66
- }
67
-
68
- async function getOne(contentId: number, collection: CollectionParameter = null) {
69
- const result = await db.contentProgress.getOneProgressByContentId(contentId, collection)
70
- return result.data
71
- }
72
-
73
- async function writeOne(contentId: number, progress: number, collection: CollectionParameter = null) {
74
- await db.contentProgress.recordProgress(contentId, collection, progress, testMetadata, 0, { skipPush: true })
75
- }
76
-
77
- function buildHierarchy(tree: HierarchyTreeNode) {
78
- const parents: Record<number, number> = {}
79
- const children: Record<number, number[]> = {}
80
- const metadata: Record<number, { type: string; brand: string; parent_id: number }> = {}
81
-
82
- function walk(node: HierarchyTreeNode, parentId: number) {
83
- const childIds = (node.children ?? []).map(c => c.id)
84
- metadata[node.id] = {
85
- type: node.type ?? 'lesson',
86
- brand: node.brand ?? 'drumeo',
87
- parent_id: parentId,
88
- }
89
- if (parentId) parents[node.id] = parentId
90
- if (childIds.length > 0) children[node.id] = childIds
91
- for (const c of node.children ?? []) walk(c, node.id)
92
- }
93
-
94
- walk(tree, 0)
95
-
96
- return { topLevelId: tree.id, parents, children, metadata }
97
- }
98
-
99
- const flatHierarchy = (contentId: number) => buildHierarchy({ id: contentId, type: 'lesson' })
100
-
101
- const flatHierarchyForMany = (contentIds: number[]) => ({
102
- topLevelId: contentIds[0],
103
- parents: {},
104
- children: {},
105
- metadata: Object.fromEntries(contentIds.map(id => [id, { ...testMetadata }])),
106
- })
107
-
108
- describe('contentStatusCompletedOffline', () => {
109
-
110
- describe('a-la-carte collection', () => {
111
- test('singular a-la-carte lesson', async () => {
112
- await contentStatusCompletedOffline(500, null, flatHierarchy(500))
113
- const record = await getOne(500, null)
114
-
115
- expectProgress(record, { percent: 100, collection: selfCollection })
116
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledTimes(1)
117
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
118
- })
119
-
120
- test('child a-la-carte lesson does NOT bubble to parent', async () => {
121
- const hierarchy = buildHierarchy({
122
- id: 1, type: 'course', children: [
123
- { id: 100, type: 'lesson' },
124
- { id: 200, type: 'lesson' },
125
- ],
126
- })
127
-
128
- await contentStatusCompletedOffline(100, null, hierarchy)
129
-
130
- const child = await getOne(100, null)
131
- const sibling = await getOne(200, null)
132
- const parent = await getOne(1, null)
133
-
134
- expectProgress(child, { percent: 100, collection: selfCollection })
135
- expect(sibling).toBeNull()
136
- expect(parent).toBeNull()
137
-
138
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledTimes(1)
139
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
140
- })
141
- })
142
-
143
- describe('learning path collection', () => {
144
- test('learning path lesson writes LP record and duplicates to a-la-carte without bubbling', async () => {
145
- const hierarchy = buildHierarchy({
146
- id: 999, type: 'learning-path-v2', children: [
147
- { id: 100, type: 'lesson' },
148
- { id: 300, type: 'lesson' },
149
- ],
150
- })
151
-
152
- await contentStatusCompletedOffline(100, lpCollection, hierarchy)
153
-
154
- const childLP = await getOne(100, lpCollection)
155
- const lpLP = await getOne(999, lpCollection)
156
- const childALC = await getOne(100, null)
157
- const lpALC = await getOne(999, null)
158
- const siblingALC = await getOne(300, null)
159
-
160
- expectProgress(childLP, { percent: 100, collection: lpCollection })
161
- expect(lpLP).toBeNull()
162
- expectProgress(childALC, { percent: 100, collection: selfCollection })
163
- expect(lpALC).toBeNull()
164
- expect(siblingALC).toBeNull()
165
-
166
- expect(learningPathsMock.onLearningPathCompletedActions).not.toHaveBeenCalled()
167
-
168
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
169
- })
170
-
171
- // not possible to complete parents offline, currently
172
- // test('learning path completion writes LP record but skips onLearningPathCompletedActions (offline)', async () => {})
173
- })
174
- })
175
-
176
- describe('contentStatusCompletedManyOffline', () => {
177
- test('all ids written in a-la-carte', async () => {
178
- await contentStatusCompletedManyOffline(
179
- [50001, 50002, 50003],
180
- null,
181
- flatHierarchyForMany([50001, 50002, 50003]),
182
- )
183
-
184
- expectProgress(await getOne(50001, null), { percent: 100, collection: selfCollection })
185
- expectProgress(await getOne(50002, null), { percent: 100, collection: selfCollection })
186
- expectProgress(await getOne(50003, null), { percent: 100, collection: selfCollection })
187
-
188
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
189
- })
190
- })
191
-
192
- describe('contentStatusStartedOffline', () => {
193
- test('singular a-la-carte lesson started', async () => {
194
- await contentStatusStartedOffline(500, null, flatHierarchy(500))
195
- const record = await getOne(500, null)
196
-
197
- expectProgress(record, { percent: 0, state: 'started', collection: selfCollection })
198
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
199
- })
200
- })
201
-
202
- describe('contentStatusResetOffline', () => {
203
- describe('a-la-carte collection', () => {
204
- test('reset singular a-la-carte lesson', async () => {
205
- await writeOne(500, 100)
206
- await contentStatusResetOffline(500)
207
-
208
- const record = await getOne(500, null)
209
-
210
- expect(record).toBeNull()
211
-
212
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledTimes(1)
213
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
214
- })
215
-
216
- test('reset child a-la-carte lesson does NOT touch parent', async () => {
217
- await writeOne(100, 100)
218
- await writeOne(200, 100)
219
- await writeOne(1, 100)
220
-
221
- await contentStatusResetOffline(100)
222
-
223
- const child = await getOne(100, null)
224
- const sibling = await getOne(200, null)
225
- const parent = await getOne(1, null)
226
-
227
- expect(child).toBeNull()
228
- expectProgress(sibling, { percent: 100 })
229
- expectProgress(parent, { percent: 100 })
230
-
231
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledTimes(1)
232
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
233
- })
234
- })
235
-
236
- describe('learning path collection', () => {
237
- test('reset learning path lesson removes LP record only', async () => {
238
- await writeOne(100, 100, lpCollection)
239
- await writeOne(999, 50, lpCollection)
240
- await writeOne(100, 100)
241
- await writeOne(50, 50)
242
-
243
- await contentStatusResetOffline(100, lpCollection)
244
-
245
- const childLP = await getOne(100, lpCollection)
246
- const lpLP = await getOne(999, lpCollection)
247
- const childALC = await getOne(100, null)
248
- const parentALC = await getOne(50, null)
249
-
250
- expect(childLP).toBeNull()
251
- expectProgress(lpLP, { percent: 50 })
252
- expectProgress(childALC, { percent: 100 })
253
- expectProgress(parentALC, { percent: 50 })
254
-
255
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledTimes(1)
256
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
257
- })
258
- })
259
- })
260
-
261
- describe('recordWatchSessionOffline', () => {
262
-
263
- describe('a-la-carte collection', () => {
264
- test('singular a-la-carte lesson, halfway', async () => {
265
- await recordWatchSessionOffline(500, 200, 100, 30, flatHierarchy(500))
266
-
267
- const record = await getOne(500, null)
268
- expectProgress(record, { percent: 50, collection: selfCollection })
269
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
270
- })
271
-
272
- test('singular a-la-carte lesson, watched to end clamps at 99', async () => {
273
- await recordWatchSessionOffline(500, 200, 200, 60, flatHierarchy(500))
274
-
275
- const record = await getOne(500, null)
276
- expectProgress(record, { percent: 99 })
277
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
278
- })
279
-
280
- test('child a-la-carte lesson does NOT bubble to parent', async () => {
281
- const hierarchy = buildHierarchy({
282
- id: 1, type: 'course', children: [
283
- { id: 100, type: 'lesson' },
284
- { id: 200, type: 'lesson' },
285
- ],
286
- })
287
-
288
- await recordWatchSessionOffline(100, 200, 100, 30, hierarchy)
289
-
290
- const child = await getOne(100, null)
291
- const sibling = await getOne(200, null)
292
- const parent = await getOne(1, null)
293
-
294
- expectProgress(child, { percent: 50 })
295
- expect(sibling).toBeNull()
296
- expect(parent).toBeNull()
297
-
298
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
299
- })
300
-
301
- test('flushWatchSession triggers push after offline watch', async () => {
302
- await recordWatchSessionOffline(500, 200, 100, 30, flatHierarchy(500))
303
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
304
-
305
- await flushWatchSession()
306
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('flush-watch-session')
307
- })
308
- })
309
-
310
- describe('playlist collection', () => {
311
- test('singular lesson in playlist duplicates progress to a-la-carte', async () => {
312
- await recordWatchSessionOffline(500, 200, 100, 30, flatHierarchy(500), {
313
- collection: playlistCollection,
314
- })
315
-
316
- const aLaCarte = await getOne(500, null)
317
- const playlistRec = await getOne(500, playlistCollection)
318
-
319
- expectProgress(aLaCarte, { percent: 50, collection: selfCollection })
320
- expect(playlistRec).toBeNull()
321
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
322
- })
323
- })
324
-
325
- describe('learning path collection', () => {
326
- test('child lesson in LP writes LP record, duplicates to a-la-carte, no bubble', async () => {
327
- const hierarchy = buildHierarchy({
328
- id: 999, type: 'learning-path-v2', children: [
329
- { id: 100, type: 'lesson' },
330
- { id: 300, type: 'lesson' },
331
- ],
332
- })
333
-
334
- await recordWatchSessionOffline(100, 200, 100, 30, hierarchy, {
335
- collection: lpCollection,
336
- })
337
-
338
- const childLP = await getOne(100, lpCollection)
339
- const lpLP = await getOne(999, lpCollection)
340
- const childALC = await getOne(100, null)
341
- const lpALC = await getOne(999, null)
342
-
343
- expectProgress(childLP, { percent: 50, collection: lpCollection })
344
- expect(lpLP).toBeNull()
345
- expectProgress(childALC, { percent: 50, collection: selfCollection })
346
- expect(lpALC).toBeNull()
347
-
348
- expect(learningPathsMock.onLearningPathCompletedActions).not.toHaveBeenCalled()
349
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
350
- })
351
- })
352
- })