// Shop system audit tests
// Tests module resolution, caching, and runtime paths
// accessible from user code via use()

var json = use('json')
var runtime = use('runtime')
var time_mod = use('time')

return {
  // ========================================================================
  // MODULE RESOLUTION
  // ========================================================================

  test_use_core_module: function() {
    if (!is_object(json)) return "use('json') should return an object"
    if (!is_function(json.encode)) return "json should have encode function"
    if (!is_function(json.decode)) return "json should have decode function"
  },

  test_use_returns_cached: function() {
    var json2 = use('json')
    if (json != json2) return "two use('json') calls should return same reference"
  },

  test_use_time_module: function() {
    if (!is_object(time_mod)) return "use('time') should return an object"
  },

  test_use_nonexistent_disrupts: function() {
    var caught = false
    var _fn = function() {
      var x = use('nonexistent_xyz_99')
    } disruption {
      caught = true
    }
    _fn()
    if (!caught) return "use('nonexistent_xyz_99') should disrupt"
  },

  // ========================================================================
  // RUNTIME PATHS
  // ========================================================================

  test_runtime_shop_path: function() {
    if (is_null(runtime.shop_path)) return "runtime.shop_path should not be null"
    if (!is_text(runtime.shop_path)) return "runtime.shop_path should be text"
  },

  test_runtime_core_path: function() {
    if (is_null(runtime.core_path)) return "runtime.core_path should not be null"
    if (!is_text(runtime.core_path)) return "runtime.core_path should be text"
  },

  test_runtime_is_frozen: function() {
    if (!is_stone(runtime)) return "runtime should be frozen (stoned)"
  },

  // ========================================================================
  // MODULE CACHING BEHAVIOR
  // ========================================================================

  test_json_encode_decode_roundtrip: function() {
    var obj = {a: 1, b: "hello", c: [1, 2, 3]}
    var encoded = json.encode(obj)
    var decoded = json.decode(encoded)
    if (decoded.a != 1) return "roundtrip failed for number"
    if (decoded.b != "hello") return "roundtrip failed for text"
    if (length(decoded.c) != 3) return "roundtrip failed for array"
  },

  test_use_blob_module: function() {
    var b = use('blob')
    if (!is_object(b)) return "use('blob') should return an object"
  },

  test_use_math_module: function() {
    var m = use('math')
    if (!is_object(m)) return "use('math') should return an object"
  },

  test_use_random_module: function() {
    var r = use('random')
    if (!is_object(r)) return "use('random') should return an object"
  }
}
