@remediator/core
Version:
Remix/React Router 7 Mediator
119 lines (118 loc) • 5.4 kB
JavaScript
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { reMediatorPlugin } from "../vite-plugin";
import fs from "fs";
import path from "path";
// Mock fs and path
vi.mock("fs");
vi.mock("path");
const mockFs = vi.mocked(fs);
const mockPath = vi.mocked(path);
// Mock process.cwd() to provide a consistent root directory
vi.spyOn(process, "cwd").mockReturnValue("/d:/AppDev/ReMediator");
describe("reMediatorPlugin", () => {
const mockOptions = {
path: "app",
includeFileNames: ["*.mediator.ts"],
outputDir: "remediator",
};
beforeEach(() => {
vi.clearAllMocks();
// Default mocks
mockFs.existsSync.mockReturnValue(false);
mockFs.mkdirSync.mockReturnValue(undefined);
mockFs.writeFileSync.mockReturnValue(undefined);
mockFs.readdirSync.mockReturnValue([]);
mockFs.statSync.mockReturnValue({
isFile: () => false,
isDirectory: () => false,
});
// Path mocks to behave predictably
mockPath.join.mockImplementation((...args) => args.join("/"));
mockPath.relative.mockImplementation((from, to) => to.replace(from, "").replace(/^\//, ""));
mockPath.resolve.mockImplementation((...args) => args.join("/"));
mockPath.basename.mockImplementation((p) => p.split("/").pop() || "");
});
afterEach(() => {
vi.restoreAllMocks();
});
describe("plugin creation", () => {
it("should create plugin with default options", () => {
const plugin = reMediatorPlugin();
expect(plugin.name).toBe("remediator-auto-registration");
});
it("should create plugin with custom options", () => {
const plugin = reMediatorPlugin(mockOptions);
expect(plugin.name).toBe("remediator-auto-registration");
});
});
describe("generateItems", () => {
it("should generate client and manifest files correctly", async () => {
// --- Arrange ---
const files = [
"app/modules/users/getUser.mediator.ts",
"app/modules/auth/auth.mediator.ts",
];
// Mock readdirSync and statSync to simulate file structure
mockFs.readdirSync.mockImplementation((dir) => {
const dirPath = dir.toString();
if (dirPath === "app")
return ["modules"];
if (dirPath === "app/modules")
return ["users", "auth"];
if (dirPath === "app/modules/users")
return ["getUser.mediator.ts"];
if (dirPath === "app/modules/auth")
return ["auth.mediator.ts"];
return [];
});
mockFs.statSync.mockImplementation((p) => {
const pStr = p.toString();
return {
isFile: () => pStr.endsWith(".ts"),
isDirectory: () => !pStr.endsWith(".ts"),
};
});
const plugin = reMediatorPlugin(mockOptions);
// --- Act ---
await plugin.buildStart?.call(null);
// --- Assert ---
// Check that the output directory was created
expect(mockFs.mkdirSync).toHaveBeenCalledWith("remediator", {
recursive: true,
});
// Check that the manifest was written correctly
expect(mockFs.writeFileSync).toHaveBeenCalledWith("remediator/manifest.json", expect.stringContaining('"files":'));
// Check that the client file was written correctly
const clientCodeCall = mockFs.writeFileSync.mock.calls.find((call) => call[0] === "remediator/client.ts");
expect(clientCodeCall).toBeDefined();
const clientCode = clientCodeCall?.[1];
// Assert key parts of the generated client code
expect(clientCode).toContain("import { reMediator as ReMediatorClass } from '@remediator/core';");
expect(clientCode).toContain("export const reMediator = new ReMediatorClass();");
expect(clientCode).toContain("import * as app_modules_users_getUser_mediator from");
expect(clientCode).toContain("import * as app_modules_auth_auth_mediator from");
expect(clientCode).toContain("if (key.endsWith('Middleware')");
expect(clientCode).toContain("if (key.endsWith('Handler')");
expect(clientCode).toContain("reMediator.use(value as any);");
expect(clientCode).toContain("reMediator.register(requestCtor as any, new (handler as any)());");
});
});
describe("configureServer", () => {
it("should configure file watchers", () => {
const plugin = reMediatorPlugin(mockOptions);
const mockWatcher = {
add: vi.fn(),
on: vi.fn(),
};
const mockServer = {
watcher: mockWatcher,
};
if (plugin.configureServer &&
typeof plugin.configureServer === "function") {
plugin.configureServer(mockServer);
}
expect(mockWatcher.add).toHaveBeenCalledWith("app/**/*.mediator.ts");
expect(mockWatcher.on).toHaveBeenCalledWith("change", expect.any(Function));
});
});
});