def t(x,y) =
  if x != y then test.fail() end
end

def f() =
  t(metadata.cover.remove([("a","A"),("apic","XXX"),("pic","YYY"),("b","B"),("cover","YYY")]), [("a","A"),("b","B")])

  apic_cover = string.base64.decode("AGltYWdlL3BuZwADQWxidW0gY292ZXIAiVBORw0KGgoAAAANSUhEUgAAAIAAAACAEAIAAAAczCrfAAAACXBIWXMAAAAAAAAAAQCEeRdzAAACoklEQVR4nO3E4UYYAACF0StJkiRJkiRJkiRJkiRJkiTJli1btmzZsmXLli3atGnTpk2bNm3atEmSJEmSJEmSJEmSJEmSJEnS9h73Oz+O9N/VFbPvurxg9l0XZ8y+6/yE2XedHTH7rtMDZt91ssfsu453mH3X0Raz7zrcYPZdB2vMvmt/hdl37S0x+67dBWbftTPH7Lu2Z5h919YUs+/anGD2XRtjzL5rfYTZd60NMfuu1QFm37Xyh9l3Lf9i9l1LP5h91+I3Zt+18IXZd81/YvZdcx+YfdfsO2bfNfOG2XdNv2L2XVMtzL5rspnZd000Mfuu8UZm3zXWwOy7RuuZfddIHbPvGq5l9l1DNcy+a7Ca2XcNVDH7rv4KZt/1t4zZd/WVMPuu30XMvqu3gNl3/cxj9l09Ocy+63sWs+/qzmD2XV/TmH1XVwqz7/qcxOy7OhOYfdfHOGbf1RHD7LveRzH7rvYIZt/1NozZd7WFMPuu10HMvqs1gNl3tVwy+64X58y+q/mU2Xc9O2b2XU2HzL7ryT6z72rcZfZdj7aZfVfDJrPverDO7LvqV5l9171lZt9Vt8jsu+7MM/uu2llm33Vrmtl31Uwy+64b48y+q3qU2XddG2b2XVWDzL6rsp/Zd1X0Mfuu8l5m31XWw+y7SruZfVdJF7PvKu5k9l1FHcy+q7Cd2XcVtDH7rvxWZt+V95LZd+U+Z/ZdOU+ZfVf2Y2bflfWQ2Xdl3mf2XRl3mX1X+m1m35V2k9l3pV5n9l0plcy+K7mc2XcllTL7rsRiZt+VUMjsu+LzmX1XXC6z74rNZvZdMZnMvis6ndl3RaUy+67IZGbfFZHI7LvC45l9V1gss+8KjWb2XSGRzL4rOJzZdwWFMvuuwGBm3xUQyOw7YOsfJwPDsnrI0ewAAAAASUVORK5CYII=")

  parse_apic_data = string.apic.parse(apic_cover)

  expected_apic_string_data = string.base64.decode("iVBORw0KGgoAAAANSUhEUgAAAIAAAACAEAIAAAAczCrfAAAACXBIWXMAAAAAAAAAAQCEeRdzAAACoklEQVR4nO3E4UYYAACF0StJkiRJkiRJkiRJkiRJkiTJli1btmzZsmXLli3atGnTpk2bNm3atEmSJEmSJEmSJEmSJEmSJEnS9h73Oz+O9N/VFbPvurxg9l0XZ8y+6/yE2XedHTH7rtMDZt91ssfsu453mH3X0Raz7zrcYPZdB2vMvmt/hdl37S0x+67dBWbftTPH7Lu2Z5h919YUs+/anGD2XRtjzL5rfYTZd60NMfuu1QFm37Xyh9l3Lf9i9l1LP5h91+I3Zt+18IXZd81/YvZdcx+YfdfsO2bfNfOG2XdNv2L2XVMtzL5rspnZd000Mfuu8UZm3zXWwOy7RuuZfddIHbPvGq5l9l1DNcy+a7Ca2XcNVDH7rv4KZt/1t4zZd/WVMPuu30XMvqu3gNl3/cxj9l09Ocy+63sWs+/qzmD2XV/TmH1XVwqz7/qcxOy7OhOYfdfHOGbf1RHD7LveRzH7rvYIZt/1NozZd7WFMPuu10HMvqs1gNl3tVwy+64X58y+q/mU2Xc9O2b2XU2HzL7ryT6z72rcZfZdj7aZfVfDJrPverDO7LvqV5l9171lZt9Vt8jsu+7MM/uu2llm33Vrmtl31Uwy+64b48y+q3qU2XddG2b2XVWDzL6rsp/Zd1X0Mfuu8l5m31XWw+y7SruZfVdJF7PvKu5k9l1FHcy+q7Cd2XcVtDH7rvxWZt+V95LZd+U+Z/ZdOU+ZfVf2Y2bflfWQ2Xdl3mf2XRl3mX1X+m1m35V2k9l3pV5n9l0plcy+K7mc2XcllTL7rsRiZt+VUMjsu+LzmX1XXC6z74rNZvZdMZnMvis6ndl3RaUy+67IZGbfFZHI7LvC45l9V1gss+8KjWb2XSGRzL4rOJzZdwWFMvuuwGBm3xUQyOw7YOsfJwPDsnrI0ewAAAAASUVORK5CYII=")

  t(parse_apic_data, expected_apic_string_data.{description = "Album cover", picture_type = 3, mime = "image/png"})

  pic_cover = string.base64.decode("[encoded data, 40752 characters]")

  parse_pic_data = string.pic.parse(pic_cover)

  expected_pic_string_data = string.base64.decode("[encoded data, 40744 characters]")

  t(parse_pic_data, expected_pic_string_data.{description = "", picture_type = 1, mime = "image/jpeg"})


  # From http://towerofbabel.free.fr/test/music.ogg
  flac_cover = string.base64.decode("[encoded data, 6524 characters]")

  parse_flac_cover = null.get(file.metadata.flac.cover.decode(flac_cover))

  excepted_flac_string_data = string.base64.decode("[encoded data, 6468 characters]")

  t(parse_flac_cover, excepted_flac_string_data.{picture_type = 3, mime = "image/jpeg", description = "", width = 0, height = 0, color_depth = 0, number_of_colors = null()})

  encoded_flac_cover = file.metadata.flac.cover.encode(
    picture_type=parse_flac_cover.picture_type,
    mime=parse_flac_cover.mime,
    description=parse_flac_cover.description,
    width=parse_flac_cover.width,
    height=parse_flac_cover.height,
    color_depth=parse_flac_cover.color_depth,
    number_of_colors=parse_flac_cover.number_of_colors,
    (parse_flac_cover:string))


  # The re-encoded tag doesn't match the original one b/c mime string has padding in it..
  expected_encoded_flac_codec = string.base64.decode("[encoded data, 6552 characters]")

  t(encoded_flac_cover, expected_encoded_flac_codec)

  test.pass()
end

def test_metadata_json_stringify() =
  file_metadata = [
    ("filename", "test.mp3"),
    ("temporary", "false"),
    ("initial_uri", "test.mp3"),
    ("encoder", "Lavf59.27.100"),
    ("status", "ready"),
    ("rid", "0"),
    ("artist", "Artist")
  ]
  expected_json_string = '{ "artist": "Artist" }'

  json_string = metadata.json.stringify(file_metadata)
  t(json_string, expected_json_string)

  empty_file_metadata = []
  expected_json_string = '{}'

  json_string = metadata.json.stringify(empty_file_metadata)
  t(json_string, expected_json_string)

  test.pass()
end

def test_metadata_json_parse() =
  metadata_json_string = '{"artist": "Artist"}'
  expected_parsed_metadata = [("artist", "Artist")]

  parsed_metadata = metadata.json.parse(metadata_json_string)
  t(parsed_metadata, expected_parsed_metadata)

  empty_metadata_json_string = '{}'
  expected_parsed_metadata = []

  parsed_metadata = metadata.json.parse(empty_metadata_json_string)
  t(parsed_metadata, expected_parsed_metadata)

  test.pass()
end

test.check(f)
test.check(test_metadata_json_stringify)
test.check(test_metadata_json_parse)
