|
| 1 | +import { App, TFile } from 'obsidian'; |
| 2 | +import { GraphSearchTool } from '../src/tools/graph-search'; |
| 3 | +import { GraphTraversal } from '../src/utils/graph-traversal'; |
| 4 | +import { ObsidianAPI } from '../src/utils/obsidian-api'; |
| 5 | + |
| 6 | +function makeFile(path: string, basename: string, parentName?: string): TFile { |
| 7 | + const file = new TFile(); |
| 8 | + file.path = path; |
| 9 | + file.name = `${basename}.md`; |
| 10 | + file.basename = basename; |
| 11 | + file.extension = 'md'; |
| 12 | + (file as any).parent = parentName |
| 13 | + ? { name: parentName } |
| 14 | + : null; |
| 15 | + return file; |
| 16 | +} |
| 17 | + |
| 18 | +describe('graph node titles', () => { |
| 19 | + let app: App; |
| 20 | + let traversal: GraphTraversal; |
| 21 | + let search: GraphSearchTool; |
| 22 | + let indexFile: TFile; |
| 23 | + let regularFile: TFile; |
| 24 | + let sourceFile: TFile; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + indexFile = makeFile('topics/rendering/index.md', 'index', 'rendering'); |
| 28 | + regularFile = makeFile('topics/rendering/overview.md', 'overview', 'rendering'); |
| 29 | + sourceFile = makeFile('source.md', 'source'); |
| 30 | + |
| 31 | + const filesByPath = new Map([ |
| 32 | + [indexFile.path, indexFile], |
| 33 | + [regularFile.path, regularFile], |
| 34 | + [sourceFile.path, sourceFile] |
| 35 | + ]); |
| 36 | + |
| 37 | + app = new App(); |
| 38 | + (app as any).metadataCache = { |
| 39 | + resolvedLinks: { |
| 40 | + 'source.md': { 'topics/rendering/index.md': 1 }, |
| 41 | + 'topics/rendering/index.md': { 'topics/rendering/overview.md': 1 } |
| 42 | + }, |
| 43 | + unresolvedLinks: {}, |
| 44 | + getFileCache: jest.fn().mockReturnValue({ tags: [] }) |
| 45 | + }; |
| 46 | + app.vault.getFiles = jest.fn(() => [indexFile, regularFile, sourceFile]); |
| 47 | + app.vault.getAbstractFileByPath = jest.fn((path: string) => filesByPath.get(path) ?? null); |
| 48 | + |
| 49 | + traversal = new GraphTraversal(app); |
| 50 | + search = new GraphSearchTool({} as ObsidianAPI, app); |
| 51 | + }); |
| 52 | + |
| 53 | + it('uses the parent folder as the graph title for index files', () => { |
| 54 | + expect(traversal.getNodeTitle(indexFile)).toBe('rendering'); |
| 55 | + expect(traversal.getNodeTitle(regularFile)).toBe('overview'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('uses resolved graph titles in vault node listings', () => { |
| 59 | + const nodes = traversal.getAllNodes(); |
| 60 | + |
| 61 | + expect(nodes).toEqual( |
| 62 | + expect.arrayContaining([ |
| 63 | + expect.objectContaining({ path: 'topics/rendering/index.md', title: 'rendering' }), |
| 64 | + expect.objectContaining({ path: 'topics/rendering/overview.md', title: 'overview' }) |
| 65 | + ]) |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('uses resolved graph titles in forwardlink results', () => { |
| 70 | + const result = search.search({ operation: 'forwardlinks', sourcePath: 'source.md' }); |
| 71 | + |
| 72 | + expect(result.nodes).toEqual([ |
| 73 | + expect.objectContaining({ path: 'topics/rendering/index.md', title: 'rendering' }) |
| 74 | + ]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses resolved graph titles in backlink results', () => { |
| 78 | + const result = search.search({ operation: 'backlinks', sourcePath: 'topics/rendering/overview.md' }); |
| 79 | + |
| 80 | + expect(result.nodes).toEqual([ |
| 81 | + expect.objectContaining({ path: 'topics/rendering/index.md', title: 'rendering' }) |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('uses resolved graph titles in path results', () => { |
| 86 | + const result = search.search({ |
| 87 | + operation: 'path', |
| 88 | + sourcePath: 'source.md', |
| 89 | + targetPath: 'topics/rendering/overview.md' |
| 90 | + }); |
| 91 | + |
| 92 | + expect(result.paths?.[0]).toEqual([ |
| 93 | + { path: 'source.md', title: 'source' }, |
| 94 | + { path: 'topics/rendering/index.md', title: 'rendering' }, |
| 95 | + { path: 'topics/rendering/overview.md', title: 'overview' } |
| 96 | + ]); |
| 97 | + }); |
| 98 | +}); |
0 commit comments