`edge_load_model("does_not_exist.gguf")` threw an error with unexpected message.
Expected match: "Failed to load GGUF model"
Actual message: "Model file does not exist: does_not_exist.gguf"

test_that("Error handling and edge cases work correctly", {
test_that("Invalid file paths are handled properly", {
"model_path cannot be empty|Failed to load GGUF model"
"model_path must be a string|Failed to load GGUF model"
test_that("Invalid parameters are rejected", {
# Test edge_load_model with invalid parameters
dummy_path <- "dummy.gguf"  # Will fail anyway, but testing parameter validation
"n_ctx must be positive|Failed to load GGUF model"
# Extremely large context size (should be handled gracefully)
"n_gpu_layers cannot be negative|Failed to load GGUF model"
test_that("edge_completion handles errors gracefully", {
edge_completion(NULL, "Hello", n_predict = 5),
invalid_contexts <- list("string", 123, list(), data.frame())
edge_completion(ctx, "Hello", n_predict = 5),
ctx <- edge_load_model(model_path, n_ctx = 256)
edge_completion(ctx, "Hello", n_predict = -1),
edge_completion(ctx, "Hello", n_predict = 0),
test_that("Memory constraints are handled properly", {
# Test very small context size (should work)
ctx_small <- edge_load_model(model_path, n_ctx = 16)
ctx_normal <- edge_load_model(model_path, n_ctx = 512)
skip("No test model available for memory constraint tests")
test_that("Corrupted or invalid model files are handled", {
# Create a fake GGUF file with wrong content
writeLines("This is not a real GGUF file", fake_gguf)
test_that("Edge cases in text completion", {
# Test with very long prompt (might exceed context)
long_prompt <- paste(rep("This is a very long sentence that goes on and on.", 20), collapse = " ")
# Should either work or give a reasonable error
result <- edge_completion(ctx, long_prompt, n_predict = 5)
# If it errors, the error should be informative
expect_true(grepl("context|length|size", e$message, ignore.case = TRUE))
result <- edge_completion(ctx, prompt, n_predict = 3)
# If there's an error, it should be a reasonable one
skip("No test model available for edge case tests")
test_that("Concurrent access and thread safety", {
# Test loading the same model multiple times
contexts[[i]] <- edge_load_model(model_path, n_ctx = 128)
result <- edge_completion(contexts[[i]], paste("Test", i), n_predict = 3)
skip("No test model available for concurrency tests")
test_that("Resource cleanup after errors", {
# Test that resources are properly cleaned up even when errors occur
# Multiple failed attempts should not accumulate resources