Files
OpenAppLock/SeveredUITests/RuleManagementUITests.swift
Brendan Chen e6c87baeba feat: clone Opal's rules feature with hard block enforcement
Rebuild the app around recurring screen-time blocking rules modeled on
Opal's My Apps tab:

- Onboarding with FamilyControls Screen Time authorization
- Apps home with Blocked Apps tiles and live rule cards
- New Rule sheet: Schedule/Time Limit/Open Limit types + preset gallery
- Rule editors: time windows (incl. overnight), day picker, app selection
  via FamilyActivityPicker (Block/Allow Only), rename, Hold to Commit
- Hard Mode: active hard rules cannot be edited, disabled, deleted, or
  unblocked until their window ends; soft rules pause until next window
- Shield enforcement through per-rule ManagedSettingsStore + RuleEnforcer
- 73 unit tests (Swift Testing) + 16 UI tests (XCUITest) with launch-arg
  harness for in-memory storage, mocked authorization, seeded scenarios
- docs/RULES_FEATURE_SPEC.md: spec derived from the reference recording

Known gap: background window transitions and time/open-limit thresholds
need a DeviceActivityMonitor extension; shields currently sync while the
app is running.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-12 12:35:52 -04:00

129 lines
5.1 KiB
Swift

//
// RuleManagementUITests.swift
// SeveredUITests
//
import XCTest
/// Detail sheet, editing, disabling, deleting, and unblocking seeded with
/// one actively-blocking rule ("Work Time") and one upcoming rule ("Sleep").
final class RuleManagementUITests: XCTestCase {
override func setUpWithError() throws {
continueAfterFailure = false
}
func testDetailShowsLiveStatusAndFacts() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
app.buttons["ruleCard-Work Time"].waitToAppear().tap()
XCTAssertEqual(app.staticTexts["detailRuleName"].waitToAppear().label, "Work Time")
XCTAssertTrue(app.staticTexts["detailStatusLabel"].label.contains("left"))
app.element("detailRow-During this time").waitToAppear()
XCTAssertTrue(app.element("detailRow-On these days").exists)
XCTAssertTrue(app.element("detailRow-Unblocks allowed").exists)
app.buttons["editRuleButton"].waitToAppear()
app.buttons["closeDetailButton"].tap()
app.buttons["newRuleButton"].waitToAppear()
}
func testEditRuleTogglesHardModeOn() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
app.buttons["ruleCard-Sleep"].waitToAppear().tap()
app.buttons["editRuleButton"].waitToAppear().tap()
app.switches["hardModeToggle"].waitToAppear().tap()
app.buttons["doneButton"].tap()
// Back on the detail view, unblocks are no longer allowed.
let row = app.element("detailRow-Unblocks allowed").waitToAppear()
XCTAssertTrue(row.label.contains("No"), "Expected 'Unblocks allowed: No', got: \(row.label)")
}
func testDisableRule() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
app.buttons["ruleCard-Sleep"].waitToAppear().tap()
app.buttons["editRuleButton"].waitToAppear().tap()
app.buttons["toggleEnabledButton"].waitToAppear().tap()
// The detail caption now reports the rule as disabled.
let status = app.staticTexts["detailStatusLabel"].waitToAppear()
XCTAssertTrue(status.label.contains("Disabled"), "Got: \(status.label)")
app.buttons["closeDetailButton"].tap()
let cardStatus = app.staticTexts["ruleStatus-Sleep"].waitToAppear()
XCTAssertEqual(cardStatus.label, "Disabled")
}
func testDeleteRuleRemovesCard() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
app.buttons["ruleCard-Sleep"].waitToAppear().tap()
app.buttons["editRuleButton"].waitToAppear().tap()
app.buttons["deleteRuleButton"].waitToAppear().tap()
app.buttons["newRuleButton"].waitToAppear()
XCTAssertFalse(
app.buttons["ruleCard-Sleep"].waitForExistence(timeout: 2),
"Deleted rule's card should disappear"
)
XCTAssertTrue(app.buttons["ruleCard-Work Time"].exists)
}
func testUnblockActiveSoftRule() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
// The active rule surfaces in Blocked Apps; unblocking pauses it.
app.buttons["blockedTile-Work Time"].waitToAppear().tap()
app.sheets.buttons["Unblock"].waitToAppear().tap()
app.staticTexts["nothingBlockedLabel"].waitToAppear()
XCTAssertEqual(app.staticTexts["ruleStatus-Work Time"].waitToAppear().label, "Paused")
}
}
/// Hard block behavior seeded with an actively-blocking Hard Mode rule.
final class HardModeUITests: XCTestCase {
override func setUpWithError() throws {
continueAfterFailure = false
}
func testHardLockedRuleCannotBeEdited() throws {
let app = XCUIApplication.launchSevered(seedScenario: "hard-mode-active")
app.buttons["ruleCard-Locked In"].waitToAppear().tap()
// The lock notice replaces Edit Rule entirely.
app.element("hardModeLockedNotice").waitToAppear()
XCTAssertFalse(app.buttons["editRuleButton"].exists)
XCTAssertTrue(app.element("detailRow-Unblocks allowed").label.contains("No"))
}
func testHardLockedRuleCannotBeUnblocked() throws {
let app = XCUIApplication.launchSevered(seedScenario: "hard-mode-active")
app.buttons["blockedTile-Locked In"].waitToAppear().tap()
// No unblock dialog just the refusal alert.
let alert = app.alerts["Hard Mode is on"].waitToAppear()
XCTAssertTrue(alert.staticTexts["This block can't be lifted until it ends."].exists)
alert.buttons["OK"].tap()
// Still blocked.
XCTAssertTrue(app.buttons["blockedTile-Locked In"].exists)
XCTAssertFalse(app.staticTexts["nothingBlockedLabel"].exists)
}
func testSoftRuleUnblockOfferedButHardRuleRefused() throws {
let app = XCUIApplication.launchSevered(seedScenario: "standard")
app.buttons["blockedTile-Work Time"].waitToAppear().tap()
// Soft rule: the confirmation dialog appears instead of the refusal alert.
app.sheets.buttons["Unblock"].waitToAppear()
XCTAssertFalse(app.alerts["Hard Mode is on"].exists)
}
}