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,322 @@
|
|
|
1
|
+
import { initializeTestService } from '../../initializeTests.js'
|
|
2
|
+
import {
|
|
3
|
+
averageProgressesFor,
|
|
4
|
+
bubbleProgress,
|
|
5
|
+
computeBubbleTrickleProgresses,
|
|
6
|
+
getAncestorAndSiblingIds,
|
|
7
|
+
getChildrenToDepth,
|
|
8
|
+
trickleProgress,
|
|
9
|
+
} from '../../../src/services/contentProgress.js'
|
|
10
|
+
import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
|
|
11
|
+
|
|
12
|
+
let mockProgressRecords: any[] = []
|
|
13
|
+
|
|
14
|
+
jest.mock('../../../src/services/sync/repository-proxy', () => {
|
|
15
|
+
const mockFns = {
|
|
16
|
+
contentProgress: {
|
|
17
|
+
getSomeProgressByContentIds: jest.fn().mockImplementation((contentIds) => {
|
|
18
|
+
const records = mockProgressRecords.filter(r => contentIds.includes(r.content_id))
|
|
19
|
+
return Promise.resolve({ data: records })
|
|
20
|
+
}),
|
|
21
|
+
},
|
|
22
|
+
practices: {
|
|
23
|
+
queryAll: jest.fn().mockResolvedValue({ data: [] }),
|
|
24
|
+
getAll: jest.fn().mockResolvedValue({ data: [] }),
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
return { default: mockFns, ...mockFns }
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
jest.mock('../../../src/services/sanity.js', () => ({
|
|
31
|
+
getHierarchy: jest.fn().mockResolvedValue({ metadata: {}, parents: {}, children: {} }),
|
|
32
|
+
getHierarchies: jest.fn().mockResolvedValue({ metadata: {}, parents: {}, children: {} }),
|
|
33
|
+
getSanityDate: jest.fn((date: Date) => date.toISOString()),
|
|
34
|
+
}))
|
|
35
|
+
|
|
36
|
+
jest.mock('../../../src/services/content-org/learning-paths', () => ({
|
|
37
|
+
getDailySession: jest.fn(),
|
|
38
|
+
onLearningPathCompletedActions: jest.fn(),
|
|
39
|
+
}))
|
|
40
|
+
|
|
41
|
+
const flatHierarchy = {
|
|
42
|
+
parents: {
|
|
43
|
+
200: 100,
|
|
44
|
+
201: 100,
|
|
45
|
+
202: 100,
|
|
46
|
+
},
|
|
47
|
+
children: {
|
|
48
|
+
100: [200, 201, 202],
|
|
49
|
+
},
|
|
50
|
+
metadata: {},
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const deepHierarchy = {
|
|
54
|
+
parents: {
|
|
55
|
+
300: 200,
|
|
56
|
+
301: 200,
|
|
57
|
+
200: 100,
|
|
58
|
+
201: 100,
|
|
59
|
+
},
|
|
60
|
+
children: {
|
|
61
|
+
100: [200, 201],
|
|
62
|
+
200: [300, 301],
|
|
63
|
+
},
|
|
64
|
+
metadata: {},
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const collectionSelf = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
|
|
68
|
+
|
|
69
|
+
describe('getChildrenToDepth', () => {
|
|
70
|
+
beforeEach(() => {
|
|
71
|
+
initializeTestService()
|
|
72
|
+
mockProgressRecords = []
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('no children returns empty array', () => {
|
|
76
|
+
const result = getChildrenToDepth(200, flatHierarchy, 1)
|
|
77
|
+
expect(result).toEqual([])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('direct children at depth 1', () => {
|
|
81
|
+
const result = getChildrenToDepth(100, flatHierarchy, 1)
|
|
82
|
+
expect(result).toEqual([200, 201, 202])
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('grandchildren included at depth 2', () => {
|
|
86
|
+
const result = getChildrenToDepth(100, deepHierarchy, 2)
|
|
87
|
+
expect(result).toEqual([200, 201, 300, 301])
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('depth 0 on leaf node returns empty array', () => {
|
|
91
|
+
const result = getChildrenToDepth(300, deepHierarchy, 0)
|
|
92
|
+
expect(result).toEqual([])
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
describe('getAncestorAndSiblingIds', () => {
|
|
97
|
+
beforeEach(() => {
|
|
98
|
+
initializeTestService()
|
|
99
|
+
mockProgressRecords = []
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('no parent returns empty array', () => {
|
|
103
|
+
const result = getAncestorAndSiblingIds(flatHierarchy, 100)
|
|
104
|
+
expect(result).toEqual([])
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('returns siblings and parent for direct child', () => {
|
|
108
|
+
const result = getAncestorAndSiblingIds(flatHierarchy, 200)
|
|
109
|
+
expect(result).toContain(100)
|
|
110
|
+
expect(result).toContain(200)
|
|
111
|
+
expect(result).toContain(201)
|
|
112
|
+
expect(result).toContain(202)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('returns siblings, parent, parent siblings, and grandparent for nested child', () => {
|
|
116
|
+
const result = getAncestorAndSiblingIds(deepHierarchy, 300)
|
|
117
|
+
expect(result).toContain(300)
|
|
118
|
+
expect(result).toContain(301)
|
|
119
|
+
expect(result).toContain(200)
|
|
120
|
+
expect(result).toContain(201)
|
|
121
|
+
expect(result).toContain(100)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('circular ref returns empty array and logs error', () => {
|
|
125
|
+
const circularHierarchy = {
|
|
126
|
+
parents: { 100: 100 },
|
|
127
|
+
children: { 100: [100] },
|
|
128
|
+
metadata: {},
|
|
129
|
+
}
|
|
130
|
+
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {
|
|
131
|
+
})
|
|
132
|
+
const result = getAncestorAndSiblingIds(circularHierarchy, 100)
|
|
133
|
+
expect(result).toEqual([])
|
|
134
|
+
expect(errorSpy).toHaveBeenCalled()
|
|
135
|
+
errorSpy.mockRestore()
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('stops at MAX_DEPTH of 3', () => {
|
|
139
|
+
const fourLevelHierarchy = {
|
|
140
|
+
parents: {
|
|
141
|
+
400: 300,
|
|
142
|
+
300: 200,
|
|
143
|
+
200: 100,
|
|
144
|
+
100: 50,
|
|
145
|
+
},
|
|
146
|
+
children: {
|
|
147
|
+
50: [100],
|
|
148
|
+
100: [200],
|
|
149
|
+
200: [300],
|
|
150
|
+
300: [400],
|
|
151
|
+
},
|
|
152
|
+
metadata: {},
|
|
153
|
+
}
|
|
154
|
+
const result = getAncestorAndSiblingIds(fourLevelHierarchy, 400)
|
|
155
|
+
expect(result).not.toContain(50)
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
describe('averageProgressesFor', () => {
|
|
160
|
+
beforeEach(() => {
|
|
161
|
+
initializeTestService()
|
|
162
|
+
mockProgressRecords = []
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('no parent returns empty object', () => {
|
|
166
|
+
const progressData = new Map([[100, 50]])
|
|
167
|
+
const result = averageProgressesFor(flatHierarchy, 100, progressData)
|
|
168
|
+
expect(result).toEqual({})
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('single child average equals that child progress', () => {
|
|
172
|
+
const singleChildHierarchy = {
|
|
173
|
+
parents: { 200: 100 },
|
|
174
|
+
children: { 100: [200] },
|
|
175
|
+
metadata: {},
|
|
176
|
+
}
|
|
177
|
+
const progressData = new Map([[200, 60]])
|
|
178
|
+
const result = averageProgressesFor(singleChildHierarchy, 200, progressData)
|
|
179
|
+
expect(result[100]).toBe(60)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test('multiple children average is rounded sum divided by count', () => {
|
|
183
|
+
const progressData = new Map([[200, 50], [201, 70], [202, 90]])
|
|
184
|
+
const result = averageProgressesFor(flatHierarchy, 200, progressData)
|
|
185
|
+
expect(result[100]).toBe(Math.round((50 + 70 + 90) / 3))
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test('missing child progress defaults to 0', () => {
|
|
189
|
+
const progressData = new Map([[200, 60]])
|
|
190
|
+
const result = averageProgressesFor(flatHierarchy, 200, progressData)
|
|
191
|
+
expect(result[100]).toBe(Math.round((60 + 0 + 0) / 3))
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('recurses up to compute grandparent progress', () => {
|
|
195
|
+
const progressData = new Map([[300, 80], [301, 40], [201, 50]])
|
|
196
|
+
const result = averageProgressesFor(deepHierarchy, 300, progressData)
|
|
197
|
+
expect(result[200]).toBeDefined()
|
|
198
|
+
expect(result[100]).toBeDefined()
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('trickleProgress', () => {
|
|
203
|
+
beforeEach(() => {
|
|
204
|
+
initializeTestService()
|
|
205
|
+
mockProgressRecords = []
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('no descendants returns empty object', () => {
|
|
209
|
+
const result = trickleProgress(flatHierarchy, 200, collectionSelf, 75)
|
|
210
|
+
expect(result).toEqual({})
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('all descendants receive same progress value', () => {
|
|
214
|
+
const result = trickleProgress(flatHierarchy, 100, collectionSelf, 100)
|
|
215
|
+
expect(result[200]).toBe(100)
|
|
216
|
+
expect(result[201]).toBe(100)
|
|
217
|
+
expect(result[202]).toBe(100)
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test('trickles through multiple levels in deep hierarchy', () => {
|
|
221
|
+
const result = trickleProgress(deepHierarchy, 100, collectionSelf, 50)
|
|
222
|
+
expect(result[200]).toBe(50)
|
|
223
|
+
expect(result[201]).toBe(50)
|
|
224
|
+
expect(result[300]).toBe(50)
|
|
225
|
+
expect(result[301]).toBe(50)
|
|
226
|
+
})
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
describe('bubbleProgress', () => {
|
|
230
|
+
beforeEach(() => {
|
|
231
|
+
initializeTestService()
|
|
232
|
+
mockProgressRecords = []
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('no parent returns empty object', () => {
|
|
236
|
+
const result = bubbleProgress(flatHierarchy, 100)
|
|
237
|
+
return expect(result).resolves.toEqual({})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('averages sibling progress for parent', async () => {
|
|
241
|
+
mockProgressRecords = [
|
|
242
|
+
{ content_id: 200, progress_percent: 60 },
|
|
243
|
+
{ content_id: 201, progress_percent: 30 },
|
|
244
|
+
{ content_id: 202, progress_percent: 0 },
|
|
245
|
+
]
|
|
246
|
+
const result = await bubbleProgress(flatHierarchy, 200)
|
|
247
|
+
expect(result[100]).toBe(Math.round((60 + 30 + 0) / 3))
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
test('one sibling with progress and rest at 0 yields rounded average', async () => {
|
|
251
|
+
mockProgressRecords = [
|
|
252
|
+
{ content_id: 201, progress_percent: 90 },
|
|
253
|
+
]
|
|
254
|
+
const result = await bubbleProgress(flatHierarchy, 200)
|
|
255
|
+
expect(result[100]).toBe(Math.round(90 / 3))
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
test('uses contentId own progress in calculation', async () => {
|
|
259
|
+
mockProgressRecords = [
|
|
260
|
+
{ content_id: 200, progress_percent: 100 },
|
|
261
|
+
{ content_id: 201, progress_percent: 100 },
|
|
262
|
+
{ content_id: 202, progress_percent: 100 },
|
|
263
|
+
]
|
|
264
|
+
const result = await bubbleProgress(flatHierarchy, 200)
|
|
265
|
+
expect(result[100]).toBe(100)
|
|
266
|
+
})
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
describe('computeBubbleTrickleProgresses', () => {
|
|
270
|
+
beforeEach(() => {
|
|
271
|
+
initializeTestService()
|
|
272
|
+
mockProgressRecords = []
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test('both bubble and trickle combine results', async () => {
|
|
276
|
+
mockProgressRecords = [
|
|
277
|
+
{ content_id: 200, progress_percent: 100 },
|
|
278
|
+
{ content_id: 201, progress_percent: 100 },
|
|
279
|
+
{ content_id: 202, progress_percent: 100 },
|
|
280
|
+
]
|
|
281
|
+
const result = await computeBubbleTrickleProgresses(100, 100, collectionSelf, flatHierarchy, {
|
|
282
|
+
bubble: true,
|
|
283
|
+
trickle: true,
|
|
284
|
+
})
|
|
285
|
+
expect(result[200]).toBe(100)
|
|
286
|
+
expect(result[201]).toBe(100)
|
|
287
|
+
expect(result[202]).toBe(100)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
test('bubble=false returns only trickled descendants', async () => {
|
|
291
|
+
const result = await computeBubbleTrickleProgresses(100, 75, collectionSelf, flatHierarchy, {
|
|
292
|
+
bubble: false,
|
|
293
|
+
trickle: true,
|
|
294
|
+
})
|
|
295
|
+
expect(result[200]).toBe(75)
|
|
296
|
+
expect(result[201]).toBe(75)
|
|
297
|
+
expect(result[202]).toBe(75)
|
|
298
|
+
expect(result[100]).toBeUndefined()
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
test('trickle=false returns only bubbled ancestors', async () => {
|
|
302
|
+
mockProgressRecords = [
|
|
303
|
+
{ content_id: 200, progress_percent: 60 },
|
|
304
|
+
]
|
|
305
|
+
const result = await computeBubbleTrickleProgresses(200, 60, collectionSelf, flatHierarchy, {
|
|
306
|
+
bubble: true,
|
|
307
|
+
trickle: false,
|
|
308
|
+
})
|
|
309
|
+
expect(result[100]).toBeDefined()
|
|
310
|
+
expect(result[200]).toBeUndefined()
|
|
311
|
+
expect(result[201]).toBeUndefined()
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
test('empty hierarchy returns empty object', async () => {
|
|
315
|
+
const emptyHierarchy = { parents: {}, children: {}, metadata: {} }
|
|
316
|
+
const result = await computeBubbleTrickleProgresses(100, 50, collectionSelf, emptyHierarchy, {
|
|
317
|
+
bubble: true,
|
|
318
|
+
trickle: true,
|
|
319
|
+
})
|
|
320
|
+
expect(result).toEqual({})
|
|
321
|
+
})
|
|
322
|
+
})
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { initializeTestService } from '../../initializeTests.js'
|
|
2
|
+
import {
|
|
3
|
+
duplicateProgressForIds,
|
|
4
|
+
extractFromRecordId,
|
|
5
|
+
filterOutLearningPathsForDuplication,
|
|
6
|
+
filterOutNegativeProgress,
|
|
7
|
+
generateRecordId,
|
|
8
|
+
normalizeCollection,
|
|
9
|
+
normalizeContentId,
|
|
10
|
+
normalizeContentIds,
|
|
11
|
+
} from '../../../src/services/contentProgress.js'
|
|
12
|
+
import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
|
|
13
|
+
|
|
14
|
+
let mockProgressRecords: any[] = []
|
|
15
|
+
|
|
16
|
+
jest.mock('../../../src/services/sync/repository-proxy', () => {
|
|
17
|
+
const mockFns = {
|
|
18
|
+
contentProgress: {
|
|
19
|
+
getOneProgressByContentId: jest.fn().mockImplementation((contentId) => {
|
|
20
|
+
const record = mockProgressRecords.find(r => r.content_id === contentId)
|
|
21
|
+
return Promise.resolve({ data: record || null })
|
|
22
|
+
}),
|
|
23
|
+
getSomeProgressByContentIds: jest.fn().mockImplementation((contentIds) => {
|
|
24
|
+
const records = mockProgressRecords.filter(r => contentIds.includes(r.content_id))
|
|
25
|
+
return Promise.resolve({ data: records })
|
|
26
|
+
}),
|
|
27
|
+
recordProgress: jest.fn().mockResolvedValue({ data: null }),
|
|
28
|
+
requestPushUnsynced: jest.fn(),
|
|
29
|
+
},
|
|
30
|
+
practices: {
|
|
31
|
+
queryAll: jest.fn().mockResolvedValue({ data: [] }),
|
|
32
|
+
getAll: jest.fn().mockResolvedValue({ data: [] }),
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
return { default: mockFns, ...mockFns }
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
jest.mock('../../../src/services/sanity.js', () => ({
|
|
39
|
+
getHierarchy: jest.fn().mockResolvedValue({ metadata: {}, parents: {}, children: {} }),
|
|
40
|
+
getHierarchies: jest.fn().mockResolvedValue({ metadata: {}, parents: {}, children: {} }),
|
|
41
|
+
getSanityDate: jest.fn((date: Date) => date.toISOString()),
|
|
42
|
+
}))
|
|
43
|
+
|
|
44
|
+
jest.mock('../../../src/services/content-org/learning-paths', () => ({
|
|
45
|
+
getDailySession: jest.fn(),
|
|
46
|
+
onLearningPathCompletedActions: jest.fn(),
|
|
47
|
+
}))
|
|
48
|
+
|
|
49
|
+
const flushPromises = () => new Promise(resolve => setImmediate(resolve))
|
|
50
|
+
|
|
51
|
+
const mockRepo = jest.requireMock('../../../src/services/sync/repository-proxy')
|
|
52
|
+
|
|
53
|
+
describe('filterOutNegativeProgress', () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
initializeTestService()
|
|
56
|
+
mockProgressRecords = []
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('drops entry when new progress is less than existing', () => {
|
|
60
|
+
const result = filterOutNegativeProgress({ 101: 30 }, { 101: { progress: 70 } })
|
|
61
|
+
expect(result).not.toHaveProperty('101')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('keeps entry when new progress equals existing', () => {
|
|
65
|
+
const result = filterOutNegativeProgress({ 101: 50 }, { 101: { progress: 50 } })
|
|
66
|
+
expect(result).toHaveProperty('101', 50)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('keeps entry when new progress is greater than existing', () => {
|
|
70
|
+
const result = filterOutNegativeProgress({ 101: 80 }, { 101: { progress: 50 } })
|
|
71
|
+
expect(result).toHaveProperty('101', 80)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('keeps entry when existing progress is 0 and new is also 0', () => {
|
|
75
|
+
const result = filterOutNegativeProgress({ 101: 0 }, { 101: { progress: 0 } })
|
|
76
|
+
expect(result).toHaveProperty('101', 0)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('drops only entries below existing, keeps others in mixed set', () => {
|
|
80
|
+
const result = filterOutNegativeProgress(
|
|
81
|
+
{ 101: 20, 102: 80, 103: 40 },
|
|
82
|
+
{ 101: { progress: 70 }, 102: { progress: 20 }, 103: { progress: 0 } },
|
|
83
|
+
)
|
|
84
|
+
expect(result).not.toHaveProperty('101')
|
|
85
|
+
expect(result).toHaveProperty('102')
|
|
86
|
+
expect(result).toHaveProperty('103')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('returns a new object and does not mutate input', () => {
|
|
90
|
+
const progresses: Record<string, number> = { 101: 10 }
|
|
91
|
+
const result = filterOutNegativeProgress(progresses, { 101: { progress: 50 } })
|
|
92
|
+
expect(result).not.toBe(progresses)
|
|
93
|
+
expect(progresses).toHaveProperty('101', 10)
|
|
94
|
+
expect(result).not.toHaveProperty('101')
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('filterOutLearningPathsForDuplication', () => {
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
initializeTestService()
|
|
101
|
+
mockProgressRecords = []
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('non-LP collection — all entries pass through', () => {
|
|
105
|
+
const progresses = { 101: 30, 102: 50 }
|
|
106
|
+
const result = filterOutLearningPathsForDuplication(progresses, {
|
|
107
|
+
type: COLLECTION_TYPE.SELF,
|
|
108
|
+
id: COLLECTION_ID_SELF,
|
|
109
|
+
})
|
|
110
|
+
expect(result).toEqual({ 101: 30, 102: 50 })
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('LP collection — excludes entry whose id matches collection id', () => {
|
|
114
|
+
const progresses = { 200: 50, 101: 30, 102: 60 }
|
|
115
|
+
const result = filterOutLearningPathsForDuplication(progresses, {
|
|
116
|
+
type: COLLECTION_TYPE.LEARNING_PATH,
|
|
117
|
+
id: 200,
|
|
118
|
+
})
|
|
119
|
+
expect(result).not.toHaveProperty('200')
|
|
120
|
+
expect(result).toEqual({ 101: 30, 102: 60 })
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('LP collection — string key is coerced via +id comparison', () => {
|
|
124
|
+
const progresses = { '300': 40, 101: 30 }
|
|
125
|
+
const result = filterOutLearningPathsForDuplication(progresses, {
|
|
126
|
+
type: COLLECTION_TYPE.LEARNING_PATH,
|
|
127
|
+
id: 300,
|
|
128
|
+
})
|
|
129
|
+
expect(result).not.toHaveProperty('300')
|
|
130
|
+
expect(result).toHaveProperty('101')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('returns a new object, not the original', () => {
|
|
134
|
+
const progresses = { 101: 30 }
|
|
135
|
+
const result = filterOutLearningPathsForDuplication(progresses, {
|
|
136
|
+
type: COLLECTION_TYPE.SELF,
|
|
137
|
+
id: COLLECTION_ID_SELF,
|
|
138
|
+
})
|
|
139
|
+
expect(result).not.toBe(progresses)
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
describe('duplicateProgressForIds', () => {
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
initializeTestService()
|
|
146
|
+
mockProgressRecords = []
|
|
147
|
+
jest.clearAllMocks()
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('empty object — no recordProgress and no requestPushUnsynced', async () => {
|
|
151
|
+
await duplicateProgressForIds({})
|
|
152
|
+
await flushPromises()
|
|
153
|
+
expect(mockRepo.contentProgress.recordProgress).not.toHaveBeenCalled()
|
|
154
|
+
expect(mockRepo.contentProgress.requestPushUnsynced).not.toHaveBeenCalled()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('single entry — calls recordProgress once and skips push', async () => {
|
|
158
|
+
await duplicateProgressForIds({ 101: 50 })
|
|
159
|
+
await flushPromises()
|
|
160
|
+
expect(mockRepo.contentProgress.recordProgress).toHaveBeenCalledTimes(1)
|
|
161
|
+
expect(mockRepo.contentProgress.requestPushUnsynced).not.toHaveBeenCalled()
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('multiple entries — calls recordProgress per entry and skips push', async () => {
|
|
165
|
+
await duplicateProgressForIds({ 101: 30, 102: 50, 103: 70 })
|
|
166
|
+
await flushPromises()
|
|
167
|
+
expect(mockRepo.contentProgress.recordProgress).toHaveBeenCalledTimes(3)
|
|
168
|
+
expect(mockRepo.contentProgress.requestPushUnsynced).not.toHaveBeenCalled()
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
describe('normalizeContentId', () => {
|
|
173
|
+
beforeEach(() => {
|
|
174
|
+
initializeTestService()
|
|
175
|
+
mockProgressRecords = []
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('number — returns same number', () => {
|
|
179
|
+
expect(normalizeContentId(123)).toBe(123)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test('numeric string — returns number', () => {
|
|
183
|
+
expect(normalizeContentId('123')).toBe(123)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('"0" — returns 0', () => {
|
|
187
|
+
expect(normalizeContentId('0')).toBe(0)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('non-numeric string — throws with correct message', () => {
|
|
191
|
+
expect(() => normalizeContentId('abc')).toThrow('Invalid content id: abc')
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
describe('normalizeContentIds', () => {
|
|
196
|
+
beforeEach(() => {
|
|
197
|
+
initializeTestService()
|
|
198
|
+
mockProgressRecords = []
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('mixed array — all normalized to numbers', () => {
|
|
202
|
+
expect(normalizeContentIds([1, '2', 3])).toEqual([1, 2, 3])
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test('empty array — returns empty array', () => {
|
|
206
|
+
expect(normalizeContentIds([])).toEqual([])
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('throws on invalid entry', () => {
|
|
210
|
+
expect(() => normalizeContentIds([1, 'abc' as any])).toThrow('Invalid content id: abc')
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
describe('normalizeCollection', () => {
|
|
215
|
+
beforeEach(() => {
|
|
216
|
+
initializeTestService()
|
|
217
|
+
mockProgressRecords = []
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test('null — returns null', () => {
|
|
221
|
+
expect(normalizeCollection(null)).toBeNull()
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test('undefined — returns null', () => {
|
|
225
|
+
expect(normalizeCollection(undefined)).toBeNull()
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test('valid with numeric id — returns { type, id }', () => {
|
|
229
|
+
expect(normalizeCollection({ type: COLLECTION_TYPE.SELF, id: 123 })).toEqual({
|
|
230
|
+
type: 'self',
|
|
231
|
+
id: 123,
|
|
232
|
+
})
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('valid with string id — returns { type, id: number }', () => {
|
|
236
|
+
expect(normalizeCollection({ type: COLLECTION_TYPE.LEARNING_PATH, id: '456' as any })).toEqual({
|
|
237
|
+
type: 'learning-path-v2',
|
|
238
|
+
id: 456,
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test('invalid type — throws', () => {
|
|
243
|
+
expect(() => normalizeCollection({ type: 'invalid' as any, id: 1 })).toThrow(
|
|
244
|
+
'Invalid collection type: invalid',
|
|
245
|
+
)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('non-numeric string id — throws', () => {
|
|
249
|
+
expect(() => normalizeCollection({ type: COLLECTION_TYPE.SELF, id: 'abc' as any })).toThrow(
|
|
250
|
+
'Invalid collection id: abc',
|
|
251
|
+
)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
describe('generateRecordId', () => {
|
|
256
|
+
beforeEach(() => {
|
|
257
|
+
initializeTestService()
|
|
258
|
+
mockProgressRecords = []
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
test('0 contentId — returns null (falsy check)', () => {
|
|
262
|
+
expect(generateRecordId(0, null)).toBeNull()
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
test('null contentId — returns null', () => {
|
|
266
|
+
expect(generateRecordId(null as any, null)).toBeNull()
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
test('null collection — uses SELF defaults', () => {
|
|
270
|
+
expect(generateRecordId(123, null)).toBe(
|
|
271
|
+
`123:${COLLECTION_TYPE.SELF}:${COLLECTION_ID_SELF}`,
|
|
272
|
+
)
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
test('LP collection — returns correct format', () => {
|
|
276
|
+
expect(
|
|
277
|
+
generateRecordId(123, { type: COLLECTION_TYPE.LEARNING_PATH, id: 456 }),
|
|
278
|
+
).toBe('123:learning-path-v2:456')
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
test('string contentId — normalized before use', () => {
|
|
282
|
+
expect(generateRecordId('123' as any, null)).toBe(
|
|
283
|
+
`123:${COLLECTION_TYPE.SELF}:${COLLECTION_ID_SELF}`,
|
|
284
|
+
)
|
|
285
|
+
})
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
describe('extractFromRecordId', () => {
|
|
289
|
+
beforeEach(() => {
|
|
290
|
+
initializeTestService()
|
|
291
|
+
mockProgressRecords = []
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
test('null — returns null', () => {
|
|
295
|
+
expect(extractFromRecordId(null)).toBeNull()
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
test('undefined — returns null', () => {
|
|
299
|
+
expect(extractFromRecordId(undefined as any)).toBeNull()
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
test('self record id — correct parse', () => {
|
|
303
|
+
expect(extractFromRecordId('123:self:0')).toEqual({
|
|
304
|
+
contentId: 123,
|
|
305
|
+
collection: { type: 'self', id: 0 },
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
test('LP record id — correct parse', () => {
|
|
310
|
+
expect(extractFromRecordId('456:learning-path-v2:789')).toEqual({
|
|
311
|
+
contentId: 456,
|
|
312
|
+
collection: { type: 'learning-path-v2', id: 789 },
|
|
313
|
+
})
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
test('missing collection type — defaults to SELF', () => {
|
|
317
|
+
expect(extractFromRecordId('123::')).toEqual({
|
|
318
|
+
contentId: 123,
|
|
319
|
+
collection: { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF },
|
|
320
|
+
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test('missing collection id — defaults to COLLECTION_ID_SELF', () => {
|
|
324
|
+
expect(extractFromRecordId('123:self:')).toEqual({
|
|
325
|
+
contentId: 123,
|
|
326
|
+
collection: { type: 'self', id: COLLECTION_ID_SELF },
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
})
|