66 lines
2.6 KiB
JavaScript
66 lines
2.6 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const {
|
|
extractFileTitleFromUrl,
|
|
extractAssetFromApiResponse,
|
|
sanitizeMetadataText,
|
|
} = require("../lib/wikimedia");
|
|
|
|
test("extractFileTitleFromUrl supporte les URLs Commons et les fragments media de Wikipedia", () => {
|
|
const commonsUrl = "https://commons.wikimedia.org/wiki/File:IBookG3_Palourde2.png";
|
|
const wikipediaMediaUrl = "https://en.wikipedia.org/wiki/IBook#/media/File:IBookG3_Palourde2.png";
|
|
|
|
assert.equal(extractFileTitleFromUrl(commonsUrl), "File:IBookG3_Palourde2.png");
|
|
assert.equal(extractFileTitleFromUrl(wikipediaMediaUrl), "File:IBookG3_Palourde2.png");
|
|
});
|
|
|
|
test("sanitizeMetadataText decode le HTML de Commons", () => {
|
|
const rawValue = "No machine-readable author provided. <a href=\"//commons.wikimedia.org/wiki/User:Ocmey\">Ocmey</a> assumed & credited.";
|
|
assert.equal(
|
|
sanitizeMetadataText(rawValue),
|
|
"No machine-readable author provided. Ocmey assumed & credited."
|
|
);
|
|
});
|
|
|
|
test("extractAssetFromApiResponse reconstruit l'attribution et la description", () => {
|
|
const response = {
|
|
query: {
|
|
pages: {
|
|
"903939": {
|
|
title: "File:IBookG3 Palourde2.png",
|
|
imageinfo: [
|
|
{
|
|
url: "https://upload.wikimedia.org/wikipedia/commons/b/b3/IBookG3_Palourde2.png",
|
|
descriptionurl: "https://commons.wikimedia.org/wiki/File:IBookG3_Palourde2.png",
|
|
descriptionshorturl: "https://commons.wikimedia.org/w/index.php?curid=903939",
|
|
extmetadata: {
|
|
ImageDescription: {
|
|
value: "iBook G3 Open and Closed",
|
|
},
|
|
Credit: {
|
|
value: "No machine-readable source provided. Own work assumed (based on copyright claims).",
|
|
},
|
|
Artist: {
|
|
value: "No machine-readable author provided. <a href=\"//commons.wikimedia.org/wiki/User:Ocmey\" title=\"User:Ocmey\">Ocmey</a> assumed (based on copyright claims).",
|
|
},
|
|
LicenseShortName: {
|
|
value: "Public domain",
|
|
},
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const asset = extractAssetFromApiResponse(response);
|
|
|
|
assert.equal(asset.fileName, "IBookG3_Palourde2.png");
|
|
assert.equal(asset.description, "iBook G3 Open and Closed");
|
|
assert.equal(
|
|
asset.attribution,
|
|
"By No machine-readable author provided. Ocmey assumed (based on copyright claims). - No machine-readable source provided. Own work assumed (based on copyright claims)., Public Domain, https://commons.wikimedia.org/w/index.php?curid=903939"
|
|
);
|
|
});
|