var toml = use('toml')

// Test basic types
var basic_obj = {
  string_value: "hello world",
  number_value: 42,
  float_value: 3.14,
  bool_true: true,
  bool_false: false,
  array_mixed: ["string", 123, true, false]
}

log.console("Testing basic types...")
var encoded = toml.encode(basic_obj)
log.console("Encoded:")
log.console(encoded)
log.console("")

var decoded = toml.decode(encoded)
log.console("Decoded:")
log.console(JSON.stringify(decoded, null, 2))
log.console("")

// Test nested objects
var nested_obj = {
  name: "Test Config",
  version: 1.0,
  database: {
    host: "localhost",
    port: 5432,
    credentials: {
      username: "admin",
      password: "secret123"
    }
  },
  servers: {
    alpha: {
      ip: "192.168.1.1",
      port: 8080
    },
    beta: {
      ip: "192.168.1.2", 
      port: 8081
    }
  },
  features: ["auth", "api", "websocket"]
}

log.console("Testing nested objects...")
encoded = toml.encode(nested_obj)
log.console("Encoded:")
log.console(encoded)
log.console("")

decoded = toml.decode(encoded)
log.console("Decoded:")
log.console(JSON.stringify(decoded, null, 2))
log.console("")

// Test edge cases
var edge_cases = {
  empty_string: "",
  string_with_quotes: 'She said "Hello"',
  empty_array: [],
  single_array: [42],
  nested_empty: {
    section: {}
  }
}

log.console("Testing edge cases...")
encoded = toml.encode(edge_cases)
log.console("Encoded:")
log.console(encoded)
log.console("")

decoded = toml.decode(encoded)
log.console("Decoded:")
log.console(JSON.stringify(decoded, null, 2))
log.console("")

// Verify round-trip conversion
log.console("Verifying round-trip conversion...")
function deep_equal(a, b) {
  if (a == b) return true
  if (a == null || b == null) return false
  if (typeof a != typeof b) return false
  
  if (typeof a == 'object') {
    var keys_a = Object.keys(a)
    var keys_b = Object.keys(b)
    if (keys_a.length != keys_b.length) return false
    
    for (var i = 0; i < keys_a.length; i++) {
      if (!deep_equal(a[keys_a[i]], b[keys_a[i]])) return false
    }
    return true
  }
  
  return false
}

var test_cases = [basic_obj, nested_obj, edge_cases]
var all_passed = true

for (var i = 0; i < test_cases.length; i++) {
  var original = test_cases[i]
  var round_trip = toml.decode(toml.encode(original))
  var passed = deep_equal(original, round_trip)
  log.console("Test case " + (i + 1) + ": " + (passed ? "PASSED" : "FAILED"))
  if (!passed) {
    all_passed = false
    log.console("Original:", JSON.stringify(original))
    log.console("Round-trip:", JSON.stringify(round_trip))
  }
}

log.console("")
log.console("Overall result: " + (all_passed ? "ALL TESTS PASSED" : "SOME TESTS FAILED"))

$_.stop()