import "../../runtime/eltest.el"
// test_env.el - native test suite for runtime/env.el
//
// Covers: env() for reading environment variables, args() returning a list,
// state_set/get/del/keys via the env module re-exports, uuid_new/uuid_v4
// format validation.

test "env-missing-returns-empty" {
    let v: String = env("__EL_NO_SUCH_VAR_XYZ__")
    assert v == "", "missing env var returns empty string"
}

test "env-path-is-set" {
    // PATH is expected to be set in virtually any UNIX environment.
    let v: String = env("PATH")
    assert str_len(v) > 0, "PATH env var is non-empty"
}

test "env-home-is-set" {
    // HOME is present on macOS/Linux test environments.
    let v: String = env("HOME")
    assert str_len(v) > 0, "HOME env var is non-empty"
}

test "args-returns-list" {
    let a: [String] = args()
    // Even with no arguments, the list should be non-null (at minimum the
    // program name is argv[0]).
    let n: Int = native_list_len(a)
    assert n >= 0, "args returns a list (may be empty if runtime strips argv)"
}

test "uuid-new-format" {
    let id: String = uuid_new()
    // UUID v4: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx  (36 chars)
    let n: Int = str_len(id)
    assert n == 36, "uuid_new returns 36-character string"
    // Check the dashes at correct positions
    let d1: String = str_char_at(id, 8)
    let d2: String = str_char_at(id, 13)
    let d3: String = str_char_at(id, 18)
    let d4: String = str_char_at(id, 23)
    assert d1 == "-", "dash at position 8"
    assert d2 == "-", "dash at position 13"
    assert d3 == "-", "dash at position 18"
    assert d4 == "-", "dash at position 23"
}

test "uuid-v4-format" {
    let id: String = uuid_v4()
    let n: Int = str_len(id)
    assert n == 36, "uuid_v4 returns 36-character string"
    // The version nibble must be '4'
    let version_char: String = str_char_at(id, 14)
    assert version_char == "4", "uuid_v4 version nibble is '4'"
}

test "uuid-uniqueness" {
    let id1: String = uuid_new()
    let id2: String = uuid_new()
    assert !str_eq(id1, id2), "two uuid_new calls produce different UUIDs"
}

test "env-state-set-get-via-env-module" {
    // runtime/env.el re-exports state_set / state_get.
    state_set("env_test_key", "env_test_val")
    let v: String = state_get("env_test_key")
    assert v == "env_test_val", "state_set/get work via env module"
    state_del("env_test_key")
    let after: String = state_get("env_test_key")
    assert after == "", "state_del removes key"
}

test "env-state-keys-json-via-env-module" {
    state_set("esk_a", "1")
    state_set("esk_b", "2")
    let ks: String = state_keys()
    assert str_starts_with(ks, "["), "state_keys returns JSON array"
    assert str_contains(ks, "esk_a"), "esk_a in keys"
    assert str_contains(ks, "esk_b"), "esk_b in keys"
    state_del("esk_a")
    state_del("esk_b")
}
