Files
honeyDueKMP/iosApp/HoneyDueUITests/Tests/TaskIntegrationTests.swift
treyt 5c360a2796 Rearchitect UI test suite for complete, non-flaky coverage against live API
- Migrate Suite4-10, SmokeTests, NavigationCriticalPathTests to AuthenticatedTestCase
  with seeded admin account and real backend login
- Add 34 accessibility identifiers across 11 app views (task completion, profile,
  notifications, theme, join residence, manage users, forms)
- Create FeatureCoverageTests (14 tests) covering previously untested features:
  profile edit, theme selection, notification prefs, task completion, manage users,
  join residence, task templates
- Create MultiUserSharingTests (18 API tests) and MultiUserSharingUITests (8 XCUI
  tests) for full cross-user residence sharing lifecycle
- Add cleanup infrastructure: SuiteZZ_CleanupTests auto-wipes test data after runs,
  cleanup_test_data.sh script for manual reset via admin API
- Add share code API methods to TestAccountAPIClient (generateShareCode, joinWithCode,
  getShareCode, listResidenceUsers, removeUser)
- Fix app bugs found by tests:
  - ResidencesListView join callback now uses forceRefresh:true
  - APILayer invalidates task cache when residence count changes
  - AllTasksView auto-reloads tasks when residence list changes
- Fix test quality: keyboard focus waits, Save/Add button label matching,
  Documents tab label (Docs), remove API verification from UI tests
- DataLayerTests and PasswordResetTests now verify through UI, not API calls

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-15 17:32:13 -05:00

277 lines
12 KiB
Swift

import XCTest
/// Integration tests for task operations against the real local backend.
///
/// Test Plan IDs: TASK-010, TASK-012, plus create/edit flows.
/// Data is seeded via API and cleaned up in tearDown.
final class TaskIntegrationTests: AuthenticatedTestCase {
override var useSeededAccount: Bool { true }
// MARK: - Create Task
func testTASK_CreateTaskAppearsInList() {
// Seed a residence via API so task creation has a valid target
let residence = cleaner.seedResidence()
navigateToTasks()
let addButton = app.buttons[AccessibilityIdentifiers.Task.addButton].firstMatch
let emptyState = app.otherElements[AccessibilityIdentifiers.Task.emptyStateView]
let taskList = app.otherElements[AccessibilityIdentifiers.Task.tasksList]
let loaded = addButton.waitForExistence(timeout: defaultTimeout)
|| emptyState.waitForExistence(timeout: 3)
|| taskList.waitForExistence(timeout: 3)
XCTAssertTrue(loaded, "Tasks screen should load")
if addButton.exists && addButton.isHittable {
addButton.forceTap()
} else {
let emptyAddButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Add' OR label CONTAINS[c] 'Create'")
).firstMatch
emptyAddButton.waitForExistenceOrFail(timeout: defaultTimeout)
emptyAddButton.forceTap()
}
let titleField = app.textFields[AccessibilityIdentifiers.Task.titleField]
titleField.waitForExistenceOrFail(timeout: defaultTimeout)
let uniqueTitle = "IntTest Task \(Int(Date().timeIntervalSince1970))"
titleField.forceTap()
titleField.typeText(uniqueTitle)
let saveButton = app.buttons[AccessibilityIdentifiers.Task.saveButton]
let scrollContainer = app.scrollViews.firstMatch.exists ? app.scrollViews.firstMatch : app.collectionViews.firstMatch
saveButton.scrollIntoView(in: scrollContainer)
saveButton.forceTap()
let newTask = app.staticTexts[uniqueTitle]
XCTAssertTrue(
newTask.waitForExistence(timeout: longTimeout),
"Newly created task should appear"
)
}
// MARK: - TASK-010: Uncancel Task
func testTASK010_UncancelTaskFlow() throws {
// Seed a cancelled task via API
let residence = cleaner.seedResidence()
let cancelledTask = TestDataSeeder.createCancelledTask(token: session.token, residenceId: residence.id)
cleaner.trackTask(cancelledTask.id)
navigateToTasks()
// Pull to refresh until the cancelled task is visible
let taskText = app.staticTexts[cancelledTask.title]
pullToRefreshUntilVisible(taskText)
guard taskText.waitForExistence(timeout: defaultTimeout) else {
throw XCTSkip("Cancelled task not visible in current view")
}
taskText.forceTap()
// Look for an uncancel or reopen button
let uncancelButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Uncancel' OR label CONTAINS[c] 'Reopen' OR label CONTAINS[c] 'Restore'")
).firstMatch
if uncancelButton.waitForExistence(timeout: defaultTimeout) {
uncancelButton.forceTap()
let statusText = app.staticTexts.containing(
NSPredicate(format: "label CONTAINS[c] 'Cancelled'")
).firstMatch
XCTAssertFalse(statusText.exists, "Task should no longer show as cancelled after uncancel")
}
}
// MARK: - TASK-010 (v2): Uncancel Task Restores Cancelled Task to Active Lifecycle
func test15_uncancelRestorescancelledTask() throws {
// Seed a residence and a task, then cancel the task via API
let residence = cleaner.seedResidence(name: "Uncancel Test Residence \(Int(Date().timeIntervalSince1970))")
let task = cleaner.seedTask(residenceId: residence.id, title: "Uncancel Me \(Int(Date().timeIntervalSince1970))")
guard TestAccountAPIClient.cancelTask(token: session.token, id: task.id) != nil else {
throw XCTSkip("Could not cancel task via API — skipping uncancel test")
}
navigateToTasks()
// Pull to refresh until the cancelled task is visible
let taskText = app.staticTexts[task.title]
pullToRefreshUntilVisible(taskText)
guard taskText.waitForExistence(timeout: longTimeout) else {
throw XCTSkip("Cancelled task '\(task.title)' not visible — may require a Cancelled filter to be active")
}
taskText.forceTap()
// Look for an uncancel / reopen / restore action
let uncancelButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Uncancel' OR label CONTAINS[c] 'Reopen' OR label CONTAINS[c] 'Restore'")
).firstMatch
guard uncancelButton.waitForExistence(timeout: defaultTimeout) else {
throw XCTSkip("No uncancel button found — feature may not yet be implemented in UI")
}
uncancelButton.forceTap()
// After uncancelling, the task should no longer show a Cancelled status label
let cancelledLabel = app.staticTexts.containing(
NSPredicate(format: "label CONTAINS[c] 'Cancelled'")
).firstMatch
XCTAssertFalse(
cancelledLabel.waitForExistence(timeout: defaultTimeout),
"Task should no longer display 'Cancelled' status after being restored"
)
}
// MARK: - TASK-004: Create Task from Template
func test16_createTaskFromTemplate() throws {
// Seed a residence so template-created tasks have a valid target
cleaner.seedResidence(name: "Template Test Residence \(Int(Date().timeIntervalSince1970))")
navigateToTasks()
// Tap the add task button (or empty-state equivalent)
let addButton = app.buttons[AccessibilityIdentifiers.Task.addButton].firstMatch
let emptyAddButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Add' OR label CONTAINS[c] 'Create'")
).firstMatch
let addVisible = addButton.waitForExistence(timeout: defaultTimeout) || emptyAddButton.waitForExistence(timeout: 3)
XCTAssertTrue(addVisible, "An add/create task button should be visible on the tasks screen")
if addButton.exists && addButton.isHittable {
addButton.forceTap()
} else {
emptyAddButton.forceTap()
}
// Look for a Templates or Browse Templates option within the add-task flow.
// NOTE: The exact accessibility identifier for the template browser is not yet defined
// in AccessibilityIdentifiers.swift. The identifiers below use the pattern established
// in the codebase (e.g., "TaskForm.TemplatesButton") and will need to be wired up in
// the SwiftUI view when the template browser feature is implemented.
let templateButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Template' OR label CONTAINS[c] 'Browse'")
).firstMatch
guard templateButton.waitForExistence(timeout: defaultTimeout) else {
throw XCTSkip("Template browser not yet reachable from the add-task flow — skipping")
}
templateButton.forceTap()
// Select the first available template
let firstTemplate = app.cells.firstMatch
guard firstTemplate.waitForExistence(timeout: defaultTimeout) else {
throw XCTSkip("No templates available in template browser — skipping")
}
firstTemplate.forceTap()
// After selecting a template the form should be pre-filled the title field should
// contain something (i.e., not be empty)
let titleField = app.textFields[AccessibilityIdentifiers.Task.titleField]
titleField.waitForExistenceOrFail(timeout: defaultTimeout)
let preFilledTitle = titleField.value as? String ?? ""
XCTAssertFalse(
preFilledTitle.isEmpty,
"Title field should be pre-filled by the selected template"
)
// Save the templated task
let saveButton = app.buttons[AccessibilityIdentifiers.Task.saveButton]
let scrollContainer = app.scrollViews.firstMatch.exists ? app.scrollViews.firstMatch : app.collectionViews.firstMatch
saveButton.scrollIntoView(in: scrollContainer)
saveButton.forceTap()
// The task should now appear in the list
let savedTask = app.staticTexts[preFilledTitle]
XCTAssertTrue(
savedTask.waitForExistence(timeout: longTimeout),
"Task created from template ('\(preFilledTitle)') should appear in the task list"
)
}
// MARK: - TASK-012: Delete Task
func testTASK012_DeleteTaskUpdatesViews() {
// Create a task via UI first (since Kanban board uses cached data)
let residence = cleaner.seedResidence()
navigateToTasks()
let addButton = app.buttons[AccessibilityIdentifiers.Task.addButton].firstMatch
let emptyAddButton = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Add' OR label CONTAINS[c] 'Create'")
).firstMatch
let addVisible = addButton.waitForExistence(timeout: defaultTimeout) || emptyAddButton.waitForExistence(timeout: 3)
XCTAssertTrue(addVisible, "Add task button should be visible")
if addButton.exists && addButton.isHittable {
addButton.forceTap()
} else {
emptyAddButton.forceTap()
}
let titleField = app.textFields[AccessibilityIdentifiers.Task.titleField]
titleField.waitForExistenceOrFail(timeout: defaultTimeout)
let uniqueTitle = "Delete Task \(Int(Date().timeIntervalSince1970))"
titleField.forceTap()
titleField.typeText(uniqueTitle)
let saveButton = app.buttons[AccessibilityIdentifiers.Task.saveButton]
let scrollContainer = app.scrollViews.firstMatch.exists ? app.scrollViews.firstMatch : app.collectionViews.firstMatch
if scrollContainer.exists {
saveButton.scrollIntoView(in: scrollContainer)
}
saveButton.forceTap()
// Wait for the task to appear in the Kanban board
let taskText = app.staticTexts[uniqueTitle]
taskText.waitForExistenceOrFail(timeout: longTimeout)
// Tap the "Actions" menu on the task card to reveal cancel option
let actionsMenu = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Actions'")
).firstMatch
if actionsMenu.waitForExistence(timeout: defaultTimeout) {
actionsMenu.forceTap()
} else {
taskText.forceTap()
}
// Tap cancel (tasks use "Cancel Task" semantics)
let deleteButton = app.buttons[AccessibilityIdentifiers.Task.deleteButton]
if !deleteButton.waitForExistence(timeout: defaultTimeout) {
let cancelTask = app.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Cancel Task'")
).firstMatch
cancelTask.waitForExistenceOrFail(timeout: 5)
cancelTask.forceTap()
} else {
deleteButton.forceTap()
}
// Confirm cancellation
let confirmDelete = app.alerts.buttons.containing(
NSPredicate(format: "label CONTAINS[c] 'Confirm' OR label CONTAINS[c] 'Yes' OR label CONTAINS[c] 'Cancel Task'")
).firstMatch
let alertConfirmButton = app.buttons[AccessibilityIdentifiers.Alert.confirmButton]
if alertConfirmButton.waitForExistence(timeout: shortTimeout) {
alertConfirmButton.tap()
} else if confirmDelete.waitForExistence(timeout: shortTimeout) {
confirmDelete.tap()
}
// Verify the task is removed or moved to a different column
let deletedTask = app.staticTexts[uniqueTitle]
XCTAssertTrue(
deletedTask.waitForNonExistence(timeout: longTimeout),
"Cancelled task should no longer appear in active views"
)
}
}