fix(training): 修复课程学习倒计时停顿
This commit is contained in:
@@ -203,6 +203,93 @@ final class TrainingDocumentPreviewTests: XCTestCase {
|
||||
XCTAssertFalse(calls.contains { ["sections", "section"].contains($0.name) })
|
||||
}
|
||||
|
||||
/// 实际课程滚动回调持续触发时,按钮倒计时仍随有效阅读时间减少。
|
||||
func testContinuousCourseScrollingKeepsCountdownRunning() async throws {
|
||||
let document = try temporaryDocument(text: (1...120).map { "第 \($0) 行:滑动阅读时仍应累计有效读时。" }.joined(separator: "\n"), kind: .txt)
|
||||
defer { try? FileManager.default.removeItem(at: document.fileURL.deletingLastPathComponent()) }
|
||||
let (controller, _, scroll, window) = try await makeCourseWindow(document: document, readingSeconds: 0)
|
||||
defer { closeWindow(window) }
|
||||
let action = try XCTUnwrap(allViews(controller.view).compactMap { $0 as? UIButton }
|
||||
.first { $0.accessibilityIdentifier == "training.reading.action" })
|
||||
func remainingSeconds() -> Int? {
|
||||
guard let title = action.configuration?.title, let start = title.lastIndex(of: "("),
|
||||
let end = title.range(of: "s)")?.lowerBound else { return nil }
|
||||
return Int(title[title.index(after: start)..<end])
|
||||
}
|
||||
try await waitUntil { remainingSeconds() != nil }
|
||||
let before = try XCTUnwrap(remainingSeconds())
|
||||
let deadline = ContinuousClock.now.advanced(by: .milliseconds(3200))
|
||||
var frame = 0
|
||||
while ContinuousClock.now < deadline {
|
||||
scroll.setContentOffset(CGPoint(x: 0, y: 220 + frame % 80), animated: false)
|
||||
frame += 1
|
||||
try await Task.sleep(for: .milliseconds(16))
|
||||
}
|
||||
XCTAssertGreaterThan(frame, 60)
|
||||
let after = try XCTUnwrap(remainingSeconds())
|
||||
XCTAssertLessThanOrEqual(after, before - 2, "正文持续可见时,滚动不能重置计时起点")
|
||||
record(UIGraphicsImageRenderer(size: controller.view.bounds.size).image { _ in
|
||||
controller.view.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
|
||||
}, name: "课程持续滚动倒计时")
|
||||
}
|
||||
|
||||
/// 慢心跳不能冻结真实页面倒计时,读满后仍须等待后端确认才能开始考试。
|
||||
func testSlowHeartbeatKeepsCountdownRunningUntilServerConfirmsReading() async throws {
|
||||
let document = try temporaryDocument(text: (1...120).map { "第 \($0) 行:课程阅读期间上报进度不应打断倒计时。" }.joined(separator: "\n"), kind: .txt)
|
||||
defer { try? FileManager.default.removeItem(at: document.fileURL.deletingLastPathComponent()) }
|
||||
let gate = TrainingCourseHeartbeatGate()
|
||||
defer { Task { await gate.releaseAll() } }
|
||||
var configuration = TrainingDataStub.Configuration()
|
||||
configuration.readingSeconds = 14
|
||||
let source = TrainingDataStub(configuration: configuration)
|
||||
await source.configure {
|
||||
$0.heartbeatHandler = { call in
|
||||
await gate.wait(sequence: call.sequence)
|
||||
return TrainingHeartbeatConfirmation(sessionID: call.sessionID, acceptedSequence: call.sequence,
|
||||
acceptedActiveSecondsTotal: call.activeSecondsTotal, sessionClosed: call.final,
|
||||
progress: TrainingRemoteFixtures.progress(seconds: min(20, 14 + call.activeSecondsTotal)))
|
||||
}
|
||||
}
|
||||
let controller = TrainingCourseViewController(taskID: "task_1", service: source,
|
||||
documentLoader: TrainingFixtureDocumentLoader { _, _ in document })
|
||||
let navigation = UINavigationController(rootViewController: controller)
|
||||
let window = try makeWindow(navigation)
|
||||
defer { closeWindow(window) }
|
||||
let action = try XCTUnwrap(allViews(controller.view).compactMap { $0 as? UIButton }
|
||||
.first { $0.accessibilityIdentifier == "training.reading.action" })
|
||||
try await waitUntil {
|
||||
await source.heartbeats.count == 1 && action.configuration?.title == "开始考试(1s)"
|
||||
}
|
||||
let firstHeartbeats = await source.heartbeats
|
||||
let first = try XCTUnwrap(firstHeartbeats.first)
|
||||
XCTAssertEqual(first.sequence, 1)
|
||||
XCTAssertEqual(first.activeSecondsTotal, 5)
|
||||
XCTAssertFalse(first.final)
|
||||
try await waitUntil { action.configuration?.title == "开始考试(0s)" }
|
||||
XCTAssertFalse(action.isEnabled, "本地读满而首个心跳尚未返回,不能提前开始考试")
|
||||
let heldHeartbeats = await source.heartbeats
|
||||
XCTAssertEqual(heldHeartbeats.count, 1, "同一会话的心跳必须串行发送")
|
||||
action.sendActions(for: .touchUpInside)
|
||||
XCTAssertTrue(navigation.topViewController === controller)
|
||||
let callsBeforeConfirmation = await source.calls
|
||||
XCTAssertFalse(callsBeforeConfirmation.contains { ["paper", "questions", "draft"].contains($0.name) })
|
||||
|
||||
await gate.release(sequence: 1)
|
||||
try await waitUntil { await source.heartbeats.count == 2 }
|
||||
let finalHeartbeats = await source.heartbeats
|
||||
let final = try XCTUnwrap(finalHeartbeats.last)
|
||||
XCTAssertEqual(final.sessionID, first.sessionID)
|
||||
XCTAssertEqual(final.sequence, 2)
|
||||
XCTAssertEqual(final.activeSecondsTotal, 6)
|
||||
XCTAssertTrue(final.final)
|
||||
XCTAssertEqual(action.configuration?.title, "开始考试(0s)")
|
||||
XCTAssertFalse(action.isEnabled, "后端只确认了 19 秒,最终 20 秒确认返回前仍应禁用")
|
||||
|
||||
await gate.release(sequence: 2)
|
||||
try await waitUntil { action.isEnabled && action.configuration?.title == "开始考试" }
|
||||
XCTAssertTrue(navigation.topViewController === controller)
|
||||
}
|
||||
|
||||
/// 长文件由课程页面统一滚动,原生信息随正文离屏,换为短文件后不残留空白高度。
|
||||
func testLongTextUsesOuterPageScrollingAndShrinksForNextDocument() async throws {
|
||||
let lines = (1...120).map { "第 \($0) 行:这是完整课程阅读内容,向上滑动时标题和正文应一同移动。" }
|
||||
@@ -374,8 +461,10 @@ final class TrainingDocumentPreviewTests: XCTestCase {
|
||||
return TrainingLocalDocument(fileURL: file, kind: kind, name: file.lastPathComponent, temporaryDirectory: nil)
|
||||
}
|
||||
|
||||
private func makeCourseWindow(document: TrainingLocalDocument) async throws -> (TrainingCourseViewController, TrainingDocumentPreviewView, UIScrollView, UIWindow) {
|
||||
let source = TrainingDataStub()
|
||||
private func makeCourseWindow(document: TrainingLocalDocument, readingSeconds: Int = 20) async throws -> (TrainingCourseViewController, TrainingDocumentPreviewView, UIScrollView, UIWindow) {
|
||||
var configuration = TrainingDataStub.Configuration()
|
||||
configuration.readingSeconds = readingSeconds
|
||||
let source = TrainingDataStub(configuration: configuration)
|
||||
let name = document.name
|
||||
let mime: String
|
||||
switch document.kind {
|
||||
@@ -469,3 +558,27 @@ final class TrainingDocumentPreviewTests: XCTestCase {
|
||||
guard reached else { throw CancellationError() }
|
||||
}
|
||||
}
|
||||
|
||||
/// 精确挂起课程心跳响应,确保超时或断言失败后也能释放所有等待。
|
||||
private actor TrainingCourseHeartbeatGate {
|
||||
private var releasedSequences: Set<Int> = []
|
||||
private var didReleaseAll = false
|
||||
private var waiters: [Int: [CheckedContinuation<Void, Never>]] = [:]
|
||||
|
||||
func wait(sequence: Int) async {
|
||||
guard !didReleaseAll, !releasedSequences.contains(sequence) else { return }
|
||||
await withCheckedContinuation { waiters[sequence, default: []].append($0) }
|
||||
}
|
||||
|
||||
func release(sequence: Int) {
|
||||
releasedSequences.insert(sequence)
|
||||
waiters.removeValue(forKey: sequence)?.forEach { $0.resume() }
|
||||
}
|
||||
|
||||
func releaseAll() {
|
||||
didReleaseAll = true
|
||||
let pending = waiters.values.flatMap { $0 }
|
||||
waiters.removeAll()
|
||||
pending.forEach { $0.resume() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user