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,47 +0,0 @@
1
- import { fetchSimilarItems } from '../../src/services/recommendations.js'
2
-
3
- jest.mock('../../src/infrastructure/http/HttpClient.ts', () => {
4
- const mockPost = jest.fn()
5
- const MockHttpClient = jest.fn().mockImplementation(() => ({ post: mockPost }))
6
- MockHttpClient._mockPost = mockPost
7
- return { HttpClient: MockHttpClient, GET: jest.fn() }
8
- })
9
-
10
- const mockPost = () => require('../../src/infrastructure/http/HttpClient.ts').HttpClient._mockPost
11
-
12
- describe('fetchSimilarItems', () => {
13
- beforeEach(() => {
14
- mockPost().mockReset()
15
- })
16
-
17
- test('returns empty array when content_id is falsy', async () => {
18
- expect(await fetchSimilarItems(null, 'drumeo')).toEqual([])
19
- expect(await fetchSimilarItems(0, 'drumeo')).toEqual([])
20
- expect(await fetchSimilarItems('', 'drumeo')).toEqual([])
21
- })
22
-
23
- test('parses string content_id to integer for filtering', async () => {
24
- mockPost().mockResolvedValue({ similar_items: [42, 99] })
25
- const result = await fetchSimilarItems('42', 'drumeo', 10)
26
- expect(result).not.toContain(42)
27
- expect(result).toEqual([99])
28
- })
29
-
30
- test('filters out the requested content_id from results', async () => {
31
- mockPost().mockResolvedValue({ similar_items: [1, 2, 3] })
32
- const result = await fetchSimilarItems(2, 'drumeo', 10)
33
- expect(result).toEqual([1, 3])
34
- })
35
-
36
- test('respects count limit', async () => {
37
- mockPost().mockResolvedValue({ similar_items: [10, 20, 30, 40, 50, 60] })
38
- const result = await fetchSimilarItems(99, 'drumeo', 3)
39
- expect(result).toHaveLength(3)
40
- })
41
-
42
- test('returns null on error', async () => {
43
- mockPost().mockRejectedValue(new Error('network failure'))
44
- const result = await fetchSimilarItems(1, 'drumeo')
45
- expect(result).toBeNull()
46
- })
47
- })
@@ -1,200 +0,0 @@
1
- import { initializeTestService } from '../initializeTests.js'
2
- import { getSortOrder } from '../../src/services/sanity.js'
3
- import { processMetadata } from '../../src/contentMetaData.js'
4
- import { FilterBuilder } from '../../src/filterBuilder.js'
5
- import { getFieldsForContentTypeWithFilteredChildren } from '../../src/contentTypeConfig.js'
6
-
7
- jest.mock('../../src/services/permissions/index.ts', () => ({
8
- ...jest.requireActual('../../src/services/permissions/index.ts'),
9
- getPermissionsAdapter: jest.fn().mockReturnValue({
10
- fetchUserPermissions: jest.fn().mockResolvedValue({ permissions: [108, 91, 92], isAdmin: false }),
11
- isAdmin: jest.fn().mockReturnValue(false),
12
- generatePermissionsFilter: jest.fn().mockReturnValue(
13
- `(!defined(permission_v2) || array::intersects(permission_v2, [108,91,92]))`
14
- ),
15
- }),
16
- }))
17
-
18
- describe('Filter Builder', function() {
19
- beforeEach(() => {
20
- initializeTestService()
21
- })
22
-
23
- test('baseConstructor', async () => {
24
- const filter = 'railcontent_id = 111'
25
- let builder = new FilterBuilder(filter, { bypassPermissions: true })
26
- let finalFilter = await builder.buildFilter(filter)
27
- let clauses = spliceFilterForAnds(finalFilter)
28
- // bypassPermissions: true + default statuses auto-sets pullFutureContent: true via getFutureScheduledContentsOnly
29
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id)
30
- expect(clauses[0].phrase).toBe(filter)
31
- expect(clauses[1].field).toBe('status')
32
- expect(clauses[2].phrase).toBe('!defined(deprecated_railcontent_id)')
33
-
34
- builder = new FilterBuilder('', { bypassPermissions: true })
35
- finalFilter = await builder.buildFilter(filter)
36
- clauses = spliceFilterForAnds(finalFilter)
37
- // clauses: [0] status in [...], [1] !defined(deprecated_railcontent_id)
38
- expect(clauses[0].field).toBe('status')
39
- expect(clauses[0].operator).toBe('in')
40
- expect(clauses[1].phrase).toBe('!defined(deprecated_railcontent_id)')
41
- })
42
-
43
- test('withOnlyFilterAvailableStatuses', async () => {
44
- const filter = 'railcontent_id = 111'
45
- const builder = FilterBuilder.withOnlyFilterAvailableStatuses(filter, ['published', 'unlisted'], true)
46
- const finalFilter = await builder.buildFilter()
47
- const clauses = spliceFilterForAnds(finalFilter)
48
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id), [3] published_on <=
49
- expect(clauses[0].phrase).toBe(filter)
50
- expect(clauses[1].field).toBe('status')
51
- expect(clauses[1].operator).toBe('in')
52
- expect(clauses[1].condition).toBe(`['published','unlisted']`)
53
- expect(clauses[3].field).toBe('published_on')
54
- })
55
-
56
- test('withContentStatusAndFutureScheduledContent', async () => {
57
- const filter = 'railcontent_id = 111'
58
- const builder = new FilterBuilder(filter, {
59
- availableContentStatuses: ['published', 'unlisted', 'scheduled'], getFutureScheduledContentsOnly: true,
60
- })
61
- const finalFilter = await builder.buildFilter()
62
- const clauses = spliceFilterForAnds(finalFilter)
63
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id)
64
- // pullFutureContent is set true by getFutureScheduledContentsOnly so no published_on <= clause
65
- expect(clauses[0].phrase).toBe(filter)
66
- expect(clauses[1].field).toBe('status')
67
- expect(clauses[1].operator).toBe('in')
68
- expect(clauses[1].condition).toBe(`['published','unlisted','scheduled']`)
69
- })
70
-
71
- test('withUserPermissions', async () => {
72
- const filter = 'railcontent_id = 111'
73
- const builder = new FilterBuilder(filter)
74
- const finalFilter = await builder.buildFilter()
75
- // permissions from initializeTestService: [108, 91, 92]
76
- // adapter wraps as: (!defined(permission_v2) || array::intersects(...))
77
- const expected = `(!defined(permission_v2) || array::intersects(permission_v2, [108,91,92]))`
78
- const isMatch = finalFilter.includes(expected)
79
- expect(isMatch).toBeTruthy()
80
- })
81
-
82
- test('withUserPermissionsForPlusUser', async () => {
83
- const filter = 'railcontent_id = 111'
84
- const builder = new FilterBuilder(filter)
85
- const finalFilter = await builder.buildFilter()
86
- // permissions from initializeTestService: [108, 91, 92]
87
- // adapter wraps as: (!defined(permission_v2) || array::intersects(...))
88
- const expected = `(!defined(permission_v2) || array::intersects(permission_v2, [108,91,92]))`
89
- const isMatch = finalFilter.includes(expected)
90
- expect(isMatch).toBeTruthy()
91
- })
92
-
93
- test('withPermissionBypass', async () => {
94
- const filter = 'railcontent_id = 111'
95
- const builder = new FilterBuilder(filter, {
96
- bypassPermissions: true, pullFutureContent: false,
97
- })
98
- const finalFilter = await builder.buildFilter()
99
- const clauses = spliceFilterForAnds(finalFilter)
100
- // bypassPermissions: true skips the permission clause entirely
101
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id)
102
- const expected = `(!defined(permission_v2) || array::intersects(permission_v2, [108,91,92]))`
103
- const isMatch = finalFilter.includes(expected)
104
- expect(isMatch).toBeFalsy()
105
- expect(clauses[0].field).toBe('railcontent_id')
106
- expect(clauses[1].field).toBe('status')
107
- expect(clauses[2].phrase).toBe('!defined(deprecated_railcontent_id)')
108
- })
109
-
110
- test('withPublishOnRestrictions', async () => {
111
- const filter = 'railcontent_id = 111'
112
- let builder = new FilterBuilder(filter, {
113
- pullFutureContent: true, bypassPermissions: true,
114
- })
115
-
116
- let finalFilter = await builder.buildFilter()
117
- let clauses = spliceFilterForAnds(finalFilter)
118
- // pullFutureContent: true — _applyPublishingDateRestrictions does nothing (else branch)
119
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id)
120
- expect(clauses[0].phrase).toBe(filter)
121
- expect(clauses[1].field).toBe('status')
122
- expect(clauses[1].operator).toBe('in')
123
- expect(clauses[2].phrase).toBe('!defined(deprecated_railcontent_id)')
124
-
125
- builder = new FilterBuilder(filter, {
126
- getFutureContentOnly: true, bypassPermissions: true,
127
- })
128
- finalFilter = await builder.buildFilter()
129
- clauses = spliceFilterForAnds(finalFilter)
130
- // getFutureContentOnly: true — published_on >= is appended
131
- // clauses: [0] railcontent_id = 111, [1] status in [...], [2] !defined(deprecated_railcontent_id), [3] published_on >=
132
- expect(clauses[0].phrase).toBe(filter)
133
- expect(clauses[3].field).toBe('published_on')
134
- expect(clauses[3].operator).toBe('>=')
135
- })
136
-
137
- function spliceFilterForAnds(filter: string): {
138
- phrase: string;
139
- field: string;
140
- operator: string;
141
- condition: string }[] {
142
- // this will not correctly split complex filters with && and || conditions.
143
- let phrases = filter.split(' && ')
144
- let clauses = []
145
- phrases.forEach((phrase) => {
146
- let field = phrase.substring(0, phrase.indexOf(' '))
147
- //if(field.charAt(0) === '(' ) field = field.substring(1);
148
- const temp = phrase.substring(phrase.indexOf(' ') + 1)
149
- const operator = temp.substring(0, temp.indexOf(' '))
150
- let condition = temp.substring(temp.indexOf(' ') + 1)
151
- //if(condition.charAt(condition.length) === ')') condition = condition.slice(-1);
152
- clauses.push({ phrase, field, operator, condition })
153
- })
154
- return clauses
155
- }
156
- })
157
-
158
- describe('Sanity Queries', function() {
159
- beforeEach(() => {
160
- initializeTestService()
161
- })
162
-
163
- test('getSortOrder', () => {
164
- let sort = getSortOrder()
165
- expect(sort).toBe('published_on desc')
166
- sort = getSortOrder('published_on')
167
- expect(sort).toBe('published_on asc')
168
- sort = getSortOrder('recommended')
169
- expect(sort).toBe('published_on desc')
170
- sort = getSortOrder('slug')
171
- expect(sort).toBe('!defined(title_for_sort), title_for_sort asc, !defined(title), lower(title) asc')
172
- sort = getSortOrder('-slug')
173
- expect(sort).toBe('!defined(title_for_sort), title_for_sort desc, !defined(title), lower(title) desc')
174
- sort = getSortOrder('-slug', 'drumeo', true)
175
- expect(sort).toBe('name desc')
176
- sort = getSortOrder('popularity')
177
- expect(sort).toBe('popularity asc')
178
- sort = getSortOrder('-popularity')
179
- expect(sort).toBe('coalesce(popularity, -1) desc')
180
- sort = getSortOrder('popularity', 'drumeo', 'artist')
181
- expect(sort).toBe('popularity.drumeo asc')
182
- sort = getSortOrder('-popularity', 'drumeo', 'genre')
183
- expect(sort).toBe('coalesce(popularity.drumeo, -1) desc')
184
- })
185
-
186
- test('invalidContentType', async () => {
187
- const metaData = processMetadata('guitareo', 'not a real type')
188
- expect(metaData).toBeNull()
189
- })
190
-
191
- test('newAndScheduledProjectionIncludesChildren', async () => {
192
- const projection = await getFieldsForContentTypeWithFilteredChildren('new-and-scheduled')
193
- expect(projection).toContain('"children": child[')
194
- })
195
-
196
- test('newAndScheduledProjectionIncludesParentFields', async () => {
197
- const projection = await getFieldsForContentTypeWithFilteredChildren('new-and-scheduled')
198
- expect(projection).toContain('show_in_new_feed')
199
- })
200
- })
@@ -1,44 +0,0 @@
1
- import { searchAlgolia } from '../../src/services/search'
2
-
3
- const mockPost = jest.fn()
4
-
5
- jest.mock('../../src/infrastructure/http/HttpClient', () => ({
6
- POST: (...args: unknown[]) => mockPost(...args),
7
- }))
8
-
9
- describe('searchAlgolia', () => {
10
- beforeEach(() => {
11
- mockPost.mockReset()
12
- })
13
-
14
- test('posts requests to the correct endpoint', async () => {
15
- mockPost.mockResolvedValue({ results: [] })
16
-
17
- await searchAlgolia([{ query: 'drum', hitsPerPage: 5 }])
18
-
19
- expect(mockPost).toHaveBeenCalledWith('/api/content/v1/search', {
20
- requests: [{ query: 'drum', hitsPerPage: 5 }],
21
- })
22
- })
23
-
24
- test('returns the response from the endpoint', async () => {
25
- const mockResponse = {
26
- results: [{ hits: [{ objectID: 'abc123' }], nbHits: 1 }],
27
- }
28
- mockPost.mockResolvedValue(mockResponse)
29
-
30
- const result = await searchAlgolia([{ query: 'drum' }])
31
-
32
- expect(result).toEqual(mockResponse)
33
- })
34
-
35
- test('passes multiple requests', async () => {
36
- mockPost.mockResolvedValue({ results: [] })
37
-
38
- await searchAlgolia([{ query: 'drum' }, { query: 'piano' }])
39
-
40
- expect(mockPost).toHaveBeenCalledWith('/api/content/v1/search', {
41
- requests: [{ query: 'drum' }, { query: 'piano' }],
42
- })
43
- })
44
- })
@@ -1,62 +0,0 @@
1
- import { composeHandlers } from '../../src/services/sentry/index'
2
-
3
- test('returns value from first handler when it returns non-defined', () => {
4
- const first = jest.fn().mockReturnValue('first')
5
- const second = jest.fn().mockReturnValue('second')
6
- const composed = composeHandlers(first, second)
7
- const result = composed('arg1' as any, 'arg2' as any)
8
-
9
- expect(result).toBe('first')
10
- })
11
-
12
- test('does not call subsequent handlers when first returns a value', () => {
13
- const first = jest.fn().mockReturnValue('first')
14
- const second = jest.fn().mockReturnValue('second')
15
- const composed = composeHandlers(first, second)
16
- composed('arg1' as any, 'arg2' as any)
17
- expect(second).not.toHaveBeenCalled()
18
- })
19
-
20
- test('skips first handler returning undefined and uses second', () => {
21
- const first = jest.fn().mockReturnValue(undefined)
22
- const second = jest.fn().mockReturnValue('second')
23
- const composed = composeHandlers(first, second)
24
- const result = composed('arg1' as any, 'arg2' as any)
25
-
26
- expect(result).toBe('second')
27
- })
28
-
29
- test('skips multiple undefined handlers to find first returning value', () => {
30
- const first = jest.fn().mockReturnValue(undefined)
31
- const second = jest.fn().mockReturnValue(undefined)
32
- const third = jest.fn().mockReturnValue('third')
33
- const composed = composeHandlers(first, second, third)
34
- const result = composed('arg1' as any, 'arg2' as any)
35
-
36
- expect(result).toBe('third')
37
- })
38
-
39
- test('returns args[0] when all handlers return undefined', () => {
40
- const first = jest.fn().mockReturnValue(undefined)
41
- const second = jest.fn().mockReturnValue(undefined)
42
- const composed = composeHandlers(first, second)
43
- const result = composed('fallback' as any, 'arg2' as any)
44
-
45
- expect(result).toBe('fallback')
46
- })
47
-
48
- test('returns args[0] when no handlers are provided', () => {
49
- const composed = composeHandlers()
50
- const result = composed('fallback' as any, 'arg2' as any)
51
- expect(result).toBe('fallback')
52
- })
53
-
54
-
55
- test('passes all arguments to each handler', () => {
56
- const first = jest.fn().mockReturnValue(undefined)
57
- const second = jest.fn().mockReturnValue('second')
58
- const composed = composeHandlers(first, second)
59
- composed('arg1' as any, 'arg2' as any)
60
- expect(first).toHaveBeenCalledWith('arg1', 'arg2')
61
- expect(second).toHaveBeenCalledWith('arg1', 'arg2')
62
- })
@@ -1,279 +0,0 @@
1
- let mockProgressRecords: any[] = []
2
- let mockRecordProgressMany = jest.fn()
3
- let mockEraseProgressMany = jest.fn()
4
-
5
- const repoMocks = {
6
- contentProgress: {
7
- getSomeProgressByContentIds: jest.fn().mockImplementation((contentIds: number[]) => {
8
- const records = mockProgressRecords.filter((r) => contentIds.includes(r.content_id))
9
- return Promise.resolve({ data: records })
10
- }),
11
- recordProgressMany: mockRecordProgressMany,
12
- eraseProgressMany: mockEraseProgressMany,
13
- },
14
- }
15
-
16
- jest.mock('../../../../src/services/sync/repository-proxy', () => ({
17
- __esModule: true,
18
- default: repoMocks,
19
- ...repoMocks,
20
- }))
21
-
22
- import {
23
- averageProgressesFor,
24
- bubbleAndTrickleProgressesSafely,
25
- bubbleProgress,
26
- computeBubbleTrickleProgresses,
27
- filterOutNegativeProgress,
28
- getAncestorAndSiblingIds,
29
- getChildrenToDepth,
30
- trickleProgress,
31
- } from '../../../../src/services/progress/internal/bubble'
32
- import type { Hierarchy } from '../../../../src/services/progress/types'
33
- import { COLLECTION_TYPE } from '../../../../src/services/sync/models/ContentProgress'
34
-
35
- beforeEach(() => {
36
- jest.clearAllMocks()
37
- mockProgressRecords = []
38
- mockRecordProgressMany.mockResolvedValue({ data: null })
39
- mockEraseProgressMany.mockResolvedValue({ data: null })
40
- })
41
-
42
- describe('filterOutNegativeProgress', () => {
43
- test('drops entry when new progress is less than existing', () => {
44
- const result = filterOutNegativeProgress(
45
- { 101: 30 },
46
- { 101: { progress: 70, last_update: 0, status: 'started' } }
47
- )
48
- expect(result).not.toHaveProperty('101')
49
- })
50
-
51
- test('keeps entry when new progress equals existing', () => {
52
- const result = filterOutNegativeProgress(
53
- { 101: 50 },
54
- { 101: { progress: 50, last_update: 0, status: 'started' } }
55
- )
56
- expect(result[101]).toBe(50)
57
- })
58
-
59
- test('keeps entry when new progress is greater than existing', () => {
60
- const result = filterOutNegativeProgress(
61
- { 101: 80 },
62
- { 101: { progress: 50, last_update: 0, status: 'started' } }
63
- )
64
- expect(result[101]).toBe(80)
65
- })
66
-
67
- test('keeps entry when no existing snapshot', () => {
68
- const result = filterOutNegativeProgress({ 101: 10 }, {})
69
- expect(result[101]).toBe(10)
70
- })
71
-
72
- test('drops only entries below existing, keeps others in mixed set', () => {
73
- const result = filterOutNegativeProgress(
74
- { 101: 20, 102: 80, 103: 40 },
75
- {
76
- 101: { progress: 70, last_update: 0, status: 'started' },
77
- 102: { progress: 20, last_update: 0, status: 'started' },
78
- 103: { progress: 0, last_update: 0, status: '' },
79
- }
80
- )
81
- expect(result).not.toHaveProperty('101')
82
- expect(result[102]).toBe(80)
83
- expect(result[103]).toBe(40)
84
- })
85
-
86
- test('returns new object, does not mutate input', () => {
87
- const progresses = { 101: 10 }
88
- const result = filterOutNegativeProgress(progresses, {
89
- 101: { progress: 50, last_update: 0, status: 'started' },
90
- })
91
- expect(result).not.toBe(progresses)
92
- expect(progresses[101]).toBe(10)
93
- })
94
- })
95
-
96
- describe('getChildrenToDepth', () => {
97
- const hierarchy: Hierarchy = {
98
- parents: { 2: 1, 3: 1, 4: 2 },
99
- children: { 1: [2, 3], 2: [4], 3: [], 4: [] },
100
- }
101
-
102
- test('returns direct children', () => {
103
- expect(getChildrenToDepth(1, hierarchy)).toEqual(expect.arrayContaining([2, 3]))
104
- })
105
-
106
- test('returns empty when no children', () => {
107
- expect(getChildrenToDepth(4, hierarchy)).toEqual([])
108
- })
109
-
110
- test('returns empty for unknown parent', () => {
111
- expect(getChildrenToDepth(99, hierarchy)).toEqual([])
112
- })
113
- })
114
-
115
- describe('getAncestorAndSiblingIds', () => {
116
- const hierarchy: Hierarchy = {
117
- parents: { 2: 1, 3: 1, 4: 2, 5: 2 },
118
- children: { 1: [2, 3], 2: [4, 5] },
119
- }
120
-
121
- test('returns siblings and parent for child node', () => {
122
- const result = getAncestorAndSiblingIds(hierarchy, 4)
123
- expect(result).toEqual(expect.arrayContaining([5, 2]))
124
- })
125
-
126
- test('returns empty for root', () => {
127
- expect(getAncestorAndSiblingIds(hierarchy, 1)).toEqual([])
128
- })
129
-
130
- test('handles circular dependency without infinite loop', () => {
131
- const circular: Hierarchy = { parents: { 1: 1 }, children: { 1: [] } }
132
- expect(getAncestorAndSiblingIds(circular, 1)).toEqual([])
133
- })
134
- })
135
-
136
- describe('averageProgressesFor', () => {
137
- const hierarchy: Hierarchy = {
138
- parents: { 2: 1, 3: 1, 4: 2, 5: 2 },
139
- children: { 1: [2, 3], 2: [4, 5] },
140
- }
141
-
142
- test('averages child progresses up the chain', () => {
143
- const progress = new Map<number, number>([
144
- [4, 100],
145
- [5, 0],
146
- [2, 50],
147
- [3, 50],
148
- ])
149
- const result = averageProgressesFor(hierarchy, 4, progress)
150
- expect(result[2]).toBe(50)
151
- expect(result[1]).toBe(50)
152
- })
153
-
154
- test('returns empty for root', () => {
155
- expect(averageProgressesFor(hierarchy, 1, new Map())).toEqual({})
156
- })
157
-
158
- test('treats missing child progress as 0', () => {
159
- const progress = new Map<number, number>([[4, 80]])
160
- const result = averageProgressesFor(hierarchy, 4, progress)
161
- expect(result[2]).toBe(40)
162
- })
163
- })
164
-
165
- describe('trickleProgress', () => {
166
- const hierarchy: Hierarchy = {
167
- parents: { 2: 1, 3: 1, 4: 2 },
168
- children: { 1: [2, 3], 2: [4], 3: [], 4: [] },
169
- }
170
-
171
- test('returns descendants with given progress', () => {
172
- const result = trickleProgress(hierarchy, 1, 75)
173
- expect(result[2]).toBe(75)
174
- expect(result[3]).toBe(75)
175
- })
176
-
177
- test('returns empty when no descendants', () => {
178
- expect(trickleProgress(hierarchy, 4, 100)).toEqual({})
179
- })
180
- })
181
-
182
- describe('bubbleProgress', () => {
183
- const hierarchy: Hierarchy = {
184
- parents: { 2: 1, 3: 1 },
185
- children: { 1: [2, 3] },
186
- }
187
-
188
- test('aggregates sibling progresses into parent', async () => {
189
- mockProgressRecords = [
190
- { content_id: 2, progress_percent: 100 },
191
- { content_id: 3, progress_percent: 0 },
192
- ]
193
- const result = await bubbleProgress(hierarchy, 2)
194
- expect(result[1]).toBe(50)
195
- })
196
- })
197
-
198
- describe('computeBubbleTrickleProgresses', () => {
199
- const hierarchy: Hierarchy = {
200
- parents: { 2: 1 },
201
- children: { 1: [2], 2: [] },
202
- }
203
-
204
- test('combines bubble and trickle', async () => {
205
- mockProgressRecords = [{ content_id: 2, progress_percent: 80 }]
206
- const result = await computeBubbleTrickleProgresses(2, 80, hierarchy)
207
- expect(result[1]).toBe(80)
208
- })
209
-
210
- test('respects { bubble: false }', async () => {
211
- mockProgressRecords = [{ content_id: 2, progress_percent: 80 }]
212
- const result = await computeBubbleTrickleProgresses(2, 80, hierarchy, undefined, {
213
- bubble: false,
214
- })
215
- expect(result).not.toHaveProperty('1')
216
- })
217
-
218
- test('respects { trickle: false }', async () => {
219
- const result = await computeBubbleTrickleProgresses(1, 50, hierarchy, undefined, {
220
- trickle: false,
221
- })
222
- expect(result).not.toHaveProperty('2')
223
- })
224
- })
225
-
226
- describe('bubbleAndTrickleProgressesSafely', () => {
227
- test('passes positive progresses to recordProgressMany', async () => {
228
- await bubbleAndTrickleProgressesSafely({ 1: 50, 2: 100 }, {})
229
- expect(mockRecordProgressMany).toHaveBeenCalledWith(
230
- { 1: 50, 2: 100 },
231
- undefined,
232
- {},
233
- { skipPush: true, accessedDirectly: true, allowRegression: true }
234
- )
235
- expect(mockEraseProgressMany).not.toHaveBeenCalled()
236
- })
237
-
238
- test('isResetAction separates zero progresses into eraseProgressMany', async () => {
239
- await bubbleAndTrickleProgressesSafely(
240
- { 1: 50, 2: 0, 3: 0 },
241
- {},
242
- { isResetAction: true }
243
- )
244
- expect(mockRecordProgressMany).toHaveBeenCalledWith(
245
- { 1: 50 },
246
- undefined,
247
- {},
248
- expect.objectContaining({ allowRegression: true })
249
- )
250
- expect(mockEraseProgressMany).toHaveBeenCalledWith([2, 3], undefined, { skipPush: true })
251
- })
252
-
253
- test('forwards collection argument', async () => {
254
- const collection = { type: COLLECTION_TYPE.LEARNING_PATH, id: 10 }
255
- await bubbleAndTrickleProgressesSafely({ 1: 50 }, {}, {}, collection)
256
- expect(mockRecordProgressMany).toHaveBeenCalledWith(
257
- { 1: 50 },
258
- collection,
259
- {},
260
- expect.anything()
261
- )
262
- })
263
-
264
- test('forwards accessedDirectly option', async () => {
265
- await bubbleAndTrickleProgressesSafely({ 1: 50 }, {}, { accessedDirectly: false })
266
- expect(mockRecordProgressMany).toHaveBeenCalledWith(
267
- { 1: 50 },
268
- undefined,
269
- {},
270
- expect.objectContaining({ accessedDirectly: false })
271
- )
272
- })
273
-
274
- test('skips both calls when all empty', async () => {
275
- await bubbleAndTrickleProgressesSafely({}, {})
276
- expect(mockRecordProgressMany).not.toHaveBeenCalled()
277
- expect(mockEraseProgressMany).not.toHaveBeenCalled()
278
- })
279
- })