perf(training): 优化答题自动保存与培训页面交互
This commit is contained in:
@@ -3,12 +3,11 @@ import Testing
|
||||
import UIKit
|
||||
@testable import suixinkan
|
||||
|
||||
/// 分别在分页正文和位置保存阶段,覆盖前后翻题的同步动作锁。
|
||||
/// 仅未缓存题页读取需要同步动作锁,位置保存由后台串行处理。
|
||||
enum TrainingNavigationScenario: String, CaseIterable, Sendable {
|
||||
case previousQuestions, nextQuestions, previousPosition, nextPosition
|
||||
case previousQuestions, nextQuestions
|
||||
|
||||
var isPrevious: Bool { self == .previousQuestions || self == .previousPosition }
|
||||
var waitsForQuestions: Bool { self == .previousQuestions || self == .nextQuestions }
|
||||
var isPrevious: Bool { self == .previousQuestions }
|
||||
var initialOrder: Int { isPrevious ? 11 : 10 }
|
||||
var targetOrder: Int { isPrevious ? 10 : 11 }
|
||||
var initialOffset: Int { isPrevious ? 10 : 0 }
|
||||
@@ -64,7 +63,7 @@ private struct TrainingNavigationButtonAppearance: Equatable {
|
||||
struct TrainingExamNavigationInteractionTests {
|
||||
private let context = TrainingContext(accountScope: "exam_navigation", scenicID: 10, storeID: 3)
|
||||
|
||||
@Test("前后翻题等待题页或位置保存时不置灰,重复动作不会偏离目标", arguments: TrainingNavigationScenario.allCases)
|
||||
@Test("未缓存题页读取时不置灰,重复动作不会偏离目标", arguments: TrainingNavigationScenario.allCases)
|
||||
func navigationPreservesAppearanceAndTarget(scenario: TrainingNavigationScenario) async throws {
|
||||
let domain = try makeDefaults()
|
||||
defer { domain.defaults.removePersistentDomain(forName: domain.name) }
|
||||
@@ -72,14 +71,9 @@ struct TrainingExamNavigationInteractionTests {
|
||||
defer { Task { await gate.release() } }
|
||||
var configuration = TrainingDataStub.Configuration()
|
||||
configuration.questionsHandler = { offset, limit in
|
||||
if scenario.waitsForQuestions, offset == scenario.targetOffset { await gate.wait() }
|
||||
if offset == scenario.targetOffset { await gate.wait() }
|
||||
return Self.questions(total: 30, offset: offset, limit: limit)
|
||||
}
|
||||
configuration.saveHandler = { request in
|
||||
if !scenario.waitsForQuestions { await gate.wait() }
|
||||
return TrainingDraftConfirmation(draftRevision: request.baseRevision,
|
||||
savedAt: TrainingRemoteFixtures.now, answeredCount: 30)
|
||||
}
|
||||
let source = TrainingDataStub(configuration: configuration)
|
||||
await source.seedDraft(revision: 5, answers: Dictionary(uniqueKeysWithValues: (1...30).map { ("q\($0)", ["A"]) }), lastOrder: scenario.initialOrder)
|
||||
let model = makeModel(defaults: domain.defaults)
|
||||
@@ -125,7 +119,7 @@ struct TrainingExamNavigationInteractionTests {
|
||||
#expect(await source.submissionRequests.isEmpty)
|
||||
let questionsWhileWaiting = await source.calls.filter { $0.name == "questions" }
|
||||
#expect(questionsWhileWaiting.map(\.offset) == [scenario.initialOffset, scenario.targetOffset])
|
||||
#expect(await source.savedRequests.count == (scenario.waitsForQuestions ? 0 : 1))
|
||||
#expect(await source.savedRequests.isEmpty)
|
||||
|
||||
await gate.release()
|
||||
try await waitUntil {
|
||||
@@ -136,6 +130,7 @@ struct TrainingExamNavigationInteractionTests {
|
||||
#expect(model.errorMessage == nil)
|
||||
#expect([previous, next, submit].allSatisfy { $0.isEnabled && $0.isUserInteractionEnabled })
|
||||
#expect([previous, next, submit].map { TrainingNavigationButtonAppearance($0) } == Array(appearances.prefix(3)))
|
||||
#expect(await model.flushDraft(service: source))
|
||||
let requests = await source.savedRequests
|
||||
#expect(requests.count == 1)
|
||||
#expect(requests.first?.lastQuestionOrder == scenario.targetOrder)
|
||||
@@ -176,6 +171,7 @@ struct TrainingExamNavigationInteractionTests {
|
||||
#expect(allowed.alpha == 1)
|
||||
#expect(button("training.exam.submit", in: controller.view)?.isEnabled == true)
|
||||
#expect(controller.navigationItem.rightBarButtonItem?.isEnabled == true)
|
||||
#expect(await model.flushDraft(service: source))
|
||||
let before = await source.calls
|
||||
disabled.sendActions(for: .touchUpInside)
|
||||
try await settleTransitions([navigation, controller])
|
||||
@@ -271,12 +267,75 @@ struct TrainingExamNavigationInteractionTests {
|
||||
#expect(finalCalls.filter { $0.name == "questions" }.map(\.offset) == [400, 490])
|
||||
#expect(finalCalls.filter { $0.name == "draft" }.map(\.limit) == [0, 10, 10])
|
||||
#expect(finalCalls.filter { $0.name == "questions" }.allSatisfy { $0.limit == 10 })
|
||||
#expect(await model.flushDraft(service: source))
|
||||
let saves = await source.savedRequests
|
||||
#expect(saves.count == 1)
|
||||
#expect(saves.first?.lastQuestionOrder == 500 && saves.first?.changes.isEmpty == true)
|
||||
#expect(await source.submissionRequests.isEmpty)
|
||||
}
|
||||
|
||||
@Test("保存挂起时缓存翻题和答题卡继续可用,本地答案不会被旧页刷新覆盖")
|
||||
func cachedNavigationAndAnswerCardRemainAvailableWhileSaving() async throws {
|
||||
let manager = GlobalLoadingManager.shared
|
||||
manager.hideAll()
|
||||
defer { manager.hideAll() }
|
||||
let domain = try makeDefaults()
|
||||
defer { domain.defaults.removePersistentDomain(forName: domain.name) }
|
||||
let gate = TrainingNavigationRequestGate()
|
||||
defer { Task { await gate.release() } }
|
||||
var configuration = TrainingDataStub.Configuration()
|
||||
configuration.questionCount = 3
|
||||
configuration.saveHandler = { request in
|
||||
await gate.wait()
|
||||
return TrainingDraftConfirmation(draftRevision: request.baseRevision + (request.changes.isEmpty ? 0 : 1),
|
||||
savedAt: TrainingRemoteFixtures.now, answeredCount: 3)
|
||||
}
|
||||
let source = TrainingDataStub(configuration: configuration)
|
||||
await source.seedDraft(revision: 5, answers: ["q1": ["A"], "q2": ["A"], "q3": ["A"]], lastOrder: 2)
|
||||
let model = makeModel(defaults: domain.defaults)
|
||||
let controller = TrainingExamViewController(taskID: "task_1", service: source, viewModel: model)
|
||||
let navigation = UINavigationController(rootViewController: controller)
|
||||
let window = try makeWindow(navigation)
|
||||
defer { window.isHidden = true }
|
||||
try await waitUntil { model.canEdit && !manager.isShowing && self.button("training.exam.next", in: controller.view)?.isEnabled == true }
|
||||
try await settleTransitions([navigation, controller])
|
||||
let initialReads = await source.calls.filter { ["task", "progress", "paper", "questions", "draft"].contains($0.name) }
|
||||
let optionB = try #require(button("training.option.B", in: controller.view))
|
||||
optionB.sendActions(for: .touchUpInside)
|
||||
try await waitUntil { await gate.requestCount == 1 }
|
||||
#expect(model.isSaving && model.canEdit && !manager.isShowing)
|
||||
let next = try #require(button("training.exam.next", in: controller.view))
|
||||
next.sendActions(for: .touchUpInside)
|
||||
// 缓存跳转须当场完成,不能等正在保存的首批请求。
|
||||
#expect(model.currentIndex == 2 && model.currentQuestion?.id == "q3")
|
||||
#expect(!model.isBusy && model.isSaving && !manager.isShowing)
|
||||
#expect(!next.isEnabled)
|
||||
let previous = try #require(button("training.exam.previous", in: controller.view))
|
||||
#expect(previous.isEnabled && previous.isUserInteractionEnabled)
|
||||
previous.sendActions(for: .touchUpInside)
|
||||
#expect(model.currentIndex == 1 && model.currentSelectedIDs == ["B"])
|
||||
#expect(button("training.option.B", in: controller.view)?.accessibilityTraits.contains(.selected) == true)
|
||||
#expect(!manager.isShowing && model.canEdit)
|
||||
let cardSheet = try await openAnswerCard(controller)
|
||||
let card = try #require(cardSheet.topViewController as? TrainingAnswerCardViewController)
|
||||
let collection = try #require(allViews(card.view).compactMap { $0 as? UICollectionView }.first)
|
||||
try await waitUntil { collection.numberOfItems(inSection: 0) == 3 }
|
||||
#expect(!manager.isShowing && model.isSaving)
|
||||
let done = try #require(card.navigationItem.rightBarButtonItem)
|
||||
try activate(done)
|
||||
try await waitUntil { controller.presentedViewController == nil }
|
||||
try await settleTransitions([navigation, controller, cardSheet])
|
||||
#expect(model.currentIndex == 1 && model.currentSelectedIDs == ["B"])
|
||||
#expect(!manager.isShowing && model.canEdit)
|
||||
#expect(await source.savedRequests.count == 1)
|
||||
let finalReads = await source.calls.filter { ["task", "progress", "paper", "questions", "draft"].contains($0.name) }
|
||||
#expect(finalReads == initialReads)
|
||||
#expect(navigation.topViewController === controller)
|
||||
await gate.release()
|
||||
#expect(await model.flushDraft(service: source))
|
||||
#expect(!model.isSaving && !model.hasUnsavedChanges && model.currentSelectedIDs == ["B"])
|
||||
}
|
||||
|
||||
private nonisolated static func questions(total: Int, offset: Int, limit: Int) -> TrainingQuestionPage {
|
||||
let end = min(total, offset + limit)
|
||||
let items: [TrainingQuestionItem] = offset < end ? ((offset + 1)...end).map(TrainingRemoteFixtures.question) : []
|
||||
@@ -300,9 +359,7 @@ struct TrainingExamNavigationInteractionTests {
|
||||
let window = UIWindow(windowScene: scene)
|
||||
window.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
|
||||
window.rootViewController = navigation
|
||||
navigation.beginAppearanceTransition(true, animated: false)
|
||||
window.isHidden = false
|
||||
navigation.endAppearanceTransition()
|
||||
navigation.view.layoutIfNeeded()
|
||||
return window
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user