library(testthat) test_that("new_graph creates valid graph objects", { # Test with default empty lists g <- new_graph() expect_s3_class(g, "graph") expect_type(g, "list") expect_named(g, c("nodes", "edges", "combos")) expect_equal(length(g$nodes), 0) expect_equal(length(g$edges), 0) expect_equal(length(g$combos), 0) # Test with provided data nodes <- list(list(id = "1"), list(id = "2")) edges <- list(list(source = "1", target = "2")) combos <- list(list(id = "combo1")) g <- new_graph(nodes, edges, combos) expect_s3_class(g, "graph") expect_identical(g$nodes, nodes) expect_identical(g$edges, edges) expect_identical(g$combos, combos) }) test_that("is_graph correctly identifies graph objects", { g <- new_graph() expect_true(is_graph(g)) expect_false(is_graph(list())) expect_false(is_graph("not a graph")) expect_false(is_graph(NULL)) expect_false(is_graph(123)) expect_false(is_graph(data.frame())) }) test_that("graph_nodes extracts nodes correctly", { nodes <- list(list(id = "1"), list(id = "2")) g <- new_graph(nodes = nodes) expect_identical(graph_nodes(g), nodes) # Test with empty graph empty_g <- new_graph() expect_equal(length(graph_nodes(empty_g)), 0) expect_type(graph_nodes(empty_g), "list") }) test_that("graph_edges extracts edges correctly", { edges <- list( list(source = "1", target = "2"), list(source = "2", target = "3") ) g <- new_graph(edges = edges) expect_identical(graph_edges(g), edges) # Test with empty graph empty_g <- new_graph() expect_equal(length(graph_edges(empty_g)), 0) expect_type(graph_edges(empty_g), "list") }) test_that("graph_combos extracts combos correctly", { combos <- list(list(id = "combo1"), list(id = "combo2")) g <- new_graph(combos = combos) expect_identical(graph_combos(g), combos) # Test with empty graph empty_g <- new_graph() expect_equal(length(graph_combos(empty_g)), 0) expect_type(graph_combos(empty_g), "list") }) test_that("accessor functions handle missing components gracefully", { # Create graph-like object missing some components incomplete <- structure(list(nodes = list()), class = "graph") expect_equal(graph_nodes(incomplete), list()) expect_null(graph_edges(incomplete)) expect_null(graph_combos(incomplete)) })