97 lines
3.1 KiB
TypeScript
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");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|