# ═══════════════════════════════════════════════════════════════════════════════
# tbdflow — Commit Types (data-driven)
# ═══════════════════════════════════════════════════════════════════════════════
# User Story: As a developer, I want tbdflow to support all conventional
# commit types so that my team's workflow is not limited by tooling.
# ═══════════════════════════════════════════════════════════════════════════════

feature "tbdflow Commit Type Coverage"

actors: Terminal, System

settings {
    timeout_seconds = 60
    stop_on_failure = false
    shell_path = "/bin/bash"
}

var REPO_DIR = "/tmp/tbdflow_types_test"
var BARE_REPO = "/tmp/tbdflow_types_bare.git"
var COMMIT_TYPES = ["feat", "fix", "chore", "docs", "refactor", "test", "build", "ci", "perf", "revert", "style"]

# ═══════════════════════════════════════════════════════════════════════════════
# IMPLEMENTATION LAYER — Tasks hide the "how"
# ═══════════════════════════════════════════════════════════════════════════════

task init_repo(repo, bare) {
    Terminal run "rm -rf ${repo} ${bare} && mkdir -p ${repo}"
    Terminal run "git init --bare ${bare}"
    Terminal run "git -C ${repo} init && git -C ${repo} commit --allow-empty -m 'init'"
    Terminal run "git -C ${repo} remote add origin ${bare}"
    Terminal run "git -C ${repo} push -u origin main"
    Terminal set_cwd "${repo}"
}

task cleanup_repo(repo, bare) {
    Terminal run "rm -rf ${repo} ${bare}"
}

task make_change_and_commit(type) {
    Terminal run "echo ${type} > ${type}.txt"
    Terminal run "tbdflow commit -t ${type} -m 'validate ${type} type'"
}

task verify_commit_succeeded() {
    Terminal last_command succeeded
    Terminal output_not_contains "Error"
}

# ═══════════════════════════════════════════════════════════════════════════════
# BUSINESS SPECIFICATION LAYER — Scenarios describe the "what"
# ═══════════════════════════════════════════════════════════════════════════════

# AC1: Every standard conventional commit type must be accepted.

scenario "Every valid commit type is accepted" {

    test Setup "Initialise test repository" {
        given:
            Test can_start
        when:
            init_repo("${REPO_DIR}", "${BARE_REPO}")
        then:
            Terminal last_command succeeded
    }

    foreach TYPE in ${COMMIT_TYPES} {
        test "Commit_${TYPE}" "tbdflow accepts type '${TYPE}'" {
            given:
                Test has_succeeded Setup
                System log "Testing commit type: ${TYPE}"
            when:
                make_change_and_commit("${TYPE}")
            then:
                verify_commit_succeeded()
        }
    }

    after {
        cleanup_repo("${REPO_DIR}", "${BARE_REPO}")
    }
}
