musora-content-services 2.158.3 → 2.159.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +12 -0
- package/.github/workflows/automated-testing.yml +21 -1
- package/CHANGELOG.md +8 -0
- package/README.md +21 -2
- package/jest.config.js +1 -4
- package/jest.integration.config.js +6 -0
- package/jest.live.config.js +1 -5
- package/package.json +5 -2
- package/src/contentTypeConfig.js +8 -5
- package/src/index.d.ts +2 -6
- package/src/index.js +2 -6
- package/src/services/content-org/learning-paths.ts +44 -39
- package/src/services/contentProgress.js +216 -207
- package/src/services/offline/progress.ts +107 -27
- package/src/services/sanity.js +55 -64
- package/src/services/sync/models/ContentProgress.ts +50 -34
- package/src/services/sync/repositories/content-progress.ts +105 -92
- package/test/{unit → integration}/awards/award-exclusion-handling.test.ts +2 -2
- package/test/integration/content-progress/__mocks__/mocks.ts +104 -0
- package/test/integration/content-progress/contentProgress.test.ts +335 -0
- package/test/integration/content-progress/e2eOfflineProgress.test.ts +352 -0
- package/test/integration/content-progress/e2eProgress.test.ts +612 -0
- package/test/integration/content-progress/getters.test.ts +334 -0
- package/test/integration/content-progress/helpers.test.ts +263 -0
- package/test/integration/content-progress/offlineContentProgress.test.ts +226 -0
- package/test/integration/forums.test.ts +209 -0
- package/test/integration/initializeTestDB.ts +80 -0
- package/test/{unit → integration}/sync/fetch.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/content-likes.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/practices.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/progress.test.ts +1 -1
- package/test/{unit → integration}/sync/repositories/user-award-progress.test.ts +1 -1
- package/test/{unit → integration}/sync/store/cross-user-protection.test.ts +2 -2
- package/test/{unit → integration}/sync/store/store-idb.test.ts +2 -2
- package/test/{unit → integration}/sync/store/store.test.ts +2 -2
- package/test/unit/content-progress/bubbleTrickle.test.ts +322 -0
- package/test/unit/content-progress/helpers.test.ts +329 -0
- package/test/unit/content-progress/navigateTo.test.ts +381 -0
- package/test/unit/contentMetaData.test.ts +58 -0
- package/tools/generate-index.cjs +6 -3
- package/test/SKIPPED_TESTS.md +0 -151
- package/test/integration/content.test.js +0 -107
- package/test/integration/contentProgress.test.js +0 -73
- package/test/integration/forum.test.js +0 -16
- package/test/integration/sanityQueryService.test.js +0 -681
- package/test/unit/contentProgress.test.ts +0 -81
- /package/test/{unit → integration}/awards/internal/image-utils.test.ts +0 -0
- /package/test/{unit → integration}/infrastructure/FetchRequestExecutor.test.ts +0 -0
- /package/test/{unit → integration}/notifications.test.ts +0 -0
- /package/test/{unit → integration}/sync/adapters/idb-errors.test.ts +0 -0
- /package/test/{unit → integration}/sync/adapters/sqlite-errors.test.ts +0 -0
- /package/test/{unit → integration}/sync/repositories/user-award-progress.static.test.ts +0 -0
- /package/test/{unit → integration}/userActivity.test.ts +0 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { initializeTestDB } from '../initializeTestDB'
|
|
2
|
+
import {
|
|
3
|
+
contentStatusCompletedManyOffline,
|
|
4
|
+
contentStatusCompletedOffline,
|
|
5
|
+
contentStatusResetOffline,
|
|
6
|
+
contentStatusStartedOffline,
|
|
7
|
+
duplicateProgressToALaCarteOffline,
|
|
8
|
+
recordWatchSessionOffline,
|
|
9
|
+
} from '../../../src/services/offline/progress'
|
|
10
|
+
import { getProgressState } from '../../../src/services/contentProgress.js'
|
|
11
|
+
import { COLLECTION_ID_SELF, COLLECTION_TYPE } from '../../../src/services/sync/models/ContentProgress'
|
|
12
|
+
import db from '../../../src/services/sync/repository-proxy'
|
|
13
|
+
|
|
14
|
+
jest.mock('../../../src/services/sanity.js', () => require('./__mocks__/mocks').mockSanity())
|
|
15
|
+
jest.mock('../../../src/services/content-org/learning-paths.ts', () => require('./__mocks__/mocks').mockLearningPaths())
|
|
16
|
+
jest.mock('../../../src/services/awards/internal/content-progress-observer', () => require('./__mocks__/mocks').mockContentProgressObserver())
|
|
17
|
+
jest.mock('../../../src/services/progress-events', () => require('./__mocks__/mocks').mockProgressEvents())
|
|
18
|
+
|
|
19
|
+
jest.mock('../../../src/services/userActivity', () => ({
|
|
20
|
+
trackUserPractice: jest.fn().mockResolvedValue(undefined),
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
const meta = { brand: 'drumeo', type: 'lesson', parent_id: 0 }
|
|
24
|
+
const collectionSelf = { type: COLLECTION_TYPE.SELF, id: COLLECTION_ID_SELF }
|
|
25
|
+
const collectionLP = (id: number) => ({ type: COLLECTION_TYPE.LEARNING_PATH, id })
|
|
26
|
+
|
|
27
|
+
const flushPromises = async () => {
|
|
28
|
+
for (let i = 0; i < 200; i++) {
|
|
29
|
+
await new Promise(resolve => setImmediate(resolve))
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const hierarchyFor = (contentId: number) => ({
|
|
34
|
+
topLevelId: contentId,
|
|
35
|
+
parents: {},
|
|
36
|
+
children: {},
|
|
37
|
+
metadata: { [contentId]: meta },
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const ctx = initializeTestDB()
|
|
41
|
+
|
|
42
|
+
// ─── contentStatusCompletedOffline ────────────────────────────────────────────
|
|
43
|
+
|
|
44
|
+
describe('contentStatusCompletedOffline', () => {
|
|
45
|
+
test('sets state to completed in DB', async () => {
|
|
46
|
+
await contentStatusCompletedOffline(500, null, hierarchyFor(500))
|
|
47
|
+
expect(await getProgressState(500)).toBe('completed')
|
|
48
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('sets progress_percent to 100 in DB', async () => {
|
|
52
|
+
await contentStatusCompletedOffline(500, null, hierarchyFor(500))
|
|
53
|
+
const record = await db.contentProgress.getOneProgressByContentId(500, null)
|
|
54
|
+
expect(record.data?.progress_percent).toBe(100)
|
|
55
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('defaults to SELF collection when null is passed', async () => {
|
|
59
|
+
await contentStatusCompletedOffline(500, null, hierarchyFor(500))
|
|
60
|
+
const record = await db.contentProgress.getOneProgressByContentId(500, null)
|
|
61
|
+
expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
|
|
62
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// ─── contentStatusCompletedManyOffline ────────────────────────────────────────
|
|
67
|
+
|
|
68
|
+
describe('contentStatusCompletedManyOffline', () => {
|
|
69
|
+
test('sets all ids to completed state in DB', async () => {
|
|
70
|
+
const hierarchy = {
|
|
71
|
+
topLevelId: 501,
|
|
72
|
+
parents: {},
|
|
73
|
+
children: {},
|
|
74
|
+
metadata: { 501: meta, 502: meta, 503: meta },
|
|
75
|
+
}
|
|
76
|
+
await contentStatusCompletedManyOffline([501, 502, 503], null, hierarchy)
|
|
77
|
+
expect(await getProgressState(501)).toBe('completed')
|
|
78
|
+
expect(await getProgressState(502)).toBe('completed')
|
|
79
|
+
expect(await getProgressState(503)).toBe('completed')
|
|
80
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('each record has progress_percent of 100', async () => {
|
|
84
|
+
const hierarchy = {
|
|
85
|
+
topLevelId: 501,
|
|
86
|
+
parents: {},
|
|
87
|
+
children: {},
|
|
88
|
+
metadata: { 501: meta, 502: meta },
|
|
89
|
+
}
|
|
90
|
+
await contentStatusCompletedManyOffline([501, 502], null, hierarchy)
|
|
91
|
+
const r1 = await db.contentProgress.getOneProgressByContentId(501, null)
|
|
92
|
+
const r2 = await db.contentProgress.getOneProgressByContentId(502, null)
|
|
93
|
+
expect(r1.data?.progress_percent).toBe(100)
|
|
94
|
+
expect(r2.data?.progress_percent).toBe(100)
|
|
95
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
// ─── contentStatusStartedOffline ──────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
describe('contentStatusStartedOffline', () => {
|
|
102
|
+
test('sets state to started in DB', async () => {
|
|
103
|
+
await contentStatusStartedOffline(600, null, hierarchyFor(600))
|
|
104
|
+
expect(await getProgressState(600)).toBe('started')
|
|
105
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('sets progress_percent to 0 in DB', async () => {
|
|
109
|
+
await contentStatusStartedOffline(600, null, hierarchyFor(600))
|
|
110
|
+
const record = await db.contentProgress.getOneProgressByContentId(600, null)
|
|
111
|
+
expect(record.data?.progress_percent).toBe(0)
|
|
112
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// ─── contentStatusResetOffline ────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
describe('contentStatusResetOffline', () => {
|
|
119
|
+
test('removes record from DB so getProgressState returns empty string', async () => {
|
|
120
|
+
await contentStatusCompletedOffline(700, null, hierarchyFor(700))
|
|
121
|
+
expect(await getProgressState(700)).toBe('completed')
|
|
122
|
+
await contentStatusResetOffline(700)
|
|
123
|
+
expect(await getProgressState(700)).toBe('')
|
|
124
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('save-content-progress')
|
|
125
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('defaults to SELF collection when called without collection', async () => {
|
|
129
|
+
await db.contentProgress.recordProgress(701, null, 80, meta, undefined, { skipPush: true })
|
|
130
|
+
await contentStatusResetOffline(701)
|
|
131
|
+
expect(await getProgressState(701)).toBe('')
|
|
132
|
+
expect(ctx.pushSpies.contentProgress).toHaveBeenCalledWith('reset-status')
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// ─── recordWatchSessionOffline ────────────────────────────────────────────────
|
|
137
|
+
|
|
138
|
+
describe('recordWatchSessionOffline', () => {
|
|
139
|
+
const mockUserActivity = jest.requireMock('../../../src/services/userActivity')
|
|
140
|
+
|
|
141
|
+
beforeEach(() => {
|
|
142
|
+
jest.clearAllMocks()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('writes progress to DB and calls trackUserPractice', async () => {
|
|
146
|
+
await recordWatchSessionOffline(234, 200, 100, 30, hierarchyFor(234))
|
|
147
|
+
const record = await db.contentProgress.getOneProgressByContentId(234, null)
|
|
148
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
149
|
+
expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(234, 30, expect.any(Object))
|
|
150
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('normalizes string contentId to number in DB', async () => {
|
|
154
|
+
await recordWatchSessionOffline('234' as any, 200, 100, 30, hierarchyFor(234))
|
|
155
|
+
const record = await db.contentProgress.getOneProgressByContentId(234, null)
|
|
156
|
+
expect(record.data?.content_id).toBe(234)
|
|
157
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test('full duration (200/200) clamps to 99 — never reaches 100 via watch', async () => {
|
|
161
|
+
await recordWatchSessionOffline(234, 200, 200, 60, hierarchyFor(234))
|
|
162
|
+
const record = await db.contentProgress.getOneProgressByContentId(234, null)
|
|
163
|
+
expect(record.data?.progress_percent).toBe(99)
|
|
164
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('passes instrumentId and categoryId through to trackUserPractice', async () => {
|
|
168
|
+
await recordWatchSessionOffline(234, 200, 100, 30, hierarchyFor(234), {
|
|
169
|
+
instrumentId: 7,
|
|
170
|
+
categoryId: 9,
|
|
171
|
+
})
|
|
172
|
+
expect(mockUserActivity.trackUserPractice).toHaveBeenCalledWith(
|
|
173
|
+
234,
|
|
174
|
+
30,
|
|
175
|
+
expect.objectContaining({ instrumentId: 7, categoryId: 9 }),
|
|
176
|
+
)
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
// ─── duplicateProgressToALaCarteOffline ───────────────────────────────────────
|
|
181
|
+
|
|
182
|
+
describe('duplicateProgressToALaCarteOffline', () => {
|
|
183
|
+
test('writes SELF records for a-la-carte collection', async () => {
|
|
184
|
+
await duplicateProgressToALaCarteOffline({ 101: 50 }, { 101: meta }, collectionSelf)
|
|
185
|
+
await flushPromises()
|
|
186
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
187
|
+
expect(record.data?.collection_type).toBe(COLLECTION_TYPE.SELF)
|
|
188
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('LP collection excludes the LP id itself from duplication', async () => {
|
|
192
|
+
await duplicateProgressToALaCarteOffline({ 200: 50, 101: 75 }, { 200: meta, 101: meta }, collectionLP(200))
|
|
193
|
+
await flushPromises()
|
|
194
|
+
const lpRecord = await db.contentProgress.getOneProgressByContentId(200, null)
|
|
195
|
+
const lessonRecord = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
196
|
+
expect(lpRecord.data).toBeNull()
|
|
197
|
+
expect(lessonRecord.data?.progress_percent).toBe(75)
|
|
198
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('lower progress than existing is filtered — existing stays unchanged', async () => {
|
|
202
|
+
await db.contentProgress.recordProgress(101, null, 80, meta, undefined, { skipPush: true })
|
|
203
|
+
await duplicateProgressToALaCarteOffline({ 101: 50 }, { 101: meta }, collectionSelf)
|
|
204
|
+
await flushPromises()
|
|
205
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
206
|
+
expect(record.data?.progress_percent).toBe(80)
|
|
207
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
test('equal progress passes and record is updated', async () => {
|
|
211
|
+
await db.contentProgress.recordProgress(101, null, 50, meta, undefined, { skipPush: true })
|
|
212
|
+
await duplicateProgressToALaCarteOffline({ 101: 50 }, { 101: meta }, collectionSelf)
|
|
213
|
+
await flushPromises()
|
|
214
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
215
|
+
expect(record.data?.progress_percent).toBe(50)
|
|
216
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
test('empty progresses results in no new records', async () => {
|
|
220
|
+
await duplicateProgressToALaCarteOffline({}, {}, collectionSelf)
|
|
221
|
+
await flushPromises()
|
|
222
|
+
const record = await db.contentProgress.getOneProgressByContentId(101, null)
|
|
223
|
+
expect(record.data).toBeNull()
|
|
224
|
+
expect(ctx.pushSpies.contentProgress).not.toHaveBeenCalled()
|
|
225
|
+
})
|
|
226
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { getActiveDiscussions } from '@/services/forums/forums'
|
|
2
|
+
|
|
3
|
+
const mockGet = jest.fn()
|
|
4
|
+
|
|
5
|
+
jest.mock('@/infrastructure/http/HttpClient', () => ({
|
|
6
|
+
HttpClient: jest.fn().mockImplementation(() => ({
|
|
7
|
+
get: mockGet,
|
|
8
|
+
})),
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
function makeThread(overrides = {}) {
|
|
12
|
+
return {
|
|
13
|
+
id: 1,
|
|
14
|
+
slug: 'test-thread',
|
|
15
|
+
title: 'Test Thread',
|
|
16
|
+
locked: false,
|
|
17
|
+
pinned: false,
|
|
18
|
+
state: 'open',
|
|
19
|
+
category_id: 10,
|
|
20
|
+
post_count: 1,
|
|
21
|
+
is_read: false,
|
|
22
|
+
author: {
|
|
23
|
+
id: 100,
|
|
24
|
+
display_name: 'Jane Doe',
|
|
25
|
+
profile_picture_url: 'https://example.com/avatar.jpg',
|
|
26
|
+
access_level: 'member',
|
|
27
|
+
signature: null,
|
|
28
|
+
},
|
|
29
|
+
last_post: {
|
|
30
|
+
id: 200,
|
|
31
|
+
content: 'Hello world',
|
|
32
|
+
created_at: '2024-01-01T00:00:00Z',
|
|
33
|
+
created_at_human: '1 day ago',
|
|
34
|
+
author: null,
|
|
35
|
+
like_count: 0,
|
|
36
|
+
is_liked: false,
|
|
37
|
+
},
|
|
38
|
+
...overrides,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function makePaginatedResponse(threads = [makeThread()]) {
|
|
43
|
+
return {
|
|
44
|
+
data: threads,
|
|
45
|
+
meta: {
|
|
46
|
+
current_page: 1,
|
|
47
|
+
per_page: 10,
|
|
48
|
+
total: threads.length,
|
|
49
|
+
last_page: 1,
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe('getActiveDiscussions', () => {
|
|
55
|
+
test('returns transformed data and meta', async () => {
|
|
56
|
+
mockGet.mockResolvedValue(makePaginatedResponse())
|
|
57
|
+
|
|
58
|
+
const result = await getActiveDiscussions('drumeo')
|
|
59
|
+
|
|
60
|
+
expect(result.data).toHaveLength(1)
|
|
61
|
+
expect(result.meta).toBeDefined()
|
|
62
|
+
expect(result.meta.current_page).toBe(1)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('maps thread fields correctly', async () => {
|
|
66
|
+
mockGet.mockResolvedValue(makePaginatedResponse())
|
|
67
|
+
|
|
68
|
+
const result = await getActiveDiscussions('drumeo')
|
|
69
|
+
const thread = result.data[0]
|
|
70
|
+
|
|
71
|
+
expect(thread.id).toBe(1)
|
|
72
|
+
expect(thread.url).toBe('forums/threads/10/1')
|
|
73
|
+
expect(thread.title).toBe('Test Thread')
|
|
74
|
+
expect(thread.post).toBe('Hello world')
|
|
75
|
+
expect(thread.author.id).toBe(100)
|
|
76
|
+
expect(thread.author.name).toBe('Jane Doe')
|
|
77
|
+
expect(thread.author.avatar).toBe('https://example.com/avatar.jpg')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('maps author avatar to empty string when profile_picture_url is null', async () => {
|
|
81
|
+
const thread = makeThread({
|
|
82
|
+
author: { id: 1, display_name: 'No Avatar', profile_picture_url: null, access_level: 'member', signature: null },
|
|
83
|
+
})
|
|
84
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
85
|
+
|
|
86
|
+
const result = await getActiveDiscussions('drumeo')
|
|
87
|
+
|
|
88
|
+
expect(result.data[0].author.avatar).toBe('')
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('filters out threads with null last_post', async () => {
|
|
92
|
+
const threads = [makeThread({ last_post: null }), makeThread({ id: 2 })]
|
|
93
|
+
mockGet.mockResolvedValue(makePaginatedResponse(threads))
|
|
94
|
+
|
|
95
|
+
const result = await getActiveDiscussions('drumeo')
|
|
96
|
+
|
|
97
|
+
expect(result.data).toHaveLength(1)
|
|
98
|
+
expect(result.data[0].id).toBe(2)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('filters out threads with null author', async () => {
|
|
102
|
+
const threads = [makeThread({ author: null }), makeThread({ id: 2 })]
|
|
103
|
+
mockGet.mockResolvedValue(makePaginatedResponse(threads))
|
|
104
|
+
|
|
105
|
+
const result = await getActiveDiscussions('drumeo')
|
|
106
|
+
|
|
107
|
+
expect(result.data).toHaveLength(1)
|
|
108
|
+
expect(result.data[0].id).toBe(2)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('passes brand, page and limit to the HTTP call', async () => {
|
|
112
|
+
mockGet.mockResolvedValue(makePaginatedResponse())
|
|
113
|
+
|
|
114
|
+
await getActiveDiscussions('pianote', { page: 3, limit: 25 })
|
|
115
|
+
|
|
116
|
+
expect(mockGet).toHaveBeenCalledWith(
|
|
117
|
+
'/api/forums/v1/threads/latest?brand=pianote&page=3&limit=25'
|
|
118
|
+
)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('defaults page to 1 and limit to 10', async () => {
|
|
122
|
+
mockGet.mockResolvedValue(makePaginatedResponse())
|
|
123
|
+
|
|
124
|
+
await getActiveDiscussions('drumeo')
|
|
125
|
+
|
|
126
|
+
expect(mockGet).toHaveBeenCalledWith(
|
|
127
|
+
'/api/forums/v1/threads/latest?brand=drumeo&page=1&limit=10'
|
|
128
|
+
)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('stripHtml', () => {
|
|
132
|
+
test('removes blockquote and its content', async () => {
|
|
133
|
+
const thread = makeThread({
|
|
134
|
+
last_post: { id: 1, content: 'Before<blockquote>quoted text</blockquote>After', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
135
|
+
})
|
|
136
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
137
|
+
|
|
138
|
+
const result = await getActiveDiscussions('drumeo')
|
|
139
|
+
|
|
140
|
+
expect(result.data[0].post).toBe('BeforeAfter')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('removes iframes and their content', async () => {
|
|
144
|
+
const thread = makeThread({
|
|
145
|
+
last_post: { id: 1, content: 'Before<iframe src="x">content</iframe>After', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
146
|
+
})
|
|
147
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
148
|
+
|
|
149
|
+
const result = await getActiveDiscussions('drumeo')
|
|
150
|
+
|
|
151
|
+
expect(result.data[0].post).toBe('BeforeAfter')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('converts <br> to space to prevent word concatenation', async () => {
|
|
155
|
+
const thread = makeThread({
|
|
156
|
+
last_post: { id: 1, content: 'word1<br>word2<br/>word3', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
157
|
+
})
|
|
158
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
159
|
+
|
|
160
|
+
const result = await getActiveDiscussions('drumeo')
|
|
161
|
+
|
|
162
|
+
expect(result.data[0].post).toBe('word1 word2 word3')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('converts closing block-level tags to spaces', async () => {
|
|
166
|
+
const thread = makeThread({
|
|
167
|
+
last_post: { id: 1, content: '<p>word1</p><div>word2</div>', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
168
|
+
})
|
|
169
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
170
|
+
|
|
171
|
+
const result = await getActiveDiscussions('drumeo')
|
|
172
|
+
|
|
173
|
+
expect(result.data[0].post).toBe('word1 word2')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
test('decodes HTML entities', async () => {
|
|
177
|
+
const thread = makeThread({
|
|
178
|
+
last_post: { id: 1, content: 'a & b <c> "d" 'e' f g', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
179
|
+
})
|
|
180
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
181
|
+
|
|
182
|
+
const result = await getActiveDiscussions('drumeo')
|
|
183
|
+
|
|
184
|
+
expect(result.data[0].post).toBe("a & b <c> \"d\" 'e' f g")
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
test('removes remaining HTML tags', async () => {
|
|
188
|
+
const thread = makeThread({
|
|
189
|
+
last_post: { id: 1, content: '<strong>bold</strong> and <em>italic</em>', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
190
|
+
})
|
|
191
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
192
|
+
|
|
193
|
+
const result = await getActiveDiscussions('drumeo')
|
|
194
|
+
|
|
195
|
+
expect(result.data[0].post).toBe('bold and italic')
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('replaces img tags with spaces', async () => {
|
|
199
|
+
const thread = makeThread({
|
|
200
|
+
last_post: { id: 1, content: 'word1<img src="x.jpg">word2', created_at: '', created_at_human: '', author: null, like_count: 0, is_liked: false },
|
|
201
|
+
})
|
|
202
|
+
mockGet.mockResolvedValue(makePaginatedResponse([thread]))
|
|
203
|
+
|
|
204
|
+
const result = await getActiveDiscussions('drumeo')
|
|
205
|
+
|
|
206
|
+
expect(result.data[0].post).toBe('word1 word2')
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { initializeTestService } from '../initializeTests.js'
|
|
2
|
+
import SyncManager from '@/services/sync/manager'
|
|
3
|
+
import { SyncTelemetry } from '@/services/sync/telemetry'
|
|
4
|
+
import { makeContext, makeDatabase, makeTelemetry, makeUserScope } from '../unit/sync/helpers'
|
|
5
|
+
import db from '@/services/sync/repository-proxy'
|
|
6
|
+
|
|
7
|
+
export interface PushSpies {
|
|
8
|
+
contentProgress: jest.SpyInstance
|
|
9
|
+
practices: jest.SpyInstance
|
|
10
|
+
likes: jest.SpyInstance
|
|
11
|
+
userAwardProgress: jest.SpyInstance
|
|
12
|
+
practiceDayNotes: jest.SpyInstance
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface TestDBContext {
|
|
16
|
+
pushSpies: PushSpies
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const waitForPushCall = async (
|
|
20
|
+
spy: jest.SpyInstance,
|
|
21
|
+
cause: string,
|
|
22
|
+
timeoutMs = 2000,
|
|
23
|
+
): Promise<void> => {
|
|
24
|
+
const start = Date.now()
|
|
25
|
+
while (Date.now() - start < timeoutMs) {
|
|
26
|
+
if (spy.mock.calls.some(c => c[0] === cause)) return
|
|
27
|
+
await new Promise(resolve => setImmediate(resolve))
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function initializeMockPushes(): PushSpies {
|
|
32
|
+
return {
|
|
33
|
+
contentProgress: jest.spyOn(db.contentProgress, 'requestPushUnsynced').mockImplementation(() => {
|
|
34
|
+
}),
|
|
35
|
+
practices: jest.spyOn(db.practices, 'requestPushUnsynced').mockImplementation(() => {
|
|
36
|
+
}),
|
|
37
|
+
likes: jest.spyOn(db.likes, 'requestPushUnsynced').mockImplementation(() => {
|
|
38
|
+
}),
|
|
39
|
+
userAwardProgress: jest.spyOn(db.userAwardProgress, 'requestPushUnsynced').mockImplementation(() => {
|
|
40
|
+
}),
|
|
41
|
+
practiceDayNotes: jest.spyOn(db.practiceDayNotes, 'requestPushUnsynced').mockImplementation(() => {
|
|
42
|
+
}),
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function initializeTestDB(): TestDBContext {
|
|
47
|
+
const ctx: TestDBContext = { pushSpies: {} as PushSpies }
|
|
48
|
+
|
|
49
|
+
beforeAll(() => {
|
|
50
|
+
global.fetch = jest.fn().mockResolvedValue({
|
|
51
|
+
ok: true,
|
|
52
|
+
headers: { get: (key: string) => key === 'X-Sync-Intended-User-Id' ? '1' : null },
|
|
53
|
+
json: () => Promise.resolve({
|
|
54
|
+
meta: { since: null, max_updated_at: Date.now(), timestamp: Date.now() },
|
|
55
|
+
entries: [],
|
|
56
|
+
}),
|
|
57
|
+
} as any)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
let teardown: ((mode?: any) => Promise<void>) | null = null
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
initializeTestService()
|
|
64
|
+
const userScope = makeUserScope()
|
|
65
|
+
SyncTelemetry.setInstance(makeTelemetry(userScope))
|
|
66
|
+
const manager = new SyncManager(userScope, makeContext(), () => makeDatabase())
|
|
67
|
+
teardown = SyncManager.assignAndSetupInstance(manager)
|
|
68
|
+
ctx.pushSpies = initializeMockPushes()
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
afterEach(async () => {
|
|
72
|
+
for (let i = 0; i < 200; i++) await new Promise(resolve => setImmediate(resolve))
|
|
73
|
+
await teardown?.('reset')
|
|
74
|
+
teardown = null
|
|
75
|
+
SyncTelemetry.clearInstance()
|
|
76
|
+
;(SyncManager as any).instance = null
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
return ctx
|
|
80
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fflate from 'fflate'
|
|
2
2
|
import { handlePull, type SyncPullResponse } from '@/services/sync/fetch'
|
|
3
|
-
import { makeContext } from '
|
|
3
|
+
import { makeContext } from '../../unit/sync/helpers/index'
|
|
4
4
|
import type { EpochMs } from '@/services/sync/index'
|
|
5
5
|
|
|
6
6
|
// ---
|
|
@@ -2,7 +2,7 @@ jest.mock('@/services/sync/manager', () => ({ default: class SyncManager {} }))
|
|
|
2
2
|
jest.mock('@/services/sync/repository-proxy', () => ({ db: {} }))
|
|
3
3
|
|
|
4
4
|
import { Database } from '@nozbe/watermelondb'
|
|
5
|
-
import { makeDatabase, makeStore, resetDatabase } from '
|
|
5
|
+
import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
|
|
6
6
|
import ContentLike from '@/services/sync/models/ContentLike'
|
|
7
7
|
import LikesRepository from '@/services/sync/repositories/content-likes'
|
|
8
8
|
|
|
@@ -2,7 +2,7 @@ jest.mock('@/services/sync/manager', () => ({ default: class SyncManager {} }))
|
|
|
2
2
|
jest.mock('@/services/sync/repository-proxy', () => ({ db: {} }))
|
|
3
3
|
|
|
4
4
|
import { Database } from '@nozbe/watermelondb'
|
|
5
|
-
import { makeDatabase, makeStore, resetDatabase } from '
|
|
5
|
+
import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
|
|
6
6
|
import Practice from '@/services/sync/models/Practice'
|
|
7
7
|
import PracticesRepository from '@/services/sync/repositories/practices'
|
|
8
8
|
|
|
@@ -4,7 +4,7 @@ jest.mock('@/services/sync/manager', () => ({ default: class SyncManager {} }))
|
|
|
4
4
|
jest.mock('@/services/sync/repository-proxy', () => ({ db: {} }))
|
|
5
5
|
|
|
6
6
|
import { Database } from '@nozbe/watermelondb'
|
|
7
|
-
import { makeDatabase, makeStore, resetDatabase } from '
|
|
7
|
+
import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
|
|
8
8
|
import ContentProgress, {
|
|
9
9
|
COLLECTION_TYPE,
|
|
10
10
|
COLLECTION_ID_SELF,
|
|
@@ -8,7 +8,7 @@ jest.mock('../../../../src/services/awards/internal/award-definitions', () => ({
|
|
|
8
8
|
}))
|
|
9
9
|
|
|
10
10
|
import { Database } from '@nozbe/watermelondb'
|
|
11
|
-
import { makeDatabase, makeStore, resetDatabase } from '
|
|
11
|
+
import { makeDatabase, makeStore, resetDatabase } from '../../../unit/sync/helpers/index'
|
|
12
12
|
import UserAwardProgress from '@/services/sync/models/UserAwardProgress'
|
|
13
13
|
import UserAwardProgressRepository from '@/services/sync/repositories/user-award-progress'
|
|
14
14
|
import type { CompletionData, AwardDefinition } from '../../../../src/services/awards/types'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Database } from '@nozbe/watermelondb'
|
|
2
|
-
import { makeTelemetry, makeContext, makePullMock, makePushMock } from '
|
|
3
|
-
import TestModel, { makeTestDatabase } from '
|
|
2
|
+
import { makeTelemetry, makeContext, makePullMock, makePushMock } from '../../../unit/sync/helpers/index'
|
|
3
|
+
import TestModel, { makeTestDatabase } from '../../../unit/sync/helpers/TestModel'
|
|
4
4
|
import SyncStore from '@/services/sync/store/index'
|
|
5
5
|
import SyncRetry from '@/services/sync/retry'
|
|
6
6
|
import SyncRunScope from '@/services/sync/run-scope'
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { IDBFactory } from 'fake-indexeddb'
|
|
18
|
-
import { makeTelemetry, makeContext, makeUserScope, makePullMock, makePushMock, TEST_USER_ID } from '
|
|
19
|
-
import TestModel, { makeTestDatabase } from '
|
|
18
|
+
import { makeTelemetry, makeContext, makeUserScope, makePullMock, makePushMock, TEST_USER_ID } from '../../../unit/sync/helpers/index'
|
|
19
|
+
import TestModel, { makeTestDatabase } from '../../../unit/sync/helpers/TestModel'
|
|
20
20
|
import SyncStore from '@/services/sync/store/index'
|
|
21
21
|
import SyncRetry from '@/services/sync/retry'
|
|
22
22
|
import SyncRunScope from '@/services/sync/run-scope'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Database } from '@nozbe/watermelondb'
|
|
2
|
-
import { makeTelemetry, makeContext, makeUserScope, makePullMock, makePushMock } from '
|
|
3
|
-
import TestModel, { makeTestDatabase } from '
|
|
2
|
+
import { makeTelemetry, makeContext, makeUserScope, makePullMock, makePushMock } from '../../../unit/sync/helpers/index'
|
|
3
|
+
import TestModel, { makeTestDatabase } from '../../../unit/sync/helpers/TestModel'
|
|
4
4
|
import SyncStore from '@/services/sync/store/index'
|
|
5
5
|
import SyncRetry from '@/services/sync/retry'
|
|
6
6
|
import SyncRunScope from '@/services/sync/run-scope'
|