mindnet_obsidian/src/tests/dictionary/ConfigPathManager.test.ts
Lars b0efa32c66
Some checks are pending
Node.js build / build (20.x) (push) Waiting to run
Node.js build / build (22.x) (push) Waiting to run
Implement chain roles and templates management in Mindnet plugin
- Added support for loading and reloading chain roles and templates from specified YAML configuration files, enhancing the plugin's flexibility.
- Introduced new settings for defining paths to chain roles and templates, improving user configurability.
- Implemented commands for debugging loaded chain roles and templates, providing users with insights into their configurations.
- Enhanced the Mindnet settings interface to include options for managing chain roles and templates paths, improving user experience with clear descriptions and validation features.
2026-01-18 14:50:17 +01:00

97 lines
3.1 KiB
TypeScript

/**
* Tests for ConfigPathManager path resolution and existence handling.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { ConfigPathManager } from "../../dictionary/ConfigPathManager";
import type { App, TFile } from "obsidian";
describe("ConfigPathManager", () => {
let mockApp: App;
let mockFile: TFile;
beforeEach(() => {
mockFile = {
path: "_system/dictionary/test.yaml",
name: "test.yaml",
extension: "yaml",
basename: "test",
stat: {
size: 100,
ctime: Date.now(),
mtime: Date.now(),
},
} as TFile;
mockApp = {
vault: {
getAbstractFileByPath: vi.fn(),
read: vi.fn(),
},
} as unknown as App;
});
describe("resolvePath", () => {
it("should resolve existing file path", () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(mockFile);
const result = ConfigPathManager.resolvePath(mockApp, "_system/dictionary/test.yaml");
expect(result.exists).toBe(true);
expect(result.file).toBe(mockFile);
expect(result.resolvedPath).toBe("_system/dictionary/test.yaml");
expect(result.error).toBeNull();
});
it("should return error when file does not exist", () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(null);
const result = ConfigPathManager.resolvePath(mockApp, "_system/dictionary/missing.yaml");
expect(result.exists).toBe(false);
expect(result.file).toBeNull();
expect(result.resolvedPath).toBe("_system/dictionary/missing.yaml");
expect(result.error).toContain("not found");
});
it("should normalize path separators", () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(mockFile);
const result = ConfigPathManager.resolvePath(mockApp, "_system\\dictionary\\test.yaml");
expect(result.resolvedPath).toBe("_system/dictionary/test.yaml");
});
});
describe("readFile", () => {
it("should read file content when file exists", async () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(mockFile);
vi.mocked(mockApp.vault.read).mockResolvedValue("test content");
const result = await ConfigPathManager.readFile(mockApp, "_system/dictionary/test.yaml");
expect(result.content).toBe("test content");
expect(result.error).toBeNull();
});
it("should return error when file does not exist", async () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(null);
const result = await ConfigPathManager.readFile(mockApp, "_system/dictionary/missing.yaml");
expect(result.content).toBe("");
expect(result.error).toContain("not found");
});
it("should handle read errors", async () => {
vi.mocked(mockApp.vault.getAbstractFileByPath).mockReturnValue(mockFile);
vi.mocked(mockApp.vault.read).mockRejectedValue(new Error("Read failed"));
const result = await ConfigPathManager.readFile(mockApp, "_system/dictionary/test.yaml");
expect(result.content).toBe("");
expect(result.error).toContain("Failed to read file");
});
});
});