import "../../runtime/eltest.el"
// test_state.el - native test suite for runtime/state.el
//
// Covers: state_set/get/del, state_has, state_get_or, state_keys,
// and edge cases such as empty values, overwrite, and multiple keys.

test "state-set-get-basic" {
    state_set("test_key", "hello")
    let v: String = state_get("test_key")
    assert v == "hello", "get returns set value"
}

test "state-get-missing" {
    let v: String = state_get("__nonexistent_key_xyz__")
    assert v == "", "missing key returns empty string"
}

test "state-overwrite" {
    state_set("ow_key", "first")
    state_set("ow_key", "second")
    let v: String = state_get("ow_key")
    assert v == "second", "second write overwrites first"
}

test "state-del" {
    state_set("del_key", "to be deleted")
    state_del("del_key")
    let v: String = state_get("del_key")
    assert v == "", "deleted key returns empty string"
}

test "state-del-nonexistent" {
    // Should not panic or error on deleting a non-existent key.
    state_del("__never_set_key__")
    let v: String = state_get("__never_set_key__")
    assert v == "", "del of nonexistent key is a no-op"
}

test "state-has" {
    state_set("has_key", "value")
    assert state_has("has_key"), "has returns true for set key"
    assert !state_has("__no_has_key__"), "has returns false for absent key"
    state_del("has_key")
    assert !state_has("has_key"), "has returns false after del"
}

test "state-get-or" {
    state_set("gor_key", "actual")
    let v1: String = state_get_or("gor_key", "default")
    assert v1 == "actual", "get_or returns value when key set"
    let v2: String = state_get_or("__absent_gor_key__", "fallback")
    assert v2 == "fallback", "get_or returns default when key absent"
}

test "state-multiple-keys" {
    state_set("mk_a", "alpha")
    state_set("mk_b", "beta")
    state_set("mk_c", "gamma")
    let a: String = state_get("mk_a")
    let b: String = state_get("mk_b")
    let c: String = state_get("mk_c")
    assert a == "alpha", "key a correct"
    assert b == "beta", "key b correct"
    assert c == "gamma", "key c correct"
    state_del("mk_a")
    state_del("mk_b")
    state_del("mk_c")
}

test "state-keys-returns-json-array" {
    state_set("keys_test_1", "v1")
    state_set("keys_test_2", "v2")
    let ks: String = state_keys()
    // The result is a JSON array string like ["keys_test_1","keys_test_2",...]
    assert str_starts_with(ks, "["), "state_keys returns JSON array"
    assert str_ends_with(ks, "]"), "state_keys JSON array is closed"
    assert str_contains(ks, "keys_test_1"), "keys array contains keys_test_1"
    assert str_contains(ks, "keys_test_2"), "keys array contains keys_test_2"
    state_del("keys_test_1")
    state_del("keys_test_2")
}

test "state-numeric-value-as-string" {
    state_set("num_key", "42")
    let v: String = state_get("num_key")
    let n: Int = str_to_int(v)
    assert n == 42, "stored numeric string round-trips to int"
    state_del("num_key")
}

test "state-long-value" {
    let long_val: String = str_repeat("abcdefghij", 100)
    state_set("long_val_key", long_val)
    let got: String = state_get("long_val_key")
    assert got == long_val, "long value round-trips correctly"
    let got_len: Int = str_len(got)
    assert got_len == 1000, "long value is 1000 bytes"
    state_del("long_val_key")
}
