require "mockcc"

require "gfs"

local function setup()
  fs._reset()
  http._reset()
end

local function testInstall()
  setup()
  http._register("https://api.github.com/gists/1", [=[
{
  "files": {
    "uno.lua": {
      "filename": "uno.lua",
      "content": "hi"
    },
    "dos.lua": {
      "filename": "dos.lua",
      "content": "bye"
    },
    "skip": {
      "filename": "skip",
      "content": "no"
    }
  }
}
]=])

  gfs.install("1", "one")

  assert(fs._at("/gists/one/id.txt"), "Missing the id file")
  assert(fs._read("/gists/one/id.txt") == "1", "The id file is incorrect: " .. fs._read("/gists/one/id.txt"))
  assert(fs._at("/gists/one/uno"), "Missing the first file")
  assert(fs._read("/gists/one/uno") == "hi", "The first file is incorrect: " .. fs._read("/gists/one/uno"))
  assert(fs._at("/gists/one/dos"), "Missing the second file")
  assert(fs._read("/gists/one/dos") == "bye", "The second file is incorrect: " .. fs._read("/gists/one/dos"))
  assert(not fs._at("/gists/one/skip"), "Did not skip non-Lua file")
end
testInstall()

local function testRefresh()
  setup()
  fs._write("/gists/two/id.txt", "2")
  fs._write("/gists/two/existing", "nice")
  fs._write("/gists/two/gone", "ouch")
  http._register("https://api.github.com/gists/2", [=[
{
  "files": {
    "new.lua": {
      "content": "sweet"
    },
    "existing.lua": {
      "content": "even better"
    }
  }
}
]=])

  gfs.refresh("two")

  assert(fs._at("/gists/two/id.txt"), "Missing the id file")
  assert(fs._read("/gists/two/id.txt") == "2", "The id file is incorrect: " .. fs._read("/gists/two/id.txt"))
  assert(fs._at("/gists/two/new"), "Missing the new file")
  assert(fs._read("/gists/two/new") == "sweet", "The new file is incorrect: " .. fs._read("/gists/two/new"))
  assert(fs._at("/gists/two/existing"), "Missing the existing file")
  assert(fs._read("/gists/two/existing") == "even better", "The existing file is incorrect: " .. fs._read("/gists/two/existing"))
  assert(not fs._at("/gists/two/gone"), "Did not remove the old file")
end
testRefresh()

local function testGo()
  setup()

  gfs.go("one")

  assert(shell._CURRENT == "/gists/one", "Incorrect current directory " .. shell._CURRENT)
end
testGo()

print("Passed")