musora-content-services 2.167.2 → 2.167.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/.agent/decisions/2026-07-20-mu2-1724-songs-lessons-counts.md +33 -0
  2. package/.agent/decisions/2026-07-20-query-builder-tostring.md +37 -0
  3. package/CHANGELOG.md +8 -0
  4. package/package.json +2 -1
  5. package/src/contentTypeConfig.js +1 -0
  6. package/src/lib/sanity/query.ts +5 -0
  7. package/src/services/config.js +15 -0
  8. package/src/services/content/artist.ts +1 -3
  9. package/src/services/content/counts.ts +47 -0
  10. package/src/services/content/genre.ts +1 -3
  11. package/src/services/content/instructor.ts +1 -3
  12. package/src/services/userActivity.js +2 -0
  13. package/src/version-info.js +1 -0
  14. package/.claude/settings.local.json +0 -53
  15. package/test/initializeTests.js +0 -60
  16. package/test/integration/awards/award-exclusion-handling.test.ts +0 -275
  17. package/test/integration/awards/internal/image-utils.test.ts +0 -86
  18. package/test/integration/content-progress/__mocks__/mocks.ts +0 -104
  19. package/test/integration/content-progress/contentProgress.test.ts +0 -335
  20. package/test/integration/content-progress/e2eOfflineProgress.test.ts +0 -352
  21. package/test/integration/content-progress/e2eProgress.test.ts +0 -612
  22. package/test/integration/content-progress/getters.test.ts +0 -334
  23. package/test/integration/content-progress/helpers.test.ts +0 -263
  24. package/test/integration/content-progress/offlineContentProgress.test.ts +0 -226
  25. package/test/integration/forums.test.ts +0 -209
  26. package/test/integration/infrastructure/FetchRequestExecutor.test.ts +0 -88
  27. package/test/integration/initializeTestDB.ts +0 -80
  28. package/test/integration/learning-paths.test.ts +0 -854
  29. package/test/integration/notifications.test.ts +0 -178
  30. package/test/integration/progress-row/content-card.test.ts +0 -283
  31. package/test/integration/sync/adapters/idb-errors.test.ts +0 -144
  32. package/test/integration/sync/adapters/sqlite-errors.test.ts +0 -173
  33. package/test/integration/sync/fetch.test.ts +0 -224
  34. package/test/integration/sync/repositories/content-likes.test.ts +0 -99
  35. package/test/integration/sync/repositories/practices.test.ts +0 -179
  36. package/test/integration/sync/repositories/progress.test.ts +0 -245
  37. package/test/integration/sync/repositories/user-award-progress.static.test.ts +0 -68
  38. package/test/integration/sync/repositories/user-award-progress.test.ts +0 -387
  39. package/test/integration/sync/store/cross-user-protection.test.ts +0 -217
  40. package/test/integration/sync/store/store-idb.test.ts +0 -180
  41. package/test/integration/sync/store/store.test.ts +0 -361
  42. package/test/integration/user/permissions.test.js +0 -20
  43. package/test/integration/userActivity.test.ts +0 -102
  44. package/test/live/README.md +0 -29
  45. package/test/live/content.test.js +0 -116
  46. package/test/localStorageMock.js +0 -21
  47. package/test/log.js +0 -5
  48. package/test/mockData/award-definitions.js +0 -171
  49. package/test/mockData/mockData_fetchByRailContentIds_one_content.json +0 -35
  50. package/test/mockData/mockData_progress_content.json +0 -182
  51. package/test/mockData/mockData_sanity_progress_content.json +0 -1451
  52. package/test/mockData/mockData_user_practices.json +0 -9
  53. package/test/setupConsole.js +0 -6
  54. package/test/setupNetworkGuard.js +0 -3
  55. package/test/setupTimers.js +0 -13
  56. package/test/unit/HttpClient.test.ts +0 -267
  57. package/test/unit/awards/award-alacarte-observer.test.ts +0 -199
  58. package/test/unit/awards/award-auto-refresh.test.ts +0 -84
  59. package/test/unit/awards/award-calculations.test.ts +0 -34
  60. package/test/unit/awards/award-callbacks.test.ts +0 -144
  61. package/test/unit/awards/award-certificate-display.test.ts +0 -330
  62. package/test/unit/awards/award-collection-edge-cases.test.ts +0 -212
  63. package/test/unit/awards/award-collection-filtering.test.ts +0 -287
  64. package/test/unit/awards/award-completion-flow.test.ts +0 -306
  65. package/test/unit/awards/award-multi-lesson.test.ts +0 -243
  66. package/test/unit/awards/award-observer-integration.test.ts +0 -327
  67. package/test/unit/awards/award-query-messages.test.ts +0 -448
  68. package/test/unit/awards/award-user-collection.test.ts +0 -416
  69. package/test/unit/awards/duplicate-prevention.test.ts +0 -120
  70. package/test/unit/awards/helpers/completion-mock.ts +0 -56
  71. package/test/unit/awards/helpers/index.ts +0 -3
  72. package/test/unit/awards/helpers/mock-setup.ts +0 -79
  73. package/test/unit/awards/helpers/progress-emitter.ts +0 -45
  74. package/test/unit/awards/message-generator.test.ts +0 -162
  75. package/test/unit/content-progress/bubbleTrickle.test.ts +0 -322
  76. package/test/unit/content-progress/helpers.test.ts +0 -329
  77. package/test/unit/content-progress/navigateTo.test.ts +0 -392
  78. package/test/unit/contentLikes.test.ts +0 -62
  79. package/test/unit/contentMetaData.test.ts +0 -58
  80. package/test/unit/dataContext.test.ts +0 -38
  81. package/test/unit/dateUtils.test.ts +0 -188
  82. package/test/unit/endScreen.test.js +0 -712
  83. package/test/unit/eventsAPI.test.ts +0 -159
  84. package/test/unit/imageSRCBuilder.test.ts +0 -37
  85. package/test/unit/imageSRCVerify.test.ts +0 -155
  86. package/test/unit/infrastructure/DefaultHeaderProvider.test.ts +0 -39
  87. package/test/unit/infrastructure/sanity/ContentClient.test.ts +0 -168
  88. package/test/unit/infrastructure/sanity/DefaultConfigProvider.test.ts +0 -93
  89. package/test/unit/infrastructure/sanity/FetchQueryExecutor.test.ts +0 -174
  90. package/test/unit/infrastructure/sanity/SanityClient.test.ts +0 -140
  91. package/test/unit/lib/__snapshots__/filter.test.ts.snap +0 -5
  92. package/test/unit/lib/filter.test.ts +0 -1154
  93. package/test/unit/lib/lastUpdated.test.ts +0 -21
  94. package/test/unit/lib/query.test.ts +0 -1469
  95. package/test/unit/lib/sanity/decorators/base.test.ts +0 -368
  96. package/test/unit/lib/sanity/decorators/navigate-to.test.ts +0 -361
  97. package/test/unit/lib/sanity/decorators/need-access.test.ts +0 -89
  98. package/test/unit/lib/sanity/decorators/need-lifetime-upgrade.test.ts +0 -85
  99. package/test/unit/lib/sanity/decorators/page-type.test.ts +0 -81
  100. package/test/unit/playerSettings.test.ts +0 -73
  101. package/test/unit/progress-row/content-card.test.ts +0 -314
  102. package/test/unit/progress-row/playlist-card.test.ts +0 -104
  103. package/test/unit/progressRows.test.ts +0 -243
  104. package/test/unit/recommendations.test.js +0 -47
  105. package/test/unit/sanityQueryService.test.ts +0 -200
  106. package/test/unit/search.test.ts +0 -44
  107. package/test/unit/sentry.test.ts +0 -62
  108. package/test/unit/services/progress-internal/bubble.test.ts +0 -279
  109. package/test/unit/services/progress.test.ts +0 -396
  110. package/test/unit/streakMessage.test.ts +0 -251
  111. package/test/unit/sync/context.test.ts +0 -51
  112. package/test/unit/sync/effects/logout-warning.test.ts +0 -158
  113. package/test/unit/sync/effects/push-failure-notification.test.ts +0 -196
  114. package/test/unit/sync/errors/sync-errors.test.ts +0 -106
  115. package/test/unit/sync/errors/validators.test.ts +0 -61
  116. package/test/unit/sync/helpers/TestModel.ts +0 -44
  117. package/test/unit/sync/helpers/index.ts +0 -172
  118. package/test/unit/sync/manager.test.ts +0 -303
  119. package/test/unit/sync/models/user-award-progress.test.ts +0 -82
  120. package/test/unit/sync/resolver.test.ts +0 -229
  121. package/test/unit/sync/retry.test.ts +0 -314
  122. package/test/unit/sync/run-scope.test.ts +0 -23
  123. package/test/unit/sync/stale-record-cleanup.test.ts +0 -107
  124. package/test/unit/sync/store/push-coalescer.test.ts +0 -156
  125. package/test/unit/sync/store-configs.test.ts +0 -37
  126. package/test/unit/sync/telemetry/sync-telemetry.test.ts +0 -118
  127. package/test/unit/sync/utils/event-emitter.test.ts +0 -64
  128. package/test/unit/sync/utils/throttle.test.ts +0 -245
  129. package/test/unit/url-builder.test.ts +0 -72
  130. package/test/unit/user/account.test.ts +0 -66
  131. package/test/unit/user/permissions.test.js +0 -191
@@ -1,245 +0,0 @@
1
- // Break circular dep: sync/index → manager → award chain → UserAwardProgressRepository
2
- // extends SyncRepository before it's defined in the module evaluation order.
3
- jest.mock('@/services/sync/manager', () => ({ default: class SyncManager {} }))
4
- jest.mock('@/services/sync/repository-proxy', () => ({ db: {} }))
5
-
6
- import { Database } from '@nozbe/watermelondb'
7
- import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
8
- import ContentProgress, {
9
- COLLECTION_TYPE,
10
- COLLECTION_ID_SELF,
11
- STATE,
12
- } from '@/services/sync/models/ContentProgress'
13
- import ProgressRepository from '@/services/sync/repositories/content-progress'
14
-
15
- let db: Database
16
- let repo: ProgressRepository
17
-
18
- const BRAND = 'drumeo'
19
- const TYPE = 'lesson'
20
-
21
- function upsertProgress(
22
- contentId: number,
23
- progressPct: number,
24
- overrides: Partial<{
25
- collection: { type: COLLECTION_TYPE; id: number }
26
- brand: string
27
- contentType: string
28
- parentId: number
29
- lastInteractedALaCarte: number | null
30
- }> = {}
31
- ) {
32
- const id = ContentProgress.generateId(contentId, overrides.collection ?? null)
33
- return repo['store'].upsertOne(id, r => {
34
- r.content_id = contentId
35
- r.content_brand = overrides.brand ?? BRAND
36
- r.content_type = overrides.contentType ?? TYPE
37
- r.content_parent_id = overrides.parentId ?? 0
38
- r.collection_type = overrides.collection?.type ?? COLLECTION_TYPE.SELF
39
- r.collection_id = overrides.collection?.id ?? COLLECTION_ID_SELF
40
- r.progress_percent = progressPct
41
- if (overrides.lastInteractedALaCarte !== undefined) {
42
- r.last_interacted_a_la_carte = overrides.lastInteractedALaCarte
43
- }
44
- })
45
- }
46
-
47
- beforeEach(() => {
48
- db = makeDatabase()
49
- const { store } = makeStore(ContentProgress, db)
50
- repo = new ProgressRepository(store)
51
- })
52
-
53
- afterEach(async () => {
54
- await resetDatabase(db)
55
- })
56
-
57
- // ---
58
-
59
- describe('started / completed', () => {
60
- test('started returns records with started state', async () => {
61
- await upsertProgress(100, 50) // started
62
- await upsertProgress(101, 100) // completed
63
- await upsertProgress(102, 30) // started
64
-
65
- const result = await repo.started()
66
- expect(result.map(r => r.content_id).sort()).toEqual([100, 102])
67
- })
68
-
69
- test('completed returns records with completed state', async () => {
70
- await upsertProgress(200, 100) // completed
71
- await upsertProgress(201, 50) // started
72
- await upsertProgress(202, 100) // completed
73
-
74
- const result = await repo.completed()
75
- expect(result.map(r => r.content_id).sort()).toEqual([200, 202])
76
- })
77
-
78
- test('started with limit respects limit', async () => {
79
- await Promise.all([1, 2, 3, 4, 5].map(id => upsertProgress(id, 50)))
80
-
81
- const result = await repo.started(2)
82
- expect(result).toHaveLength(2)
83
- })
84
-
85
- test('started with onlyIds returns content_id array', async () => {
86
- await upsertProgress(300, 50)
87
-
88
- const result = await repo.started(undefined, { onlyIds: true })
89
- expect(result).toContain(300)
90
- })
91
- })
92
-
93
- describe('startedOrCompleted', () => {
94
- test('returns all started and completed records', async () => {
95
- await upsertProgress(400, 50) // started
96
- await upsertProgress(401, 100) // completed
97
- await upsertProgress(402, 30) // started
98
-
99
- const result = await repo.startedOrCompleted()
100
- expect(result.data.map(r => r.content_id).sort()).toEqual([400, 401, 402])
101
- })
102
-
103
- test('filters by brand', async () => {
104
- await upsertProgress(500, 50, { brand: 'drumeo' })
105
- await upsertProgress(501, 50, { brand: 'pianote' })
106
-
107
- const result = await repo.startedOrCompleted({ brand: 'drumeo' })
108
- expect(result.data).toHaveLength(1)
109
- expect(result.data[0].content_id).toBe(500)
110
- })
111
-
112
- test('filters by updatedAfter', async () => {
113
- const now = Date.now()
114
- await upsertProgress(600, 50)
115
-
116
- // Record updated_at is set by BaseModel._prepareCreate to Date.now()
117
- const result = await repo.startedOrCompleted({ updatedAfter: now - 1000 })
118
- expect(result.data.length).toBeGreaterThanOrEqual(1)
119
-
120
- const resultFuture = await repo.startedOrCompleted({ updatedAfter: now + 60000 })
121
- expect(resultFuture.data).toHaveLength(0)
122
- })
123
- })
124
-
125
- describe('collectionTypeFilter', () => {
126
- test('no filter returns all collection types', async () => {
127
- await upsertProgress(700, 50)
128
- await upsertProgress(700, 50, { collection: { type: COLLECTION_TYPE.LEARNING_PATH, id: 99 } })
129
-
130
- const result = await repo.started()
131
- expect(result).toHaveLength(2)
132
- })
133
-
134
- test('aLaCarte filter returns only self-accessed records with lastInteractedALaCarte set', async () => {
135
- const now = Date.now()
136
- await upsertProgress(800, 50, { lastInteractedALaCarte: now }) // aLaCarte accessed
137
- await upsertProgress(801, 50, { lastInteractedALaCarte: null }) // not aLaCarte
138
- await upsertProgress(800, 50, { collection: { type: COLLECTION_TYPE.LEARNING_PATH, id: 42 } }) // LP
139
-
140
- const result = await repo.started(undefined, { include: { aLaCarte: true } })
141
- const ids = result.map(r => r.content_id)
142
- expect(ids).toContain(800)
143
- expect(ids).not.toContain(801)
144
- })
145
-
146
- test('learningPaths filter returns LP parent records', async () => {
147
- // LP parent: collection_type=learning-path-v2 AND content_id === collection_id
148
- await upsertProgress(900, 50, { collection: { type: COLLECTION_TYPE.LEARNING_PATH, id: 900 } }) // parent
149
- await upsertProgress(901, 50, { collection: { type: COLLECTION_TYPE.LEARNING_PATH, id: 900 } }) // child
150
- await upsertProgress(900, 50) // self record for same content
151
-
152
- const result = await repo.started(undefined, { include: { learningPaths: true } })
153
- const ids = result.map(r => r.content_id)
154
- expect(ids).toContain(900)
155
- // child (content_id 901, collection_id 900) should NOT be in result
156
- expect(ids).not.toContain(901)
157
- })
158
- })
159
-
160
- describe('getOneProgressByContentId', () => {
161
- test('returns record for matching contentId', async () => {
162
- await upsertProgress(1000, 75)
163
-
164
- const result = await repo.getOneProgressByContentId(1000)
165
- expect(result.data).not.toBeNull()
166
- expect(result.data!.content_id).toBe(1000)
167
- expect(result.data!.progress_percent).toBe(75)
168
- })
169
-
170
- test('returns null when no record', async () => {
171
- const result = await repo.getOneProgressByContentId(9999)
172
- expect(result.data).toBeNull()
173
- })
174
- })
175
-
176
- describe('getSomeProgressByContentIds', () => {
177
- test('returns multiple records', async () => {
178
- await upsertProgress(1100, 50)
179
- await upsertProgress(1101, 100)
180
- await upsertProgress(1102, 30)
181
-
182
- const result = await repo.getSomeProgressByContentIds([1100, 1101])
183
- expect(result.data).toHaveLength(2)
184
- expect(result.data.map(r => r.content_id).sort()).toEqual([1100, 1101])
185
- })
186
- })
187
-
188
- describe('recordProgress', () => {
189
- test('creates progress record via repository', async () => {
190
- await repo.recordProgress(1200, null, 60, { brand: BRAND, type: TYPE, parent_id: 0 })
191
-
192
- const result = await repo.getOneProgressByContentId(1200)
193
- expect(result.data!.progress_percent).toBe(60)
194
- expect(result.data!.state).toBe(STATE.STARTED)
195
- })
196
-
197
- test('sets state to completed at 100%', async () => {
198
- await repo.recordProgress(1300, null, 100, { brand: BRAND, type: TYPE, parent_id: 0 })
199
-
200
- const result = await repo.getOneProgressByContentId(1300)
201
- expect(result.data!.state).toBe(STATE.COMPLETED)
202
- })
203
-
204
- test('progress_percent never goes backwards', async () => {
205
- await repo.recordProgress(1400, null, 80, { brand: BRAND, type: TYPE, parent_id: 0 })
206
- await repo.recordProgress(1400, null, 40, { brand: BRAND, type: TYPE, parent_id: 0 })
207
-
208
- const result = await repo.getOneProgressByContentId(1400)
209
- expect(result.data!.progress_percent).toBe(80)
210
- })
211
- })
212
-
213
- describe('eraseProgress', () => {
214
- test('removes the record', async () => {
215
- await upsertProgress(1500, 50)
216
- await repo.eraseProgress(1500, null)
217
-
218
- const result = await repo.getOneProgressByContentId(1500)
219
- expect(result.data).toBeNull()
220
- })
221
- })
222
-
223
- describe('mostRecentlyUpdatedId', () => {
224
- test('returns id of most recently updated record', async () => {
225
- await upsertProgress(1600, 50)
226
- // Small delay so updated_at differs
227
- await new Promise(r => setTimeout(r, 2))
228
- await upsertProgress(1601, 50)
229
-
230
- const result = await repo.mostRecentlyUpdatedId([1600, 1601])
231
- // 1601 was updated last
232
- expect(result.data).toBe(ContentProgress.generateId(1601, null))
233
- })
234
- })
235
-
236
- describe('completedByContentIds', () => {
237
- test('returns only completed records in the provided set', async () => {
238
- await upsertProgress(1700, 100) // completed
239
- await upsertProgress(1701, 50) // started
240
- await upsertProgress(1702, 100) // completed
241
-
242
- const result = await repo.completedByContentIds([1700, 1701, 1702])
243
- expect(result.data.map(r => r.content_id).sort()).toEqual([1700, 1702])
244
- })
245
- })
@@ -1,68 +0,0 @@
1
- jest.mock('../../../../src/services/sync/manager', () => ({ default: class SyncManager {} }))
2
- jest.mock('../../../../src/services/sync/repository-proxy', () => ({ db: {} }))
3
-
4
- import UserAwardProgressRepository from '../../../../src/services/sync/repositories/user-award-progress'
5
-
6
- describe('UserAwardProgressRepository static methods', () => {
7
- describe('isCompleted', () => {
8
- test('returns true when completed_at is set and progress_percentage is 100', () => {
9
- const result = UserAwardProgressRepository.isCompleted({
10
- completed_at: '2024-01-01T00:00:00Z',
11
- progress_percentage: 100,
12
- })
13
- expect(result).toBe(true)
14
- })
15
- test('returns false when completed_at is null', () => {
16
- const result = UserAwardProgressRepository.isCompleted({
17
- completed_at: null,
18
- progress_percentage: 100,
19
- })
20
- expect(result).toBe(false)
21
- })
22
- test('returns false when progress_percentage is less than 100', () => {
23
- const result = UserAwardProgressRepository.isCompleted({
24
- completed_at: '2024-01-01T00:00:00Z',
25
- progress_percentage: 50,
26
- })
27
- expect(result).toBe(false)
28
- })
29
- })
30
- describe('isInProgress', () => {
31
- test('returns true when progress is greater than 0 and not completed', () => {
32
- const result = UserAwardProgressRepository.isInProgress({
33
- completed_at: null,
34
- progress_percentage: 50,
35
- })
36
- expect(result).toBe(true)
37
- })
38
- test('returns false when award is completed', () => {
39
- const result = UserAwardProgressRepository.isInProgress({
40
- completed_at: '2024-01-01T00:00:00Z',
41
- progress_percentage: 100,
42
- })
43
- expect(result).toBe(false)
44
- })
45
- test('returns true when progress is 0 and not completed', () => {
46
- const result = UserAwardProgressRepository.isInProgress({
47
- completed_at: null,
48
- progress_percentage: 0,
49
- })
50
- expect(result).toBe(true)
51
- })
52
- })
53
- describe('completedAtDate', () => {
54
- test('returns a Date object when completed_at is set', () => {
55
- const result = UserAwardProgressRepository.completedAtDate({
56
- completed_at: '2024-01-01T00:00:00Z',
57
- })
58
- expect(result).toBeInstanceOf(Date)
59
- expect(result?.toISOString()).toBe('2024-01-01T00:00:00.000Z')
60
- })
61
- test('returns null when completed_at is null', () => {
62
- const result = UserAwardProgressRepository.completedAtDate({
63
- completed_at: null,
64
- })
65
- expect(result).toBeNull()
66
- })
67
- })
68
- })
@@ -1,387 +0,0 @@
1
- jest.mock('@/services/sync/manager', () => ({ default: class SyncManager {} }))
2
- jest.mock('@/services/sync/repository-proxy', () => ({ db: {} }))
3
- jest.mock('../../../../src/services/awards/internal/award-definitions', () => ({
4
- awardDefinitions: {
5
- getByContentId: jest.fn(),
6
- getByContentIds: jest.fn(),
7
- },
8
- }))
9
-
10
- import { Database } from '@nozbe/watermelondb'
11
- import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
12
- import UserAwardProgress from '@/services/sync/models/UserAwardProgress'
13
- import UserAwardProgressRepository from '@/services/sync/repositories/user-award-progress'
14
- import type { CompletionData, AwardDefinition } from '../../../../src/services/awards/types'
15
-
16
- let db: Database
17
- let repo: UserAwardProgressRepository
18
-
19
- const COMPLETION_DATA: CompletionData = {
20
- content_title: 'Test Course',
21
- completed_at: '2024-01-15T10:00:00.000Z',
22
- days_user_practiced: 30,
23
- practice_minutes: 600,
24
- }
25
-
26
- function upsertAward(
27
- awardId: string,
28
- progressPct: number,
29
- overrides: Partial<{
30
- completedAt: string | null
31
- completionData: CompletionData | null
32
- progressData: any
33
- }> = {}
34
- ) {
35
- return repo['store'].upsertOne(awardId, r => {
36
- r.award_id = awardId
37
- r.progress_percentage = progressPct
38
- if (overrides.completedAt !== undefined) {
39
- r.completed_at = overrides.completedAt
40
- }
41
- if (overrides.completionData !== undefined) {
42
- r.completion_data = overrides.completionData
43
- }
44
- if (overrides.progressData !== undefined) {
45
- r.progress_data = overrides.progressData
46
- }
47
- })
48
- }
49
-
50
- beforeEach(() => {
51
- db = makeDatabase()
52
- const { store } = makeStore(UserAwardProgress, db)
53
- repo = new UserAwardProgressRepository(store)
54
- })
55
-
56
- afterEach(async () => {
57
- await resetDatabase(db)
58
- })
59
-
60
- // ---
61
-
62
- describe('static helpers', () => {
63
- test('isCompleted returns true when completed_at set and progress is 100', () => {
64
- expect(UserAwardProgressRepository.isCompleted({
65
- completed_at: '2024-01-15T10:00:00.000Z',
66
- progress_percentage: 100,
67
- })).toBe(true)
68
- })
69
-
70
- test('isCompleted returns false when progress is 100 but completed_at is null', () => {
71
- expect(UserAwardProgressRepository.isCompleted({
72
- completed_at: null,
73
- progress_percentage: 100,
74
- })).toBe(false)
75
- })
76
-
77
- test('isCompleted returns false when completed_at set but progress is not 100', () => {
78
- expect(UserAwardProgressRepository.isCompleted({
79
- completed_at: '2024-01-15T10:00:00.000Z',
80
- progress_percentage: 50,
81
- })).toBe(false)
82
- })
83
-
84
- test('isInProgress returns true when progress >= 0 and not completed', () => {
85
- expect(UserAwardProgressRepository.isInProgress({
86
- completed_at: null,
87
- progress_percentage: 50,
88
- })).toBe(true)
89
- })
90
-
91
- test('isInProgress returns false when completed', () => {
92
- expect(UserAwardProgressRepository.isInProgress({
93
- completed_at: '2024-01-15T10:00:00.000Z',
94
- progress_percentage: 100,
95
- })).toBe(false)
96
- })
97
-
98
- test('completedAtDate returns Date for valid ISO string', () => {
99
- const result = UserAwardProgressRepository.completedAtDate({
100
- completed_at: '2024-01-15T10:00:00.000Z',
101
- })
102
- expect(result).toBeInstanceOf(Date)
103
- expect(result!.getFullYear()).toBe(2024)
104
- })
105
-
106
- test('completedAtDate returns null when completed_at is null', () => {
107
- expect(UserAwardProgressRepository.completedAtDate({ completed_at: null })).toBeNull()
108
- })
109
- })
110
-
111
- // ---
112
-
113
- describe('getAll', () => {
114
- test('returns all records', async () => {
115
- await upsertAward('award-1', 50)
116
- await upsertAward('award-2', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
117
-
118
- const result = await repo.getAll()
119
- expect(result.data).toHaveLength(2)
120
- })
121
-
122
- test('onlyCompleted filters to completed records', async () => {
123
- await upsertAward('award-1', 50)
124
- await upsertAward('award-2', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
125
- await upsertAward('award-3', 100, { completedAt: '2024-01-16T10:00:00.000Z' })
126
-
127
- const result = await repo.getAll({ onlyCompleted: true })
128
- expect(result.data).toHaveLength(2)
129
- expect(result.data.every(r => r.completed_at !== null)).toBe(true)
130
- })
131
-
132
- test('respects limit', async () => {
133
- await Promise.all(['a', 'b', 'c', 'd'].map(id => upsertAward(id, 50)))
134
-
135
- const result = await repo.getAll({ limit: 2 })
136
- expect(result.data).toHaveLength(2)
137
- })
138
-
139
- test('returns empty when no records', async () => {
140
- const result = await repo.getAll()
141
- expect(result.data).toHaveLength(0)
142
- })
143
- })
144
-
145
- // ---
146
-
147
- describe('getCompleted', () => {
148
- test('returns only records with completed_at set', async () => {
149
- await upsertAward('award-1', 50)
150
- await upsertAward('award-2', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
151
-
152
- const result = await repo.getCompleted()
153
- expect(result.data).toHaveLength(1)
154
- expect(result.data[0].award_id).toBe('award-2')
155
- })
156
-
157
- test('respects limit', async () => {
158
- await upsertAward('award-1', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
159
- await upsertAward('award-2', 100, { completedAt: '2024-01-16T10:00:00.000Z' })
160
- await upsertAward('award-3', 100, { completedAt: '2024-01-17T10:00:00.000Z' })
161
-
162
- const result = await repo.getCompleted(2)
163
- expect(result.data).toHaveLength(2)
164
- })
165
- })
166
-
167
- // ---
168
-
169
- describe('getInProgress', () => {
170
- test('returns records with progress > 0 and no completed_at', async () => {
171
- await upsertAward('in-progress', 50)
172
- await upsertAward('completed', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
173
- await upsertAward('not-started', 0)
174
-
175
- const result = await repo.getInProgress()
176
- const ids = result.data.map(r => r.award_id)
177
- expect(ids).toContain('in-progress')
178
- expect(ids).not.toContain('completed')
179
- expect(ids).not.toContain('not-started')
180
- })
181
-
182
- test('respects limit', async () => {
183
- await Promise.all(['a', 'b', 'c'].map(id => upsertAward(id, 50)))
184
-
185
- const result = await repo.getInProgress(2)
186
- expect(result.data).toHaveLength(2)
187
- })
188
- })
189
-
190
- // ---
191
-
192
- describe('getByAwardId / getByAwardIds', () => {
193
- test('getByAwardId returns matching record', async () => {
194
- await upsertAward('award-abc', 75)
195
-
196
- const result = await repo.getByAwardId('award-abc')
197
- expect(result.data).not.toBeNull()
198
- expect(result.data!.award_id).toBe('award-abc')
199
- })
200
-
201
- test('getByAwardId returns null for missing id', async () => {
202
- const result = await repo.getByAwardId('nonexistent')
203
- expect(result.data).toBeNull()
204
- })
205
-
206
- test('getByAwardIds returns all matching records', async () => {
207
- await upsertAward('award-1', 25)
208
- await upsertAward('award-2', 50)
209
- await upsertAward('award-3', 75)
210
-
211
- const result = await repo.getByAwardIds(['award-1', 'award-3'])
212
- expect(result.data).toHaveLength(2)
213
- expect(result.data.map(r => r.award_id).sort()).toEqual(['award-1', 'award-3'])
214
- })
215
-
216
- test('getByAwardIds returns empty for empty input', async () => {
217
- const result = await repo.getByAwardIds([])
218
- expect(result.data).toHaveLength(0)
219
- })
220
- })
221
-
222
- // ---
223
-
224
- describe('hasCompletedAward', () => {
225
- test('returns true when award is completed', async () => {
226
- await upsertAward('award-done', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
227
-
228
- expect(await repo.hasCompletedAward('award-done')).toBe(true)
229
- })
230
-
231
- test('returns false when award has progress but not completed', async () => {
232
- await upsertAward('award-partial', 80)
233
-
234
- expect(await repo.hasCompletedAward('award-partial')).toBe(false)
235
- })
236
-
237
- test('returns false when progress is 100 but completed_at is null', async () => {
238
- await upsertAward('award-no-date', 100)
239
-
240
- expect(await repo.hasCompletedAward('award-no-date')).toBe(false)
241
- })
242
-
243
- test('returns false when award does not exist', async () => {
244
- expect(await repo.hasCompletedAward('nonexistent')).toBe(false)
245
- })
246
- })
247
-
248
- // ---
249
-
250
- describe('recordAwardProgress', () => {
251
- test('creates record with progress percentage', async () => {
252
- await repo.recordAwardProgress('award-x', 60)
253
-
254
- const result = await repo.getByAwardId('award-x')
255
- expect(result.data!.progress_percentage).toBe(60)
256
- expect(result.data!.award_id).toBe('award-x')
257
- })
258
-
259
- test('sets completedAt when provided', async () => {
260
- const completedAt = '2024-06-01T12:00:00.000Z'
261
- await repo.recordAwardProgress('award-x', 100, { completedAt })
262
-
263
- const result = await repo.getByAwardId('award-x')
264
- expect(result.data!.completed_at).toBe(completedAt)
265
- })
266
-
267
- test('sets completionData when provided', async () => {
268
- await repo.recordAwardProgress('award-x', 100, {
269
- completedAt: COMPLETION_DATA.completed_at,
270
- completionData: COMPLETION_DATA,
271
- })
272
-
273
- const result = await repo.getByAwardId('award-x')
274
- expect(result.data!.completion_data).toEqual(COMPLETION_DATA)
275
- })
276
-
277
- test('sets progressData when provided', async () => {
278
- const progressData = { lesson_ids: [1, 2, 3], current: 2 }
279
- await repo.recordAwardProgress('award-x', 50, { progressData })
280
-
281
- const result = await repo.getByAwardId('award-x')
282
- expect(result.data!.progress_data).toEqual(progressData)
283
- })
284
-
285
- test('updates existing record on second call', async () => {
286
- await repo.recordAwardProgress('award-x', 40)
287
- await repo.recordAwardProgress('award-x', 80)
288
-
289
- const result = await repo.getByAwardId('award-x')
290
- expect(result.data!.progress_percentage).toBe(80)
291
- })
292
- })
293
-
294
- // ---
295
-
296
- describe('completeAward', () => {
297
- test('sets progress to 100 and records completionData', async () => {
298
- await repo.completeAward('award-x', COMPLETION_DATA)
299
-
300
- const result = await repo.getByAwardId('award-x')
301
- expect(result.data!.progress_percentage).toBe(100)
302
- expect(result.data!.completion_data).toEqual(COMPLETION_DATA)
303
- })
304
-
305
- test('sets completed_at to an ISO date string', async () => {
306
- const before = new Date().toISOString()
307
- await repo.completeAward('award-x', COMPLETION_DATA)
308
- const after = new Date().toISOString()
309
-
310
- const result = await repo.getByAwardId('award-x')
311
- const completedAt = result.data!.completed_at!
312
- expect(completedAt >= before).toBe(true)
313
- expect(completedAt <= after).toBe(true)
314
- })
315
-
316
- test('hasCompletedAward returns true after completeAward', async () => {
317
- await repo.completeAward('award-x', COMPLETION_DATA)
318
- expect(await repo.hasCompletedAward('award-x')).toBe(true)
319
- })
320
- })
321
-
322
- // ---
323
-
324
- describe('getAwardsForContent', () => {
325
- test('returns definitions and matching progress map', async () => {
326
- const { awardDefinitions } = await import('../../../../src/services/awards/internal/award-definitions')
327
- const mockDefinitions: AwardDefinition[] = [
328
- { _id: 'award-1', name: 'Test Award', content_id: 100, is_active: true, logo: null, badge: null, badge_rear: null, award: 'completion', content_type: 'lesson', type: 'course-completion', brand: 'drumeo', content_title: 'Test', award_custom_text: null, instructor_name: null, child_ids: [] },
329
- ]
330
- ;(awardDefinitions.getByContentId as jest.Mock).mockResolvedValue(mockDefinitions)
331
-
332
- await upsertAward('award-1', 50)
333
-
334
- const result = await repo.getAwardsForContent(100)
335
- expect(result.definitions).toEqual(mockDefinitions)
336
- expect(result.progress.get('award-1')).toBeDefined()
337
- expect(result.progress.get('award-1')!.progress_percentage).toBe(50)
338
- })
339
-
340
- test('returns empty progress map when no progress recorded', async () => {
341
- const { awardDefinitions } = await import('../../../../src/services/awards/internal/award-definitions')
342
- ;(awardDefinitions.getByContentId as jest.Mock).mockResolvedValue([
343
- { _id: 'award-1', name: 'Test Award', content_id: 100, is_active: true, logo: null, badge: null, badge_rear: null, award: 'completion', content_type: 'lesson', type: 'course-completion', brand: 'drumeo', content_title: 'Test', award_custom_text: null, instructor_name: null, child_ids: [] },
344
- ])
345
-
346
- const result = await repo.getAwardsForContent(100)
347
- expect(result.definitions).toHaveLength(1)
348
- expect(result.progress.size).toBe(0)
349
- })
350
- })
351
-
352
- // ---
353
-
354
- describe('getAwardsForContentMany', () => {
355
- test('returns map keyed by contentId with definitions and progress', async () => {
356
- const { awardDefinitions } = await import('../../../../src/services/awards/internal/award-definitions')
357
- const defsByContent = new Map([
358
- [100, [{ _id: 'award-100', name: 'Award 100', content_id: 100, is_active: true, logo: null, badge: null, badge_rear: null, award: 'completion', content_type: 'lesson', type: 'course-completion', brand: 'drumeo', content_title: 'Course 100', award_custom_text: null, instructor_name: null, child_ids: [] }]],
359
- [200, [{ _id: 'award-200', name: 'Award 200', content_id: 200, is_active: true, logo: null, badge: null, badge_rear: null, award: 'completion', content_type: 'lesson', type: 'course-completion', brand: 'drumeo', content_title: 'Course 200', award_custom_text: null, instructor_name: null, child_ids: [] }]],
360
- ])
361
- ;(awardDefinitions.getByContentIds as jest.Mock).mockResolvedValue(defsByContent)
362
-
363
- await upsertAward('award-100', 75)
364
-
365
- const result = await repo.getAwardsForContentMany([100, 200])
366
- expect(result.size).toBe(2)
367
- expect(result.get(100)!.progress.get('award-100')!.progress_percentage).toBe(75)
368
- expect(result.get(200)!.progress.size).toBe(0)
369
- })
370
- })
371
-
372
- // ---
373
-
374
- describe('deleteAllAwards', () => {
375
- test('returns deletedCount of 0 when no awards', async () => {
376
- const result = await repo.deleteAllAwards()
377
- expect(result.deletedCount).toBe(0)
378
- })
379
-
380
- test('returns correct deletedCount', async () => {
381
- await upsertAward('award-1', 50)
382
- await upsertAward('award-2', 100, { completedAt: '2024-01-15T10:00:00.000Z' })
383
-
384
- const result = await repo.deleteAllAwards()
385
- expect(result.deletedCount).toBe(2)
386
- })
387
- })