// test_fs.el - native test suite for runtime/fs.el
//
// Covers: fs_write/read round-trip, fs_exists, fs_mkdir, fs_list,
// fs_list_json, and edge cases (empty file, overwrite, non-existent path).

test "fs-write-and-read" {
    let path: String = "/tmp/el_test_fs_basic.txt"
    let content: String = "hello from El"
    let ok: Bool = fs_write(path, content)
    assert ok, "fs_write returns true on success"
    let got: String = fs_read(path)
    assert got == content, "fs_read returns what was written"
}

test "fs-exists" {
    let path: String = "/tmp/el_test_fs_exists.txt"
    fs_write(path, "exists check")
    let exists: Bool = fs_exists(path)
    assert exists, "fs_exists returns true for created file"
    let missing: Bool = fs_exists("/tmp/__el_no_such_file_xyz__.txt")
    assert !missing, "fs_exists returns false for non-existent file"
}

test "fs-read-nonexistent" {
    let got: String = fs_read("/tmp/__el_no_such_file_abc__.txt")
    assert got == "", "reading nonexistent file returns empty string"
}

test "fs-write-overwrite" {
    let path: String = "/tmp/el_test_fs_overwrite.txt"
    fs_write(path, "original content")
    fs_write(path, "new content")
    let got: String = fs_read(path)
    assert got == "new content", "second write overwrites first"
}

test "fs-write-empty-file" {
    let path: String = "/tmp/el_test_fs_empty.txt"
    let ok: Bool = fs_write(path, "")
    assert ok, "writing empty file succeeds"
    let got: String = fs_read(path)
    assert got == "", "reading empty file returns empty string"
    let exists: Bool = fs_exists(path)
    assert exists, "empty file still exists"
}

test "fs-write-multiline" {
    let path: String = "/tmp/el_test_fs_multiline.txt"
    let content: String = "line one\nline two\nline three"
    fs_write(path, content)
    let got: String = fs_read(path)
    assert got == content, "multiline content round-trips"
    let lines: [String] = str_split_lines(got)
    let n: Int = native_list_len(lines)
    assert n == 3, "three lines after read"
}

test "fs-mkdir" {
    let dir: String = "/tmp/el_test_fs_mkdir_dir"
    let ok: Bool = fs_mkdir(dir)
    assert ok, "fs_mkdir returns true"
    let exists: Bool = fs_exists(dir)
    assert exists, "created directory exists"
}

test "fs-list" {
    let dir: String = "/tmp/el_test_fs_list_dir"
    fs_mkdir(dir)
    fs_write(dir + "/a.txt", "a")
    fs_write(dir + "/b.txt", "b")
    let files: [String] = fs_list(dir)
    // Filter out empty strings from trailing newline
    let n: Int = native_list_len(files)
    // We expect at least 2 entries (a.txt and b.txt)
    assert n >= 2, "fs_list returns at least 2 entries"
}

test "fs-list-json" {
    let dir: String = "/tmp/el_test_fs_listjson_dir"
    fs_mkdir(dir)
    fs_write(dir + "/x.txt", "x")
    fs_write(dir + "/y.txt", "y")
    let result: String = fs_list_json(dir)
    assert str_starts_with(result, "["), "fs_list_json returns JSON array"
    assert str_ends_with(result, "]"), "fs_list_json JSON array is closed"
    // Should contain at least one filename
    assert str_len(result) > 2, "fs_list_json result is non-empty array"
}

test "fs-write-and-read-special-chars" {
    let path: String = "/tmp/el_test_fs_special.txt"
    let content: String = "tabs:\there\nnewlines: ok\n\"quoted\""
    fs_write(path, content)
    let got: String = fs_read(path)
    assert got == content, "special chars in content round-trip"
}
