Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

3.7 KB
Raw Blame
#!/usr/bin/env bats
# ============================================================================
# BATS tests for installers/lib/constants.sh
# ============================================================================
# Tests: VERSION, color codes, _sed_i()

load '../bats/bats-support/load'
load '../bats/bats-assert/load'

setup() {
    # constants.sh sources path-utils.sh via SCRIPT_DIR.
    # Point SCRIPT_DIR to the real ods directory so path-utils.sh
    # can be found at $SCRIPT_DIR/installers/lib/path-utils.sh.
    export SCRIPT_DIR="$BATS_TEST_DIRNAME/../.."

    # Prevent constants.sh from overwriting SCRIPT_DIR via its own detection
    # by pre-setting LOG_FILE so we can verify it's used
    export LOG_FILE="$BATS_TEST_TMPDIR/constants-test.log"

    # Source the library under test
    source "$BATS_TEST_DIRNAME/../../installers/lib/constants.sh"
}

# ── VERSION ─────────────────────────────────────────────────────────────────

@test "VERSION: is set and non-empty" {
    [[ -n "$VERSION" ]]
}

@test "VERSION: matches semver pattern" {
    [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
}

# ── Color codes ─────────────────────────────────────────────────────────────

@test "color codes: presentation variables are declared" {
    declare -p RED GRN BGRN DGRN MAG BMAG AMB WHT DIM NC >/dev/null
}

@test "color codes: a capable terminal initializes presentation colors" {
    run env -u NO_COLOR TERM=xterm bash -c '
        source "$1"
        [[ -n "$RED$GRN$BGRN$DGRN$MAG$BMAG$AMB$WHT$DIM$NC" ]]
    ' _ "$BATS_TEST_DIRNAME/../../installers/lib/constants.sh"
    assert_success
}

@test "color codes: NO_COLOR strips ANSI values at source" {
    run bash -c '
        export NO_COLOR=1 TERM=xterm
        source "$1"
        [[ -z "$RED$GRN$BGRN$DGRN$MAG$BMAG$AMB$WHT$DIM$NC" ]]
    ' _ "$BATS_TEST_DIRNAME/../../installers/lib/constants.sh"
    assert_success
}

@test "color codes: CURSOR is defined" {
    [[ -n "$CURSOR" ]]
}

# ── INSTALL_START_EPOCH ─────────────────────────────────────────────────────

@test "INSTALL_START_EPOCH: is a positive integer" {
    [[ "$INSTALL_START_EPOCH" =~ ^[0-9]+$ ]]
    [[ "$INSTALL_START_EPOCH" -gt 0 ]]
}

# ── SYSTEM_TZ ───────────────────────────────────────────────────────────────

@test "SYSTEM_TZ: is set and non-empty" {
    [[ -n "$SYSTEM_TZ" ]]
}

# ── _sed_i ──────────────────────────────────────────────────────────────────

@test "_sed_i: performs in-place substitution" {
    local test_file="$BATS_TEST_TMPDIR/sed-test.txt"
    echo "hello world" > "$test_file"
    _sed_i "s/hello/goodbye/g" "$test_file"
    run cat "$test_file"
    assert_output "goodbye world"
}

@test "_sed_i: handles multiple substitutions in a file" {
    local test_file="$BATS_TEST_TMPDIR/sed-multi.txt"
    printf "foo bar\nfoo baz\n" > "$test_file"
    _sed_i "s/foo/qux/g" "$test_file"
    run cat "$test_file"
    assert_line --index 0 "qux bar"
    assert_line --index 1 "qux baz"
}

@test "_sed_i: no-op when pattern does not match" {
    local test_file="$BATS_TEST_TMPDIR/sed-noop.txt"
    echo "unchanged" > "$test_file"
    _sed_i "s/missing/replaced/g" "$test_file"
    run cat "$test_file"
    assert_output "unchanged"
}