// Test script for the module system

var io = use('io')
var shop = use('shop')
var time = use('time')

log.console("== Testing Module System ==")

// Test 1: TOML parser
log.console("\n1. Testing TOML parser...")
var toml = use('toml')
var test_toml = `
module = "test"
version = "1.0.0"

[dependencies]
foo = "bar@1.0"
baz = "qux@2.0"

[arrays]
items = ["one", "two", "three"]
`
var parsed = toml.decode(test_toml)
log.console("Parsed module: " + parsed.module)
log.console("Dependencies: " + json.encode(parsed.dependencies))
log.console("✓ TOML parser working")

// Test 2: Shop initialization
log.console("\n2. Testing shop initialization...")
var test_dir = "module_test_" + time.number()
io.mkdir(test_dir)
var old_cwd = io.basedir()

// Create a test shop
io.writepath(test_dir)
shop.init()
if (io.exists('.cell/shop.toml')) {
  log.console("✓ Shop initialized successfully")
} else {
  log.console("✗ Shop initialization failed")
}

// Test 3: Module resolution
log.console("\n3. Testing module resolver...")
var resolver = use('module_resolver')

var tests = [
  {input: "core://time", expected: "/core/time"},
  {input: "mod://utils", expected: "/mod/utils"},
  {input: "./helper", expected: "./helper"},
  {input: "sprite", expected: "sprite"}
]

for (var i = 0; i < tests.length; i++) {
  var test = tests[i]
  var result = resolver.resolve(test.input)
  if (result == test.expected) {
    log.console("✓ " + test.input + " -> " + result)
  } else {
    log.console("✗ " + test.input + " -> " + result + " (expected " + test.expected + ")")
  }
}

// Clean up
io.writepath(old_cwd)
io.rm(test_dir + '/.cell/shop.toml')
io.rm(test_dir + '/.cell/lock.toml')
io.rm(test_dir + '/.cell/patches')
io.rm(test_dir + '/.cell/build')
io.rm(test_dir + '/.cell/modules')
io.rm(test_dir + '/.cell')
io.rm(test_dir)

log.console("\n== Module System Test Complete ==")

$_.stop()