defmodule Openpod.Provider.Archive.ParserTest do
  use ExUnit.Case

  alias Openpod.Provider.Archive.Parser

  setup_all do
    json_metadata = "{\"created\":1590247789,\"d1\":\"ia601402.us.archive.org\",\"d2\":\"ia801402.us.archive.org\",\"dir\":\"/8/items/incontri-a-piano-terra\",\"files\":[{\"name\": \"metadata.json\",\"source\": \"original\",\"mtime\": \"1590258296\",\"size\": \"315\",\"md5\": \"a0c0e219cf3f13e54f2a4b3efef8e5c8\",\"crc32\": \"2d181b5c\",\"sha1\": \"8244b579a759edddd01c905dd11f8565e83d0898\",\"format\": \"JSON\"},{\"name\": \"cover.jpg\",\"source\": \"original\",\"mtime\": \"1590258445\",\"size\": \"10650\",\"md5\": \"15687b23e11f0099abbfe64eb1685c31\",\"crc32\": \"fbb1516a\",\"sha1\": \"98fa929c7554241cfa92bea8eba69b39c5d47603\",\"format\": \"JPEG\",\"rotation\": \"0\"},{\"name\":\"Confini mobili sulle alpi.mp3\",\"source\":\"original\",\"mtime\":\"1590135989\",\"size\":\"46933494\",\"md5\":\"e832ee9381a4f8af2d9727e2f49126ae\",\"crc32\":\"d709dd90\",\"sha1\":\"89c820a2dfd63cfbbf7aeefd191c653756b33fe3\",\"format\":\"VBR MP3\",\"length\":\"3902.35\",\"height\":\"0\",\"width\":\"0\",\"title\":\"Confini mobili sulle alpi (italian limes)\",\"creator\":\"APE Milano\",\"album\":\"Incontri a Piano Terra\",\"track\":\"02\",\"artist\":\"APE Milano\",\"genre\":\"podcast\"},{\"name\":\"Confini mobili sulle alpi.png\",\"source\":\"derivative\",\"format\":\"PNG\",\"original\":\"Confini mobili sulle alpi.mp3\",\"mtime\":\"1590137809\",\"size\":\"34656\",\"md5\":\"63893f9b00402a107682b5317e808523\",\"crc32\":\"b59ff609\",\"sha1\":\"a396716431cd0acedd243030093d0b31d792cfb3\"},{\"name\":\"Confini mobili sulle alpi_spectrogram.png\",\"source\":\"derivative\",\"format\":\"Spectrogram\",\"original\":\"Confini mobili sulle alpi.mp3\",\"mtime\":\"1590137854\",\"size\":\"273188\",\"md5\":\"557337665c6d9f962b2e91d169f25e1b\",\"crc32\":\"08b4b57c\",\"sha1\":\"88e088f9c4954aa8f0849b7e0d69cee8d7d42327\"}],\"files_count\":31,\"item_last_updated\":1590160774,\"item_size\":244544362,\"metadata\":{\"identifier\":\"incontri-a-piano-terra\",\"mediatype\":\"audio\",\"collection\":\"opensource_audio\",\"creator\":\"APE Milano\",\"description\":\"Qualche registrazione delle attivit\\u00e0 sociali che promuoviamo al Piano Terra di Milano\",\"language\":\"ita\",\"licenseurl\":\"https://creativecommons.org/licenses/by-nc-nd/4.0/\",\"scanner\":\"Internet Archive HTML5 Uploader 1.6.4\",\"subject\":[\"ape milano\",\"podcast\",\"montagna\"],\"title\":\"Incontri a Piano Terra\",\"uploader\":\"milanoape@gmail.com\",\"publicdate\":\"2020-05-22 08:30:21\",\"addeddate\":\"2020-05-22 08:30:21\",\"curation\":\"[curator]validator@archive.org[/curator][date]20200522085526[/date][comment]checked for malware[/comment]\",\"op_link\":\"http://www.ape-alveare.it/\",\"op_category\":\"Montagna\",\"op_explicit\":\"no\"},\"server\":\"ia601402.us.archive.org\",\"uniq\":122833277,\"workable_servers\":[\"ia601402.us.archive.org\",\"ia801402.us.archive.org\"]}"

    token = %Parser{
      identifier: "incontri-a-piano-terra",
      archive_metadata: Jason.decode!(json_metadata),
    }

    {:ok, token: token}
  end

  test "podcast data are correctly converted", state do
    %{podcast: podcast} = Parser.to_openpod_data(state[:token])

    assert %{
      title: "Incontri a Piano Terra",
      description: "Qualche registrazione delle attività sociali che promuoviamo al Piano Terra di Milano",
      webmaster: "milanoape@gmail.com",
      managingEditor: "milanoape@gmail.com",
      owner: %{
        name: "APE Milano",
        email: "milanoape@gmail.com",
      },
      keywords: ["ape milano", "podcast", "montagna"],
      pubDate: ~U[2020-05-22 08:30:21Z],
      lastBuildDate: ~U[2020-05-22 15:19:34Z],
      author: "APE Milano",
      language: "ita",
      image: %{
        link: "http://www.ape-alveare.it/",
        title: "Incontri a Piano Terra",
        url: "https://archive.org/download/incontri-a-piano-terra/cover.jpg"
      },
      link: "http://www.ape-alveare.it/",
      category: "Montagna",
      explicit: "no",
    } == podcast
  end

  test "podcast subject metadata can also be a string", state do
    token = state[:token] |> Map.update!(:archive_metadata, fn metadata -> put_in(metadata, ["metadata", "subject"], "foo;bar;baz") end)
    %{podcast: podcast} = Parser.to_openpod_data(token)

    assert %{
      keywords: ["foo", "bar", "baz"],
    } = podcast
  end

  test "items data are correctly converted", state do
    %{items: items} = Parser.to_openpod_data(state[:token])

    assert [
      %{
        title: "Confini mobili sulle alpi (italian limes)",
        description: "",
        pubDate: ~U[2020-05-22 08:26:29Z],
        link: "https://archive.org/download/incontri-a-piano-terra/Confini%20mobili%20sulle%20alpi.mp3",
        length: 3902,
        summary: "",
        keywords: ["Incontri a Piano Terra", "APE Milano", "podcast"],
        explicit: "no",
        # image: "https://archive.org/download/incontri-a-piano-terra/Confini%20mobili%20sulle%20alpi.png",
        image: nil,
        size: "46933494"
      }
    ] == items
  end
end
