|
| 1 | +import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + acquireVaultRunLock, |
| 4 | + captureFailureArtifacts, |
| 5 | + clearVaultRunLockMarker, |
| 6 | + createObsidianClient, |
| 7 | + createSandboxApi, |
| 8 | +} from "obsidian-e2e"; |
| 9 | +import type { |
| 10 | + ObsidianClient, |
| 11 | + PluginHandle, |
| 12 | + SandboxApi, |
| 13 | + VaultRunLock, |
| 14 | +} from "obsidian-e2e"; |
| 15 | + |
| 16 | +const VAULT = "dev"; |
| 17 | +const PLUGIN_ID = "quickadd"; |
| 18 | +const WAIT_OPTS = { timeoutMs: 10_000, intervalMs: 200 }; |
| 19 | + |
| 20 | +let obsidian: ObsidianClient; |
| 21 | +let sandbox: SandboxApi; |
| 22 | +let qa: PluginHandle; |
| 23 | +let lock: VaultRunLock | undefined; |
| 24 | + |
| 25 | +type QuickAddData = { |
| 26 | + choices: Record<string, unknown>[]; |
| 27 | + migrations: Record<string, boolean>; |
| 28 | + enableTemplatePropertyTypes?: boolean; |
| 29 | +}; |
| 30 | + |
| 31 | +function templateChoice(id: string, templatePath: string, format: string) { |
| 32 | + return { |
| 33 | + id, |
| 34 | + name: id, |
| 35 | + type: "Template", |
| 36 | + command: false, |
| 37 | + templatePath, |
| 38 | + fileNameFormat: { enabled: true, format }, |
| 39 | + folder: { |
| 40 | + enabled: false, |
| 41 | + folders: [], |
| 42 | + chooseWhenCreatingNote: false, |
| 43 | + createInSameFolderAsActiveFile: false, |
| 44 | + chooseFromSubfolders: false, |
| 45 | + }, |
| 46 | + appendLink: false, |
| 47 | + openFile: false, |
| 48 | + fileOpening: { |
| 49 | + location: "tab", |
| 50 | + direction: "vertical", |
| 51 | + mode: "source", |
| 52 | + focus: false, |
| 53 | + }, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function clearTestChoices(data: QuickAddData) { |
| 58 | + data.choices = data.choices.filter( |
| 59 | + (choice) => !String(choice.id ?? "").startsWith("__qa-test-1140-"), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +async function seedTemplate(path: string, content: string) { |
| 64 | + await sandbox.write(path, content, { |
| 65 | + waitForContent: true, |
| 66 | + waitOptions: WAIT_OPTS, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +async function runChoice(name: string, vars: Record<string, unknown>) { |
| 71 | + await obsidian.exec("quickadd:run", { |
| 72 | + choice: name, |
| 73 | + vars: JSON.stringify(vars), |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +async function runChoiceAndWaitForContent( |
| 78 | + name: string, |
| 79 | + vars: Record<string, unknown>, |
| 80 | + file: string, |
| 81 | + expected: string, |
| 82 | +) { |
| 83 | + await runChoice(name, vars); |
| 84 | + return sandbox.waitForContent( |
| 85 | + file, |
| 86 | + (content) => content.includes(expected), |
| 87 | + WAIT_OPTS, |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +beforeAll(async () => { |
| 92 | + obsidian = createObsidianClient({ vault: VAULT }); |
| 93 | + await obsidian.verify(); |
| 94 | + |
| 95 | + lock = await acquireVaultRunLock({ |
| 96 | + vaultName: VAULT, |
| 97 | + vaultPath: await obsidian.vaultPath(), |
| 98 | + }); |
| 99 | + await lock.publishMarker(obsidian); |
| 100 | + |
| 101 | + qa = obsidian.plugin(PLUGIN_ID); |
| 102 | + sandbox = await createSandboxApi({ |
| 103 | + obsidian, |
| 104 | + sandboxRoot: "__obsidian_e2e__", |
| 105 | + testName: "template-property-links", |
| 106 | + }); |
| 107 | +}, 30_000); |
| 108 | + |
| 109 | +afterAll(async () => { |
| 110 | + await qa.restoreData(); |
| 111 | + await qa.reload(); |
| 112 | + await sandbox.cleanup(); |
| 113 | + await clearVaultRunLockMarker(obsidian).catch(() => {}); |
| 114 | + await lock?.release(); |
| 115 | +}, 15_000); |
| 116 | + |
| 117 | +beforeEach((ctx) => { |
| 118 | + ctx.onTestFailed(async () => { |
| 119 | + await captureFailureArtifacts( |
| 120 | + { id: ctx.task.id, name: ctx.task.name }, |
| 121 | + obsidian, |
| 122 | + { plugin: qa, captureOnFailure: true }, |
| 123 | + ); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe("issue 1140: list properties with links", () => { |
| 128 | + beforeAll(async () => { |
| 129 | + const root = sandbox.root; |
| 130 | + const templatePath = sandbox.path("issue-1140-template.md"); |
| 131 | + |
| 132 | + await seedTemplate( |
| 133 | + "issue-1140-template.md", |
| 134 | + [ |
| 135 | + "---", |
| 136 | + "authors: {{VALUE:authors}}", |
| 137 | + "---", |
| 138 | + "", |
| 139 | + ].join("\n"), |
| 140 | + ); |
| 141 | + |
| 142 | + await qa.data<QuickAddData>().patch((data) => { |
| 143 | + clearTestChoices(data); |
| 144 | + data.enableTemplatePropertyTypes = true; |
| 145 | + data.choices.push( |
| 146 | + templateChoice( |
| 147 | + "__qa-test-1140-single-link", |
| 148 | + templatePath, |
| 149 | + `${root}/qa-1140-single-link`, |
| 150 | + ), |
| 151 | + templateChoice( |
| 152 | + "__qa-test-1140-multi-link", |
| 153 | + templatePath, |
| 154 | + `${root}/qa-1140-multi-link`, |
| 155 | + ), |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + await qa.reload({ waitUntilReady: true }); |
| 160 | + }, 15_000); |
| 161 | + |
| 162 | + it("formats a single wikilink list item as a YAML list", async () => { |
| 163 | + const content = await runChoiceAndWaitForContent( |
| 164 | + "__qa-test-1140-single-link", |
| 165 | + { authors: ["[[John Doe]]"] }, |
| 166 | + "qa-1140-single-link.md", |
| 167 | + ' - "[[John Doe]]"', |
| 168 | + ); |
| 169 | + |
| 170 | + expect(content).toContain("authors:"); |
| 171 | + expect(content).toContain(' - "[[John Doe]]"'); |
| 172 | + expect(content).not.toContain("authors: [[John Doe]]"); |
| 173 | + }); |
| 174 | + |
| 175 | + it("formats multiple wikilinks as separate YAML list items", async () => { |
| 176 | + const content = await runChoiceAndWaitForContent( |
| 177 | + "__qa-test-1140-multi-link", |
| 178 | + { authors: ["[[John Doe]]", "[[Jane Doe]]"] }, |
| 179 | + "qa-1140-multi-link.md", |
| 180 | + ' - "[[Jane Doe]]"', |
| 181 | + ); |
| 182 | + |
| 183 | + expect(content).toContain("authors:"); |
| 184 | + expect(content).toContain(' - "[[John Doe]]"'); |
| 185 | + expect(content).toContain(' - "[[Jane Doe]]"'); |
| 186 | + expect(content).not.toContain("authors: [[John Doe]],[[Jane Doe]]"); |
| 187 | + }); |
| 188 | +}); |
0 commit comments