{"size": 2366, "ext": "js", "max_stars_repo_path": "packages/endpoint-micropub/tests/unit/media.js", "max_stars_repo_name": "paulrobertlloyd/indiekit", "max_stars_repo_stars_event_min_datetime": "2019-02-19T15:24:03.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-21T03:30:12.000Z", "max_issues_repo_path": "packages/endpoint-micropub/tests/unit/media.js", "max_issues_repo_name": "paulrobertlloyd/indiekit", "max_issues_repo_issues_event_min_datetime": "2019-02-22T13:20:34.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-27T23:04:10.000Z", "max_forks_repo_path": "packages/endpoint-micropub/tests/unit/media.js", "max_forks_repo_name": "paulrobertlloyd/indiekit", "max_forks_repo_forks_event_min_datetime": "2019-02-25T10:17:46.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-26T06:23:28.000Z"}
{"max_stars_count": 90, "max_issues_count": 236, "max_forks_count": 12, "avg_line_length": 30.7272727273, "max_line_length": 80, "alphanum_fraction": 0.6534234996}
import process from "node:process";
import "dotenv/config.js"; // eslint-disable-line import/no-unassigned-import
import test from "ava";
import { setGlobalDispatcher } from "undici";
import { mediaEndpointAgent } from "@indiekit-test/mock-agent";
import { getFixture } from "@indiekit-test/get-fixture";
import { uploadMedia } from "../../lib/media.js";

setGlobalDispatcher(mediaEndpointAgent());

test.beforeEach((t) => {
  t.context = {
    bearerToken: process.env.TEST_TOKEN,
    files: [
      {
        buffer: getFixture("file-types/photo.jpg", false),
        fieldname: "photo",
        originalname: "photo1.jpg",
      },
    ],
    publication: {
      mediaEndpoint: "https://media-endpoint.example",
    },
    properties: {
      type: "entry",
      content: ["I ate a cheese sandwich, which was nice."],
      category: ["foo", "bar"],
      audio: ["https://website.example/media/sound.mp3"],
    },
  };
});

test("Uploads attached file via media endpoint", async (t) => {
  const { bearerToken, publication, properties, files } = t.context;
  const result = await uploadMedia(bearerToken, publication, properties, files);

  t.deepEqual(result.photo, ["https://website.example/media/photo1.jpg"]);
});

test("Uploads attached files via media endpoint", async (t) => {
  const { bearerToken, publication, properties } = t.context;
  const files = [
    {
      buffer: getFixture("file-types/photo.jpg", false),
      fieldname: "photo[]",
      originalname: "photo2.jpg",
    },
    {
      buffer: getFixture("file-types/photo.jpg", false),
      fieldname: "photo[]",
      originalname: "photo3.jpg",
    },
  ];
  const result = await uploadMedia(bearerToken, publication, properties, files);

  t.deepEqual(result.photo, [
    "https://website.example/media/photo2.jpg",
    "https://website.example/media/photo3.jpg",
  ]);
});

test("Throws error if no media endpoint URL", async (t) => {
  const { bearerToken, properties, files } = t.context;

  await t.throwsAsync(uploadMedia(bearerToken, {}, properties, files), {
    message: "Failed to parse URL from undefined",
  });
});

test("Throws error uploading attached file", async (t) => {
  const { publication, properties, files } = t.context;

  await t.throwsAsync(uploadMedia("foobar", publication, properties, files), {
    message: "The token provided was malformed",
  });
});
