// // ToastCenterTests.swift // suixinkanTests // // Created by Codex on 2026/6/23. // import XCTest @testable import suixinkan @MainActor /// 全局 Toast 状态中心测试,覆盖自动隐藏和连续展示行为。 final class ToastCenterTests: XCTestCase { /// 测试 show 会立即写入当前 Toast 文案。 func testShowStoresMessage() { let center = ToastCenter(autoDismissNanoseconds: 100_000_000) center.show("保存成功") XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: "保存成功")) } /// 测试 Toast 到达自动隐藏时间后会清空文案。 func testToastAutoDismissesAfterDelay() async throws { let center = ToastCenter(autoDismissNanoseconds: 60_000_000) center.show("保存成功") try await Task.sleep(nanoseconds: 90_000_000) XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil)) } /// 测试连续 show 会覆盖旧文案,并重新计算自动隐藏时间。 func testRepeatedShowResetsAutoDismissTimer() async throws { let center = ToastCenter(autoDismissNanoseconds: 100_000_000) center.show("第一条") try await Task.sleep(nanoseconds: 70_000_000) center.show("第二条") try await Task.sleep(nanoseconds: 60_000_000) XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: "第二条")) try await Task.sleep(nanoseconds: 70_000_000) XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil)) } /// 测试手动 dismiss 会立即清空文案,并取消当前自动隐藏任务。 func testDismissClearsMessageImmediately() async throws { let center = ToastCenter(autoDismissNanoseconds: 100_000_000) center.show("保存成功") center.dismiss() XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil)) try await Task.sleep(nanoseconds: 130_000_000) XCTAssertEqual(center.snapshotForTests, ToastSnapshot(message: nil)) } }