Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+116
-5
@@ -1,8 +1,21 @@
|
||||
// @vitest-environment node
|
||||
// @vitest-environment jsdom
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { describe, expect, it } from "vitest";
|
||||
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");
|
||||
@@ -14,10 +27,108 @@ describe("Novel editor routing", () => {
|
||||
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("uploads through the canonical API host with tenant scope", () => {
|
||||
expect(uploadSource).toContain('apiPath("ai/upload"');
|
||||
expect(uploadSource).toContain('"x-tenant-id": tenantId');
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user