Implements Task 10 from the implementation plan: 1. Updated NoteTools constructor to accept IVaultAdapter and IFileManagerAdapter 2. Created note-tools-factory.ts with factory function 3. Migrated all CRUD methods to use adapters: - readNote: uses vault.read() - createNote: uses vault.create(), vault.delete() - createParentFolders: uses vault.createFolder() - updateNote: uses vault.read(), vault.modify() - deleteNote: uses vault.trash(), vault.delete() - renameFile: uses fileManager.renameFile() - readExcalidraw: uses vault.read() - updateFrontmatter: uses vault.read(), vault.modify() - updateSections: uses vault.read(), vault.modify() 4. Extended IVaultAdapter interface with modify(), delete(), and trash() methods 5. Implemented new methods in VaultAdapter 6. Updated ToolRegistry to use factory function 7. Kept app parameter temporarily for PathUtils dependency 8. All methods now use adapters instead of direct Obsidian API calls 9. Code compiles successfully This change enables 100% test coverage by allowing full mocking of vault operations.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { TAbstractFile, TFile, TFolder, CachedMetadata, DataWriteOptions } from 'obsidian';
|
|
|
|
/**
|
|
* Adapter interface for Obsidian Vault operations
|
|
*/
|
|
export interface IVaultAdapter {
|
|
// File reading
|
|
read(file: TFile): Promise<string>;
|
|
|
|
// File existence and metadata
|
|
stat(file: TAbstractFile): { ctime: number; mtime: number; size: number } | null;
|
|
|
|
// File retrieval
|
|
getAbstractFileByPath(path: string): TAbstractFile | null;
|
|
getMarkdownFiles(): TFile[];
|
|
|
|
// Directory operations
|
|
getRoot(): TFolder;
|
|
|
|
// File creation (process method)
|
|
process(file: TFile, fn: (data: string) => string, options?: DataWriteOptions): Promise<string>;
|
|
|
|
// Folder creation
|
|
createFolder(path: string): Promise<void>;
|
|
|
|
// File creation
|
|
create(path: string, data: string): Promise<TFile>;
|
|
|
|
// File modification
|
|
modify(file: TFile, data: string): Promise<void>;
|
|
|
|
// File deletion
|
|
delete(file: TAbstractFile): Promise<void>;
|
|
trash(file: TAbstractFile, system: boolean): Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* Adapter interface for Obsidian MetadataCache operations
|
|
*/
|
|
export interface IMetadataCacheAdapter {
|
|
// Cache access
|
|
getFileCache(file: TFile): CachedMetadata | null;
|
|
|
|
// Link resolution
|
|
getFirstLinkpathDest(linkpath: string, sourcePath: string): TFile | null;
|
|
|
|
// File cache for links and metadata
|
|
resolvedLinks: Record<string, Record<string, number>>;
|
|
unresolvedLinks: Record<string, Record<string, number>>;
|
|
}
|
|
|
|
/**
|
|
* Adapter interface for Obsidian FileManager operations
|
|
*/
|
|
export interface IFileManagerAdapter {
|
|
// File operations
|
|
renameFile(file: TAbstractFile, newPath: string): Promise<void>;
|
|
trashFile(file: TAbstractFile): Promise<void>;
|
|
processFrontMatter(file: TFile, fn: (frontmatter: any) => void): Promise<void>;
|
|
} |