199 lines
7.0 KiB
JavaScript
199 lines
7.0 KiB
JavaScript
// Tests for the Obsidian plugin's automatic sync: vault events push edits, create notes for files
|
|
// added to the campaign folder, and delete the note of a deleted file. The plugin is loaded from
|
|
// source with a stubbed `obsidian` module so the real class runs under node:test.
|
|
//
|
|
// Run with `npm test` from the repository root, or `node --test tests/`.
|
|
|
|
const { test } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const SOURCE = fs.readFileSync(
|
|
path.join(__dirname, "..", "obsidian-plugin", "obsidian-dragonroll", "main.js"),
|
|
"utf8",
|
|
);
|
|
|
|
function loadPluginClass() {
|
|
const module = { exports: {} };
|
|
const requireStub = (name) => {
|
|
if (name !== "obsidian") throw new Error(`unexpected require: ${name}`);
|
|
return {
|
|
Notice: class { constructor(message) { this.message = message; } },
|
|
Plugin: class {},
|
|
PluginSettingTab: class {},
|
|
Setting: class {},
|
|
normalizePath: (value) => value,
|
|
requestUrl: async () => { throw new Error("network is not stubbed"); },
|
|
};
|
|
};
|
|
new Function("module", "exports", "require", SOURCE)(module, module.exports, requireStub);
|
|
return module.exports;
|
|
}
|
|
|
|
const DragonrollPlugin = loadPluginClass();
|
|
|
|
const FILE = {
|
|
path: "Dragonroll/Campaign/Session 1.md",
|
|
basename: "Session 1",
|
|
extension: "md",
|
|
};
|
|
|
|
const BUFFER = [
|
|
"---",
|
|
"dragonrollId: n1",
|
|
"dragonrollCampaign: c1",
|
|
"tags: [session]",
|
|
"---",
|
|
"# Hello",
|
|
"",
|
|
].join("\n");
|
|
|
|
function makePlugin({ file = FILE, frontmatter = {}, settings = {}, buffer = BUFFER } = {}) {
|
|
const plugin = new DragonrollPlugin();
|
|
plugin.settings = { token: "t", campaignId: "c1", autoSync: true, snapshots: {}, ...settings };
|
|
plugin.status = { setText() {} };
|
|
plugin.suspended = false;
|
|
plugin.internalWrites = new Set();
|
|
plugin.linkedNotes = new Map();
|
|
plugin.syncChain = Promise.resolve();
|
|
plugin.saveSettings = async () => {};
|
|
plugin.requests = [];
|
|
plugin.request = async (url, options = {}) => {
|
|
plugin.requests.push({ url, options });
|
|
if (url === "/campaign/list") return { status: "ok", campaigns: [{ _id: "c1", name: "Campaign" }] };
|
|
if (url === "/note/create") return { status: "ok", note: { _id: "n2" } };
|
|
if (url === "/note/update") return { status: "ok", note: { _id: "n1" } };
|
|
return { status: "ok" };
|
|
};
|
|
plugin.app = {
|
|
vault: {
|
|
getAbstractFileByPath: () => file,
|
|
read: async () => buffer,
|
|
getMarkdownFiles: () => [file],
|
|
},
|
|
metadataCache: {
|
|
getFileCache: () => ({ frontmatter: { dragonrollId: "n1", dragonrollCampaign: "c1", ...frontmatter } }),
|
|
},
|
|
fileManager: {
|
|
processFrontMatter: async (target, apply) => {
|
|
const written = {};
|
|
apply(written);
|
|
plugin.writtenFrontmatter = written;
|
|
},
|
|
},
|
|
};
|
|
return plugin;
|
|
}
|
|
|
|
async function settle(plugin) {
|
|
await plugin.syncChain;
|
|
}
|
|
|
|
test("vault events are ignored when auto-sync is off, the file is not markdown, or the plugin wrote it", async () => {
|
|
const off = makePlugin({ settings: { autoSync: false } });
|
|
off.onVaultChange(FILE);
|
|
await settle(off);
|
|
assert.equal(off.requests.length, 0);
|
|
|
|
const notMarkdown = makePlugin();
|
|
notMarkdown.onVaultChange({ ...FILE, extension: "pdf" });
|
|
await settle(notMarkdown);
|
|
assert.equal(notMarkdown.requests.length, 0);
|
|
|
|
const importing = makePlugin();
|
|
importing.suspended = true;
|
|
importing.onVaultChange(FILE);
|
|
await settle(importing);
|
|
assert.equal(importing.requests.length, 0);
|
|
|
|
const ownWrite = makePlugin();
|
|
ownWrite.internalWrites.add(FILE.path);
|
|
ownWrite.onVaultChange(FILE);
|
|
await settle(ownWrite);
|
|
assert.equal(ownWrite.requests.length, 0);
|
|
});
|
|
|
|
test("saving a linked note updates it with the file content, frontmatter stripped", async () => {
|
|
const plugin = makePlugin();
|
|
|
|
plugin.onVaultChange(FILE);
|
|
await settle(plugin);
|
|
|
|
assert.equal(plugin.requests.length, 1);
|
|
const [request] = plugin.requests;
|
|
assert.equal(request.url, "/note/update");
|
|
assert.deepEqual(request.options.body, {
|
|
id: "n1",
|
|
title: "Session 1",
|
|
content: "---\ntags: [session]\n---\n# Hello\n",
|
|
});
|
|
assert.ok(plugin.settings.snapshots.n1, "the pushed version becomes the import baseline");
|
|
|
|
plugin.onVaultChange(FILE);
|
|
await settle(plugin);
|
|
assert.equal(plugin.requests.length, 1, "an unchanged save is not pushed again");
|
|
});
|
|
|
|
test("a new note inside the campaign folder becomes a campaign note", async () => {
|
|
const plugin = makePlugin({
|
|
file: { path: "Dragonroll/Campaign/New Note.md", basename: "New Note", extension: "md" },
|
|
frontmatter: { dragonrollId: undefined },
|
|
buffer: "Body",
|
|
});
|
|
|
|
plugin.onVaultChange(plugin.app.vault.getMarkdownFiles()[0]);
|
|
await settle(plugin);
|
|
|
|
const create = plugin.requests.find((request) => request.url === "/note/create");
|
|
assert.ok(create, "the new file was not created in the campaign");
|
|
assert.deepEqual(create.options.body, { title: "New Note", content: "Body", campaign: "c1" });
|
|
assert.deepEqual(plugin.writtenFrontmatter, {
|
|
dragonrollId: "n2",
|
|
dragonrollCampaign: "c1",
|
|
dragonrollTitle: "New Note",
|
|
});
|
|
assert.deepEqual(plugin.linkedNotes.get("Dragonroll/Campaign/New Note.md"), { noteId: "n2", campaignId: "c1" });
|
|
});
|
|
|
|
test("a new note outside the campaign folder stays local", async () => {
|
|
const plugin = makePlugin({
|
|
file: { path: "Notes/Idea.md", basename: "Idea", extension: "md" },
|
|
frontmatter: { dragonrollId: undefined },
|
|
buffer: "Body",
|
|
});
|
|
|
|
plugin.onVaultChange(plugin.app.vault.getMarkdownFiles()[0]);
|
|
await settle(plugin);
|
|
|
|
assert.equal(plugin.requests.length, 0);
|
|
});
|
|
|
|
test("deleting a linked file, or a folder holding linked files, deletes the campaign notes", async () => {
|
|
const plugin = makePlugin();
|
|
plugin.settings.snapshots.n1 = "sig";
|
|
plugin.linkedNotes.set(FILE.path, { noteId: "n1", campaignId: "c1" });
|
|
plugin.linkedNotes.set("Dragonroll/Campaign/Other.md", { noteId: "n3", campaignId: "c1" });
|
|
|
|
plugin.onVaultDelete(FILE);
|
|
await settle(plugin);
|
|
assert.deepEqual(plugin.requests.map((request) => request.url), ["/note/delete"]);
|
|
assert.deepEqual(plugin.requests[0].options.body, { id: "n1" });
|
|
assert.equal(plugin.settings.snapshots.n1, undefined);
|
|
|
|
plugin.onVaultDelete({ path: "Dragonroll/Campaign" });
|
|
await settle(plugin);
|
|
assert.deepEqual(plugin.requests[1].options.body, { id: "n3" });
|
|
assert.equal(plugin.linkedNotes.size, 0);
|
|
});
|
|
|
|
test("deleting a file linked to another campaign leaves that campaign alone", async () => {
|
|
const plugin = makePlugin();
|
|
plugin.linkedNotes.set(FILE.path, { noteId: "n9", campaignId: "c2" });
|
|
|
|
plugin.onVaultDelete(FILE);
|
|
await settle(plugin);
|
|
|
|
assert.equal(plugin.requests.length, 0);
|
|
});
|