feat(training): 支持课程附件预览与整页阅读
This commit is contained in:
@@ -24,33 +24,35 @@ struct TrainingRemoteReadingTests {
|
||||
}
|
||||
}
|
||||
|
||||
/// 百节课程首次只读一页目录和一节正文,明确选择过的正文才缓存。
|
||||
@Test("首次不遍历百节正文,目录主动分页,正文按选择复用")
|
||||
private func loadRendered(_ model: TrainingRemoteReadingViewModel) async {
|
||||
await model.load()
|
||||
if let id = model.currentAttachmentID, let previewID = model.attachmentPreviewID {
|
||||
#expect(model.markAttachmentRendered(id: id, previewID: previewID))
|
||||
}
|
||||
}
|
||||
|
||||
/// 首屏只读取一页附件并授权首项,不请求已经停用的小节 HTML。
|
||||
@Test("首次仅请求附件第一页并授权首项,不加载小节 HTML")
|
||||
func lazyCourseResources() async {
|
||||
let source = source()
|
||||
await source.configure {
|
||||
$0.courseHandler = { TrainingCourseInfo(id: "course_1", title: "课程", introduction: "简介", sectionCount: 100, attachmentCount: 20) }
|
||||
$0.sectionsHandler = { cursor in
|
||||
let range = cursor == nil ? 1...20 : 21...40
|
||||
return TrainingCursorPage(items: range.map { TrainingSectionInfo(id: "s\($0)", order: $0, title: "小节\($0)") },
|
||||
nextCursor: cursor == nil ? "next" : "last", hasMore: true, serverTime: nil)
|
||||
$0.attachmentsHandler = { _ in
|
||||
TrainingCursorPage(items: (1...20).map { TrainingRemoteFixtures.attachment(id: "file_\($0)") },
|
||||
nextCursor: "next", hasMore: true, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
var calls = await source.calls
|
||||
#expect(model.sections.count == 20)
|
||||
#expect(calls.filter { $0.name == "sections" }.count == 1)
|
||||
#expect(calls.first { $0.name == "sections" }?.limit == 20)
|
||||
#expect(calls.filter { $0.name == "section" }.count == 1)
|
||||
#expect(!calls.contains { ["attachments", "paper", "questions", "draft", "attempts"].contains($0.name) })
|
||||
await model.loadMoreSections()
|
||||
#expect(model.sections.count == 40)
|
||||
await model.selectSection(id: "s2")
|
||||
await model.selectSection(id: "s1")
|
||||
calls = await source.calls
|
||||
#expect(calls.filter { $0.name == "section" }.count == 2)
|
||||
#expect(calls.filter { $0.name == "sections" }.last?.cursor == "next")
|
||||
let calls = await source.calls
|
||||
#expect(model.attachments.count == 20)
|
||||
#expect(model.currentAttachmentID == "file_1")
|
||||
#expect(model.currentAttachmentLink?.id == "file_1")
|
||||
#expect(calls.filter { $0.name == "attachments" }.count == 1)
|
||||
#expect(calls.first { $0.name == "attachments" }?.limit == 20)
|
||||
#expect(calls.filter { $0.name == "attachmentURL" }.map(\.resourceID) == ["file_1"])
|
||||
#expect(!calls.contains { ["sections", "section", "paper", "questions", "draft", "attempts"].contains($0.name) })
|
||||
#expect(!model.isAttachmentRendered)
|
||||
}
|
||||
|
||||
/// 倒计时本地刷新,五秒合并网络上报,最后一个片段关闭会话。
|
||||
@@ -58,7 +60,7 @@ struct TrainingRemoteReadingTests {
|
||||
func batchedHeartbeatAndCompletion() async {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...4 { await model.recordVisibleTime(at: Double(second)) }
|
||||
#expect(await source.heartbeats.isEmpty)
|
||||
@@ -78,7 +80,7 @@ struct TrainingRemoteReadingTests {
|
||||
func historyAndSessionAreSeparate() async {
|
||||
let source = source(seconds: 12)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...8 { await model.recordVisibleTime(at: Double(second)) }
|
||||
let calls = await source.heartbeats
|
||||
@@ -93,7 +95,7 @@ struct TrainingRemoteReadingTests {
|
||||
func pauseDoesNotCountBackgroundTime() async {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...3 { await model.recordVisibleTime(at: Double(second)) }
|
||||
model.pauseReading()
|
||||
@@ -120,7 +122,7 @@ struct TrainingRemoteReadingTests {
|
||||
let server = TrainingReadingReplyController(loseCreateResponse: true)
|
||||
await bind(server, to: source)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
#expect(model.readingErrorMessage != nil)
|
||||
await model.startReading(at: 10)
|
||||
@@ -138,7 +140,7 @@ struct TrainingRemoteReadingTests {
|
||||
let server = TrainingReadingReplyController(loseHeartbeatResponse: true)
|
||||
await bind(server, to: source)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...5 { await model.recordVisibleTime(at: Double(second)) }
|
||||
#expect(model.progress?.readingSeconds == 0)
|
||||
@@ -158,7 +160,7 @@ struct TrainingRemoteReadingTests {
|
||||
let server = TrainingReadingReplyController(loseHeartbeatResponse: true)
|
||||
await bind(server, to: source)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...3 { await model.recordVisibleTime(at: Double(second)) }
|
||||
model.pauseReading()
|
||||
@@ -184,7 +186,7 @@ struct TrainingRemoteReadingTests {
|
||||
let gate = TrainingReadingHeartbeatGate()
|
||||
await source.configure { $0.heartbeatHandler = { await gate.confirm($0) } }
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...4 { await model.recordVisibleTime(at: Double(second)) }
|
||||
let operation = Task { await model.recordVisibleTime(at: 5) }
|
||||
@@ -216,7 +218,7 @@ struct TrainingRemoteReadingTests {
|
||||
return try await server.confirm(call)
|
||||
} }
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...3 { await model.recordVisibleTime(at: Double(second)) }
|
||||
let closing = Task { await model.flush(final: true) }
|
||||
@@ -246,7 +248,7 @@ struct TrainingRemoteReadingTests {
|
||||
func notWritableRefreshesReadOnlyState() async {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
let saved = TrainingRemoteFixtures.progress(seconds: 7)
|
||||
let readOnly = TrainingProgressSnapshot(taskID: saved.taskID, readingSeconds: saved.readingSeconds,
|
||||
@@ -263,7 +265,7 @@ struct TrainingRemoteReadingTests {
|
||||
for second in 1...5 { await model.recordVisibleTime(at: Double(second)) }
|
||||
#expect(!model.canStudy)
|
||||
#expect(model.progress?.blockReason == "closed")
|
||||
#expect(model.currentSection != nil)
|
||||
#expect(model.currentAttachmentLink != nil)
|
||||
await model.startReading(at: 100)
|
||||
#expect(await source.startReadingRequests.count == 1)
|
||||
#expect(!model.isReading)
|
||||
@@ -277,14 +279,14 @@ struct TrainingRemoteReadingTests {
|
||||
func accessDenialInvalidatesCourse(_ failure: TrainingRemoteError) async {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
await source.configure { $0.heartbeatHandler = { _ in throw failure } }
|
||||
for second in 1...5 { await model.recordVisibleTime(at: Double(second)) }
|
||||
#expect(model.isInvalidated)
|
||||
#expect(model.currentSection == nil)
|
||||
#expect(model.currentAttachmentLink == nil)
|
||||
#expect(model.progress == nil)
|
||||
#expect(model.sections.isEmpty)
|
||||
#expect(model.attachments.isEmpty)
|
||||
#expect(!model.isReading)
|
||||
}
|
||||
|
||||
@@ -298,7 +300,7 @@ struct TrainingRemoteReadingTests {
|
||||
let server = TrainingReadingReplyController(nextFailure: failure)
|
||||
await bind(server, to: source)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...5 { await model.recordVisibleTime(at: Double(second)) }
|
||||
#expect(model.isReading)
|
||||
@@ -314,12 +316,12 @@ struct TrainingRemoteReadingTests {
|
||||
#expect((await source.calls).filter { $0.name == "progress" }.count >= 2)
|
||||
}
|
||||
|
||||
/// 正文请求挂起时切身份,旧正文不能回到已经关闭的学员页面。
|
||||
@Test("身份切换丢弃迟到正文和权威进度")
|
||||
func identityChangeDiscardsLateBody() async {
|
||||
/// 授权请求挂起时切身份,旧短链不能重新打开已经关闭的页面。
|
||||
@Test("身份切换丢弃迟到附件授权和权威进度")
|
||||
func identityChangeDiscardsLateAttachment() async {
|
||||
let source = source()
|
||||
let gate = TrainingReadingBodyGate()
|
||||
await source.configure { $0.sectionHandler = { _ in await gate.body() } }
|
||||
let gate = TrainingReadingAttachmentGate()
|
||||
await source.configure { $0.attachmentURLHandler = { _ in await gate.link() } }
|
||||
let model = model(source)
|
||||
let operation = Task { await model.load() }
|
||||
await gate.waitUntilRequested()
|
||||
@@ -328,37 +330,251 @@ struct TrainingRemoteReadingTests {
|
||||
await operation.value
|
||||
#expect(model.isInvalidated)
|
||||
#expect(model.task == nil)
|
||||
#expect(model.currentSection == nil)
|
||||
#expect(model.currentAttachment == nil)
|
||||
#expect(model.currentAttachmentLink == nil)
|
||||
#expect(model.attachmentPreviewID == nil)
|
||||
#expect(model.progress == nil)
|
||||
#expect(model.sections.isEmpty)
|
||||
#expect(model.attachments.isEmpty)
|
||||
}
|
||||
|
||||
/// 元信息不提前授权文件,用户点击附件时才申请短期可打开链接。
|
||||
@Test("附件目录按需分页,点击才获取授权地址")
|
||||
/// 后续附件页需要用户主动读取,分页不能提前授权其余文档。
|
||||
@Test("附件目录主动分页且只在选择时授权后续附件")
|
||||
func attachmentsAreLazyAndAuthorized() async {
|
||||
let source = source(seconds: 20)
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { cursor in
|
||||
let id = cursor == nil ? "file_1" : "file_2"
|
||||
let item = TrainingAttachmentInfo(id: id, name: "培训.pdf", mimeType: "application/pdf", sizeBytes: 1024,
|
||||
fileID: id, downloadURL: nil, expiresAt: nil)
|
||||
return TrainingCursorPage(items: [item], nextCursor: cursor == nil ? "next" : nil,
|
||||
return TrainingCursorPage(items: [TrainingRemoteFixtures.attachment(id: id)],
|
||||
nextCursor: cursor == nil ? "next" : nil,
|
||||
hasMore: cursor == nil, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
#expect(!(await source.calls).contains { $0.name == "attachments" })
|
||||
await model.loadAttachments()
|
||||
await model.loadAttachments()
|
||||
#expect((await source.calls).filter { $0.name == "attachments" }.count == 1)
|
||||
#expect(!(await source.calls).contains { $0.name == "attachmentURL" })
|
||||
#expect((await source.calls).filter { $0.name == "attachmentURL" }.count == 1)
|
||||
await model.loadAttachments(loadMore: true)
|
||||
#expect(model.attachments.count == 2)
|
||||
let link = await model.authorizeAttachment(id: "file_1")
|
||||
#expect(link?.url.scheme == "https")
|
||||
#expect(link?.expiresAt == TrainingRemoteFixtures.now.addingTimeInterval(3600))
|
||||
#expect((await source.calls).filter { $0.name == "attachments" }.last?.cursor == "next")
|
||||
#expect((await source.calls).filter { $0.name == "attachmentURL" }.count == 1)
|
||||
await model.selectAttachment(id: "file_2")
|
||||
#expect(model.currentAttachmentID == "file_2")
|
||||
#expect(model.currentAttachmentLink?.expiresAt == TrainingRemoteFixtures.now.addingTimeInterval(3600))
|
||||
#expect((await source.calls).filter { $0.name == "attachmentURL" }.map(\.resourceID) == ["file_1", "file_2"])
|
||||
#expect(!model.isAttachmentRendered)
|
||||
}
|
||||
|
||||
/// 分页失败只提示列表错误,不能把已显示文件变成失败态或丢弃已累计秒数。
|
||||
@Test("附件分页失败可重试,当前文档继续阅读且成功后清提示")
|
||||
func attachmentPaginationErrorDoesNotInterruptReading() async throws {
|
||||
let source = source()
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { _ in
|
||||
TrainingCursorPage(items: [TrainingRemoteFixtures.attachment()], nextCursor: "next", hasMore: true, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = model(source)
|
||||
await loadRendered(model)
|
||||
let previewID = try #require(model.attachmentPreviewID)
|
||||
let link = try #require(model.currentAttachmentLink)
|
||||
await model.startReading(at: 0)
|
||||
await model.recordVisibleTime(at: 1)
|
||||
await source.configure { $0.attachmentsHandler = { _ in throw URLError(.timedOut) } }
|
||||
await model.loadAttachments(loadMore: true)
|
||||
#expect(model.attachmentListErrorMessage != nil)
|
||||
#expect(model.attachmentErrorMessage == nil && model.errorMessage == nil)
|
||||
#expect(model.isAttachmentRendered && model.isReading && model.canStudy)
|
||||
#expect(model.currentAttachmentLink == link && model.attachmentPreviewID == previewID)
|
||||
#expect(model.attachments.count == 1 && model.hasMoreAttachments)
|
||||
#expect(model.markAttachmentRendered(id: "file_1", previewID: previewID))
|
||||
await model.recordVisibleTime(at: 2)
|
||||
#expect(model.activeSecondsTotal == 2)
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { _ in
|
||||
TrainingCursorPage(items: [TrainingRemoteFixtures.attachment(id: "file_2")], nextCursor: nil, hasMore: false, serverTime: nil)
|
||||
}
|
||||
}
|
||||
await model.loadAttachments(loadMore: true)
|
||||
#expect(model.attachmentListErrorMessage == nil && model.errorMessage == nil)
|
||||
#expect(model.attachments.map(\.id) == ["file_1", "file_2"])
|
||||
#expect(!model.hasMoreAttachments)
|
||||
#expect(model.currentAttachmentLink == link && model.attachmentPreviewID == previewID)
|
||||
#expect(model.isAttachmentRendered && model.isReading)
|
||||
await model.recordVisibleTime(at: 3)
|
||||
model.pauseReading()
|
||||
await model.flush()
|
||||
let heartbeats = await source.heartbeats
|
||||
#expect(heartbeats.count == 1)
|
||||
#expect(heartbeats.first?.activeSecondsTotal == 3 && heartbeats.first?.final == true)
|
||||
#expect(await source.startReadingRequests.count == 1)
|
||||
let calls = await source.calls
|
||||
#expect(calls.filter { $0.name == "attachments" }.map(\.cursor) == [nil, "next", "next"])
|
||||
#expect(calls.filter { $0.name == "attachmentURL" }.count == 1)
|
||||
}
|
||||
|
||||
/// 授权被撤回不是普通分页故障,必须清理已经可读的文件并立即停止学习。
|
||||
@Test("附件分页返回账号或权限失效时仍清内容退出", arguments: [
|
||||
TrainingRemoteError.accountInvalid("account no longer eligible"),
|
||||
TrainingRemoteError.forbidden("assignment revoked")
|
||||
])
|
||||
func attachmentPaginationAccessDenialInvalidatesCourse(_ failure: TrainingRemoteError) async {
|
||||
let source = source()
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { _ in
|
||||
TrainingCursorPage(items: [TrainingRemoteFixtures.attachment()], nextCursor: "next", hasMore: true, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = model(source)
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
await source.configure { $0.attachmentsHandler = { _ in throw failure } }
|
||||
await model.loadAttachments(loadMore: true)
|
||||
#expect(model.isInvalidated)
|
||||
#expect(!model.isReading && !model.isAttachmentRendered)
|
||||
#expect(model.attachments.isEmpty && model.currentAttachmentLink == nil)
|
||||
#expect(model.attachmentListErrorMessage == nil)
|
||||
}
|
||||
|
||||
/// 只有文档下载与渲染都结束并由页面确认后才建立学习会话。
|
||||
@Test("附件授权不代表已展示,渲染确认前不能累计读时")
|
||||
func authorizedAttachmentWaitsForRendering() async throws {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
#expect(model.currentAttachmentLink != nil)
|
||||
await model.startReading(at: 0)
|
||||
await model.recordVisibleTime(at: 100)
|
||||
#expect(await source.startReadingRequests.isEmpty)
|
||||
#expect(await source.heartbeats.isEmpty)
|
||||
let previewID = try #require(model.attachmentPreviewID)
|
||||
#expect(!model.markAttachmentRendered(id: "another-file", previewID: previewID))
|
||||
#expect(!model.markAttachmentRendered(id: "file_1", previewID: UUID()))
|
||||
#expect(model.markAttachmentRendered(id: "file_1", previewID: previewID))
|
||||
await model.startReading(at: 200)
|
||||
await model.recordVisibleTime(at: 201)
|
||||
#expect(model.isReading)
|
||||
#expect(model.activeSecondsTotal == 1)
|
||||
#expect(await source.startReadingRequests.count == 1)
|
||||
}
|
||||
|
||||
/// 没有课程文档时保留元信息空态,不请求 HTML,也不能通过空白页获得进度。
|
||||
@Test("无附件保留课程元信息但不建立会话或回退 HTML")
|
||||
func missingAttachmentDoesNotCountReading() async {
|
||||
let source = source()
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { _ in TrainingCursorPage(items: [], nextCursor: nil, hasMore: false, serverTime: nil) }
|
||||
}
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await model.startReading(at: 0)
|
||||
await model.recordVisibleTime(at: 1000)
|
||||
#expect(model.task != nil && model.course != nil && model.progress != nil)
|
||||
#expect(model.hasLoadedAttachments)
|
||||
#expect(model.currentAttachment == nil)
|
||||
#expect(model.currentAttachmentLink == nil)
|
||||
#expect(!model.isAttachmentRendered)
|
||||
#expect(!model.markAttachmentRendered(id: "file_1", previewID: UUID()))
|
||||
#expect(!(await source.calls).contains { ["section", "sections", "attachmentURL", "startReading", "heartbeat"].contains($0.name) })
|
||||
}
|
||||
|
||||
/// 授权失败仍展示课程规则和附件名,重试成功后也要等待实际渲染。
|
||||
@Test("授权失败可重试同一附件,课程元信息不丢失")
|
||||
func authorizationFailureKeepsMetadataForRetry() async throws {
|
||||
let source = source()
|
||||
await source.configure { $0.attachmentURLHandler = { _ in throw URLError(.timedOut) } }
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
#expect(model.task != nil && model.course != nil && model.progress != nil)
|
||||
#expect(model.currentAttachmentID == "file_1")
|
||||
#expect(model.currentAttachmentLink == nil)
|
||||
#expect(model.attachmentErrorMessage != nil)
|
||||
await model.startReading(at: 0)
|
||||
#expect(await source.startReadingRequests.isEmpty)
|
||||
let failedPreviewID = try #require(model.attachmentPreviewID)
|
||||
await source.configure { $0.attachmentURLHandler = nil }
|
||||
await model.selectAttachment(id: "file_1")
|
||||
let nextPreviewID = try #require(model.attachmentPreviewID)
|
||||
#expect(nextPreviewID != failedPreviewID)
|
||||
#expect(model.currentAttachmentLink != nil)
|
||||
#expect(model.attachmentErrorMessage == nil)
|
||||
#expect(!model.markAttachmentRendered(id: "file_1", previewID: failedPreviewID))
|
||||
#expect(model.markAttachmentRendered(id: "file_1", previewID: nextPreviewID))
|
||||
}
|
||||
|
||||
/// 预览失败停止当前片段,进度刷新不得误清错误或恢复读时。
|
||||
@Test("附件预览失败停止读时,错误跨进度刷新保留")
|
||||
func previewFailureStopsReadingAndSurvivesProgressRefresh() async throws {
|
||||
let source = source()
|
||||
let model = model(source)
|
||||
await loadRendered(model)
|
||||
let previewID = try #require(model.attachmentPreviewID)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...3 { await model.recordVisibleTime(at: Double(second)) }
|
||||
model.reportAttachmentFailure(id: "file_1", previewID: previewID, message: "文档加载失败")
|
||||
#expect(!model.isReading && !model.isAttachmentRendered)
|
||||
await model.flush()
|
||||
await model.refreshProgress()
|
||||
#expect(model.attachmentErrorMessage == "文档加载失败")
|
||||
#expect(model.errorMessage == "文档加载失败")
|
||||
#expect(!model.markAttachmentRendered(id: "file_1", previewID: previewID))
|
||||
await model.startReading(at: 100)
|
||||
await model.recordVisibleTime(at: 101)
|
||||
let calls = await source.heartbeats
|
||||
#expect(calls.count == 1)
|
||||
#expect(calls.first?.activeSecondsTotal == 3 && calls.first?.final == true)
|
||||
#expect(await source.startReadingRequests.count == 1)
|
||||
#expect(model.progress?.readingSeconds == 3)
|
||||
}
|
||||
|
||||
/// 切文档关闭旧片段,旧下载成功或失败回调都不能改变新文档。
|
||||
@Test("切附件先 final 保存,旧预览回调无效,新文档重新计时")
|
||||
func switchingAttachmentClosesReadingAndRejectsLatePreview() async throws {
|
||||
let source = source()
|
||||
await source.configure {
|
||||
$0.attachmentsHandler = { _ in TrainingCursorPage(items: [TrainingRemoteFixtures.attachment(), TrainingRemoteFixtures.attachment(id: "file_2")], nextCursor: nil, hasMore: false, serverTime: nil) }
|
||||
}
|
||||
let model = model(source)
|
||||
await loadRendered(model)
|
||||
let oldPreviewID = try #require(model.attachmentPreviewID)
|
||||
await model.startReading(at: 0)
|
||||
for second in 1...3 { await model.recordVisibleTime(at: Double(second)) }
|
||||
await model.selectAttachment(id: "file_2")
|
||||
let calls = await source.heartbeats
|
||||
#expect(calls.count == 1)
|
||||
#expect(calls.first?.activeSecondsTotal == 3 && calls.first?.final == true)
|
||||
#expect(!model.isReading && !model.isAttachmentRendered)
|
||||
#expect(!model.markAttachmentRendered(id: "file_1", previewID: oldPreviewID))
|
||||
model.reportAttachmentFailure(id: "file_1", previewID: oldPreviewID, message: "迟到错误")
|
||||
#expect(model.attachmentErrorMessage == nil)
|
||||
await model.startReading(at: 100)
|
||||
#expect(await source.startReadingRequests.count == 1)
|
||||
let previewID = try #require(model.attachmentPreviewID)
|
||||
#expect(model.markAttachmentRendered(id: "file_2", previewID: previewID))
|
||||
await model.startReading(at: 200)
|
||||
await model.recordVisibleTime(at: 201)
|
||||
#expect(model.activeSecondsTotal == 1)
|
||||
#expect(await source.startReadingRequests.count == 2)
|
||||
#expect(model.progress?.readingSeconds == 3)
|
||||
}
|
||||
|
||||
/// 不能显示服务端错误返回的另一份附件,即使地址本身和有效期合法。
|
||||
@Test("附件授权响应必须匹配所选附件标识")
|
||||
func mismatchedAttachmentAuthorizationIsRejected() async {
|
||||
let source = source()
|
||||
await source.configure { $0.attachmentURLHandler = { _ in
|
||||
TrainingAttachmentLink(id: "other-file", name: "另一份文档.txt",
|
||||
url: URL(string: "https://example.test/other.txt")!,
|
||||
expiresAt: TrainingRemoteFixtures.now.addingTimeInterval(60))
|
||||
} }
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
#expect(model.currentAttachmentID == "file_1")
|
||||
#expect(model.currentAttachmentLink == nil)
|
||||
#expect(model.attachmentErrorMessage != nil)
|
||||
await model.startReading(at: 0)
|
||||
#expect(await source.startReadingRequests.isEmpty)
|
||||
}
|
||||
|
||||
/// 两种网页协议都能打开,带签名的查询参数不在客户端改写。
|
||||
@@ -414,10 +630,10 @@ struct TrainingRemoteReadingTests {
|
||||
func readOnlyDoesNotStartSession() async {
|
||||
let source = source(seconds: 20)
|
||||
let model = model(source)
|
||||
await model.load()
|
||||
await loadRendered(model)
|
||||
await model.startReading(at: 0)
|
||||
await model.recordVisibleTime(at: 1000)
|
||||
#expect(model.currentSection != nil)
|
||||
#expect(model.currentAttachmentLink != nil)
|
||||
#expect(await source.startReadingRequests.isEmpty)
|
||||
#expect(await source.heartbeats.isEmpty)
|
||||
#expect(!model.isReading)
|
||||
@@ -473,13 +689,13 @@ private actor TrainingReadingReplyController {
|
||||
}
|
||||
}
|
||||
|
||||
/// 不使用固定延时的正文响应闸门,精确控制身份切换和迟到响应的先后顺序。
|
||||
private actor TrainingReadingBodyGate {
|
||||
private var response: CheckedContinuation<TrainingSectionContent, Never>?
|
||||
/// 不使用固定延时的附件授权响应闸门,控制身份切换和迟到响应顺序。
|
||||
private actor TrainingReadingAttachmentGate {
|
||||
private var response: CheckedContinuation<TrainingAttachmentLink, Never>?
|
||||
private var waiters: [CheckedContinuation<Void, Never>] = []
|
||||
|
||||
/// 等待测试明确释放正文,通知等待请求已发出的测试代码。
|
||||
func body() async -> TrainingSectionContent {
|
||||
/// 等待测试释放短链,通知测试请求已挂起。
|
||||
func link() async -> TrainingAttachmentLink {
|
||||
await withCheckedContinuation {
|
||||
response = $0
|
||||
waiters.forEach { $0.resume() }
|
||||
@@ -487,15 +703,17 @@ private actor TrainingReadingBodyGate {
|
||||
}
|
||||
}
|
||||
|
||||
/// 不轮询地等待正文请求已经挂起。
|
||||
/// 等待授权请求已经挂起。
|
||||
func waitUntilRequested() async {
|
||||
if response != nil { return }
|
||||
await withCheckedContinuation { waiters.append($0) }
|
||||
}
|
||||
|
||||
/// 在身份已变化后返回旧账号正文。
|
||||
/// 在身份变化后返回旧任务附件地址。
|
||||
func release() {
|
||||
response?.resume(returning: TrainingSectionContent(id: "s1", order: 1, title: "旧正文", contentHTML: "<p>旧账号课程</p>"))
|
||||
response?.resume(returning: TrainingAttachmentLink(id: "file_1", name: "旧文档.txt",
|
||||
url: URL(string: "https://example.test/training.txt")!,
|
||||
expiresAt: TrainingRemoteFixtures.now.addingTimeInterval(3600)))
|
||||
response = nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user