musora-content-services 2.158.2 → 2.159.0
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.
- package/.claude/settings.local.json +12 -0
- package/.github/workflows/automated-testing.yml +21 -1
- package/CHANGELOG.md +15 -0
- package/README.md +21 -2
- package/jest.config.js +1 -4
- package/jest.integration.config.js +6 -0
- package/jest.live.config.js +1 -5
- package/package.json +5 -2
- package/src/contentTypeConfig.js +8 -5
- package/src/index.d.ts +2 -6
- package/src/index.js +2 -6
- package/src/services/content-org/learning-paths.ts +44 -39
- package/src/services/contentAggregator.js +1 -1
- package/src/services/contentProgress.js +216 -207
- package/src/services/offline/progress.ts +107 -27
- package/src/services/sanity.js +55 -64
- package/src/services/sync/models/ContentProgress.ts +50 -34
- package/src/services/sync/repositories/content-progress.ts +105 -92
- package/test/{unit → integration}/awards/award-exclusion-handling.test.ts +2 -2
- package/test/integration/content-progress/__mocks__/mocks.ts +104 -0
- package/test/integration/content-progress/contentProgress.test.ts +335 -0
- package/test/integration/content-progress/e2eOfflineProgress.test.ts +352 -0
- package/test/integration/content-progress/e2eProgress.test.ts +612 -0
- package/test/integration/content-progress/getters.test.ts +334 -0
- package/test/integration/content-progress/helpers.test.ts +263 -0
- package/test/integration/content-progress/offlineContentProgress.test.ts +226 -0
- package/test/integration/forums.test.ts +209 -0
- package/test/integration/initializeTestDB.ts +80 -0
- package/test/{unit → integration}/sync/fetch.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/content-likes.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/practices.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/progress.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/user-award-progress.test.ts +1 -1
- package/test/{unit → integration}/sync/store/cross-user-protection.test.ts +2 -2
- package/test/{unit → integration}/sync/store/store-idb.test.ts +2 -2
- package/test/{unit → integration}/sync/store/store.test.ts +2 -2
- package/test/unit/content-progress/bubbleTrickle.test.ts +322 -0
- package/test/unit/content-progress/helpers.test.ts +329 -0
- package/test/unit/content-progress/navigateTo.test.ts +381 -0
- package/test/unit/contentMetaData.test.ts +58 -0
- package/tools/generate-index.cjs +6 -3
- package/test/SKIPPED_TESTS.md +0 -151
- package/test/integration/content.test.js +0 -107
- package/test/integration/contentProgress.test.js +0 -73
- package/test/integration/forum.test.js +0 -16
- package/test/integration/sanityQueryService.test.js +0 -681
- package/test/unit/contentProgress.test.ts +0 -81
- /package/test/{unit → integration}/awards/internal/image-utils.test.ts +0 -0
- /package/test/{unit → integration}/infrastructure/FetchRequestExecutor.test.ts +0 -0
- /package/test/{unit → integration}/notifications.test.ts +0 -0
- /package/test/{unit → integration}/sync/adapters/idb-errors.test.ts +0 -0
- /package/test/{unit → integration}/sync/adapters/sqlite-errors.test.ts +0 -0
- /package/test/{unit → integration}/sync/repositories/user-award-progress.static.test.ts +0 -0
- /package/test/{unit → integration}/userActivity.test.ts +0 -0
|
@@ -0,0 +1,335 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,352 @@
|
|
|
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
|
+
})
|