|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { App } from "obsidian"; |
| 3 | +import type { IChoiceExecutor } from "src/IChoiceExecutor"; |
| 4 | +import type IMacroChoice from "src/types/choices/IMacroChoice"; |
| 5 | +import { CommandType } from "src/types/macros/CommandType"; |
| 6 | +import type { IUserScript } from "src/types/macros/IUserScript"; |
| 7 | +import { collectChoiceRequirements } from "./collectChoiceRequirements"; |
| 8 | + |
| 9 | +const { getUserScriptMock } = vi.hoisted(() => ({ |
| 10 | + getUserScriptMock: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("src/utilityObsidian", () => ({ |
| 14 | + getMarkdownFilesInFolder: vi.fn(() => []), |
| 15 | + getMarkdownFilesWithTag: vi.fn(() => []), |
| 16 | + getUserScript: getUserScriptMock, |
| 17 | + isFolder: vi.fn(() => false), |
| 18 | +})); |
| 19 | + |
| 20 | +function createMacroChoice(script: IUserScript): IMacroChoice { |
| 21 | + return { |
| 22 | + id: "macro-choice", |
| 23 | + name: "Macro Choice", |
| 24 | + type: "Macro", |
| 25 | + command: false, |
| 26 | + runOnStartup: false, |
| 27 | + macro: { |
| 28 | + id: "macro-choice", |
| 29 | + name: "Macro Choice", |
| 30 | + commands: [script], |
| 31 | + }, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe("collectChoiceRequirements - macro script metadata", () => { |
| 36 | + const app = {} as App; |
| 37 | + const plugin = {} as any; |
| 38 | + const choiceExecutor: IChoiceExecutor = { |
| 39 | + execute: vi.fn(), |
| 40 | + variables: new Map<string, unknown>(), |
| 41 | + }; |
| 42 | + |
| 43 | + const scriptCommand: IUserScript = { |
| 44 | + id: "script-1", |
| 45 | + name: "Script 1", |
| 46 | + type: CommandType.UserScript, |
| 47 | + path: "script.js", |
| 48 | + settings: {}, |
| 49 | + }; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + getUserScriptMock.mockReset(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("reads quickadd.inputs from function exports", async () => { |
| 56 | + const exported = (() => {}) as ((...args: unknown[]) => unknown) & { |
| 57 | + quickadd?: unknown; |
| 58 | + }; |
| 59 | + exported.quickadd = { |
| 60 | + inputs: [{ id: "project", type: "text", label: "Project" }], |
| 61 | + }; |
| 62 | + getUserScriptMock.mockResolvedValue(exported); |
| 63 | + |
| 64 | + const requirements = await collectChoiceRequirements( |
| 65 | + app, |
| 66 | + plugin, |
| 67 | + choiceExecutor, |
| 68 | + createMacroChoice(scriptCommand), |
| 69 | + ); |
| 70 | + |
| 71 | + expect(requirements).toEqual( |
| 72 | + expect.arrayContaining([ |
| 73 | + expect.objectContaining({ |
| 74 | + id: "project", |
| 75 | + type: "text", |
| 76 | + label: "Project", |
| 77 | + source: "script", |
| 78 | + }), |
| 79 | + ]), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("ignores malformed input entries", async () => { |
| 84 | + const exported = (() => {}) as ((...args: unknown[]) => unknown) & { |
| 85 | + quickadd?: unknown; |
| 86 | + }; |
| 87 | + exported.quickadd = { |
| 88 | + inputs: [{ id: "missingType" }, { type: "text" }, null], |
| 89 | + }; |
| 90 | + getUserScriptMock.mockResolvedValue(exported); |
| 91 | + |
| 92 | + const requirements = await collectChoiceRequirements( |
| 93 | + app, |
| 94 | + plugin, |
| 95 | + choiceExecutor, |
| 96 | + createMacroChoice(scriptCommand), |
| 97 | + ); |
| 98 | + |
| 99 | + expect(requirements).toEqual([]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments