|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { App } from "obsidian"; |
| 3 | +import { CaptureChoiceEngine } from "./CaptureChoiceEngine"; |
| 4 | +import type ICaptureChoice from "../types/choices/ICaptureChoice"; |
| 5 | +import type { IChoiceExecutor } from "../IChoiceExecutor"; |
| 6 | + |
| 7 | +const { setUseSelectionAsCaptureValueMock } = vi.hoisted(() => ({ |
| 8 | + setUseSelectionAsCaptureValueMock: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../formatters/captureChoiceFormatter", () => ({ |
| 12 | + CaptureChoiceFormatter: class { |
| 13 | + setLinkToCurrentFileBehavior() {} |
| 14 | + setUseSelectionAsCaptureValue(value: boolean) { |
| 15 | + setUseSelectionAsCaptureValueMock(value); |
| 16 | + } |
| 17 | + setTitle() {} |
| 18 | + setDestinationFile() {} |
| 19 | + setDestinationSourcePath() {} |
| 20 | + async formatContentOnly(content: string) { |
| 21 | + return content; |
| 22 | + } |
| 23 | + async formatContentWithFile() { |
| 24 | + return ""; |
| 25 | + } |
| 26 | + async formatFileName(name: string) { |
| 27 | + return name; |
| 28 | + } |
| 29 | + getAndClearTemplatePropertyVars() { |
| 30 | + return new Map(); |
| 31 | + } |
| 32 | + }, |
| 33 | + setUseSelectionAsCaptureValueMock, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("../utilityObsidian", () => ({ |
| 37 | + appendToCurrentLine: vi.fn(), |
| 38 | + getMarkdownFilesInFolder: vi.fn(() => []), |
| 39 | + getMarkdownFilesWithTag: vi.fn(() => []), |
| 40 | + insertFileLinkToActiveView: vi.fn(), |
| 41 | + insertOnNewLineAbove: vi.fn(), |
| 42 | + insertOnNewLineBelow: vi.fn(), |
| 43 | + isFolder: vi.fn(() => false), |
| 44 | + isTemplaterTriggerOnCreateEnabled: vi.fn(() => false), |
| 45 | + jumpToNextTemplaterCursorIfPossible: vi.fn(), |
| 46 | + openExistingFileTab: vi.fn(() => null), |
| 47 | + openFile: vi.fn(), |
| 48 | + overwriteTemplaterOnce: vi.fn(), |
| 49 | + templaterParseTemplate: vi.fn(async (_app, content) => content), |
| 50 | + waitForTemplaterTriggerOnCreateToComplete: vi.fn(), |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock("three-way-merge", () => ({ |
| 54 | + default: vi.fn(() => ({})), |
| 55 | + __esModule: true, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("src/gui/InputSuggester/inputSuggester", () => ({ |
| 59 | + default: class {}, |
| 60 | +})); |
| 61 | + |
| 62 | +vi.mock("obsidian-dataview", () => ({ |
| 63 | + getAPI: vi.fn(), |
| 64 | +})); |
| 65 | + |
| 66 | +vi.mock("../main", () => ({ |
| 67 | + default: class QuickAddMock {}, |
| 68 | +})); |
| 69 | + |
| 70 | +const createApp = () => |
| 71 | + ({ |
| 72 | + vault: { |
| 73 | + adapter: { |
| 74 | + exists: vi.fn(async () => false), |
| 75 | + }, |
| 76 | + }, |
| 77 | + workspace: { |
| 78 | + getActiveFile: vi.fn(() => null), |
| 79 | + }, |
| 80 | + fileManager: { |
| 81 | + getNewFileParent: vi.fn(() => ({ path: "" })), |
| 82 | + }, |
| 83 | + } as unknown as App); |
| 84 | + |
| 85 | +const createChoice = (overrides: Partial<ICaptureChoice> = {}): ICaptureChoice => ({ |
| 86 | + id: "capture-choice-id", |
| 87 | + name: "Capture Choice", |
| 88 | + type: "Capture", |
| 89 | + command: false, |
| 90 | + captureTo: "Inbox.md", |
| 91 | + captureToActiveFile: false, |
| 92 | + createFileIfItDoesntExist: { |
| 93 | + enabled: false, |
| 94 | + createWithTemplate: false, |
| 95 | + template: "", |
| 96 | + }, |
| 97 | + format: { enabled: false, format: "" }, |
| 98 | + prepend: false, |
| 99 | + appendLink: false, |
| 100 | + task: false, |
| 101 | + insertAfter: { |
| 102 | + enabled: false, |
| 103 | + after: "", |
| 104 | + insertAtEnd: false, |
| 105 | + considerSubsections: false, |
| 106 | + createIfNotFound: false, |
| 107 | + createIfNotFoundLocation: "", |
| 108 | + }, |
| 109 | + newLineCapture: { |
| 110 | + enabled: false, |
| 111 | + direction: "below", |
| 112 | + }, |
| 113 | + openFile: false, |
| 114 | + fileOpening: { |
| 115 | + location: "tab", |
| 116 | + direction: "vertical", |
| 117 | + mode: "default", |
| 118 | + focus: true, |
| 119 | + }, |
| 120 | + ...overrides, |
| 121 | +}); |
| 122 | + |
| 123 | +const createExecutor = (): IChoiceExecutor => ({ |
| 124 | + execute: vi.fn(), |
| 125 | + variables: new Map<string, unknown>(), |
| 126 | +}); |
| 127 | + |
| 128 | +describe("CaptureChoiceEngine selection-as-value resolution", () => { |
| 129 | + beforeEach(() => { |
| 130 | + setUseSelectionAsCaptureValueMock.mockClear(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("uses global setting when no override is set", async () => { |
| 134 | + const engine = new CaptureChoiceEngine( |
| 135 | + createApp(), |
| 136 | + { settings: { useSelectionAsCaptureValue: false } } as any, |
| 137 | + createChoice(), |
| 138 | + createExecutor(), |
| 139 | + ); |
| 140 | + |
| 141 | + await engine.run(); |
| 142 | + |
| 143 | + expect(setUseSelectionAsCaptureValueMock).toHaveBeenCalledWith(false); |
| 144 | + }); |
| 145 | + |
| 146 | + it("uses per-choice override when provided", async () => { |
| 147 | + const engine = new CaptureChoiceEngine( |
| 148 | + createApp(), |
| 149 | + { settings: { useSelectionAsCaptureValue: true } } as any, |
| 150 | + createChoice({ useSelectionAsCaptureValue: false }), |
| 151 | + createExecutor(), |
| 152 | + ); |
| 153 | + |
| 154 | + await engine.run(); |
| 155 | + |
| 156 | + expect(setUseSelectionAsCaptureValueMock).toHaveBeenCalledWith(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it("allows per-choice override to enable selection", async () => { |
| 160 | + const engine = new CaptureChoiceEngine( |
| 161 | + createApp(), |
| 162 | + { settings: { useSelectionAsCaptureValue: false } } as any, |
| 163 | + createChoice({ useSelectionAsCaptureValue: true }), |
| 164 | + createExecutor(), |
| 165 | + ); |
| 166 | + |
| 167 | + await engine.run(); |
| 168 | + |
| 169 | + expect(setUseSelectionAsCaptureValueMock).toHaveBeenCalledWith(true); |
| 170 | + }); |
| 171 | +}); |
0 commit comments