# CRUD Cycle
# ---
# Pattern: Create, Read, Update, Delete — full lifecycle test
# What it tests: Creates an entity, verifies it appears in a list,
#                edits it, verifies the change, then deletes it.
# Customize:
#   - Update selectors, field labels, and entity names for your app
#
# This pattern is idempotent — it cleans up after itself by deleting the
# created entity, so it can run repeatedly without side effects.
#
# Auth: This hunt assumes you're already authenticated. Use `prowl login`
#       to capture auth state, or add your own login steps before this hunt.
#
# Tip: Uses `onDialog` to handle the browser confirmation dialog on delete.

name: crud-cycle
description: Full create-read-update-delete lifecycle

tags:
  - admin
  - crud
  - lifecycle
  - data-management

vars:
  ITEM_NAME: "Test Item"
  UPDATED_NAME: "Updated Item"

steps:
  # CREATE — navigate to the creation page and fill the form
  - navigate: "/items/new"

  - fill:
      "Item Name": "{{ITEM_NAME}}"

  - click: "Create"

  - waitForNetworkIdle:
      timeout: 5000

  # READ — verify the new item appears in the list
  - navigate: "/items"
  - assert:
      visible: "{{ITEM_NAME}}"

  # UPDATE — click the item, edit it, save
  - click: "{{ITEM_NAME}}"

  - fill:
      "Item Name": "{{UPDATED_NAME}}"

  - click: "Save"

  - waitForNetworkIdle:
      timeout: 5000

  # Verify the update took effect
  - navigate: "/items"
  - assert:
      visible: "{{UPDATED_NAME}}"

  # DELETE — register a dialog handler, then click delete
  # The onDialog step must come BEFORE the action that triggers the dialog.
  - click: "{{UPDATED_NAME}}"
  - onDialog:
      action: accept
  - click: "Delete"

  - waitForNetworkIdle:
      timeout: 5000

  # Verify the item is gone
  - navigate: "/items"
  - assert:
      notVisible: "{{UPDATED_NAME}}"

assertions:
  - noConsoleErrors: true
