test_that("sync_server creates valid server object", {
  server <- sync_server(data_dir = tempdir())
  on.exit(server$close())

  state <- attr(server, "sync")
  expect_s3_class(server, "autosync_server")
  expect_type(state$peer_id, "character")
  expect_type(state$storage_id, "character")
  expect_true(is.environment(state$documents))
  expect_true(is.environment(state$sync_states))
  expect_true(is.environment(state$connections))
})

test_that("sync_server with TLS creates wss URL", {
  server <- sync_server(
    port = 3030L,
    tls = nanonext::tls_config(server = nanonext::write_cert()$server),
    data_dir = tempdir()
  )
  on.exit(server$close())

  expect_true(grepl("^wss://", server$url))
})

test_that("sync_server without TLS creates ws URL", {
  server <- sync_server(data_dir = tempdir())
  on.exit(server$close())

  expect_true(grepl("^ws://", server$url))
})

test_that("sync_server with ephemeral storage_id", {
  server <- sync_server(storage_id = NA, data_dir = tempdir())
  on.exit(server$close())

  state <- attr(server, "sync")
  expect_null(state$storage_id)
})

test_that("create_document generates valid ID", {
  server <- sync_server(data_dir = tempdir())
  on.exit({
    server$close()
    unlink(file.path(tempdir(), "*.automerge"))
  })

  doc_id <- create_document(server)
  expect_type(doc_id, "character")
  expect_true(nchar(doc_id) > 20) # Base58Check encoded 16 bytes + version + checksum
  expect_true(doc_id %in% list_documents(server))
})

test_that("get_document retrieves created document", {
  server <- sync_server(data_dir = tempdir())
  on.exit({
    server$close()
    unlink(file.path(tempdir(), "*.automerge"))
  })

  doc_id <- create_document(server)
  doc <- get_document(server, doc_id)
  expect_true(inherits(doc, "am_doc"))
})

test_that("list_documents returns all documents", {
  server <- sync_server(data_dir = tempdir())
  on.exit({
    server$close()
    unlink(file.path(tempdir(), "*.automerge"))
  })

  expect_length(list_documents(server), 0)

  id1 <- create_document(server)
  id2 <- create_document(server)

  docs <- list_documents(server)
  expect_length(docs, 2)
  expect_true(id1 %in% docs)
  expect_true(id2 %in% docs)
})

test_that("print.autosync_server works", {
  server <- sync_server(data_dir = tempdir())
  on.exit(server$close())

  output <- capture.output(print(server))
  expect_true(any(grepl("Automerge Sync Server", output)))
  expect_true(any(grepl(paste0("URL: ", server$url), output, fixed = TRUE)))
})

test_that("generate_document_id creates valid IDs", {
  id <- generate_document_id()
  expect_type(id, "character")
  expect_true(nchar(id) > 20)
  # Should be decodable to 16 bytes
  bytes <- secretbase::base58dec(id, convert = FALSE)
  expect_length(bytes, 16L)
})

test_that("sync_server with custom storage_id", {
  server <- sync_server(
    storage_id = "customStorageId",
    data_dir = tempdir()
  )
  on.exit(server$close())

  state <- attr(server, "sync")
  expect_equal(state$storage_id, "customStorageId")
})
