Ajoute un script d'import d'images Wikimedia
This commit is contained in:
42
tools/tests/bundles.test.js
Normal file
42
tools/tests/bundles.test.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { findLatestBundle, resolveBundlePath } = require("../lib/bundles");
|
||||
|
||||
/**
|
||||
* Crée un bundle Hugo minimal pour les tests.
|
||||
* @param {string} rootDir Racine temporaire.
|
||||
* @param {string} relativePath Chemin relatif du bundle.
|
||||
* @returns {Promise<string>} Chemin absolu du bundle créé.
|
||||
*/
|
||||
async function createBundle(rootDir, relativePath) {
|
||||
const bundleDir = path.join(rootDir, relativePath);
|
||||
await fs.mkdir(bundleDir, { recursive: true });
|
||||
await fs.writeFile(path.join(bundleDir, "index.md"), "---\ntitle: Test\ndate: 2026-03-20T12:00:00+01:00\n---\n", "utf8");
|
||||
return bundleDir;
|
||||
}
|
||||
|
||||
test("resolveBundlePath accepte un dossier de bundle ou un index.md", () => {
|
||||
const bundleDir = path.resolve("content/example/bundle");
|
||||
const indexPath = path.join(bundleDir, "index.md");
|
||||
|
||||
assert.equal(resolveBundlePath(bundleDir), bundleDir);
|
||||
assert.equal(resolveBundlePath(indexPath), bundleDir);
|
||||
});
|
||||
|
||||
test("findLatestBundle retourne le bundle modifie le plus recemment", async () => {
|
||||
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "bundles-test-"));
|
||||
const olderBundle = await createBundle(tempRoot, "alpha/article-a");
|
||||
const newerBundle = await createBundle(tempRoot, "beta/article-b");
|
||||
|
||||
await fs.utimes(olderBundle, new Date("2026-03-20T10:00:00Z"), new Date("2026-03-20T10:00:00Z"));
|
||||
await fs.utimes(newerBundle, new Date("2026-03-20T11:00:00Z"), new Date("2026-03-20T11:00:00Z"));
|
||||
|
||||
const latestBundle = await findLatestBundle(tempRoot);
|
||||
|
||||
assert.equal(latestBundle, newerBundle);
|
||||
|
||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
||||
});
|
||||
65
tools/tests/wikimedia.test.js
Normal file
65
tools/tests/wikimedia.test.js
Normal file
@@ -0,0 +1,65 @@
|
||||
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"
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user