feat(training): 新增已结束筛选并精简课程展示
新增已结束任务筛选与只读回顾资格,保留搜索和游标分页。移除任务小节数量及课程附件文件名、切换入口,并更新接口和界面回归测试。
This commit is contained in:
@@ -44,6 +44,59 @@ final class TrainingAPITests: XCTestCase {
|
||||
XCTAssertEqual(session.requests[0].value(forHTTPHeaderField: "X-OS-TYPE"), "iOS")
|
||||
}
|
||||
|
||||
func testEndedTaskFilterAndReadOnlyResourcesDecodeServerState() async throws {
|
||||
var endedFlags = flagsJSON
|
||||
endedFlags.merge(["can_study": false, "can_start_exam": false, "block_reason": "ended"]) { _, new in new }
|
||||
var endedTask = taskJSON
|
||||
endedTask.merge(["task_status": "closed", "learner_status": "ended", "learning": endedFlags]) { _, new in new }
|
||||
var endedDetail = endedTask
|
||||
endedDetail.merge(["required_reading_seconds": 20, "reading_rule": "course_duration", "scoring": NSNull(), "server_time": dateText]) { _, new in new }
|
||||
var endedProgress = progressJSON
|
||||
endedProgress.merge(endedFlags) { _, new in new }
|
||||
endedProgress["learner_status"] = "ended"
|
||||
let taskState: [String: Any] = ["learner_status": "ended", "completed_at": dateText, "can_retry": false, "block_reason": "ended"]
|
||||
let session = MockURLSession(responses: [
|
||||
try envelope(["items": [endedTask], "next_cursor": NSNull(), "has_more": false, "server_time": dateText]),
|
||||
try envelope(endedDetail),
|
||||
try envelope(endedProgress),
|
||||
try envelope(["attempt": attemptJSON, "task_state": taskState, "server_time": dateText]),
|
||||
])
|
||||
let api = TrainingAPI(client: APIClient(environment: .testing, session: session))
|
||||
|
||||
let page = try await api.tasks(status: .ended, keyword: "规范", cursor: nil, limit: 20)
|
||||
let task = try await api.task(id: "task_101")
|
||||
let progress = try await api.progress(taskID: "task_101")
|
||||
let attempt = try await api.attempt(id: "attempt_12")
|
||||
|
||||
let summary = try XCTUnwrap(page.items.first)
|
||||
for item in [summary, task.summary] {
|
||||
XCTAssertEqual(item.learnerStatus, .ended)
|
||||
XCTAssertEqual(item.taskStatus, .closed)
|
||||
XCTAssertTrue(item.learning.canViewCourse)
|
||||
XCTAssertFalse(item.learning.canStudy)
|
||||
XCTAssertFalse(item.learning.canStartExam)
|
||||
XCTAssertEqual(item.learning.blockReason, "ended")
|
||||
}
|
||||
XCTAssertEqual(progress.learnerStatus, .ended)
|
||||
XCTAssertTrue(progress.canViewCourse)
|
||||
XCTAssertFalse(progress.canStudy)
|
||||
XCTAssertFalse(progress.canStartExam)
|
||||
XCTAssertFalse(progress.canRetry)
|
||||
XCTAssertEqual(progress.blockReason, "ended")
|
||||
XCTAssertEqual(attempt.taskState.learnerStatus, .ended)
|
||||
XCTAssertNotNil(attempt.taskState.completedAt, "已结束状态仍保留此前完成记录")
|
||||
XCTAssertFalse(attempt.taskState.canRetry)
|
||||
XCTAssertEqual(attempt.taskState.blockReason, "ended")
|
||||
XCTAssertEqual(try query(session.requests[0]), ["learner_status": "ended", "keyword": "规范", "limit": "20"])
|
||||
XCTAssertEqual(session.requests.map { $0.url?.path }, [
|
||||
"/api/app/training/tasks", "/api/app/training/tasks/task_101",
|
||||
"/api/app/training/tasks/task_101/progress", "/api/app/training/attempts/attempt_12",
|
||||
])
|
||||
XCTAssertTrue(session.requests.allSatisfy { $0.httpMethod == "GET" })
|
||||
let encoded = try JSONEncoder().encode(TrainingLearnerStatus.ended)
|
||||
XCTAssertEqual(try JSONDecoder().decode(String.self, from: encoded), "ended")
|
||||
}
|
||||
|
||||
func testCourseResourcesCarryTaskAuthorizationAndDoNotAggregate() async throws {
|
||||
let session = MockURLSession(responses: [
|
||||
try envelope(["course_snapshot_id": "cs_3", "title": "规范", "introduction": "简介", "section_count": 1, "attachment_count": 1]),
|
||||
|
||||
@@ -9,16 +9,30 @@ enum TrainingRemoteFixtures {
|
||||
TrainingQuestionItem(id: "q\(order)", order: order, type: .single, stem: "培训题目 \(order)", options: [TrainingQuestionOption(id: "A", text: "正确选择"), TrainingQuestionOption(id: "B", text: "另一选择")])
|
||||
}
|
||||
/// 不含正文和答案的任务摘要。
|
||||
static func task(id: String = "task_1", title: String = "一线服务规范学习", questionCount: Int = 30, readingCompleted: Bool = true) -> TrainingTaskSummary {
|
||||
TrainingTaskSummary(id: id, title: title, publisherName: "培训负责人", mode: .exam, taskStatus: .published, learnerStatus: .pendingExam, startAt: nil, endAt: nil, courseSnapshotID: "course_1", courseTitle: "景区服务规范", paperSnapshotID: "paper_1", sectionCount: 2, questionCount: questionCount, passScore: 80, learning: TrainingLearningFlags(progressPercent: readingCompleted ? 100 : 0, readingCompleted: readingCompleted, hasDraft: false, canViewCourse: true, canStudy: !readingCompleted, canStartExam: readingCompleted, blockReason: readingCompleted ? nil : "reading_required"))
|
||||
static func task(id: String = "task_1", title: String = "一线服务规范学习", questionCount: Int = 30, readingCompleted: Bool = true, learnerStatus: TrainingLearnerStatus = .pendingExam) -> TrainingTaskSummary {
|
||||
TrainingTaskSummary(id: id, title: title, publisherName: "培训负责人", mode: .exam,
|
||||
taskStatus: learnerStatus == .ended ? .closed : .published, learnerStatus: learnerStatus,
|
||||
startAt: nil, endAt: nil, courseSnapshotID: "course_1", courseTitle: "景区服务规范", paperSnapshotID: "paper_1",
|
||||
sectionCount: 2, questionCount: questionCount, passScore: 80,
|
||||
learning: TrainingLearningFlags(progressPercent: readingCompleted ? 100 : 0, readingCompleted: readingCompleted,
|
||||
hasDraft: false, canViewCourse: true, canStudy: learnerStatus == .pendingExam && !readingCompleted,
|
||||
canStartExam: learnerStatus == .pendingExam && readingCompleted,
|
||||
blockReason: learnerStatus == .ended ? "ended" : readingCompleted ? nil : "reading_required"))
|
||||
}
|
||||
/// 轻量任务详情。
|
||||
static func detail(questionCount: Int = 30, readingCompleted: Bool = true) -> TrainingTaskDetail {
|
||||
TrainingTaskDetail(summary: task(questionCount: questionCount, readingCompleted: readingCompleted), requiredReadingSeconds: 20, readingRule: "course_duration", scoring: "equal-percentage-v1", serverTime: now)
|
||||
}
|
||||
/// 服务端确认读时与资格。
|
||||
static func progress(seconds: Int = 20, completed: Bool = false) -> TrainingProgressSnapshot {
|
||||
TrainingProgressSnapshot(taskID: "task_1", readingSeconds: seconds, requiredReadingSeconds: 20, completedSectionCount: seconds >= 20 ? 2 : 0, sectionCount: 2, progressPercent: seconds >= 20 ? 100 : 0, readingCompleted: seconds >= 20, learnerStatus: completed ? .completed : .pendingExam, completedAt: completed ? now : nil, hasDraft: false, canViewCourse: true, canStudy: seconds < 20 && !completed, canStartExam: seconds >= 20 && !completed, canRetry: !completed, blockReason: completed ? "completed" : seconds < 20 ? "reading_required" : nil, serverTime: now)
|
||||
static func progress(seconds: Int = 20, completed: Bool = false, learnerStatus: TrainingLearnerStatus? = nil) -> TrainingProgressSnapshot {
|
||||
let status = learnerStatus ?? (completed ? .completed : .pendingExam)
|
||||
return TrainingProgressSnapshot(taskID: "task_1", readingSeconds: seconds, requiredReadingSeconds: 20,
|
||||
completedSectionCount: seconds >= 20 ? 2 : 0, sectionCount: 2, progressPercent: seconds >= 20 ? 100 : 0,
|
||||
readingCompleted: seconds >= 20, learnerStatus: status, completedAt: completed ? now : nil, hasDraft: false,
|
||||
canViewCourse: true, canStudy: seconds < 20 && status == .pendingExam,
|
||||
canStartExam: seconds >= 20 && status == .pendingExam, canRetry: status == .pendingExam,
|
||||
blockReason: status == .ended ? "ended" : completed ? "completed" : seconds < 20 ? "reading_required" : nil,
|
||||
serverTime: now)
|
||||
}
|
||||
/// 轻量课程文档,默认使用可由本地替身稳定预览的纯文本。
|
||||
static func attachment(id: String = "file_1", name: String = "培训资料.txt", mimeType: String = "text/plain") -> TrainingAttachmentInfo {
|
||||
|
||||
@@ -378,12 +378,12 @@ struct TrainingRemoteExamTests {
|
||||
#expect(try store.load(taskID: "task_1", context: context) == pending)
|
||||
}
|
||||
|
||||
@Test("服务端已完成资格阻止读取试卷和新的交卷")
|
||||
func completedGatePreventsNewSubmission() async throws {
|
||||
@Test("服务端已完成或已结束资格阻止读取试卷和新的交卷", arguments: [TrainingLearnerStatus.completed, .ended])
|
||||
func completedGatePreventsNewSubmission(status: TrainingLearnerStatus) async throws {
|
||||
let domain = try makeDefaults()
|
||||
defer { domain.defaults.removePersistentDomain(forName: domain.name) }
|
||||
var configuration = TrainingDataStub.Configuration()
|
||||
configuration.progressHandler = { _ in TrainingRemoteFixtures.progress(completed: true) }
|
||||
configuration.progressHandler = { _ in TrainingRemoteFixtures.progress(completed: status == .completed, learnerStatus: status) }
|
||||
let source = TrainingDataStub(configuration: configuration)
|
||||
let model = makeModel(defaults: domain.defaults)
|
||||
await model.load(service: source)
|
||||
|
||||
@@ -8,6 +8,14 @@ struct TrainingRemoteListTests {
|
||||
@Test("首页只请求概览与三条任务摘要")
|
||||
func homeUsesSmallResources() async {
|
||||
let source = TrainingDataStub()
|
||||
await source.configure {
|
||||
$0.tasksHandler = { status, _, cursor, limit in
|
||||
#expect(status == .pendingExam)
|
||||
#expect(cursor == nil)
|
||||
#expect(limit == 3)
|
||||
return TrainingCursorPage(items: [TrainingRemoteFixtures.task()], nextCursor: nil, hasMore: false, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = TrainingRemoteCenterViewModel()
|
||||
await model.load(service: source)
|
||||
let calls = await source.calls
|
||||
@@ -17,25 +25,26 @@ struct TrainingRemoteListTests {
|
||||
#expect(model.latestScore == "—")
|
||||
}
|
||||
|
||||
@Test("任务筛选传给服务端,用户翻页才消费游标")
|
||||
func serverFilteringAndPagination() async {
|
||||
@Test("任务筛选传给服务端,用户翻页才消费游标", arguments: [TrainingLearnerStatus.completed, .ended])
|
||||
func serverFilteringAndPagination(status expectedStatus: TrainingLearnerStatus) async {
|
||||
let source = TrainingDataStub()
|
||||
await source.configure {
|
||||
$0.tasksHandler = { status, keyword, cursor, _ in
|
||||
#expect(status == .completed)
|
||||
#expect(status == expectedStatus)
|
||||
#expect(keyword == "服务")
|
||||
return TrainingCursorPage(items: [TrainingRemoteFixtures.task(id: cursor == nil ? "first" : "second")], nextCursor: cursor == nil ? "next" : nil, hasMore: cursor == nil, serverTime: nil)
|
||||
return TrainingCursorPage(items: [TrainingRemoteFixtures.task(id: cursor == nil ? "first" : "second", learnerStatus: expectedStatus)], nextCursor: cursor == nil ? "next" : nil, hasMore: cursor == nil, serverTime: nil)
|
||||
}
|
||||
}
|
||||
let model = TrainingRemoteCenterViewModel()
|
||||
model.selectTab(.tasks)
|
||||
model.filter = .completed
|
||||
model.filter = expectedStatus
|
||||
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.tasks.allSatisfy { $0.learnerStatus == expectedStatus })
|
||||
#expect(!model.hasMore)
|
||||
#expect(await source.calls.count == 3)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,28 @@ struct TrainingRemoteReadingTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test("已结束任务可加载附件回顾,但不建立学习会话或上报读时")
|
||||
func endedTaskIsReadOnly() async {
|
||||
let source = source(seconds: 0)
|
||||
await source.configure {
|
||||
$0.taskHandler = { _ in
|
||||
TrainingTaskDetail(summary: TrainingRemoteFixtures.task(readingCompleted: false, learnerStatus: .ended),
|
||||
requiredReadingSeconds: 20, readingRule: "course_duration", scoring: "equal-percentage-v1", serverTime: TrainingRemoteFixtures.now)
|
||||
}
|
||||
$0.progressHandler = { _ in TrainingRemoteFixtures.progress(seconds: 0, learnerStatus: .ended) }
|
||||
}
|
||||
let model = model(source)
|
||||
await loadRendered(model)
|
||||
#expect(model.currentAttachmentLink != nil)
|
||||
#expect(model.isAttachmentRendered)
|
||||
#expect(!model.canStudy)
|
||||
await model.startReading(at: 0)
|
||||
await model.recordVisibleTime(at: 10)
|
||||
#expect(!model.isReading)
|
||||
#expect(!(await source.calls).contains { ["startReading", "heartbeat"].contains($0.name) })
|
||||
#expect(await source.heartbeats.isEmpty)
|
||||
}
|
||||
|
||||
/// 首屏只读取一页附件并授权首项,不请求已经停用的小节 HTML。
|
||||
@Test("首次仅请求附件第一页并授权首项,不加载小节 HTML")
|
||||
func lazyCourseResources() async {
|
||||
|
||||
@@ -25,6 +25,7 @@ struct TrainingUIRenderingTests {
|
||||
Attachment.record(render(navigation.view), named: "培训首页", as: .png)
|
||||
let labels = allViews(controller.view).compactMap { $0 as? UILabel }
|
||||
#expect(labels.contains { $0.text == "一线服务规范学习" })
|
||||
#expect(labels.contains { $0.text == "学习 + 考试 · 30 题 · 80 分及格" })
|
||||
#expect(!labels.contains { $0.text?.contains("未同步景区后台") == true })
|
||||
|
||||
let original = try await source.task(id: "demo-service")
|
||||
@@ -34,6 +35,9 @@ struct TrainingUIRenderingTests {
|
||||
scoring: original.scoring, course: original.course, questions: original.questions, progress: original.progress)
|
||||
let cell = TrainingTaskCell(style: .default, reuseIdentifier: nil)
|
||||
cell.configure(task: longTask)
|
||||
#expect(allViews(cell.contentView).compactMap { $0 as? UILabel }.contains {
|
||||
$0.text == "学习 + 考试 · \(longTask.questions.count) 题 · \(longTask.passScore) 分及格"
|
||||
})
|
||||
let width: CGFloat = 320
|
||||
let shortCell = TrainingTaskCell(style: .default, reuseIdentifier: nil)
|
||||
shortCell.configure(task: original)
|
||||
@@ -55,6 +59,59 @@ struct TrainingUIRenderingTests {
|
||||
cell.layer.render(in: context.cgContext)
|
||||
}
|
||||
Attachment.record(cardImage, named: "320点长标题任务卡", as: .png)
|
||||
cell.configure(task: try await source.task(id: "demo-study"))
|
||||
#expect(allViews(cell.contentView).compactMap { $0 as? UILabel }.contains { $0.text == "仅学习" })
|
||||
}
|
||||
|
||||
@Test("任务页三种筛选独立查询,已结束任务显示回顾入口")
|
||||
func endedFilterAndReadOnlyCard() async throws {
|
||||
let source = TrainingDataStub()
|
||||
await source.configure {
|
||||
$0.tasksHandler = { status, _, _, _ in
|
||||
let state = status ?? .pendingExam
|
||||
return TrainingCursorPage(items: [TrainingRemoteFixtures.task(id: state.apiValue,
|
||||
title: "\(state.title)任务", learnerStatus: state)], nextCursor: nil, hasMore: false, serverTime: nil)
|
||||
}
|
||||
}
|
||||
for width: CGFloat in [320, 390] {
|
||||
let controller = TrainingCenterViewController(service: source)
|
||||
let navigation = UINavigationController(rootViewController: controller)
|
||||
let window = try makeWindow(navigation, width: width)
|
||||
defer { window.isHidden = true }
|
||||
controller.selectTab(.tasks)
|
||||
let buttons = allViews(controller.view).compactMap { $0 as? UIButton }
|
||||
.filter { $0.accessibilityIdentifier?.hasPrefix("training.filter.") == true }
|
||||
#expect(buttons.count == 3)
|
||||
for state in [TrainingLearnerStatus.ended, .completed, .pendingExam, .ended] {
|
||||
let index = try #require(TrainingLearnerStatus.allCases.firstIndex(of: state))
|
||||
let button = try #require(buttons.first { $0.tag == index })
|
||||
button.sendActions(for: .touchUpInside)
|
||||
let deadline = ContinuousClock.now.advanced(by: .seconds(5))
|
||||
while ContinuousClock.now < deadline {
|
||||
navigation.view.layoutIfNeeded()
|
||||
let hasTask = allViews(controller.view).compactMap { $0 as? UILabel }.contains { $0.text == "\(state.title)任务" }
|
||||
if button.accessibilityTraits.contains(.selected), hasTask { break }
|
||||
try await Task.sleep(for: .milliseconds(10))
|
||||
}
|
||||
#expect(button.accessibilityTraits.contains(.selected))
|
||||
#expect(buttons.filter { $0.accessibilityTraits.contains(.selected) }.count == 1)
|
||||
#expect(allViews(controller.view).compactMap { $0 as? UILabel }.contains { $0.text == "\(state.title)任务" })
|
||||
}
|
||||
#expect(buttons.map { $0.configuration?.title } == ["待考试 1", "已完成 0", "已结束"])
|
||||
for button in buttons {
|
||||
let frame = button.convert(button.bounds, to: controller.view)
|
||||
#expect(frame.minX >= 0 && frame.maxX <= width + 1)
|
||||
#expect(frame.height >= 44)
|
||||
}
|
||||
let labels = allViews(controller.view).compactMap { $0 as? UILabel }
|
||||
#expect(labels.contains { $0.text == "已结束" && $0.textColor == TrainingUI.muted })
|
||||
#expect(labels.contains { $0.text == "任务已结束,仅可回顾课程内容" })
|
||||
let action = try #require(allViews(controller.view).compactMap { $0 as? UIButton }
|
||||
.first { $0.accessibilityIdentifier == "training.task.action.ended" })
|
||||
#expect(action.configuration?.title == "回顾学习")
|
||||
#expect(action.isEnabled)
|
||||
Attachment.record(render(navigation.view), named: "\(Int(width))点-已结束筛选与只读任务", as: .png)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("课程与考试步骤等宽居中,窄屏和大字体不裁切")
|
||||
@@ -103,6 +160,8 @@ struct TrainingUIRenderingTests {
|
||||
let buttons = allViews(controller.view).compactMap { $0 as? UIButton }
|
||||
#expect(!buttons.contains { $0.accessibilityIdentifier == "training.reading.sections" || $0.accessibilityIdentifier == "training.reading.attachments" })
|
||||
#expect(!buttons.contains { $0.configuration?.title == "章节目录" || $0.configuration?.title == "课程附件" })
|
||||
#expect(!buttons.contains { $0.accessibilityIdentifier == "training.reading.documentPicker" })
|
||||
#expect(!buttons.contains { $0.configuration?.title == "切换附件" || $0.configuration?.title == "培训资料.txt" })
|
||||
let frame = step.convert(step.bounds, to: controller.view)
|
||||
#expect(abs(frame.minY - controller.view.safeAreaInsets.top - 8) < 1)
|
||||
let preview = try #require(allViews(controller.view).compactMap { $0 as? TrainingDocumentPreviewView }.first)
|
||||
|
||||
Reference in New Issue
Block a user