musora-content-services 2.94.8 → 2.95.1
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 +18 -0
- package/CHANGELOG.md +18 -0
- package/CLAUDE.md +408 -0
- package/babel.config.cjs +10 -0
- package/jsdoc.json +2 -1
- package/package.json +2 -2
- package/src/constants/award-assets.js +35 -0
- package/src/filterBuilder.js +7 -2
- package/src/index.d.ts +26 -5
- package/src/index.js +26 -5
- package/src/services/awards/award-callbacks.js +165 -0
- package/src/services/awards/award-query.js +495 -0
- package/src/services/awards/internal/.indexignore +1 -0
- package/src/services/awards/internal/award-definitions.js +239 -0
- package/src/services/awards/internal/award-events.js +102 -0
- package/src/services/awards/internal/award-manager.js +162 -0
- package/src/services/awards/internal/certificate-builder.js +66 -0
- package/src/services/awards/internal/completion-data-generator.js +84 -0
- package/src/services/awards/internal/content-progress-observer.js +137 -0
- package/src/services/awards/internal/image-utils.js +62 -0
- package/src/services/awards/internal/message-generator.js +17 -0
- package/src/services/awards/internal/types.js +5 -0
- package/src/services/awards/types.d.ts +79 -0
- package/src/services/awards/types.js +101 -0
- package/src/services/config.js +24 -4
- package/src/services/content-org/learning-paths.ts +19 -15
- package/src/services/gamification/awards.ts +114 -83
- package/src/services/progress-events.js +58 -0
- package/src/services/progress-row/method-card.js +20 -5
- package/src/services/sanity.js +1 -1
- package/src/services/sync/fetch.ts +10 -2
- package/src/services/sync/manager.ts +6 -0
- package/src/services/sync/models/ContentProgress.ts +5 -6
- package/src/services/sync/models/UserAwardProgress.ts +55 -0
- package/src/services/sync/models/index.ts +1 -0
- package/src/services/sync/repositories/content-progress.ts +47 -25
- package/src/services/sync/repositories/index.ts +1 -0
- package/src/services/sync/repositories/practices.ts +16 -1
- package/src/services/sync/repositories/user-award-progress.ts +133 -0
- package/src/services/sync/repository-proxy.ts +6 -0
- package/src/services/sync/retry.ts +12 -11
- package/src/services/sync/schema/index.ts +18 -3
- package/src/services/sync/store/index.ts +53 -8
- package/src/services/sync/store/push-coalescer.ts +3 -3
- package/src/services/sync/store-configs.ts +7 -1
- package/src/services/userActivity.js +0 -1
- package/test/HttpClient.test.js +6 -6
- package/test/awards/award-alacarte-observer.test.js +196 -0
- package/test/awards/award-auto-refresh.test.js +83 -0
- package/test/awards/award-calculations.test.js +33 -0
- package/test/awards/award-certificate-display.test.js +328 -0
- package/test/awards/award-collection-edge-cases.test.js +210 -0
- package/test/awards/award-collection-filtering.test.js +285 -0
- package/test/awards/award-completion-flow.test.js +213 -0
- package/test/awards/award-exclusion-handling.test.js +273 -0
- package/test/awards/award-multi-lesson.test.js +241 -0
- package/test/awards/award-observer-integration.test.js +325 -0
- package/test/awards/award-query-messages.test.js +438 -0
- package/test/awards/award-user-collection.test.js +412 -0
- package/test/awards/duplicate-prevention.test.js +118 -0
- package/test/awards/helpers/completion-mock.js +54 -0
- package/test/awards/helpers/index.js +3 -0
- package/test/awards/helpers/mock-setup.js +69 -0
- package/test/awards/helpers/progress-emitter.js +39 -0
- package/test/awards/message-generator.test.js +162 -0
- package/test/initializeTests.js +6 -0
- package/test/mockData/award-definitions.js +171 -0
- package/test/sync/models/award-database-integration.test.js +519 -0
- package/tools/generate-index.cjs +9 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { contentProgressObserver } from '../../src/services/awards/internal/content-progress-observer'
|
|
2
|
+
import { awardEvents } from '../../src/services/awards/internal/award-events'
|
|
3
|
+
import { mockAwardDefinitions } from '../mockData/award-definitions'
|
|
4
|
+
import { setupDefaultMocks, setupAwardEventListeners } from './helpers'
|
|
5
|
+
import { COLLECTION_TYPE, emitProgress, waitForDebounce } from './helpers/progress-emitter'
|
|
6
|
+
|
|
7
|
+
jest.mock('../../src/services/sanity', () => ({
|
|
8
|
+
default: { fetch: jest.fn() },
|
|
9
|
+
fetchSanity: jest.fn()
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
jest.mock('../../src/services/sync/repository-proxy', () => {
|
|
13
|
+
const mockFns = {
|
|
14
|
+
contentProgress: {
|
|
15
|
+
getOneProgressByContentId: jest.fn(),
|
|
16
|
+
getSomeProgressByContentIds: jest.fn(),
|
|
17
|
+
queryOne: jest.fn(),
|
|
18
|
+
queryAll: jest.fn()
|
|
19
|
+
},
|
|
20
|
+
practices: {
|
|
21
|
+
sumPracticeMinutesForContent: jest.fn()
|
|
22
|
+
},
|
|
23
|
+
userAwardProgress: {
|
|
24
|
+
hasCompletedAward: jest.fn(),
|
|
25
|
+
recordAwardProgress: jest.fn(),
|
|
26
|
+
getByAwardId: jest.fn()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { default: mockFns, ...mockFns }
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
import sanityClient, { fetchSanity } from '../../src/services/sanity'
|
|
33
|
+
import db from '../../src/services/sync/repository-proxy'
|
|
34
|
+
import { awardDefinitions } from '../../src/services/awards/internal/award-definitions'
|
|
35
|
+
|
|
36
|
+
describe('Award Collection Filtering - Edge Cases', () => {
|
|
37
|
+
let listeners
|
|
38
|
+
|
|
39
|
+
beforeEach(async () => {
|
|
40
|
+
jest.clearAllMocks()
|
|
41
|
+
awardEvents.removeAllListeners()
|
|
42
|
+
|
|
43
|
+
sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
|
|
44
|
+
setupDefaultMocks(db, fetchSanity)
|
|
45
|
+
|
|
46
|
+
await awardDefinitions.refresh()
|
|
47
|
+
|
|
48
|
+
listeners = setupAwardEventListeners(awardEvents)
|
|
49
|
+
|
|
50
|
+
await contentProgressObserver.start()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
awardEvents.removeAllListeners()
|
|
55
|
+
awardDefinitions.clear()
|
|
56
|
+
contentProgressObserver.stop()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('Child in collection with no awards', () => {
|
|
60
|
+
test('gracefully handles child with no matching awards', async () => {
|
|
61
|
+
emitProgress({
|
|
62
|
+
contentId: 999999,
|
|
63
|
+
collectionType: 'skill-pack',
|
|
64
|
+
collectionId: 999000
|
|
65
|
+
})
|
|
66
|
+
await waitForDebounce()
|
|
67
|
+
|
|
68
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
69
|
+
expect(listeners.progress).not.toHaveBeenCalled()
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
describe('Collection type case sensitivity for learning paths', () => {
|
|
74
|
+
test('wrong case LP collection type falls back to a la carte', async () => {
|
|
75
|
+
emitProgress({
|
|
76
|
+
contentId: 418004,
|
|
77
|
+
collectionType: 'Learning-Path-V2',
|
|
78
|
+
collectionId: 418010
|
|
79
|
+
})
|
|
80
|
+
await waitForDebounce()
|
|
81
|
+
|
|
82
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('case mismatch in non-LP still triggers (a la carte)', async () => {
|
|
86
|
+
emitProgress({
|
|
87
|
+
contentId: 418001,
|
|
88
|
+
collectionType: 'SKILL-PACK',
|
|
89
|
+
collectionId: 418000
|
|
90
|
+
})
|
|
91
|
+
await waitForDebounce()
|
|
92
|
+
|
|
93
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('Debouncing per collection', () => {
|
|
98
|
+
test('rapid completion of children debounces per award content_id', async () => {
|
|
99
|
+
emitProgress({
|
|
100
|
+
contentId: 418001,
|
|
101
|
+
collectionType: 'skill-pack',
|
|
102
|
+
collectionId: 418000
|
|
103
|
+
})
|
|
104
|
+
emitProgress({
|
|
105
|
+
contentId: 418002,
|
|
106
|
+
collectionType: 'skill-pack',
|
|
107
|
+
collectionId: 418000
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
await waitForDebounce()
|
|
111
|
+
|
|
112
|
+
expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledTimes(1)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('rapid completion of children in different contexts processes separately', async () => {
|
|
116
|
+
emitProgress({
|
|
117
|
+
contentId: 418004,
|
|
118
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
119
|
+
collectionId: 418010
|
|
120
|
+
})
|
|
121
|
+
emitProgress({
|
|
122
|
+
contentId: 416448
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
await waitForDebounce()
|
|
126
|
+
|
|
127
|
+
expect(listeners.granted.mock.calls.length).toBeGreaterThanOrEqual(2)
|
|
128
|
+
const awardIds = listeners.granted.mock.calls.map(call => call[0].awardId)
|
|
129
|
+
expect(awardIds).toContain('b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e')
|
|
130
|
+
expect(awardIds).toContain('0238b1e5-ebee-42b3-9390-91467d113575')
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('Observer state management', () => {
|
|
135
|
+
test('stop clears debounce timers', async () => {
|
|
136
|
+
emitProgress({
|
|
137
|
+
contentId: 418001,
|
|
138
|
+
collectionType: 'skill-pack',
|
|
139
|
+
collectionId: 418000
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
contentProgressObserver.stop()
|
|
143
|
+
|
|
144
|
+
expect(contentProgressObserver.debounceTimers.size).toBe(0)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('stop clears processing content IDs', async () => {
|
|
148
|
+
emitProgress({
|
|
149
|
+
contentId: 418001,
|
|
150
|
+
collectionType: 'skill-pack',
|
|
151
|
+
collectionId: 418000
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
contentProgressObserver.stop()
|
|
155
|
+
|
|
156
|
+
expect(contentProgressObserver.processingContentIds.size).toBe(0)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('restart after stop works correctly', async () => {
|
|
160
|
+
contentProgressObserver.stop()
|
|
161
|
+
await contentProgressObserver.start()
|
|
162
|
+
|
|
163
|
+
emitProgress({
|
|
164
|
+
contentId: 418001,
|
|
165
|
+
collectionType: 'skill-pack',
|
|
166
|
+
collectionId: 418000
|
|
167
|
+
})
|
|
168
|
+
await waitForDebounce()
|
|
169
|
+
|
|
170
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
171
|
+
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
describe('Collection ID type handling', () => {
|
|
175
|
+
test('collection ID as number matches correctly for LP', async () => {
|
|
176
|
+
emitProgress({
|
|
177
|
+
contentId: 418004,
|
|
178
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
179
|
+
collectionId: 418010
|
|
180
|
+
})
|
|
181
|
+
await waitForDebounce()
|
|
182
|
+
|
|
183
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
184
|
+
expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
|
|
185
|
+
)
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test('LP collection ID mismatch with correct type fails', async () => {
|
|
189
|
+
emitProgress({
|
|
190
|
+
contentId: 418004,
|
|
191
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
192
|
+
collectionId: 999999
|
|
193
|
+
})
|
|
194
|
+
await waitForDebounce()
|
|
195
|
+
|
|
196
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
test('non-LP collection ID mismatch still triggers (a la carte)', async () => {
|
|
200
|
+
emitProgress({
|
|
201
|
+
contentId: 418001,
|
|
202
|
+
collectionType: 'skill-pack',
|
|
203
|
+
collectionId: 418002
|
|
204
|
+
})
|
|
205
|
+
await waitForDebounce()
|
|
206
|
+
|
|
207
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
208
|
+
})
|
|
209
|
+
})
|
|
210
|
+
})
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { contentProgressObserver } from '../../src/services/awards/internal/content-progress-observer'
|
|
2
|
+
import { awardEvents } from '../../src/services/awards/internal/award-events'
|
|
3
|
+
import { mockAwardDefinitions } from '../mockData/award-definitions'
|
|
4
|
+
import { setupDefaultMocks, setupAwardEventListeners } from './helpers'
|
|
5
|
+
import { COLLECTION_TYPE, emitProgress, waitForDebounce } from './helpers/progress-emitter'
|
|
6
|
+
|
|
7
|
+
jest.mock('../../src/services/sanity', () => ({
|
|
8
|
+
default: { fetch: jest.fn() },
|
|
9
|
+
fetchSanity: jest.fn()
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
jest.mock('../../src/services/sync/repository-proxy', () => {
|
|
13
|
+
const mockFns = {
|
|
14
|
+
contentProgress: {
|
|
15
|
+
getOneProgressByContentId: jest.fn(),
|
|
16
|
+
getSomeProgressByContentIds: jest.fn(),
|
|
17
|
+
queryOne: jest.fn(),
|
|
18
|
+
queryAll: jest.fn()
|
|
19
|
+
},
|
|
20
|
+
practices: {
|
|
21
|
+
sumPracticeMinutesForContent: jest.fn()
|
|
22
|
+
},
|
|
23
|
+
userAwardProgress: {
|
|
24
|
+
hasCompletedAward: jest.fn(),
|
|
25
|
+
recordAwardProgress: jest.fn(),
|
|
26
|
+
getByAwardId: jest.fn()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { default: mockFns, ...mockFns }
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
import sanityClient, { fetchSanity } from '../../src/services/sanity'
|
|
33
|
+
import db from '../../src/services/sync/repository-proxy'
|
|
34
|
+
import { awardDefinitions } from '../../src/services/awards/internal/award-definitions'
|
|
35
|
+
|
|
36
|
+
describe('Award Collection Filtering', () => {
|
|
37
|
+
let listeners
|
|
38
|
+
|
|
39
|
+
beforeEach(async () => {
|
|
40
|
+
jest.clearAllMocks()
|
|
41
|
+
awardEvents.removeAllListeners()
|
|
42
|
+
|
|
43
|
+
sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
|
|
44
|
+
setupDefaultMocks(db, fetchSanity)
|
|
45
|
+
|
|
46
|
+
await awardDefinitions.refresh()
|
|
47
|
+
|
|
48
|
+
listeners = setupAwardEventListeners(awardEvents)
|
|
49
|
+
|
|
50
|
+
await contentProgressObserver.start()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
awardEvents.removeAllListeners()
|
|
55
|
+
awardDefinitions.clear()
|
|
56
|
+
contentProgressObserver.stop()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('Collection type matching', () => {
|
|
60
|
+
test('child in learning-path-v2 triggers only learning-path-v2 award', async () => {
|
|
61
|
+
emitProgress({
|
|
62
|
+
contentId: 418004,
|
|
63
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
64
|
+
collectionId: 418010
|
|
65
|
+
})
|
|
66
|
+
await waitForDebounce()
|
|
67
|
+
|
|
68
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
69
|
+
expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
|
|
70
|
+
)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('child in skill-pack triggers only skill-pack award', async () => {
|
|
74
|
+
emitProgress({
|
|
75
|
+
contentId: 418001,
|
|
76
|
+
collectionType: 'skill-pack',
|
|
77
|
+
collectionId: 418000
|
|
78
|
+
})
|
|
79
|
+
await waitForDebounce()
|
|
80
|
+
|
|
81
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
82
|
+
expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('child in course triggers course award (a la carte)', async () => {
|
|
87
|
+
emitProgress({
|
|
88
|
+
contentId: 416448
|
|
89
|
+
})
|
|
90
|
+
await waitForDebounce()
|
|
91
|
+
|
|
92
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
93
|
+
expect.objectContaining({ awardId: '0238b1e5-ebee-42b3-9390-91467d113575' })
|
|
94
|
+
)
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('Collection ID matching', () => {
|
|
99
|
+
test('same content_type but different content_id does not trigger', async () => {
|
|
100
|
+
emitProgress({
|
|
101
|
+
contentId: 418004,
|
|
102
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
103
|
+
collectionId: 999999
|
|
104
|
+
})
|
|
105
|
+
await waitForDebounce()
|
|
106
|
+
|
|
107
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('both type and ID must match to trigger award', async () => {
|
|
111
|
+
emitProgress({
|
|
112
|
+
contentId: 418001,
|
|
113
|
+
collectionType: 'skill-pack',
|
|
114
|
+
collectionId: 418000
|
|
115
|
+
})
|
|
116
|
+
await waitForDebounce()
|
|
117
|
+
|
|
118
|
+
const call = listeners.granted.mock.calls[0]
|
|
119
|
+
expect(call).toBeDefined()
|
|
120
|
+
expect(call[0].definition.content_type).toBe('skill-pack')
|
|
121
|
+
expect(call[0].definition.content_id).toBe(418000)
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
describe('Overlapping children', () => {
|
|
126
|
+
test('shared child in non-LP context triggers all matching awards (a la carte)', async () => {
|
|
127
|
+
emitProgress({
|
|
128
|
+
contentId: 418003,
|
|
129
|
+
collectionType: 'skill-pack',
|
|
130
|
+
collectionId: 418000
|
|
131
|
+
})
|
|
132
|
+
await waitForDebounce()
|
|
133
|
+
|
|
134
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
135
|
+
expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
|
|
136
|
+
)
|
|
137
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
138
|
+
expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
|
|
139
|
+
)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
test('shared child in learning-path-v2 context triggers only learning-path award', async () => {
|
|
143
|
+
emitProgress({
|
|
144
|
+
contentId: 418003,
|
|
145
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
146
|
+
collectionId: 418010
|
|
147
|
+
})
|
|
148
|
+
await waitForDebounce()
|
|
149
|
+
|
|
150
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
151
|
+
expect.objectContaining({ awardId: 'b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e' })
|
|
152
|
+
)
|
|
153
|
+
expect(listeners.granted).not.toHaveBeenCalledWith(
|
|
154
|
+
expect.objectContaining({ awardId: 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d' })
|
|
155
|
+
)
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
test('non-LP context triggers all awards, LP context triggers only matching LP award', async () => {
|
|
159
|
+
emitProgress({
|
|
160
|
+
contentId: 418003,
|
|
161
|
+
collectionType: 'skill-pack',
|
|
162
|
+
collectionId: 418000
|
|
163
|
+
})
|
|
164
|
+
await waitForDebounce()
|
|
165
|
+
|
|
166
|
+
expect(listeners.granted).toHaveBeenCalledTimes(2)
|
|
167
|
+
|
|
168
|
+
listeners.granted.mockClear()
|
|
169
|
+
db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
|
|
170
|
+
|
|
171
|
+
emitProgress({
|
|
172
|
+
contentId: 418003,
|
|
173
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
174
|
+
collectionId: 418010
|
|
175
|
+
})
|
|
176
|
+
await waitForDebounce()
|
|
177
|
+
|
|
178
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
describe('A la carte collection context', () => {
|
|
183
|
+
test('collectionType null and collectionId null triggers awards (a la carte)', async () => {
|
|
184
|
+
emitProgress({ contentId: 418001 })
|
|
185
|
+
await waitForDebounce()
|
|
186
|
+
|
|
187
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('collectionType present but collectionId null triggers awards (a la carte)', async () => {
|
|
191
|
+
emitProgress({
|
|
192
|
+
contentId: 418001,
|
|
193
|
+
collectionType: 'skill-pack',
|
|
194
|
+
collectionId: null
|
|
195
|
+
})
|
|
196
|
+
await waitForDebounce()
|
|
197
|
+
|
|
198
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('collectionType null but collectionId present triggers awards (a la carte)', async () => {
|
|
202
|
+
emitProgress({
|
|
203
|
+
contentId: 418001,
|
|
204
|
+
collectionType: null,
|
|
205
|
+
collectionId: 418000
|
|
206
|
+
})
|
|
207
|
+
await waitForDebounce()
|
|
208
|
+
|
|
209
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
210
|
+
})
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
describe('Direct string comparison', () => {
|
|
214
|
+
test('learning-path-v2 matches learning-path-v2 exactly', async () => {
|
|
215
|
+
emitProgress({
|
|
216
|
+
contentId: 418004,
|
|
217
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
218
|
+
collectionId: 418010
|
|
219
|
+
})
|
|
220
|
+
await waitForDebounce()
|
|
221
|
+
|
|
222
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
223
|
+
expect.objectContaining({
|
|
224
|
+
definition: expect.objectContaining({
|
|
225
|
+
content_type: 'learning-path-v2'
|
|
226
|
+
})
|
|
227
|
+
})
|
|
228
|
+
)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
test('skill-pack matches skill-pack exactly', async () => {
|
|
232
|
+
emitProgress({
|
|
233
|
+
contentId: 418001,
|
|
234
|
+
collectionType: 'skill-pack',
|
|
235
|
+
collectionId: 418000
|
|
236
|
+
})
|
|
237
|
+
await waitForDebounce()
|
|
238
|
+
|
|
239
|
+
expect(listeners.granted).toHaveBeenCalledWith(
|
|
240
|
+
expect.objectContaining({
|
|
241
|
+
definition: expect.objectContaining({
|
|
242
|
+
content_type: 'skill-pack'
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
)
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
describe('Non-matching collection context', () => {
|
|
250
|
+
test('LP child in wrong LP collection ID ignores award', async () => {
|
|
251
|
+
emitProgress({
|
|
252
|
+
contentId: 418004,
|
|
253
|
+
collectionType: COLLECTION_TYPE.LEARNING_PATH,
|
|
254
|
+
collectionId: 999999
|
|
255
|
+
})
|
|
256
|
+
await waitForDebounce()
|
|
257
|
+
|
|
258
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
259
|
+
expect(listeners.progress).not.toHaveBeenCalled()
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
test('non-LP child with wrong collection still triggers (a la carte)', async () => {
|
|
263
|
+
emitProgress({
|
|
264
|
+
contentId: 418001,
|
|
265
|
+
collectionType: 'skill-pack',
|
|
266
|
+
collectionId: 418010
|
|
267
|
+
})
|
|
268
|
+
await waitForDebounce()
|
|
269
|
+
|
|
270
|
+
expect(listeners.granted).toHaveBeenCalled()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
test('child not in any award ignores completely', async () => {
|
|
274
|
+
emitProgress({
|
|
275
|
+
contentId: 999999,
|
|
276
|
+
collectionType: 'skill-pack',
|
|
277
|
+
collectionId: 418000
|
|
278
|
+
})
|
|
279
|
+
await waitForDebounce()
|
|
280
|
+
|
|
281
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
282
|
+
expect(listeners.progress).not.toHaveBeenCalled()
|
|
283
|
+
})
|
|
284
|
+
})
|
|
285
|
+
})
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { awardManager } from '../../src/services/awards/internal/award-manager'
|
|
2
|
+
import { awardEvents } from '../../src/services/awards/internal/award-events'
|
|
3
|
+
import { mockAwardDefinitions, getAwardByContentId } from '../mockData/award-definitions'
|
|
4
|
+
import { setupDefaultMocks, setupAwardEventListeners } from './helpers'
|
|
5
|
+
import { mockCompletionStates, mockAllCompleted } from './helpers/completion-mock'
|
|
6
|
+
|
|
7
|
+
jest.mock('../../src/services/sanity', () => ({
|
|
8
|
+
default: { fetch: jest.fn() },
|
|
9
|
+
fetchSanity: jest.fn()
|
|
10
|
+
}))
|
|
11
|
+
|
|
12
|
+
jest.mock('../../src/services/sync/repository-proxy', () => {
|
|
13
|
+
const mockFns = {
|
|
14
|
+
contentProgress: {
|
|
15
|
+
getOneProgressByContentId: jest.fn(),
|
|
16
|
+
getSomeProgressByContentIds: jest.fn(),
|
|
17
|
+
queryOne: jest.fn(),
|
|
18
|
+
queryAll: jest.fn()
|
|
19
|
+
},
|
|
20
|
+
practices: {
|
|
21
|
+
sumPracticeMinutesForContent: jest.fn()
|
|
22
|
+
},
|
|
23
|
+
userAwardProgress: {
|
|
24
|
+
hasCompletedAward: jest.fn(),
|
|
25
|
+
recordAwardProgress: jest.fn(),
|
|
26
|
+
getByAwardId: jest.fn()
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { default: mockFns, ...mockFns }
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
import sanityClient, { fetchSanity } from '../../src/services/sanity'
|
|
33
|
+
import db from '../../src/services/sync/repository-proxy'
|
|
34
|
+
import { awardDefinitions } from '../../src/services/awards/internal/award-definitions'
|
|
35
|
+
|
|
36
|
+
describe('Award Completion Flow - E2E Scenarios', () => {
|
|
37
|
+
let listeners
|
|
38
|
+
const testAward = getAwardByContentId(416446)
|
|
39
|
+
const courseId = 416446
|
|
40
|
+
|
|
41
|
+
beforeEach(async () => {
|
|
42
|
+
jest.clearAllMocks()
|
|
43
|
+
awardEvents.removeAllListeners()
|
|
44
|
+
|
|
45
|
+
sanityClient.fetch = jest.fn().mockResolvedValue(mockAwardDefinitions)
|
|
46
|
+
setupDefaultMocks(db, fetchSanity, { practiceMinutes: 180 })
|
|
47
|
+
|
|
48
|
+
await awardDefinitions.refresh()
|
|
49
|
+
|
|
50
|
+
listeners = setupAwardEventListeners(awardEvents)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
awardEvents.removeAllListeners()
|
|
55
|
+
awardDefinitions.clear()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('Scenario: User completes all lessons in course', () => {
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
mockAllCompleted(db)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('award is granted with 100% progress', async () => {
|
|
64
|
+
await awardManager.onContentCompleted(courseId)
|
|
65
|
+
|
|
66
|
+
expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
|
|
67
|
+
testAward._id,
|
|
68
|
+
100,
|
|
69
|
+
expect.objectContaining({
|
|
70
|
+
completedAt: expect.any(Number),
|
|
71
|
+
completionData: expect.objectContaining({
|
|
72
|
+
content_title: expect.any(String),
|
|
73
|
+
days_user_practiced: expect.any(Number),
|
|
74
|
+
practice_minutes: 180
|
|
75
|
+
}),
|
|
76
|
+
progressData: expect.any(Object),
|
|
77
|
+
immediate: true
|
|
78
|
+
})
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
expect(listeners.granted).toHaveBeenCalledTimes(1)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('awardGranted event contains complete payload', async () => {
|
|
85
|
+
await awardManager.onContentCompleted(courseId)
|
|
86
|
+
|
|
87
|
+
const payload = listeners.granted.mock.calls[0][0]
|
|
88
|
+
|
|
89
|
+
expect(payload).toMatchObject({
|
|
90
|
+
awardId: testAward._id,
|
|
91
|
+
definition: expect.objectContaining({
|
|
92
|
+
name: testAward.name,
|
|
93
|
+
badge: testAward.badge,
|
|
94
|
+
award: testAward.award
|
|
95
|
+
}),
|
|
96
|
+
completionData: expect.objectContaining({
|
|
97
|
+
content_title: expect.any(String),
|
|
98
|
+
days_user_practiced: expect.any(Number),
|
|
99
|
+
practice_minutes: 180
|
|
100
|
+
}),
|
|
101
|
+
popupMessage: expect.stringContaining('Adrian Guided Course Test'),
|
|
102
|
+
timestamp: expect.any(Number)
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('popup message contains correct practice data', async () => {
|
|
107
|
+
await awardManager.onContentCompleted(courseId)
|
|
108
|
+
|
|
109
|
+
const payload = listeners.granted.mock.calls[0][0]
|
|
110
|
+
|
|
111
|
+
expect(payload.popupMessage).toContain('180 minutes')
|
|
112
|
+
expect(payload.popupMessage).toContain('Adrian Guided Course Test')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('award is immediately synced to backend', async () => {
|
|
116
|
+
await awardManager.onContentCompleted(courseId)
|
|
117
|
+
|
|
118
|
+
expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
|
|
119
|
+
testAward._id,
|
|
120
|
+
100,
|
|
121
|
+
expect.objectContaining({
|
|
122
|
+
completedAt: expect.any(Number),
|
|
123
|
+
immediate: true
|
|
124
|
+
})
|
|
125
|
+
)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('multiple event listeners all receive notification', async () => {
|
|
129
|
+
const listener1 = jest.fn()
|
|
130
|
+
const listener2 = jest.fn()
|
|
131
|
+
const listener3 = jest.fn()
|
|
132
|
+
|
|
133
|
+
awardEvents.on('awardGranted', listener1)
|
|
134
|
+
awardEvents.on('awardGranted', listener2)
|
|
135
|
+
awardEvents.on('awardGranted', listener3)
|
|
136
|
+
|
|
137
|
+
await awardManager.onContentCompleted(courseId)
|
|
138
|
+
|
|
139
|
+
expect(listener1).toHaveBeenCalledTimes(1)
|
|
140
|
+
expect(listener2).toHaveBeenCalledTimes(1)
|
|
141
|
+
expect(listener3).toHaveBeenCalledTimes(1)
|
|
142
|
+
|
|
143
|
+
const payload1 = listener1.mock.calls[0][0]
|
|
144
|
+
const payload2 = listener2.mock.calls[0][0]
|
|
145
|
+
expect(payload1.awardId).toBe(payload2.awardId)
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe('Scenario: Parent course completed but children incomplete', () => {
|
|
150
|
+
const multiLessonAward = getAwardByContentId(417049)
|
|
151
|
+
const parentCourseId = 417049
|
|
152
|
+
|
|
153
|
+
test('does not grant award when parent completed but only 2 of 4 children completed', async () => {
|
|
154
|
+
mockCompletionStates(db, [417045, 417046])
|
|
155
|
+
|
|
156
|
+
await awardManager.onContentCompleted(parentCourseId)
|
|
157
|
+
|
|
158
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
159
|
+
expect(db.userAwardProgress.recordAwardProgress).toHaveBeenCalledWith(
|
|
160
|
+
multiLessonAward._id,
|
|
161
|
+
50,
|
|
162
|
+
expect.objectContaining({
|
|
163
|
+
progressData: expect.any(Object)
|
|
164
|
+
})
|
|
165
|
+
)
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('emits awardProgress event with partial progress when parent completed', async () => {
|
|
169
|
+
mockCompletionStates(db, [417045, 417046])
|
|
170
|
+
|
|
171
|
+
await awardManager.onContentCompleted(parentCourseId)
|
|
172
|
+
|
|
173
|
+
expect(listeners.progress).toHaveBeenCalledWith(
|
|
174
|
+
expect.objectContaining({
|
|
175
|
+
awardId: multiLessonAward._id,
|
|
176
|
+
progressPercentage: 50,
|
|
177
|
+
timestamp: expect.any(Number)
|
|
178
|
+
})
|
|
179
|
+
)
|
|
180
|
+
})
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
describe('Scenario: Content has no associated award', () => {
|
|
184
|
+
test('completes gracefully without errors', async () => {
|
|
185
|
+
const nonExistentCourseId = 999999
|
|
186
|
+
|
|
187
|
+
await expect(
|
|
188
|
+
awardManager.onContentCompleted(nonExistentCourseId)
|
|
189
|
+
).resolves.not.toThrow()
|
|
190
|
+
|
|
191
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
192
|
+
})
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
describe('Scenario: User already earned the award', () => {
|
|
196
|
+
beforeEach(() => {
|
|
197
|
+
db.userAwardProgress.hasCompletedAward.mockResolvedValue(true)
|
|
198
|
+
mockAllCompleted(db)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('does not grant award again', async () => {
|
|
202
|
+
await awardManager.onContentCompleted(courseId)
|
|
203
|
+
|
|
204
|
+
expect(listeners.granted).not.toHaveBeenCalled()
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
test('does not update progress', async () => {
|
|
208
|
+
await awardManager.onContentCompleted(courseId)
|
|
209
|
+
|
|
210
|
+
expect(db.userAwardProgress.recordAwardProgress).not.toHaveBeenCalled()
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
})
|