Skip to content

Commit d9a0b50

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

File tree

2 files changed

+124
-5
lines changed

2 files changed

+124
-5
lines changed

src/preflight/collectChoiceRequirements.test.ts

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import { beforeEach, describe, expect, it, vi } from "vitest";
22
import type { App } from "obsidian";
33
import type { IChoiceExecutor } from "src/IChoiceExecutor";
4+
import type ICaptureChoice from "src/types/choices/ICaptureChoice";
45
import type IMacroChoice from "src/types/choices/IMacroChoice";
56
import { CommandType } from "src/types/macros/CommandType";
67
import type { IUserScript } from "src/types/macros/IUserScript";
78
import { collectChoiceRequirements } from "./collectChoiceRequirements";
89

9-
const { getUserScriptMock } = vi.hoisted(() => ({
10+
const {
11+
getMarkdownFilesInFolderMock,
12+
getMarkdownFilesWithTagMock,
13+
getUserScriptMock,
14+
isFolderMock,
15+
} = vi.hoisted(() => ({
16+
getMarkdownFilesInFolderMock: vi.fn(() => []),
17+
getMarkdownFilesWithTagMock: vi.fn(() => []),
1018
getUserScriptMock: vi.fn(),
19+
isFolderMock: vi.fn(() => false),
1120
}));
1221

1322
vi.mock("src/utilityObsidian", () => ({
14-
getMarkdownFilesInFolder: vi.fn(() => []),
15-
getMarkdownFilesWithTag: vi.fn(() => []),
23+
getMarkdownFilesInFolder: getMarkdownFilesInFolderMock,
24+
getMarkdownFilesWithTag: getMarkdownFilesWithTagMock,
1625
getUserScript: getUserScriptMock,
17-
isFolder: vi.fn(() => false),
26+
isFolder: isFolderMock,
1827
}));
1928

2029
function createMacroChoice(script: IUserScript): IMacroChoice {
@@ -32,6 +41,45 @@ function createMacroChoice(script: IUserScript): IMacroChoice {
3241
};
3342
}
3443

44+
function createCaptureChoice(captureTo: string): ICaptureChoice {
45+
return {
46+
id: "capture-choice",
47+
name: "Capture Choice",
48+
type: "Capture",
49+
command: false,
50+
captureTo,
51+
captureToActiveFile: false,
52+
createFileIfItDoesntExist: {
53+
enabled: false,
54+
createWithTemplate: false,
55+
template: "",
56+
},
57+
format: { enabled: false, format: "" },
58+
prepend: false,
59+
appendLink: false,
60+
task: false,
61+
insertAfter: {
62+
enabled: false,
63+
after: "",
64+
insertAtEnd: false,
65+
considerSubsections: false,
66+
createIfNotFound: false,
67+
createIfNotFoundLocation: "",
68+
},
69+
newLineCapture: {
70+
enabled: false,
71+
direction: "below",
72+
},
73+
openFile: false,
74+
fileOpening: {
75+
location: "tab",
76+
direction: "vertical",
77+
mode: "default",
78+
focus: true,
79+
},
80+
};
81+
}
82+
3583
describe("collectChoiceRequirements - macro script metadata", () => {
3684
const app = {} as App;
3785
const plugin = {} as any;
@@ -49,7 +97,13 @@ describe("collectChoiceRequirements - macro script metadata", () => {
4997
};
5098

5199
beforeEach(() => {
100+
getMarkdownFilesInFolderMock.mockReset();
101+
getMarkdownFilesWithTagMock.mockReset();
52102
getUserScriptMock.mockReset();
103+
isFolderMock.mockReset();
104+
getMarkdownFilesInFolderMock.mockReturnValue([]);
105+
getMarkdownFilesWithTagMock.mockReturnValue([]);
106+
isFolderMock.mockReturnValue(false);
53107
});
54108

55109
it("reads quickadd.inputs from function exports", async () => {
@@ -80,6 +134,32 @@ describe("collectChoiceRequirements - macro script metadata", () => {
80134
);
81135
});
82136

137+
it("reads quickadd.inputs from object exports", async () => {
138+
getUserScriptMock.mockResolvedValue({
139+
quickadd: {
140+
inputs: [{ id: "project", type: "text", label: "Project" }],
141+
},
142+
});
143+
144+
const requirements = await collectChoiceRequirements(
145+
app,
146+
plugin,
147+
choiceExecutor,
148+
createMacroChoice(scriptCommand),
149+
);
150+
151+
expect(requirements).toEqual(
152+
expect.arrayContaining([
153+
expect.objectContaining({
154+
id: "project",
155+
type: "text",
156+
label: "Project",
157+
source: "script",
158+
}),
159+
]),
160+
);
161+
});
162+
83163
it("ignores malformed input entries", async () => {
84164
const exported = (() => {}) as ((...args: unknown[]) => unknown) & {
85165
quickadd?: unknown;
@@ -99,3 +179,42 @@ describe("collectChoiceRequirements - macro script metadata", () => {
99179
expect(requirements).toEqual([]);
100180
});
101181
});
182+
183+
describe("collectChoiceRequirements - capture targets", () => {
184+
const app = {} as App;
185+
const plugin = {
186+
settings: {
187+
inputPrompt: "single-line",
188+
globalVariables: {},
189+
useSelectionAsCaptureValue: true,
190+
},
191+
} as any;
192+
const choiceExecutor: IChoiceExecutor = {
193+
execute: vi.fn(),
194+
variables: new Map<string, unknown>(),
195+
};
196+
197+
beforeEach(() => {
198+
getMarkdownFilesInFolderMock.mockReset();
199+
getMarkdownFilesWithTagMock.mockReset();
200+
isFolderMock.mockReset();
201+
getMarkdownFilesInFolderMock.mockReturnValue([]);
202+
getMarkdownFilesWithTagMock.mockReturnValue([]);
203+
});
204+
205+
it("normalizes capture folder paths ending in .md", async () => {
206+
isFolderMock.mockReturnValue(true);
207+
208+
await collectChoiceRequirements(
209+
app,
210+
plugin,
211+
choiceExecutor,
212+
createCaptureChoice("Projects.md"),
213+
);
214+
215+
expect(getMarkdownFilesInFolderMock).toHaveBeenCalledWith(
216+
app,
217+
"Projects/",
218+
);
219+
});
220+
});

src/preflight/collectChoiceRequirements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async function collectForCaptureChoice(
187187
if (isTagTarget) {
188188
files = getMarkdownFilesWithTag(app, normalizedTarget);
189189
} else {
190-
const folder = normalizedTarget.replace(/^\/$|\/\.md$|^\.md$/, "");
190+
const folder = normalizedTarget.replace(/(?:\/|\.md)+$/g, "");
191191
const base =
192192
folder === "" ? "" : folder.endsWith("/") ? folder : `${folder}/`;
193193
files = getMarkdownFilesInFolder(app, base);

0 commit comments

Comments
 (0)