Files
echo-du-huit/nextcloud.test.ts
T
Pierre Martin 058229dce3 feat: write the mairie status back to Nextcloud over CalDAV
REPORT by UID gives href + ETag + calendar-data, then PUT with If-Match: the
resource URL is never guessed and a concurrent edit yields 412 instead of an
overwrite. URL_COLLECTION is derived from URL_CALENDRIER.
2026-08-13 16:50:16 +02:00

76 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, test } from "bun:test";
import {
URL_CALENDRIER,
URL_COLLECTION,
recupererFluxCalendrier,
} from "./extension/nextcloud.js";
const FLUX_VALIDE =
"BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nSUMMARY:Atelier\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
function fetchQuiRenvoie(corps: string, status = 200) {
return async () => new Response(corps, { status });
}
describe("recupererFluxCalendrier", () => {
test("succès : 200 + VCALENDAR → { ok:true, flux }", async () => {
const res = await recupererFluxCalendrier(fetchQuiRenvoie(FLUX_VALIDE));
expect(res).toEqual({ ok: true, flux: FLUX_VALIDE });
});
test("non-connecté : 200 + page de login HTML → non-connecte", async () => {
const res = await recupererFluxCalendrier(
fetchQuiRenvoie("<!DOCTYPE html><html><body>Login</body></html>"),
);
expect(res).toEqual({ ok: false, erreur: "non-connecte" });
});
test("non-connecté : status 401 → non-connecte", async () => {
const res = await recupererFluxCalendrier(fetchQuiRenvoie("", 401));
expect(res).toEqual({ ok: false, erreur: "non-connecte" });
});
test("non-connecté : status 403 → non-connecte", async () => {
const res = await recupererFluxCalendrier(fetchQuiRenvoie("", 403));
expect(res).toEqual({ ok: false, erreur: "non-connecte" });
});
test("serveur : status 500 → serveur", async () => {
const res = await recupererFluxCalendrier(fetchQuiRenvoie("", 500));
expect(res).toEqual({ ok: false, erreur: "serveur" });
});
test("réseau : fetch lève → reseau", async () => {
const res = await recupererFluxCalendrier(async () => {
throw new TypeError("Failed to fetch");
});
expect(res).toEqual({ ok: false, erreur: "reseau" });
});
test("BOM / espaces en tête : reste un succès", async () => {
const corps = "\r\n" + FLUX_VALIDE;
const res = await recupererFluxCalendrier(fetchQuiRenvoie(corps));
expect(res).toEqual({ ok: true, flux: corps });
});
test("appelle fetchImpl avec URL_CALENDRIER et credentials:include", async () => {
let urlVue: unknown;
let optionsVues: unknown;
await recupererFluxCalendrier(async (url, options) => {
urlVue = url;
optionsVues = options;
return new Response(FLUX_VALIDE);
});
expect(urlVue).toBe(URL_CALENDRIER);
expect(optionsVues).toEqual({ credentials: "include" });
});
});
describe("URL_COLLECTION", () => {
test("= URL_CALENDRIER sans ?export, et se termine par /", () => {
expect(URL_COLLECTION).toBe(URL_CALENDRIER.replace("?export", ""));
expect(URL_COLLECTION.endsWith("/")).toBe(true);
expect(URL_COLLECTION).not.toContain("?export");
});
});