Skip to content

Commit f4d6a71

Browse files
committed
codex: address PR review feedback (#1129)
1 parent 6935b29 commit f4d6a71

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
});

src/preflight/collectChoiceRequirements.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ function toFieldRequirement(input: unknown): FieldRequirement | null {
7878

7979
function getQuickAddScriptInputs(userScript: unknown): unknown[] {
8080
const readInputs = (value: unknown): unknown[] => {
81-
if (!value || typeof value !== "object") return [];
81+
if (
82+
!value ||
83+
(typeof value !== "object" && typeof value !== "function")
84+
) {
85+
return [];
86+
}
8287
const quickadd = (value as { quickadd?: unknown }).quickadd;
8388
if (!quickadd || typeof quickadd !== "object") return [];
8489
const inputs = (quickadd as { inputs?: unknown }).inputs;

0 commit comments

Comments
 (0)