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,334 @@
|
|
|
1
|
+
import { initializeTestDB } from '../initializeTestDB'
|
|
2
|
+
import {
|
|
3
|
+
_getAllStartedOrCompleted,
|
|
4
|
+
getAllCompleted,
|
|
5
|
+
getAllCompletedByIds,
|
|
6
|
+
getAllStarted,
|
|
7
|
+
getAllStartedOrCompleted,
|
|
8
|
+
getById,
|
|
9
|
+
getByIds,
|
|
10
|
+
getByRecordIds,
|
|
11
|
+
getLastInteractedOf,
|
|
12
|
+
getProgressDataByIds,
|
|
13
|
+
getProgressDataByRecordIds,
|
|
14
|
+
getProgressState,
|
|
15
|
+
getProgressStateByIds,
|
|
16
|
+
getProgressStateByRecordIds,
|
|
17
|
+
getResumeTimeSecondsByIds,
|
|
18
|
+
getResumeTimeSecondsByRecordIds,
|
|
19
|
+
getStartedOrCompletedProgressOnly,
|
|
20
|
+
} from '../../../src/services/contentProgress'
|
|
21
|
+
import db from '../../../src/services/sync/repository-proxy'
|
|
22
|
+
|
|
23
|
+
jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
|
|
24
|
+
jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
|
|
25
|
+
jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
|
|
26
|
+
jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
|
|
27
|
+
|
|
28
|
+
const meta = { brand: 'drumeo', type: 'lesson', parent_id: 0 }
|
|
29
|
+
|
|
30
|
+
initializeTestDB()
|
|
31
|
+
|
|
32
|
+
describe('getById', () => {
|
|
33
|
+
test('returns field value when record found', async () => {
|
|
34
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, 120, { skipPush: true })
|
|
35
|
+
const result = await getById(100, null, 'state', '')
|
|
36
|
+
expect(result).toBe('started')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('returns defaultValue when record not found', async () => {
|
|
40
|
+
const result = await getById(999, null, 'state', 'default-val')
|
|
41
|
+
expect(result).toBe('default-val')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('returns defaultValue when contentId is falsy', async () => {
|
|
45
|
+
const result = await getById(0, null, 'state', 'fallback')
|
|
46
|
+
expect(result).toBe('fallback')
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('getByIds', () => {
|
|
51
|
+
test('returns Map with correct values for found ids', async () => {
|
|
52
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
53
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
54
|
+
const result = await getByIds([100, 300], null, 'state', '')
|
|
55
|
+
expect(result.get(100)).toBe('started')
|
|
56
|
+
expect(result.get(300)).toBe('completed')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('defaults to defaultValue for missing ids', async () => {
|
|
60
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
61
|
+
const result = await getByIds([100, 999], null, 'state', '')
|
|
62
|
+
expect(result.get(100)).toBe('started')
|
|
63
|
+
expect(result.get(999)).toBe('')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('returns empty Map for empty array', async () => {
|
|
67
|
+
const result = await getByIds([], null, 'state', '')
|
|
68
|
+
expect(result.size).toBe(0)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('getByRecordIds', () => {
|
|
73
|
+
test('returns object with correct values for found record ids', async () => {
|
|
74
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
75
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
76
|
+
const result = await getByRecordIds(['100:self:0', '300:self:0'], 'state', '')
|
|
77
|
+
expect(result['100:self:0']).toBe('started')
|
|
78
|
+
expect(result['300:self:0']).toBe('completed')
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test('defaults to defaultValue for missing record id', async () => {
|
|
82
|
+
const result = await getByRecordIds(['999:self:0'], 'state', '')
|
|
83
|
+
expect(result['999:self:0']).toBe('')
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('getProgressState', () => {
|
|
88
|
+
test('returns started for started content', async () => {
|
|
89
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
90
|
+
expect(await getProgressState(100)).toBe('started')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('returns completed for completed content', async () => {
|
|
94
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
95
|
+
expect(await getProgressState(300)).toBe('completed')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('returns empty string for unknown content', async () => {
|
|
99
|
+
expect(await getProgressState(999)).toBe('')
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
describe('getProgressStateByIds', () => {
|
|
104
|
+
test('returns Map with correct states for multiple ids', async () => {
|
|
105
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
106
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
107
|
+
const result = await getProgressStateByIds([100, 300])
|
|
108
|
+
expect(result.get(100)).toBe('started')
|
|
109
|
+
expect(result.get(300)).toBe('completed')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('defaults to empty string for unknown ids', async () => {
|
|
113
|
+
const result = await getProgressStateByIds([999])
|
|
114
|
+
expect(result.get(999)).toBe('')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('returns empty Map for empty array', async () => {
|
|
118
|
+
const result = await getProgressStateByIds([])
|
|
119
|
+
expect(result.size).toBe(0)
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
describe('getProgressStateByRecordIds', () => {
|
|
124
|
+
test('returns correct states by record id', async () => {
|
|
125
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
126
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
127
|
+
const result = await getProgressStateByRecordIds(['100:self:0', '300:self:0'])
|
|
128
|
+
expect(result['100:self:0']).toBe('started')
|
|
129
|
+
expect(result['300:self:0']).toBe('completed')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('returns empty string for unknown record id', async () => {
|
|
133
|
+
const result = await getProgressStateByRecordIds(['999:self:0'])
|
|
134
|
+
expect(result['999:self:0']).toBe('')
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
describe('getResumeTimeSecondsByIds', () => {
|
|
139
|
+
test('returns resume time when record has one', async () => {
|
|
140
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, 120, { skipPush: true })
|
|
141
|
+
const result = await getResumeTimeSecondsByIds([100])
|
|
142
|
+
expect(result.get(100)).toBe(120)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('returns 0 for unknown ids', async () => {
|
|
146
|
+
const result = await getResumeTimeSecondsByIds([999])
|
|
147
|
+
expect(result.get(999)).toBe(0)
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('getResumeTimeSecondsByRecordIds', () => {
|
|
152
|
+
test('returns correct value by record id', async () => {
|
|
153
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, 120, { skipPush: true })
|
|
154
|
+
const result = await getResumeTimeSecondsByRecordIds(['100:self:0'])
|
|
155
|
+
expect(result['100:self:0']).toBe(120)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('returns 0 for unknown record id', async () => {
|
|
159
|
+
const result = await getResumeTimeSecondsByRecordIds(['999:self:0'])
|
|
160
|
+
expect(result['999:self:0']).toBe(0)
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
describe('getProgressDataByIds', () => {
|
|
165
|
+
test('returns correct shape for seeded content', async () => {
|
|
166
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
167
|
+
const result = await getProgressDataByIds([100], null)
|
|
168
|
+
expect(result[100].progress).toBe(20)
|
|
169
|
+
expect(result[100].status).toBe('started')
|
|
170
|
+
expect(typeof result[100].last_update).toBe('number')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('returns defaults for unknown id', async () => {
|
|
174
|
+
const result = await getProgressDataByIds([999], null)
|
|
175
|
+
expect(result[999]).toEqual({ last_update: 0, progress: 0, status: '' })
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('normalizes string content ids', async () => {
|
|
179
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
180
|
+
const result = await getProgressDataByIds(['100' as any], null)
|
|
181
|
+
expect(result[100].progress).toBe(20)
|
|
182
|
+
})
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
describe('getProgressDataByRecordIds', () => {
|
|
186
|
+
test('returns correct shape for seeded content', async () => {
|
|
187
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
188
|
+
const result = await getProgressDataByRecordIds(['100:self:0'])
|
|
189
|
+
expect(result['100:self:0'].progress).toBe(20)
|
|
190
|
+
expect(result['100:self:0'].status).toBe('started')
|
|
191
|
+
expect(typeof result['100:self:0'].last_update).toBe('number')
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('returns defaults for unknown record id', async () => {
|
|
195
|
+
const result = await getProgressDataByRecordIds(['999:self:0'])
|
|
196
|
+
expect(result['999:self:0']).toEqual({ last_update: 0, progress: 0, status: '' })
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe('getLastInteractedOf', () => {
|
|
201
|
+
test('returns a content id from the given list when records exist', async () => {
|
|
202
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
203
|
+
await db.contentProgress.recordProgress(200, null, 50, meta, undefined, { skipPush: true })
|
|
204
|
+
const result = await getLastInteractedOf([100, 200])
|
|
205
|
+
expect([100, 200]).toContain(result)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('returns undefined when no records match', async () => {
|
|
209
|
+
const result = await getLastInteractedOf([999])
|
|
210
|
+
expect(result).toBeUndefined()
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
describe('getAllStarted', () => {
|
|
215
|
+
test('returns started content ids, not completed', async () => {
|
|
216
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
217
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
218
|
+
const result = await getAllStarted()
|
|
219
|
+
expect(result).toContain(100)
|
|
220
|
+
expect(result).not.toContain(300)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test('returns empty array when no started content', async () => {
|
|
224
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
225
|
+
expect(await getAllStarted()).toHaveLength(0)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test('respects limit', async () => {
|
|
229
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
230
|
+
await db.contentProgress.recordProgress(200, null, 50, meta, undefined, { skipPush: true })
|
|
231
|
+
expect(await getAllStarted(1)).toHaveLength(1)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test('onlyIds=false returns objects with content_id', async () => {
|
|
235
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
236
|
+
const result = await getAllStarted(null, { onlyIds: false })
|
|
237
|
+
expect(result[0]).toHaveProperty('content_id', 100)
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
describe('getAllCompleted', () => {
|
|
242
|
+
test('returns completed content ids, not started', async () => {
|
|
243
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
244
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
245
|
+
const result = await getAllCompleted()
|
|
246
|
+
expect(result).toContain(300)
|
|
247
|
+
expect(result).not.toContain(100)
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
test('returns empty array when no completed content', async () => {
|
|
251
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
252
|
+
expect(await getAllCompleted()).toHaveLength(0)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('respects limit', async () => {
|
|
256
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
257
|
+
await db.contentProgress.recordProgress(400, null, 100, meta, undefined, { skipPush: true })
|
|
258
|
+
expect(await getAllCompleted(1)).toHaveLength(1)
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
describe('getAllCompletedByIds', () => {
|
|
263
|
+
test('returns only completed records within given ids', async () => {
|
|
264
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
265
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
266
|
+
await db.contentProgress.recordProgress(400, null, 100, meta, undefined, { skipPush: true })
|
|
267
|
+
const result = await getAllCompletedByIds([100, 300, 400])
|
|
268
|
+
const completedIds = result.data.map((r: any) => r.content_id)
|
|
269
|
+
expect(completedIds).toContain(300)
|
|
270
|
+
expect(completedIds).toContain(400)
|
|
271
|
+
expect(completedIds).not.toContain(100)
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('returns empty data array when no completed records match', async () => {
|
|
275
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
276
|
+
const result = await getAllCompletedByIds([100])
|
|
277
|
+
expect(result.data).toHaveLength(0)
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
describe('getAllStartedOrCompleted', () => {
|
|
282
|
+
test('returns ids for both started and completed records', async () => {
|
|
283
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
284
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
285
|
+
const result = await getAllStartedOrCompleted()
|
|
286
|
+
expect(result).toContain(100)
|
|
287
|
+
expect(result).toContain(300)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
test('respects limit', async () => {
|
|
291
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
292
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
293
|
+
await db.contentProgress.recordProgress(400, null, 100, meta, undefined, { skipPush: true })
|
|
294
|
+
const result = await getAllStartedOrCompleted({ limit: 2 })
|
|
295
|
+
expect(result).toHaveLength(2)
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
test('onlyIds=false returns full record objects', async () => {
|
|
299
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
300
|
+
const result = await getAllStartedOrCompleted({ onlyIds: false })
|
|
301
|
+
expect(result[0]).toHaveProperty('content_id')
|
|
302
|
+
expect(result[0]).toHaveProperty('state')
|
|
303
|
+
expect(result[0]).toHaveProperty('progress_percent')
|
|
304
|
+
})
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
describe('_getAllStartedOrCompleted', () => {
|
|
308
|
+
test('returns all started and completed records', async () => {
|
|
309
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
310
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
311
|
+
const result = await _getAllStartedOrCompleted()
|
|
312
|
+
const ids = result.map((r: any) => r.content_id)
|
|
313
|
+
expect(ids).toContain(100)
|
|
314
|
+
expect(ids).toContain(300)
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
test('respects limit', async () => {
|
|
318
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
319
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
320
|
+
await db.contentProgress.recordProgress(400, null, 100, meta, undefined, { skipPush: true })
|
|
321
|
+
const result = await _getAllStartedOrCompleted({ limit: 2 })
|
|
322
|
+
expect(result).toHaveLength(2)
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
describe('getStartedOrCompletedProgressOnly', () => {
|
|
327
|
+
test('returns map of content_id to progress_percent', async () => {
|
|
328
|
+
await db.contentProgress.recordProgress(100, null, 20, meta, undefined, { skipPush: true })
|
|
329
|
+
await db.contentProgress.recordProgress(300, null, 100, meta, undefined, { skipPush: true })
|
|
330
|
+
const result = await getStartedOrCompletedProgressOnly()
|
|
331
|
+
expect(result[100]).toBe(20)
|
|
332
|
+
expect(result[300]).toBe(100)
|
|
333
|
+
})
|
|
334
|
+
})
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { initializeTestDB } from '../initializeTestDB'
|
|
2
|
+
import {
|
|
3
|
+
bubbleAndTrickleProgressesSafely,
|
|
4
|
+
duplicateProgressToALaCarte,
|
|
5
|
+
getIdsWhereLastAccessedFromMethod,
|
|
6
|
+
getProgressState,
|
|
7
|
+
handleLearningPathProgressActions,
|
|
8
|
+
} from '../../../src/services/contentProgress'
|
|
9
|
+
import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
|
|
10
|
+
import db from '../../../src/services/sync/repository-proxy'
|
|
11
|
+
|
|
12
|
+
jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
|
|
13
|
+
jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
|
|
14
|
+
jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
|
|
15
|
+
jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
|
|
16
|
+
|
|
17
|
+
const meta = { brand: 'drumeo', type: 'lesson', parent_id: 0 }
|
|
18
|
+
const collectionSelf = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
|
|
19
|
+
const collectionLP = (id: number) => ({ type: COLLECTION_TYPE.LEARNING_PATH, id })
|
|
20
|
+
|
|
21
|
+
const ctx = initializeTestDB()
|
|
22
|
+
|
|
23
|
+
const flushPromises = async () => {
|
|
24
|
+
for (let i = 0; i < 200; i++) {
|
|
25
|
+
await new Promise(resolve => setImmediate(resolve))
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ─── bubbleAndTrickleProgressesSafely ─────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
describe('bubbleAndTrickleProgressesSafely', () => {
|
|
32
|
+
test('non-reset writes records to DB', async () => {
|
|
33
|
+
const progresses = { 101: 50, 102: 75 }
|
|
34
|
+
const metadata = { 101: meta, 102: meta }
|
|
35
|
+
await bubbleAndTrickleProgressesSafely(progresses, collectionSelf, metadata)
|
|
36
|
+
expect(await getProgressState(101)).toBe('started')
|
|
37
|
+
expect(await getProgressState(102)).toBe('started')
|
|
38
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('non-reset with correct progress values', async () => {
|
|
42
|
+
const progresses = { 101: 50 }
|
|
43
|
+
const metadata = { 101: meta }
|
|
44
|
+
await bubbleAndTrickleProgressesSafely(progresses, collectionSelf, metadata)
|
|
45
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
46
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
47
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('non-reset with empty progresses does not create records', async () => {
|
|
51
|
+
await bubbleAndTrickleProgressesSafely({}, collectionSelf, {})
|
|
52
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
53
|
+
expect(record.data).toBeNull()
|
|
54
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('reset action with all positive values writes records', async () => {
|
|
58
|
+
const progresses = { 101: 50, 102: 75 }
|
|
59
|
+
const metadata = { 101: meta, 102: meta }
|
|
60
|
+
await bubbleAndTrickleProgressesSafely(progresses, collectionSelf, metadata, { isResetAction: true })
|
|
61
|
+
expect(await getProgressState(101)).toBe('started')
|
|
62
|
+
expect(await getProgressState(102)).toBe('started')
|
|
63
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('reset action with all zero values erases records', async () => {
|
|
67
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true })
|
|
68
|
+
expect(await getProgressState(101)).toBe('started')
|
|
69
|
+
await bubbleAndTrickleProgressesSafely({ 101: 0 }, collectionSelf, {}, { isResetAction: true })
|
|
70
|
+
expect(await getProgressState(101)).toBe('')
|
|
71
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('reset action with mixed values writes positive and erases zeros', async () => {
|
|
75
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true })
|
|
76
|
+
const metadata = { 102: meta }
|
|
77
|
+
await bubbleAndTrickleProgressesSafely({ 101: 0, 102: 60 }, collectionSelf, metadata, { isResetAction: true })
|
|
78
|
+
expect(await getProgressState(101)).toBe('')
|
|
79
|
+
expect(await getProgressState(102)).toBe('started')
|
|
80
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('collection normalized — null collection writes to SELF', async () => {
|
|
84
|
+
const progresses = { 101: 50 }
|
|
85
|
+
const metadata = { 101: meta }
|
|
86
|
+
await bubbleAndTrickleProgressesSafely(progresses, null as any, metadata)
|
|
87
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
88
|
+
expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
|
|
89
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('bubble lowers parent from 100% to 50% via allowRegression', async () => {
|
|
93
|
+
await db.contentProgress.recordProgress(1, null, 100, meta, undefined, { skipPush: true })
|
|
94
|
+
expect((await db.contentProgress.getOneProgressByContentId(1, null)).data?.progress_percent).toBe(100)
|
|
95
|
+
|
|
96
|
+
await bubbleAndTrickleProgressesSafely({ 1: 50 }, collectionSelf, { 1: meta })
|
|
97
|
+
|
|
98
|
+
const record = await db.contentProgress.getOneProgressByContentId(1, null)
|
|
99
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
100
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
101
|
+
})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// ─── handleLearningPathProgressActions ────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
describe('handleLearningPathProgressActions', () => {
|
|
107
|
+
const mockLearningPaths = jest.requireMock('../../../src/services/content-org/learning-paths.ts')
|
|
108
|
+
|
|
109
|
+
beforeEach(() => {
|
|
110
|
+
mockLearningPaths.onLearningPathCompletedActions.mockClear()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('non-LP collection returns without action', async () => {
|
|
114
|
+
await handleLearningPathProgressActions({ 101: 100 }, collectionSelf)
|
|
115
|
+
await flushPromises()
|
|
116
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).not.toHaveBeenCalled()
|
|
117
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('LP id at 100% calls onLearningPathCompletedActions', async () => {
|
|
121
|
+
await handleLearningPathProgressActions({ 200: 100 }, collectionLP(200))
|
|
122
|
+
await flushPromises()
|
|
123
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).toHaveBeenCalledWith(200)
|
|
124
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('non-LP-id child at 100% does not call onLearningPathCompletedActions', async () => {
|
|
128
|
+
await handleLearningPathProgressActions({ 101: 100 }, collectionLP(200))
|
|
129
|
+
await flushPromises()
|
|
130
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).not.toHaveBeenCalled()
|
|
131
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('LP id at <100% does not call onLearningPathCompletedActions', async () => {
|
|
135
|
+
await handleLearningPathProgressActions({ 200: 50 }, collectionLP(200))
|
|
136
|
+
await flushPromises()
|
|
137
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).not.toHaveBeenCalled()
|
|
138
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('LP id + children at 100% calls onLearningPathCompletedActions once for LP id only', async () => {
|
|
142
|
+
await handleLearningPathProgressActions({ 200: 100, 101: 100, 102: 100 }, collectionLP(200))
|
|
143
|
+
await flushPromises()
|
|
144
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).toHaveBeenCalledTimes(1)
|
|
145
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).toHaveBeenCalledWith(200)
|
|
146
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('LP collection duplicates lesson progress to SELF records', async () => {
|
|
150
|
+
await handleLearningPathProgressActions({ 101: 50 }, collectionLP(200))
|
|
151
|
+
await flushPromises()
|
|
152
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
153
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
154
|
+
expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
|
|
155
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
// ─── duplicateProgressToALaCarte ─────────────────────────────────────────────
|
|
160
|
+
|
|
161
|
+
describe('duplicateProgressToALaCarte', () => {
|
|
162
|
+
test('writes SELF records for a-la-carte collection', async () => {
|
|
163
|
+
await duplicateProgressToALaCarte({ 101: 50 }, collectionSelf)
|
|
164
|
+
await flushPromises()
|
|
165
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
166
|
+
expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
|
|
167
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('LP collection excludes the LP id itself from duplication', async () => {
|
|
171
|
+
await duplicateProgressToALaCarte({ 200: 50, 101: 75 }, collectionLP(200))
|
|
172
|
+
await flushPromises()
|
|
173
|
+
const lpRecord = await db.contentProgress.getOneProgressByContentId(200, null)
|
|
174
|
+
const lessonRecord = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
175
|
+
expect(lpRecord.data).toBeNull()
|
|
176
|
+
expect(lessonRecord.data?.progress_percent).toBe(75)
|
|
177
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('lower progress than existing is filtered — existing stays unchanged', async () => {
|
|
181
|
+
await db.contentProgress.recordProgress(101, null, 80, meta, undefined, { skipPush: true })
|
|
182
|
+
await duplicateProgressToALaCarte({ 101: 50 }, collectionSelf)
|
|
183
|
+
await flushPromises()
|
|
184
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
185
|
+
expect(record.data?.progress_percent).toBe(80)
|
|
186
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('equal progress passes and record is updated', async () => {
|
|
190
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true })
|
|
191
|
+
await duplicateProgressToALaCarte({ 101: 50 }, collectionSelf)
|
|
192
|
+
await flushPromises()
|
|
193
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
194
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
195
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('empty progresses results in no new records', async () => {
|
|
199
|
+
await duplicateProgressToALaCarte({}, collectionSelf)
|
|
200
|
+
await flushPromises()
|
|
201
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
202
|
+
expect(record.data).toBeNull()
|
|
203
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
// ─── getIdsWhereLastAccessedFromMethod ────────────────────────────────────────
|
|
208
|
+
|
|
209
|
+
describe('getIdsWhereLastAccessedFromMethod', () => {
|
|
210
|
+
test('returns content_ids from records where last_interacted_a_la_carte is null', async () => {
|
|
211
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true, accessedDirectly: false })
|
|
212
|
+
const result = await getIdsWhereLastAccessedFromMethod([101])
|
|
213
|
+
expect(result).toContain(101)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('returns empty array when no records match', async () => {
|
|
217
|
+
const result = await getIdsWhereLastAccessedFromMethod([999])
|
|
218
|
+
expect(result).toEqual([])
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('normalizes string content ids before querying', async () => {
|
|
222
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true, accessedDirectly: false })
|
|
223
|
+
const result = await getIdsWhereLastAccessedFromMethod(['101'] as any)
|
|
224
|
+
expect(result).toContain(101)
|
|
225
|
+
})
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
// ─── Scenario: LP lesson completion ───────────────────────────────────────────
|
|
229
|
+
|
|
230
|
+
describe('Scenario: LP lesson at 100% triggers completion and duplicates to a-la-carte', () => {
|
|
231
|
+
const mockLearningPaths = jest.requireMock('../../../src/services/content-org/learning-paths.ts')
|
|
232
|
+
|
|
233
|
+
beforeEach(() => {
|
|
234
|
+
mockLearningPaths.onLearningPathCompletedActions.mockClear()
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('LP id 200 at 100% calls onLearningPathCompletedActions; lesson at 100% writes SELF record', async () => {
|
|
238
|
+
await handleLearningPathProgressActions({ 200: 100, 101: 100 }, collectionLP(200))
|
|
239
|
+
await flushPromises()
|
|
240
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).toHaveBeenCalledTimes(1)
|
|
241
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).toHaveBeenCalledWith(200)
|
|
242
|
+
const selfRecord = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
243
|
+
expect(selfRecord.data?.progress_percent).toBe(100)
|
|
244
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
245
|
+
})
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
describe('Scenario: Offline LP progress skips award check', () => {
|
|
249
|
+
const mockLearningPaths = jest.requireMock('../../../src/services/content-org/learning-paths.ts')
|
|
250
|
+
|
|
251
|
+
beforeEach(() => {
|
|
252
|
+
mockLearningPaths.onLearningPathCompletedActions.mockClear()
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('isOffline=true writes SELF record but skips onLearningPathCompletedActions', async () => {
|
|
256
|
+
await handleLearningPathProgressActions({ 101: 100 }, collectionLP(200), { isOffline: true })
|
|
257
|
+
await flushPromises()
|
|
258
|
+
expect(mockLearningPaths.onLearningPathCompletedActions).not.toHaveBeenCalled()
|
|
259
|
+
const selfRecord = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
260
|
+
expect(selfRecord.data?.progress_percent).toBe(100)
|
|
261
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
262
|
+
})
|
|
263
|
+
})
|