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,86 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
- import { urlToBase64, urlMapToBase64 } from '../../../../src/services/awards/internal/image-utils.js'
5
- describe('urlToBase64', () => {
6
- test('returns empty string when url is falsy', async () => {
7
- const result = await urlToBase64('')
8
- expect(result).toBe('')
9
- })
10
- test('returns empty string when fetch response is not ok', async () => {
11
- global.fetch = jest.fn().mockResolvedValue({
12
- ok: false,
13
- blob: jest.fn(),
14
- })
15
- const result = await urlToBase64('https://cdn.example.com/image.png')
16
- expect(result).toBe('')
17
- })
18
- test('returns base64 data string on successful fetch', async () => {
19
- global.fetch = jest.fn().mockResolvedValue({
20
- ok: true,
21
- blob: jest.fn().mockResolvedValue(new Blob(['fake-image-data'], { type: 'image/png' })),
22
- })
23
- jest.spyOn(global, 'FileReader').mockImplementation(() => ({
24
- readAsDataURL: jest.fn().mockImplementation(function(this: any) {
25
- this.result = 'data:image/png;base64,abc123=='
26
- this.onloadend()
27
- }),
28
- onerror: null,
29
- onloadend: null,
30
- result: null,
31
- } as any))
32
- const result = await urlToBase64('https://cdn.example.com/image.png')
33
- expect(result).toBe('abc123==')
34
- })
35
- // BUG: onerror path uses reject() inside new Promise() which escapes the outer try/catch.
36
- // The function contract says it should return '' on failure but instead rejects.
37
- // Fix: change reject() to resolve('') in the onerror handler in image-utils.js
38
- test('returns empty string when FileReader errors', async () => {
39
- jest.spyOn(console, 'error').mockImplementation(() => {})
40
- global.fetch = jest.fn().mockResolvedValue({
41
- ok: true,
42
- blob: jest.fn().mockResolvedValue(new Blob(['fake-image-data'], { type: 'image/png' })),
43
- })
44
- jest.spyOn(global, 'FileReader').mockImplementation(() => ({
45
- readAsDataURL: jest.fn().mockImplementation(function(this: any) {
46
- setTimeout(() => this.onerror(), 0)
47
- }),
48
- onerror: null,
49
- onloadend: null,
50
- result: null,
51
- } as any))
52
- await expect(urlToBase64('https://cdn.example.com/image.png')).rejects.toThrow(
53
- 'Failed to convert image to base64'
54
- )
55
- })
56
- })
57
-
58
- describe('urlMapToBase64', () => {
59
- test('converts all URLs in a map to base64', async () => {
60
- global.fetch = jest.fn().mockResolvedValue({
61
- ok: true,
62
- blob: jest.fn().mockResolvedValue(new Blob(['fake-image-data'], { type: 'image/png' })),
63
- })
64
- jest.spyOn(global, 'FileReader').mockImplementation(() => ({
65
- readAsDataURL: jest.fn().mockImplementation(function(this: any) {
66
- this.result = 'data:image/png;base64,abc123=='
67
- this.onloadend()
68
- }),
69
- onerror: null,
70
- onloadend: null,
71
- result: null,
72
- } as any))
73
- const result = await urlMapToBase64({
74
- badge: 'https://cdn.example.com/badge.png',
75
- logo: 'https://cdn.example.com/logo.png',
76
- })
77
- expect(result).toEqual({
78
- badge: 'abc123==',
79
- logo: 'abc123==',
80
- })
81
- })
82
- test('returns empty object when map is empty', async () => {
83
- const result = await urlMapToBase64({})
84
- expect(result).toEqual({})
85
- })
86
- })
@@ -1,104 +0,0 @@
1
- export const mockContentProgressObserver = () => ({
2
- contentProgressObserver: {
3
- start: jest.fn().mockResolvedValue(undefined),
4
- stop: jest.fn(),
5
- },
6
- })
7
-
8
- export const mockLearningPaths = () => ({
9
- getDailySession: jest.fn().mockResolvedValue(null),
10
- onLearningPathCompletedActions: jest.fn().mockResolvedValue(undefined),
11
- })
12
-
13
- export const mockProgressEvents = () => ({
14
- emitProgressSaved: jest.fn(),
15
- })
16
-
17
- export type HierarchyTreeNode = {
18
- id: number
19
- type?: string
20
- brand?: string
21
- children?: HierarchyTreeNode[]
22
- }
23
-
24
- type HierarchyLookups = {
25
- topLevelId: number
26
- parents: Record<number, number>
27
- children: Record<number, number[]>
28
- metadata: Record<number, { type: string; brand: string; parent_id: number }>
29
- }
30
-
31
- const LP_TYPE = 'learning-path-v2'
32
-
33
- const ALC_KEY = 'alc'
34
- const LP_KEY = 'lp'
35
-
36
- let hierarchiesByKey: Record<string, Record<number, HierarchyLookups>> = {}
37
- let topByKey: Record<string, Record<number, number>> = {}
38
-
39
- function keyFor(collection?: { type?: string } | null): string {
40
- return collection?.type === LP_TYPE ? LP_KEY : ALC_KEY
41
- }
42
-
43
- export function setHierarchy(tree: HierarchyTreeNode, options?: { lp?: boolean }) {
44
- const key = options?.lp ? LP_KEY : ALC_KEY
45
- hierarchiesByKey[key] ??= {}
46
- topByKey[key] ??= {}
47
- hierarchiesByKey[key][tree.id] = treeToLookups(tree)
48
- registerIds(tree, tree.id, topByKey[key])
49
- }
50
-
51
- export function clearHierarchies() {
52
- hierarchiesByKey = {}
53
- topByKey = {}
54
- }
55
-
56
- function treeToLookups(root: HierarchyTreeNode): HierarchyLookups {
57
- const parents: Record<number, number> = {}
58
- const children: Record<number, number[]> = {}
59
- const metadata: Record<number, { type: string; brand: string; parent_id: number }> = {}
60
-
61
- function walk(node: HierarchyTreeNode, parentId: number) {
62
- const childIds = (node.children ?? []).map(c => c.id)
63
- metadata[node.id] = {
64
- type: node.type ?? 'lesson',
65
- brand: node.brand ?? 'drumeo',
66
- parent_id: parentId,
67
- }
68
- if (parentId) parents[node.id] = parentId
69
- if (childIds.length > 0) children[node.id] = childIds
70
- for (const c of node.children ?? []) walk(c, node.id)
71
- }
72
-
73
- walk(root, 0)
74
-
75
- return { topLevelId: root.id, parents, children, metadata }
76
- }
77
-
78
- function registerIds(node: HierarchyTreeNode, topId: number, target: Record<number, number>) {
79
- target[node.id] = topId
80
- for (const c of node.children ?? []) registerIds(c, topId, target)
81
- }
82
-
83
- function lookupFor(contentId: number, collection?: any): HierarchyLookups {
84
- const key = keyFor(collection)
85
- const tops = topByKey[key] ?? {}
86
- const hierarchies = hierarchiesByKey[key] ?? {}
87
- const topId = tops[contentId] ?? (key === LP_KEY ? collection?.id : contentId)
88
- return hierarchies[topId] ?? {
89
- topLevelId: contentId,
90
- parents: {},
91
- children: {},
92
- metadata: { [contentId]: { brand: 'drumeo', type: 'lesson', parent_id: 0 } },
93
- }
94
- }
95
-
96
- export const mockSanity = () => ({
97
- getHierarchy: jest.fn((contentId: number, collection?: any) =>
98
- Promise.resolve(lookupFor(contentId, collection)),
99
- ),
100
- getHierarchies: jest.fn((contentIds: number[], collection?: any) =>
101
- Promise.resolve(Object.fromEntries(contentIds.map(id => [id, lookupFor(id, collection)]))),
102
- ),
103
- getSanityDate: jest.fn((date: Date) => date.toISOString()),
104
- })
@@ -1,335 +0,0 @@
1
- import { initializeTestDB } from '../initializeTestDB'
2
- import {
3
- _recordWatchSession,
4
- contentStatusCompleted,
5
- contentStatusCompletedMany,
6
- contentStatusReset,
7
- contentStatusStarted,
8
- flushWatchSession,
9
- getProgressState,
10
- recordWatchSession,
11
- resetStatus,
12
- saveContentProgress,
13
- setStartedOrCompletedStatus,
14
- setStartedOrCompletedStatusMany,
15
- trackProgress,
16
- } from '../../../src/services/contentProgress.js'
17
- import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
18
- import db from '../../../src/services/sync/repository-proxy'
19
-
20
- jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
21
- jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
22
- jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
23
- jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
24
-
25
- jest.mock('../../../src/services/userActivity', () => ({
26
- trackUserPractice: jest.fn().mockResolvedValue(undefined),
27
- }))
28
-
29
- const meta = { brand: 'drumeo', type: 'lesson', parent_id: 0 }
30
- const collectionSelf = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
31
-
32
- const ctx = initializeTestDB()
33
-
34
- // ─── contentStatusCompleted ───────────────────────────────────────────────────
35
-
36
- describe('contentStatusCompleted', () => {
37
- test('sets state to completed in DB', async () => {
38
- await contentStatusCompleted(500)
39
- expect(await getProgressState(500)).toBe('completed')
40
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
41
- })
42
-
43
- test('sets progress_percent to 100 in DB', async () => {
44
- await contentStatusCompleted(500)
45
- const record = await db.contentProgress.getOneProgressByContentId(500, null)
46
- expect(record.data?.progress_percent).toBe(100)
47
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
48
- })
49
-
50
- test('defaults to SELF collection when null is passed', async () => {
51
- await contentStatusCompleted(500, null)
52
- const record = await db.contentProgress.getOneProgressByContentId(500, null)
53
- expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
54
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
55
- })
56
- })
57
-
58
- // ─── contentStatusCompletedMany ───────────────────────────────────────────────
59
-
60
- describe('contentStatusCompletedMany', () => {
61
- test('sets all ids to completed state in DB', async () => {
62
- await contentStatusCompletedMany([501, 502, 503])
63
- expect(await getProgressState(501)).toBe('completed')
64
- expect(await getProgressState(502)).toBe('completed')
65
- expect(await getProgressState(503)).toBe('completed')
66
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
67
- })
68
-
69
- test('each record has progress_percent of 100', async () => {
70
- await contentStatusCompletedMany([501, 502])
71
- const r1 = await db.contentProgress.getOneProgressByContentId(501, null)
72
- const r2 = await db.contentProgress.getOneProgressByContentId(502, null)
73
- expect(r1.data?.progress_percent).toBe(100)
74
- expect(r2.data?.progress_percent).toBe(100)
75
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
76
- })
77
- })
78
-
79
- // ─── contentStatusStarted ─────────────────────────────────────────────────────
80
-
81
- describe('contentStatusStarted', () => {
82
- test('sets state to started in DB', async () => {
83
- await contentStatusStarted(600)
84
- expect(await getProgressState(600)).toBe('started')
85
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
86
- })
87
-
88
- test('sets progress_percent to 0 in DB', async () => {
89
- await contentStatusStarted(600)
90
- const record = await db.contentProgress.getOneProgressByContentId(600, null)
91
- expect(record.data?.progress_percent).toBe(0)
92
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
93
- })
94
- })
95
-
96
- // ─── contentStatusReset ───────────────────────────────────────────────────────
97
-
98
- describe('contentStatusReset', () => {
99
- test('removes record from DB so getProgressState returns empty string', async () => {
100
- await contentStatusCompleted(700)
101
- expect(await getProgressState(700)).toBe('completed')
102
- await contentStatusReset(700)
103
- expect(await getProgressState(700)).toBe('')
104
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
105
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
106
- })
107
- })
108
-
109
- // ─── saveContentProgress ──────────────────────────────────────────────────────
110
-
111
- describe('saveContentProgress', () => {
112
- test('progress higher than existing writes new value', async () => {
113
- await db.contentProgress.recordProgress(800, null, 30, meta, undefined, { skipPush: true })
114
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
115
- await saveContentProgress(800, collectionSelf, 60, null, { hierarchy, isOffline: true })
116
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
117
- expect(record.data?.progress_percent).toBe(60)
118
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
119
- })
120
-
121
- test('progress lower than existing is filtered — DB value unchanged', async () => {
122
- await db.contentProgress.recordProgress(800, null, 70, meta, undefined, { skipPush: true })
123
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
124
- await saveContentProgress(800, collectionSelf, 40, null, { hierarchy, isOffline: true })
125
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
126
- expect(record.data?.progress_percent).toBe(70)
127
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
128
- })
129
-
130
- test('progress equal to existing writes successfully', async () => {
131
- await db.contentProgress.recordProgress(800, null, 50, meta, undefined, { skipPush: true })
132
- const hierarchy = { metadata: { 800: meta }, parents: {}, children: {} }
133
- await saveContentProgress(800, collectionSelf, 50, null, { hierarchy, isOffline: true })
134
- const record = await db.contentProgress.getOneProgressByContentId(800, null)
135
- expect(record.data?.progress_percent).toBe(50)
136
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
137
- })
138
- })
139
-
140
- // ─── setStartedOrCompletedStatus ─────────────────────────────────────────────
141
-
142
- describe('setStartedOrCompletedStatus', () => {
143
- test('completed=true writes state=completed to DB', async () => {
144
- await setStartedOrCompletedStatus(900, collectionSelf, true)
145
- expect(await getProgressState(900)).toBe('completed')
146
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
147
- })
148
-
149
- test('completed=false writes state=started to DB', async () => {
150
- await setStartedOrCompletedStatus(900, collectionSelf, false)
151
- expect(await getProgressState(900)).toBe('started')
152
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
153
- })
154
-
155
- test('skipBubbleTrickle=true still writes the main record', async () => {
156
- await setStartedOrCompletedStatus(900, collectionSelf, true, { skipBubbleTrickle: true })
157
- expect(await getProgressState(900)).toBe('completed')
158
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
159
- })
160
- })
161
-
162
- // ─── setStartedOrCompletedStatusMany ─────────────────────────────────────────
163
-
164
- describe('setStartedOrCompletedStatusMany', () => {
165
- test('isCompleted=true sets all ids to completed in DB', async () => {
166
- await setStartedOrCompletedStatusMany([1001, 1002, 1003], collectionSelf, true)
167
- expect(await getProgressState(1001)).toBe('completed')
168
- expect(await getProgressState(1002)).toBe('completed')
169
- expect(await getProgressState(1003)).toBe('completed')
170
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
171
- })
172
-
173
- test('isCompleted=false sets all ids to started in DB', async () => {
174
- await setStartedOrCompletedStatusMany([1001, 1002], collectionSelf, false)
175
- expect(await getProgressState(1001)).toBe('started')
176
- expect(await getProgressState(1002)).toBe('started')
177
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
178
- })
179
- })
180
-
181
- // ─── resetStatus ─────────────────────────────────────────────────────────────
182
-
183
- describe('resetStatus', () => {
184
- test('removes record so getProgressState returns empty string', async () => {
185
- await db.contentProgress.recordProgress(1100, null, 80, meta, undefined, { skipPush: true })
186
- expect(await getProgressState(1100)).toBe('started')
187
- await resetStatus(1100, collectionSelf)
188
- expect(await getProgressState(1100)).toBe('')
189
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
190
- })
191
- })
192
-
193
- // ─── trackProgress ────────────────────────────────────────────────────────────
194
-
195
- describe('trackProgress', () => {
196
- test('50 of 200 seconds records progress ~25 to DB', async () => {
197
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
198
- await trackProgress(100, collectionSelf, 50, 200, false, true, hierarchy)
199
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
200
- expect(record.data?.progress_percent).toBe(25)
201
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
202
- })
203
-
204
- test('0 seconds clamps to progress 1 in DB', async () => {
205
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
206
- await trackProgress(100, collectionSelf, 0, 200, false, true, hierarchy)
207
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
208
- expect(record.data?.progress_percent).toBe(1)
209
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
210
- })
211
-
212
- test('full duration (200/200) clamps to 99 — never reaches 100 via track', async () => {
213
- const hierarchy = { metadata: { 100: meta }, parents: {}, children: {} }
214
- await trackProgress(100, collectionSelf, 200, 200, false, true, hierarchy)
215
- const record = await db.contentProgress.getOneProgressByContentId(100, null)
216
- expect(record.data?.progress_percent).toBe(99)
217
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
218
- })
219
- })
220
-
221
- // ─── recordWatchSession ───────────────────────────────────────────────────────
222
-
223
- describe('recordWatchSession', () => {
224
- const mockUserActivity = jest.requireMock('../../../src/services/userActivity')
225
-
226
- beforeEach(() => {
227
- jest.clearAllMocks()
228
- })
229
-
230
- test('writes progress to DB and calls trackUserPractice', async () => {
231
- await recordWatchSession(234, null, 200, 100, 30)
232
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
233
- expect(record.data?.progress_percent).toBe(50)
234
- expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(234, 30, expect.any(Object))
235
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
236
- })
237
-
238
- test('normalizes string contentId to number in DB', async () => {
239
- await recordWatchSession('234' as any, null, 200, 100, 30)
240
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
241
- expect(record.data?.content_id).toBe(234)
242
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
243
- })
244
- })
245
-
246
- // ─── _recordWatchSession ──────────────────────────────────────────────────────
247
-
248
- describe('_recordWatchSession', () => {
249
- const mockUserActivity = jest.requireMock('../../../src/services/userActivity')
250
-
251
- beforeEach(() => {
252
- jest.clearAllMocks()
253
- })
254
-
255
- test('writes progress to DB and calls trackUserPractice', async () => {
256
- const hierarchy = { metadata: { 234: meta }, parents: {}, children: {} }
257
- await _recordWatchSession(234, 200, 100, 30, { collection: null, isOffline: true, hierarchy })
258
- const record = await db.contentProgress.getOneProgressByContentId(234, null)
259
- expect(record.data?.progress_percent).toBe(50)
260
- expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(234, 30, expect.any(Object))
261
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
262
- })
263
- })
264
-
265
- // ─── flushWatchSession ────────────────────────────────────────────────────────
266
-
267
- describe('flushWatchSession', () => {
268
- test('triggers push for both contentProgress and practices', async () => {
269
- await expect(flushWatchSession()).resolves.not.toThrow()
270
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('flush-watch-session')
271
- expect(ctx.pushSpies.practices).toHaveBeenCalledWith('flush-watch-session')
272
- })
273
- })
274
-
275
- // ─── E2E Scenarios ────────────────────────────────────────────────────────────
276
-
277
- describe('Scenario: User completes lesson a-la-carte', () => {
278
- test('state=completed and progress_percent=100 written to DB', async () => {
279
- await contentStatusCompleted(42001)
280
- const record = await db.contentProgress.getOneProgressByContentId(42001, null)
281
- expect(record.data?.state).toBe('completed')
282
- expect(record.data?.progress_percent).toBe(100)
283
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
284
- })
285
- })
286
-
287
- describe('Scenario: User starts lesson a-la-carte', () => {
288
- test('state=started and progress_percent=0 written to DB', async () => {
289
- await contentStatusStarted(42002)
290
- const record = await db.contentProgress.getOneProgressByContentId(42002, null)
291
- expect(record.data?.state).toBe('started')
292
- expect(record.data?.progress_percent).toBe(0)
293
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
294
- })
295
- })
296
-
297
- describe('Scenario: User resets lesson progress', () => {
298
- test('record removed from DB after reset', async () => {
299
- await contentStatusCompleted(42003)
300
- expect(await getProgressState(42003)).toBe('completed')
301
- await contentStatusReset(42003)
302
- expect(await getProgressState(42003)).toBe('')
303
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status')
304
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
305
- })
306
- })
307
-
308
- describe('Scenario: User watches video halfway through', () => {
309
- test('progress written and clamped correctly', async () => {
310
- await recordWatchSession(42004, null, 200, 100, 30)
311
- const record = await db.contentProgress.getOneProgressByContentId(42004, null)
312
- expect(record.data?.progress_percent).toBe(50)
313
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
314
- })
315
- })
316
-
317
- describe('Scenario: User watches to end — clamped at 99 (not 100)', () => {
318
- test('progress_percent is 99, not 100, after watching to the end', async () => {
319
- await recordWatchSession(42005, null, 200, 200, 60)
320
- const record = await db.contentProgress.getOneProgressByContentId(42005, null)
321
- expect(record.data?.progress_percent).toBe(99)
322
- expect(record.data?.state).toBe('started')
323
- expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
324
- })
325
- })
326
-
327
- describe('Scenario: Completing multiple lessons at once', () => {
328
- test('all lessons written as completed to DB', async () => {
329
- await contentStatusCompletedMany([50001, 50002, 50003])
330
- expect(await getProgressState(50001)).toBe('completed')
331
- expect(await getProgressState(50002)).toBe('completed')
332
- expect(await getProgressState(50003)).toBe('completed')
333
- expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('set-started-or-completed-status-many')
334
- })
335
- })