|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { App, TFile } from "obsidian"; |
| 3 | +import type { IChoiceExecutor } from "./IChoiceExecutor"; |
| 4 | +import type QuickAdd from "./main"; |
| 5 | +import { QuickAddApi } from "./quickAddApi"; |
| 6 | + |
| 7 | +vi.mock("./quickAddSettingsTab", () => ({ |
| 8 | + DEFAULT_SETTINGS: {}, |
| 9 | + QuickAddSettingsTab: class {}, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("./formatters/completeFormatter", () => ({ |
| 13 | + CompleteFormatter: class CompleteFormatterMock {}, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("obsidian-dataview", () => ({ |
| 17 | + getAPI: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +const INLINE_CONTENT = ` |
| 21 | +Id:: 343434 |
| 22 | +
|
| 23 | +\`\`\`ad-note |
| 24 | +Id:: 121212 |
| 25 | +\`\`\` |
| 26 | +
|
| 27 | +\`\`\`js |
| 28 | +Id:: 999999 |
| 29 | +\`\`\` |
| 30 | +`; |
| 31 | + |
| 32 | +function createApp(content: string): App { |
| 33 | + const file = { path: "QuickAdd-Issue-998/repro.md" } as TFile; |
| 34 | + return { |
| 35 | + vault: { |
| 36 | + getMarkdownFiles: () => [file], |
| 37 | + read: vi.fn(async () => content), |
| 38 | + }, |
| 39 | + metadataCache: { |
| 40 | + getFileCache: vi.fn(() => ({ frontmatter: {} })), |
| 41 | + }, |
| 42 | + } as unknown as App; |
| 43 | +} |
| 44 | + |
| 45 | +describe("QuickAddApi.fieldSuggestions.getFieldValues", () => { |
| 46 | + let variables: Map<string, unknown>; |
| 47 | + let choiceExecutor: IChoiceExecutor; |
| 48 | + let plugin: QuickAdd; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + variables = new Map<string, unknown>(); |
| 52 | + choiceExecutor = { |
| 53 | + execute: vi.fn(), |
| 54 | + variables, |
| 55 | + } as unknown as IChoiceExecutor; |
| 56 | + plugin = {} as QuickAdd; |
| 57 | + }); |
| 58 | + |
| 59 | + it("keeps code-block values excluded by default when includeInline is true", async () => { |
| 60 | + const app = createApp(INLINE_CONTENT); |
| 61 | + const api = QuickAddApi.GetApi(app, plugin, choiceExecutor); |
| 62 | + |
| 63 | + const result = await api.fieldSuggestions.getFieldValues("Id", { |
| 64 | + includeInline: true, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(result).toEqual(["343434"]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("includes allowlisted code-block values when includeInlineCodeBlocks is provided", async () => { |
| 71 | + const app = createApp(INLINE_CONTENT); |
| 72 | + const api = QuickAddApi.GetApi(app, plugin, choiceExecutor); |
| 73 | + |
| 74 | + const result = await api.fieldSuggestions.getFieldValues("Id", { |
| 75 | + includeInline: true, |
| 76 | + includeInlineCodeBlocks: ["ad-note"], |
| 77 | + }); |
| 78 | + |
| 79 | + expect(result).toEqual(["121212", "343434"]); |
| 80 | + }); |
| 81 | + |
| 82 | + it("does not scan inline values when includeInline is false", async () => { |
| 83 | + const app = createApp(INLINE_CONTENT); |
| 84 | + const api = QuickAddApi.GetApi(app, plugin, choiceExecutor); |
| 85 | + |
| 86 | + const result = await api.fieldSuggestions.getFieldValues("Id", { |
| 87 | + includeInline: false, |
| 88 | + includeInlineCodeBlocks: ["ad-note"], |
| 89 | + }); |
| 90 | + |
| 91 | + expect(result).toEqual([]); |
| 92 | + }); |
| 93 | +}); |
0 commit comments