115 lines
4.7 KiB
Swift
115 lines
4.7 KiB
Swift
|
|
import Foundation
|
||
|
|
import XCTest
|
||
|
|
@testable import ChessPenguin
|
||
|
|
|
||
|
|
private actor PageLoaderProbe {
|
||
|
|
private(set) var calls = 0
|
||
|
|
|
||
|
|
func load(database: Database, start: UInt32, count: UInt32) async -> [GameRow] {
|
||
|
|
calls += 1
|
||
|
|
try? await Task.sleep(nanoseconds: 100_000_000)
|
||
|
|
return database.rows(start: start, count: count)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
final class GameLoadStateTests: XCTestCase {
|
||
|
|
func testLateRetryCannotReplaceGameFromNewPlace() {
|
||
|
|
var state = GameLoadState<String>()
|
||
|
|
|
||
|
|
// Retry A is pending. Place B opens and finishes first.
|
||
|
|
state.begin()
|
||
|
|
XCTAssertTrue(state.succeed(requestedPlace: 1, currentPlace: 1, game: "GAME_B"))
|
||
|
|
|
||
|
|
// Late success and failure from retry A are both ignored.
|
||
|
|
XCTAssertFalse(state.succeed(requestedPlace: 0, currentPlace: 1, game: "GAME_A"))
|
||
|
|
XCTAssertFalse(state.fail(requestedPlace: 0, currentPlace: 1, message: "late A"))
|
||
|
|
XCTAssertEqual(state.loadedPlace, 1)
|
||
|
|
XCTAssertEqual(state.game, "GAME_B")
|
||
|
|
XCTAssertNil(state.failure)
|
||
|
|
}
|
||
|
|
|
||
|
|
func testCurrentRequestStillApplies() {
|
||
|
|
var state = GameLoadState<String>()
|
||
|
|
XCTAssertTrue(state.succeed(requestedPlace: 0, currentPlace: 0, game: "GAME_A"))
|
||
|
|
XCTAssertEqual(state.loadedPlace, 0)
|
||
|
|
XCTAssertEqual(state.game, "GAME_A")
|
||
|
|
|
||
|
|
state.begin()
|
||
|
|
XCTAssertNil(state.failure)
|
||
|
|
XCTAssertTrue(state.fail(requestedPlace: 0, currentPlace: 0, message: "read error"))
|
||
|
|
XCTAssertNil(state.game)
|
||
|
|
XCTAssertEqual(state.failure, "read error")
|
||
|
|
}
|
||
|
|
|
||
|
|
func testPagesContainAtMostOneHundredRows() {
|
||
|
|
XCTAssertEqual(nextGameRowPage(loaded: 0, total: 250), GameRowPage(start: 0, count: 100))
|
||
|
|
XCTAssertEqual(nextGameRowPage(loaded: 100, total: 250), GameRowPage(start: 100, count: 100))
|
||
|
|
XCTAssertEqual(nextGameRowPage(loaded: 200, total: 250), GameRowPage(start: 200, count: 50))
|
||
|
|
XCTAssertNil(nextGameRowPage(loaded: 250, total: 250))
|
||
|
|
XCTAssertNil(nextGameRowPage(loaded: 0, total: 0))
|
||
|
|
XCTAssertEqual(nextGameRowPage(loaded: 0, total: 100), GameRowPage(start: 0, count: 100))
|
||
|
|
}
|
||
|
|
|
||
|
|
func testNextAcrossPageBoundariesRequestsPageThenUsesLoadedRow() {
|
||
|
|
// Human transition 100 → 101: place 99 needs the second page for place 100.
|
||
|
|
XCTAssertEqual(
|
||
|
|
nextGameRequest(fromPlace: 99, totalGames: 250, loadedRows: 100),
|
||
|
|
NextGameRequest(fromPlace: 99, toPlace: 100, needsNextPage: true)
|
||
|
|
)
|
||
|
|
XCTAssertEqual(
|
||
|
|
nextGameRequest(fromPlace: 100, totalGames: 250, loadedRows: 200),
|
||
|
|
NextGameRequest(fromPlace: 100, toPlace: 101, needsNextPage: false)
|
||
|
|
)
|
||
|
|
|
||
|
|
// Human transition 200 → 201 follows the same rule at the third page.
|
||
|
|
XCTAssertEqual(
|
||
|
|
nextGameRequest(fromPlace: 199, totalGames: 250, loadedRows: 200),
|
||
|
|
NextGameRequest(fromPlace: 199, toPlace: 200, needsNextPage: true)
|
||
|
|
)
|
||
|
|
XCTAssertEqual(
|
||
|
|
nextGameRequest(fromPlace: 200, totalGames: 250, loadedRows: 250),
|
||
|
|
NextGameRequest(fromPlace: 200, toPlace: 201, needsNextPage: false)
|
||
|
|
)
|
||
|
|
XCTAssertNil(nextGameRequest(fromPlace: 249, totalGames: 250, loadedRows: 250))
|
||
|
|
}
|
||
|
|
|
||
|
|
@MainActor
|
||
|
|
func testConcurrentFooterAndNextShareInFlightPageLoad() async throws {
|
||
|
|
let pgn = (0..<250).map {
|
||
|
|
"[Event \"Game \($0)\"]\n[White \"White\"]\n[Black \"Black\"]\n\n1. e4 *\n\n"
|
||
|
|
}.joined()
|
||
|
|
let database = try openBytes(name: "250.pgn", bytes: Data(pgn.utf8))
|
||
|
|
let probe = PageLoaderProbe()
|
||
|
|
let model = AppModel(
|
||
|
|
database: database,
|
||
|
|
rows: database.rows(start: 0, count: 100),
|
||
|
|
totalGames: 250
|
||
|
|
) { database, start, count in
|
||
|
|
await probe.load(database: database, start: start, count: count)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Footer starts the delayed page load. Next must await the same task, not return early.
|
||
|
|
async let footer = model.loadNextPage()
|
||
|
|
async let next = model.loadNextPage()
|
||
|
|
await next
|
||
|
|
XCTAssertEqual(model.rows.count, 200, "Next returned before the shared page was appended")
|
||
|
|
await footer
|
||
|
|
|
||
|
|
let calls = await probe.calls
|
||
|
|
XCTAssertEqual(calls, 1)
|
||
|
|
XCTAssertEqual(model.rows.count, 200)
|
||
|
|
XCTAssertFalse(model.isLoadingPage)
|
||
|
|
|
||
|
|
// The guard used by show(100) now passes for both awaiting callers.
|
||
|
|
let request = try XCTUnwrap(nextGameRequest(
|
||
|
|
fromPlace: 99,
|
||
|
|
totalGames: model.totalGames,
|
||
|
|
loadedRows: model.rows.count
|
||
|
|
))
|
||
|
|
XCTAssertEqual(request.toPlace, 100)
|
||
|
|
XCTAssertFalse(request.needsNextPage)
|
||
|
|
XCTAssertTrue(model.rows.indices.contains(request.toPlace))
|
||
|
|
XCTAssertEqual(model.rows[request.toPlace].index, 100)
|
||
|
|
}
|
||
|
|
}
|