feat(training): 接入真实培训接口与学习答题恢复

This commit is contained in:
2026-10-14 16:18:48 +08:00
parent 8862ae5401
commit 01068bb24e
28 changed files with 4945 additions and 238 deletions
@@ -0,0 +1,112 @@
import Foundation
import Testing
@testable import suixinkan
/// 任务和成绩的分页、请求边界与缓存复用回归。
@Suite("正式培训摘要与解析")
struct TrainingRemoteListTests {
@Test("首页只请求概览与三条任务摘要")
func homeUsesSmallResources() async {
let source = TrainingDataStub()
let model = TrainingRemoteCenterViewModel()
await model.load(service: source)
let calls = await source.calls
#expect(Set(calls.map(\.name)) == Set(["summary", "tasks"]))
#expect(calls.first { $0.name == "tasks" }?.limit == 3)
#expect(model.tasks.count == 1)
#expect(model.latestScore == "—")
}
@Test("任务筛选传给服务端,用户翻页才消费游标")
func serverFilteringAndPagination() async {
let source = TrainingDataStub()
await source.configure {
$0.tasksHandler = { status, keyword, cursor, _ in
#expect(status == .completed)
#expect(keyword == "服务")
return TrainingCursorPage(items: [TrainingRemoteFixtures.task(id: cursor == nil ? "first" : "second")], nextCursor: cursor == nil ? "next" : nil, hasMore: cursor == nil, serverTime: nil)
}
}
let model = TrainingRemoteCenterViewModel()
model.selectTab(.tasks)
model.filter = .completed
model.keyword = " 服务 "
await model.load(service: source)
#expect(model.tasks.map(\.id) == ["first"])
#expect(model.hasMore)
await model.loadMore(service: source)
#expect(model.tasks.map(\.id) == ["first", "second"])
#expect(!model.hasMore)
#expect(await source.calls.count == 3)
}
@Test("记录页不读取任务、试卷或课程")
func recordsAreIndependent() async {
let source = TrainingDataStub()
let model = TrainingRemoteCenterViewModel()
model.selectTab(.records)
await model.load(service: source)
#expect(Set(await source.calls.map(\.name)) == Set(["summary", "attempts"]))
}
@Test("成绩首屏不预取解析,展开后只取一页并复用")
func reviewsAreLazyAndCached() async {
let source = TrainingDataStub()
let model = TrainingRemoteResultViewModel(attemptID: "attempt_1")
await model.load(service: source)
#expect(await source.calls.map(\.name) == ["attempt"])
#expect(model.reviews.isEmpty)
await model.loadReviews(service: source)
await model.loadReviews(service: source)
#expect(model.reviews.count == 1)
#expect(await source.calls.filter { $0.name == "reviews" }.count == 1)
}
@Test("解析失败保留成绩并能重试")
func reviewsFailureDoesNotHideScore() async {
let source = TrainingDataStub()
await source.configure { $0.reviewsHandler = { _ in throw APIError.networkFailed("离线") } }
let model = TrainingRemoteResultViewModel(attemptID: "attempt_1")
await model.load(service: source)
await model.loadReviews(service: source)
#expect(model.detail != nil)
#expect(model.reviewErrorMessage != nil)
await source.configure { $0.reviewsHandler = nil }
await model.loadReviews(service: source)
#expect(model.hasLoadedReviews)
}
@Test("刷新成绩后解析不可用时移除此前缓存")
func reviewAvailabilityClearsCachedPages() async {
let source = TrainingDataStub()
let model = TrainingRemoteResultViewModel(attemptID: "attempt_1")
await model.load(service: source)
await model.loadReviews(service: source)
#expect(!model.reviews.isEmpty)
await source.configure { $0.attemptHandler = { _ in TrainingRemoteFixtures.result(reviewAvailable: false) } }
await model.load(service: source)
#expect(model.detail != nil)
#expect(model.reviews.isEmpty)
#expect(!model.hasLoadedReviews)
await model.loadReviews(service: source)
#expect(await source.calls.filter { $0.name == "reviews" }.count == 1)
}
@Test("身份变化或权限撤回后清空已缓存成绩与解析", arguments: [TrainingRemoteError.identityChanged, .forbidden("任务权限已撤回")])
func identityChangeClearsCachedResult(error: TrainingRemoteError) async {
let source = TrainingDataStub()
await source.configure { $0.reviewsHandler = { _ in
TrainingReviewPage(attemptID: "attempt_1", items: [TrainingRemoteFixtures.review()], nextCursor: "next", hasMore: true)
} }
let model = TrainingRemoteResultViewModel(attemptID: "attempt_1")
await model.load(service: source)
await model.loadReviews(service: source)
#expect(!model.reviews.isEmpty)
await source.configure { $0.reviewsHandler = { _ in throw error } }
await model.loadReviews(service: source, nextPage: true)
#expect(model.isIdentityInvalid == (error == .identityChanged))
#expect(model.isAccessRevoked == (error.code == 150101))
#expect(model.detail == nil)
#expect(model.reviews.isEmpty)
}
}