Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/novel/novel-routing.test.ts
T

135 lines
4.6 KiB
TypeScript

// @vitest-environment jsdom
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import type { EditorView } from "@tiptap/pm/view";
import { afterEach, describe, expect, it, vi } from "vitest";
import { toast } from "sonner";
import { handleImageUpload } from "./lib/utils/editor";
vi.mock("sonner", () => ({
toast: {
error: vi.fn(),
},
}));
vi.mock("~/locale/request-scoped-t", () => ({
requestScopedT: (key: string) => key,
}));
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
const editorSource = readFileSync(path.join(moduleDir, "ui/editor/index.tsx"), "utf8");
const slashCommandSource = readFileSync(path.join(moduleDir, "ui/editor/extensions/slash-command.tsx"), "utf8");
const uploadSource = readFileSync(path.join(moduleDir, "lib/utils/editor.ts"), "utf8");
describe("Novel editor routing", () => {
it("does not target disabled internal AI routes from active editor UI", () => {
expect(editorSource).not.toContain("internal/ai/generate");
expect(slashCommandSource).not.toContain("internal/ai/generate");
expect(uploadSource).not.toContain("internal/ai/upload");
expect(editorSource).not.toContain("useCompletion");
expect(slashCommandSource).not.toContain("useCompletion");
expect(slashCommandSource).not.toContain("continue-writing");
});
it("keeps inserted images local instead of calling the retired API upload", () => {
expect(uploadSource).not.toContain('apiPath("ai/upload"');
expect(uploadSource).not.toContain("fetch(");
expect(uploadSource).toContain("reader.readAsDataURL(file)");
});
});
describe("Novel editor local image insertion", () => {
afterEach(() => {
vi.clearAllMocks();
vi.unstubAllGlobals();
});
const file = (overrides: Partial<File> = {}) =>
({
name: "pilot.png",
size: 1024,
type: "image/png",
...overrides,
}) as File;
const editorView = () => {
const create = vi.fn((attributes) => attributes);
const replaceSelectionWith = vi.fn((node) => ({ node }));
const dispatch = vi.fn();
return {
create,
dispatch,
replaceSelectionWith,
view: {
dispatch,
state: {
schema: { nodes: { image: { create } } },
tr: { replaceSelectionWith },
},
} as unknown as EditorView,
};
};
const installFileReader = (result: string = "data:image/png;base64,cGlsb3Q=") => {
vi.stubGlobal("ClipboardEvent", class extends Event {});
vi.stubGlobal("DragEvent", class extends Event {});
class TestFileReader {
onerror: ((event: ProgressEvent<FileReader>) => void) | null = null;
onload: ((event: ProgressEvent<FileReader>) => void) | null = null;
readAsDataURL() {
this.onload?.({ target: { result } } as unknown as ProgressEvent<FileReader>);
}
}
vi.stubGlobal("FileReader", TestFileReader);
};
it("reads a valid image as a data URL and preserves insertion metadata", () => {
installFileReader();
const fetchSpy = vi.fn();
vi.stubGlobal("fetch", fetchSpy);
const { create, dispatch, replaceSelectionWith, view } = editorView();
handleImageUpload(file(), view, new Event("change"));
expect(fetchSpy).not.toHaveBeenCalled();
expect(create).toHaveBeenCalledWith({
src: "data:image/png;base64,cGlsb3Q=",
alt: "pilot.png",
title: "pilot.png",
});
expect(replaceSelectionWith).toHaveBeenCalledOnce();
expect(dispatch).toHaveBeenCalledOnce();
});
it("rejects non-images and files larger than 50 MB before reading", () => {
const readAsDataURL = vi.fn();
class TestFileReader {
onerror: ((event: ProgressEvent<FileReader>) => void) | null = null;
onload: ((event: ProgressEvent<FileReader>) => void) | null = null;
readAsDataURL = readAsDataURL;
}
vi.stubGlobal("FileReader", TestFileReader);
const { dispatch, view } = editorView();
handleImageUpload(file({ type: "application/pdf" }), view, new Event("change"));
handleImageUpload(file({ size: 50 * 1024 * 1024 + 1 }), view, new Event("change"));
expect(toast.error).toHaveBeenNthCalledWith(1, "novel.editor.upload.unsupportedType");
expect(toast.error).toHaveBeenNthCalledWith(2, "novel.editor.upload.tooLarge");
expect(readAsDataURL).not.toHaveBeenCalled();
expect(dispatch).not.toHaveBeenCalled();
});
it("keeps the existing inclusive 50 MB limit", () => {
installFileReader();
const { dispatch, view } = editorView();
handleImageUpload(file({ size: 50 * 1024 * 1024 }), view, new Event("change"));
expect(toast.error).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledOnce();
});
});