Diff-Endpunkt (GET /documents/{id}/diff) im versioningapi-Handler, Diff-Seite
mit freier Revisionswahl, Farbe+Symbol je Aenderung und Sprung zur naechsten
Aenderung im dms-browser-Frontend.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
39 lines
1.5 KiB
TypeScript
39 lines
1.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { fetchDiff } from "./diffApi";
|
|
|
|
const ORIGINAL_ENV = process.env;
|
|
|
|
beforeEach(() => {
|
|
process.env = { ...ORIGINAL_ENV, NEXT_PUBLIC_VERSIONING_API_URL: "http://backend.test", NEXT_PUBLIC_USER_ID: "user-a" };
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = ORIGINAL_ENV;
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("diffApi", () => {
|
|
it("fetchDiff ruft den erwarteten Endpunkt mit from/to/caller auf", async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ text: [], metadata: [] }), { status: 200 }));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
await fetchDiff("doc-1", "rev-a", "rev-b");
|
|
expect(fetchMock.mock.calls[0][0]).toBe(
|
|
"http://backend.test/documents/doc-1/diff?caller=user-a&from=rev-a&to=rev-b"
|
|
);
|
|
});
|
|
|
|
it("fetchDiff liefert Text- und Metadatendiff bei Erfolg", async () => {
|
|
const body = {
|
|
text: [{ op: "equal", text: "a" }, { op: "delete", text: "b" }, { op: "insert", text: "c" }],
|
|
metadata: [{ field: "comment", before: "alt", after: "neu" }],
|
|
};
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(JSON.stringify(body), { status: 200 })));
|
|
await expect(fetchDiff("doc-1", "rev-a", "rev-b")).resolves.toEqual(body);
|
|
});
|
|
|
|
it("fetchDiff wirft bei 403 eine verstaendliche Fehlermeldung", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
|
|
await expect(fetchDiff("doc-1", "rev-a", "rev-b")).rejects.toThrow(/Berechtigung/);
|
|
});
|
|
});
|