-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest-obsidian-plugin.js
More file actions
220 lines (199 loc) · 6.15 KB
/
Copy pathtest-obsidian-plugin.js
File metadata and controls
220 lines (199 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const assert = require('assert');
const Module = require('module');
const helpers = require('../obsidian-plugin/signboard-helpers');
function createMockObsidianModule(options = {}) {
class Modal {
constructor(app) {
this.app = app;
this.contentEl = {
textContent: '',
appendChild() {},
};
}
setTitle() {}
open() {}
close() {}
}
class Notice {
constructor(message) {
this.message = message;
}
}
class Plugin {
constructor(app) {
this.app = app;
this.registeredEvents = [];
this.ribbonIcons = [];
this.commands = [];
}
addStatusBarItem() {
return {
classList: { add() {} },
textContent: '',
remove() {},
};
}
addRibbonIcon(icon, title, callback) {
if (options.throwForIcons && options.throwForIcons.has(icon)) {
throw new Error(`Missing icon: ${icon}`);
}
this.ribbonIcons.push({ icon, title, callback });
return { remove() {} };
}
addCommand(command) {
this.commands.push(command);
return command;
}
registerEvent(eventRef) {
this.registeredEvents.push(eventRef);
}
}
class TFile {}
class TFolder {}
return {
Modal,
Notice,
Plugin,
TFile,
TFolder,
};
}
function createMockObsidianApp() {
return {
workspace: {
getActiveFile: () => null,
on: () => ({ e: { offref() {} } }),
},
metadataCache: {
getFileCache: () => null,
on: () => ({ e: { offref() {} } }),
},
vault: {
adapter: {},
getAbstractFileByPath: () => null,
getMarkdownFiles: () => [],
on: () => ({ e: { offref() {} } }),
},
};
}
async function testPluginLoadFallbacks() {
const originalLoad = Module._load;
const originalWarn = console.warn;
const pluginPath = require.resolve('../obsidian-plugin/main');
Module._load = function mockObsidianLoad(request, parent, isMain) {
if (request === 'obsidian') {
return createMockObsidianModule({
throwForIcons: new Set(['layout-dashboard']),
});
}
return originalLoad.call(this, request, parent, isMain);
};
console.warn = () => {};
try {
delete require.cache[pluginPath];
const SignboardCompanionPlugin = require(pluginPath);
const plugin = new SignboardCompanionPlugin(createMockObsidianApp());
await plugin.onload();
assert.strictEqual(plugin.ribbonIcons.length, 1, 'plugin should register a fallback ribbon icon');
assert.strictEqual(plugin.ribbonIcons[0].icon, 'dice', 'fallback icon should be used when the primary icon is unavailable');
assert(plugin.commands.length >= 4, 'plugin should register command palette commands');
} finally {
Module._load = originalLoad;
console.warn = originalWarn;
delete require.cache[pluginPath];
}
}
async function run() {
assert.strictEqual(
helpers.extractSignboardCardId('signboard://open-card?id=abc12'),
'abc12',
'should extract card id from signboard URI',
);
assert.strictEqual(
helpers.extractSignboardCardId('abc12'),
'abc12',
'should accept a pasted card id',
);
assert.strictEqual(
helpers.buildSignboardBoardUri('/Users/example/Vault/Board'),
'signboard://open-board?path=%2FUsers%2Fexample%2FVault%2FBoard',
'should encode board open URIs',
);
assert.strictEqual(
helpers.getListDisplayName('001-Doing-stock'),
'Doing',
'should display stock list names',
);
assert.strictEqual(
helpers.cleanCardTitleFromFileName('000-launch-plan-ab123.md'),
'launch plan',
'should derive titles from Signboard filenames',
);
const settings = helpers.buildBoardSettingsMarkdown();
assert(settings.includes('labels:'), 'board settings should include labels');
assert(settings.includes('colorLight: #22c55e'), 'board settings should include default label colors');
const frontmatter = {
related: '[[Existing]]',
linked_objects: [
{
type: 'obsidian-note',
title: 'Existing',
target: '[[Existing]]',
},
],
};
helpers.addLinkedObjectToFrontmatter(frontmatter, helpers.createObsidianNoteLinkedObject({
title: 'Current Note',
target: '[[Current Note]]',
path: '/tmp/Current Note.md',
}), '[[Current Note]]');
assert.deepStrictEqual(frontmatter.related, ['[[Existing]]', '[[Current Note]]']);
assert.strictEqual(frontmatter.linked_objects.length, 2);
helpers.addLinkedObjectToFrontmatter(frontmatter, helpers.createObsidianNoteLinkedObject({
title: 'Current Note',
target: '[[Current Note]]',
path: '/tmp/Current Note.md',
}), '[[Current Note]]');
assert.strictEqual(frontmatter.linked_objects.length, 2, 'linked object should dedupe by path');
assert.deepStrictEqual(frontmatter.related, ['[[Existing]]', '[[Current Note]]'], 'related links should dedupe');
const deletedNoteContext = helpers.getDeletedObsidianNoteMatchContext({
vaultPath: 'Projects/Current Note.md',
absolutePath: '/tmp/Current Note.md',
basename: 'Current Note',
});
assert.strictEqual(
helpers.linkedObjectMatchesDeletedObsidianNote(frontmatter.linked_objects[1], deletedNoteContext),
true,
'deleted note should match linked object path',
);
const cleanupFrontmatter = {
related: ['[[Existing]]', '[[Projects/Current Note]]'],
linked_objects: [
{
type: 'obsidian-note',
title: 'Existing',
target: '[[Existing]]',
},
{
type: 'obsidian-note',
title: 'Current Note',
target: '[[Projects/Current Note]]',
path: '/tmp/Current Note.md',
},
],
};
const cleanupResult = helpers.removeDeletedObsidianNoteLinksFromFrontmatter(cleanupFrontmatter, deletedNoteContext);
assert.strictEqual(cleanupResult.changed, true, 'deleted note cleanup should report a change');
assert.deepStrictEqual(cleanupFrontmatter.related, ['[[Existing]]']);
assert.deepStrictEqual(cleanupFrontmatter.linked_objects, [{
type: 'obsidian-note',
title: 'Existing',
target: '[[Existing]]',
}]);
await testPluginLoadFallbacks();
console.log('Obsidian plugin helper tests passed.');
}
run().catch((error) => {
console.error(error);
process.exitCode = 1;
});