import test from "ava";
import { Media } from "../../lib/models.js";

test('test media model basics work', async (t) => {
  try {
    let test1 = await Media.insert({
      "src": "/good.mp4",
      "preload": "none",
      "torrent_url": "/torrents/sample.torrent"
    });

    let test2 = await Media.first({id: test1.id});
    t.is(test1.id, test2.id);
    t.not(test2.src, undefined);

    let count = await Media.count({id: test2.id});
    t.is(count, 1);

    let all = await Media.all({src: test1.src});
    t.is(all.length, 1);
    t.is(all[0].src, test1.src);

    let res = await Media.update({id: test1.id}, {src: "/invalid.mp4"});
    t.is(res, 1);

    res = await Media.first({src: "/invalid.mp4"});
    t.is(res.id, test2.id);
    t.is(res.src, "/invalid.mp4");

    res = await Media.delete({id: test2.id});
    t.is(res, 1);

    count = await Media.count({id: test2.id});
    t.is(count, 0);

    all = await Media.all({id: test2.id});
    t.is(all.length, 0);

  } catch (error) {
    console.log(error);
    t.fail(error.message);
  }
})
