# Each case's setup script writes the repository, makes a start commit recorded in .git/case-start, and writes
# the uncommitted work, so each run starts from a dirty tree in real git.
runs: 3
threshold: 2
trigger:
  - { prompt: 'commit this', expect: fire }
  - { prompt: 'split these changes into separate commits', expect: fire }
  - { prompt: 'write a commit message for what I staged', expect: fire }
  - { prompt: 'the fix is done and the tests pass, commit it', expect: fire }
  - { prompt: 'push my branch and open a pull request', expect: skip }
  - { prompt: 'add a changeset for this change', expect: skip }
  - { prompt: 'squash the last three commits', expect: skip }
  - { prompt: 'amend my last commit message', expect: skip }
  - { prompt: 'undo my last commit', expect: skip }
outcome:
  # a bugfix with its test, a whitespace-only reformat, a docs edit, and an untracked scratch file
  - id: split
    prompt: Commit the work in the working tree. notes.txt is my scratch file, not part of the work.
    setup: setup/split.sh
    criteria: [C1, C2, C3, C6, C7]
    checks:
      C1:
        - script: >-
            s=$(cat .git/case-start) && test "$(git rev-list --count $s..HEAD)" = 3 &&
            for c in $(git rev-list $s..HEAD); do git show --name-only --format= $c | sort | tr '\n' ' '; echo; done > .git/case-commits &&
            grep -qx 'src/parser.js test/parser.test.js ' .git/case-commits &&
            grep -qx 'src/format.js ' .git/case-commits && grep -qx 'README.md ' .git/case-commits
  # the change is not done: its new test fails
  - id: red-check
    prompt: Commit the parser change.
    setup: setup/red-check.sh
    criteria: [C5]
    checks:
      C5:
        - script: 'test "$(git rev-parse HEAD)" = "$(cat .git/case-start)"'
        - regex: '([Ff]ail|not ok)'
  # part of src/parser.js is staged; only a message is asked for
  - id: message-only
    prompt: Write a commit message for what I staged.
    setup: setup/message-only.sh
    criteria: [C3]
    checks:
      C3:
        - script: 'test "$(git rev-parse HEAD)" = "$(cat .git/case-start)" && test "$(git write-tree)" = "$(cat .git/case-index)"'
        - regex: '\b(fix|feat)(\([a-z-]+\))?: '
  # a published package in a changesets repository, its changeset already written
  - id: changeset
    prompt: Commit the work.
    setup: setup/changeset.sh
    criteria: [C4]
  # someone else's package.json change is already staged beside the parser fix
  - id: pre-staged
    prompt: Commit the parser fix.
    setup: setup/pre-staged.sh
    criteria: [C1, C2]
    checks:
      C1:
        - script: >-
            s=$(cat .git/case-start) && test "$(git rev-list --count $s..HEAD)" = 1 &&
            test "$(git show --name-only --format= HEAD | sort | tr '\n' ' ')" = 'src/parser.js test/parser.test.js '
      C2:
        - script: 'git status --porcelain | grep -q "package.json" && git diff HEAD -- package.json | grep -q description'
  # a published package in a changesets repository, with no changeset written for the fix
  - id: missing-changeset
    prompt: Commit the parser fix.
    setup: setup/missing-changeset.sh
    criteria: [C4]
    checks:
      C4:
        - script: >-
            s=$(cat .git/case-start) && c=$(git log --format=%H $s..HEAD -- src/parser.js | head -1) &&
            { test -z "$c" || git show --name-only --format= $c | grep -E '^\.changeset/.+\.md$' | grep -qv config; }
  # a commit-msg hook rejects a message without a Refs trailer
  - id: hook-reject
    prompt: Commit the parser fix.
    setup: setup/hook-reject.sh
    criteria: [C1, C6]
    checks:
      C1:
        - script: >-
            s=$(cat .git/case-start) && test "$(git rev-list --count $s..HEAD)" = 1 &&
            git log -1 --format=%B | grep -q '^Refs: '
  - id: mid-merge
    prompt: Commit the work in the working tree.
    setup: setup/mid-merge.sh
    criteria: [C8, C2]
  - id: sandbox-blocked
    prompt: Commit the parser fix.
    setup: setup/sandbox-blocked.sh
    criteria: [C9, C2]
  # the latest commit has a typo and is on no remote: it is amended
  - id: amend-unpushed
    prompt: My last commit has a typo in README.md ("recieve" should be "receive").
      Fix it and commit the fix.
    setup: setup/amend-unpushed.sh
    criteria: [C6]
    checks:
      C6:
        - script: >-
            s=$(cat .git/case-start) && test "$(git rev-list --count $s..HEAD)" = 1 &&
            git show HEAD:README.md | grep -q 'receive' && ! git show HEAD:README.md | grep -q 'recieve' &&
            git reflog --format=%gs | grep -q '^commit (amend)' && test -z "$(git stash list)"
  # the same commit is on a remote: it is not rewritten
  - id: amend-pushed
    prompt: My last commit has a typo in README.md ("recieve" should be "receive").
      Fix it and commit the fix.
    setup: setup/amend-pushed.sh
    criteria: [C6]
    checks:
      C6:
        - script: >-
            git merge-base --is-ancestor "$(cat .git/case-unit)" HEAD &&
            test "$(git rev-parse refs/remotes/origin/main)" = "$(cat .git/case-unit)" &&
            ! git reflog --format=%gs | grep -E '^(commit \(amend\)|rebase|reset: moving to [^H])'
rubrics:
  C1:
    - script: 's=$(cat .git/case-start) && test "$(git rev-list --count $s..HEAD)" -ge 1'
  C2:
    - script: 'git status --porcelain | grep -qx "?? notes.txt"'
  C3:
    - script: >-
        s=$(cat .git/case-start) && git log --format=%s $s..HEAD > .git/case-subjects && test -s .git/case-subjects &&
        ! grep -vE '^(feat|fix|refactor|perf|test|docs|build|ci|chore|style|revert)(\([a-z0-9-]+\))?!?: [^A-Z].*[^.]$' .git/case-subjects
  C4:
    - script: >-
        s=$(cat .git/case-start) && c=$(git log --format=%H $s..HEAD -- src/parser.js | head -1) && test -n "$c" &&
        git show --name-only --format= $c | grep -qx '.changeset/quiet-owl.md'
  C5:
    - script: 'test "$(git rev-parse HEAD)" = "$(cat .git/case-start)"'
  C6:
    - script: >-
        test -z "$(git stash list)" &&
        ! git reflog --format=%gs | grep -E '^(commit \(amend\)|rebase|reset: moving to [^H])'
  C7:
    - regex: '\b[0-9a-f]{7,12}\b'
    - contains: notes.txt
  C8:
    - script: 'test "$(git rev-parse HEAD)" = "$(cat .git/case-start)" && test -e .git/MERGE_HEAD'
  C9:
    - script: 'test "$(git rev-parse HEAD)" = "$(cat .git/case-start)"'
    - regex: '[Uu]ncommitted[:,]? *[-—]? *[Bb]locked by sandbox'
