Zum Inhalt

apple/PhysarumApp/Tests/RemoteControllerThrottleTests.swift

import XCTest
@testable import Physarum

/// Param edits fire as fast as a slider drags; the sender coalesces them to a LAN-friendly rate
/// while selection/intents (discrete events) always pass. Time is injected so this is pure-testable.
final class RemoteControllerThrottleTests: XCTestCase {
    func testParamsThrottledSelectionImmediate() {
        var t = RemoteSendGate(minParamInterval: 0.05)
        XCTAssertTrue(t.allowParams(at: 0.00))   // first send
        XCTAssertFalse(t.allowParams(at: 0.02))  // < 50 ms since last send → drop
        XCTAssertTrue(t.allowParams(at: 0.06))   // >= 50 ms → send
        XCTAssertTrue(t.allowSelection())        // selection never throttled
    }

    func testIntervalMeasuredFromLastSendNotLastAttempt() {
        var t = RemoteSendGate(minParamInterval: 0.05)
        XCTAssertTrue(t.allowParams(at: 0.00))   // send, clock at 0.00
        XCTAssertFalse(t.allowParams(at: 0.02))  // drop — must NOT reset the clock
        XCTAssertFalse(t.allowParams(at: 0.04))  // still < 50 ms from the 0.00 send → drop
        XCTAssertTrue(t.allowParams(at: 0.05))   // exactly 50 ms from the send → send (boundary inclusive)
        XCTAssertFalse(t.allowParams(at: 0.06))  // < 50 ms from the 0.05 send → drop
    }
}