diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e794774..70aeae2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -65,7 +65,7 @@ jobs: cache: pnpm - name: Install dependencies run: pnpm install --frozen-lockfile - - name: Build + - name: Typecheck (plugin and tests) and build run: pnpm run build - name: Lint run: pnpm run lint diff --git a/__tests__/kanbanHelper.test.ts b/__tests__/kanbanHelper.test.ts index 02fc9f1..1dc05ab 100644 --- a/__tests__/kanbanHelper.test.ts +++ b/__tests__/kanbanHelper.test.ts @@ -1,6 +1,7 @@ import {afterEach, beforeEach, describe, it, expect, vi} from "vitest"; import {KanbanHelper} from "../src/automators/onFileModifyAutomators/kanbanHelper"; -import {Notice, TFile} from "obsidian"; +import {type TFile} from "obsidian"; +import {createTFile, Notice} from "../tests/obsidian-stub"; import {log} from "../src/logger/logManager"; import {GuiLogger} from "../src/logger/guiLogger"; @@ -9,7 +10,7 @@ type MockFn = ReturnType; vi.spyOn(console, "debug").mockImplementation(() => {}); const resetRegisteredLoggers = () => { - (log.constructor as {loggers: unknown[]}).loggers = []; + (log.constructor as unknown as {loggers: unknown[]}).loggers = []; }; beforeEach(() => { @@ -121,11 +122,11 @@ function setupBoard(board: string, notes: Record, boardName = const app = createApp(); const plugin = createPlugin(app, [{boardName, property: "status"}]); const helper = new KanbanHelper(plugin as any); - const boardFile = new TFile(`${boardName}.md`); + const boardFile = createTFile(`${boardName}.md`); const noteFiles: Record = {}; for (const name of Object.keys(notes)) { - noteFiles[name] = new TFile(notes[name].path ?? `${name}.md`); + noteFiles[name] = createTFile(notes[name].path ?? `${name}.md`); } app.metadataCache.getFileCache.mockReturnValue(buildBoardCache(board)); @@ -160,7 +161,7 @@ describe("KanbanHelper link resolution", () => { const plugin = createPlugin(app); const helper = new KanbanHelper(plugin as any); - const targetFile = new TFile("Folder/My Note.md"); + const targetFile = createTFile("Folder/My Note.md"); app.metadataCache.getFirstLinkpathDest.mockImplementation((linkpath: string) => { if (linkpath === "Folder/My Note") { return targetFile; @@ -183,7 +184,7 @@ describe("KanbanHelper link resolution", () => { // Path resolution is only used for links that already name a folder path: such a // path identifies exactly one file, so the direct lookup is unambiguous. - const targetFile = new TFile("Folder/Note.md"); + const targetFile = createTFile("Folder/Note.md"); app.metadataCache.getFirstLinkpathDest.mockReturnValue(null); app.vault.getAbstractFileByPath.mockImplementation((path: string) => { return path === "Folder/Note.md" ? targetFile : null; @@ -206,8 +207,8 @@ describe("KanbanHelper link resolution", () => { const plugin = createPlugin(app); const helper = new KanbanHelper(plugin as any); - const rootNote = new TFile("Note.md"); - const folderNote = new TFile("Areas/Note.md"); + const rootNote = createTFile("Note.md"); + const folderNote = createTFile("Areas/Note.md"); app.metadataCache.getFirstLinkpathDest.mockReturnValue(null); // The real vault WOULD return the root file here; the fix must not act on it. app.vault.getAbstractFileByPath.mockImplementation((path: string) => @@ -226,7 +227,7 @@ describe("KanbanHelper link resolution", () => { const plugin = createPlugin(app); const helper = new KanbanHelper(plugin as any); - const targetFile = new TFile("Folder/Note.md"); + const targetFile = createTFile("Folder/Note.md"); app.metadataCache.getFirstLinkpathDest.mockReturnValue(null); app.vault.getAbstractFileByPath.mockReturnValue(null); app.vault.getMarkdownFiles.mockReturnValue([targetFile]); @@ -242,8 +243,8 @@ describe("KanbanHelper link resolution", () => { const plugin = createPlugin(app); const helper = new KanbanHelper(plugin as any); - const noteA = new TFile("Areas/Note.md"); - const noteB = new TFile("Archive/Note.md"); + const noteA = createTFile("Areas/Note.md"); + const noteB = createTFile("Archive/Note.md"); app.metadataCache.getFirstLinkpathDest.mockReturnValue(null); app.vault.getAbstractFileByPath.mockReturnValue(null); app.vault.getMarkdownFiles.mockReturnValue([noteA, noteB]); @@ -260,7 +261,7 @@ describe("KanbanHelper link resolution", () => { const plugin = createPlugin(app); const helper = new KanbanHelper(plugin as any); - const unrelatedFile = new TFile("Other/Note.md"); + const unrelatedFile = createTFile("Other/Note.md"); app.metadataCache.getFirstLinkpathDest.mockReturnValue(null); app.vault.getAbstractFileByPath.mockReturnValue(null); app.vault.getMarkdownFiles.mockReturnValue([unrelatedFile]); @@ -501,8 +502,8 @@ describe("KanbanHelper updates linked file properties", () => { const app = createApp(); const plugin = createPlugin(app, [{boardName: "Board", property: "status"}]); const helper = new KanbanHelper(plugin as any); - const boardFile = new TFile("Board.md"); - const noteFile = new TFile("Note.md"); + const boardFile = createTFile("Board.md"); + const noteFile = createTFile("Note.md"); const staleLink = {link: "Note", original: "[[Note]]", position: {start: {line: 2, col: 6}}}; app.metadataCache.getFileCache.mockReturnValue({ @@ -528,9 +529,9 @@ describe("KanbanHelper updates linked file properties", () => { const app = createApp(); const plugin = createPlugin(app, [{boardName: "Board", property: "status"}]); const helper = new KanbanHelper(plugin as any); - const boardFile = new TFile("Board.md"); - const rootTask = new TFile("Task.md"); - const archiveTask = new TFile("Archive/Task.md"); + const boardFile = createTFile("Board.md"); + const rootTask = createTFile("Task.md"); + const archiveTask = createTFile("Archive/Task.md"); const board = "## Doing\n\n- [ ] [[Task]]\n"; app.metadataCache.getFileCache.mockReturnValue(buildBoardCache(board)); @@ -552,8 +553,8 @@ describe("KanbanHelper updates linked file properties", () => { const app = createApp(); const plugin = createPlugin(app, [{boardName: "Board", property: "status"}]); const helper = new KanbanHelper(plugin as any); - const boardFile = new TFile("Board.md"); - const note = new TFile("Areas/Task.md"); + const boardFile = createTFile("Board.md"); + const note = createTFile("Areas/Task.md"); const board = "## Doing\n\n- [ ] [[Task]]\n"; app.metadataCache.getFileCache.mockReturnValue(buildBoardCache(board)); @@ -615,7 +616,7 @@ describe("KanbanHelper updates linked file properties", () => { it("does nothing when the modified file is not a configured board", async () => { const board = "## Doing\n\n- [ ] [[Note]]\n"; const {helper, plugin} = setupBoard(board, {Note: {status: "Backlog"}}); - const otherFile = new TFile("Not A Board.md"); + const otherFile = createTFile("Not A Board.md"); await helper.onFileModify(otherFile); diff --git a/package.json b/package.json index 05f9d47..ab586a7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "scripts": { "dev": "node esbuild.config.mjs", "build": "pnpm run typecheck && node esbuild.config.mjs production", - "typecheck": "tsc --noEmit -p tsconfig.src.json && svelte-check --tsconfig ./tsconfig.src.json --threshold error", + "typecheck": "tsc --noEmit -p tsconfig.src.json && svelte-check --tsconfig ./tsconfig.src.json --threshold error && pnpm run typecheck:tests", + "typecheck:tests": "tsc --noEmit -p tsconfig.json", "lint": "eslint . --max-warnings=0", "test": "vitest run --passWithNoTests", "test:watch": "vitest", diff --git a/src/Modals/GenericPrompt/valueSuggest.test.ts b/src/Modals/GenericPrompt/valueSuggest.test.ts index 59f90a5..c3fa8af 100644 --- a/src/Modals/GenericPrompt/valueSuggest.test.ts +++ b/src/Modals/GenericPrompt/valueSuggest.test.ts @@ -1,5 +1,6 @@ import {describe, expect, it} from "vitest"; -import {TFile} from "obsidian"; +import {type TFile} from "obsidian"; +import {createTFile} from "../../../tests/obsidian-stub"; import {filterSuggestions, getDateInputType, getKnownPropertyNames, getValueSuggestions} from "./valueSuggest"; import {MetaType} from "../../Types/metaType"; @@ -14,7 +15,7 @@ const makeApp = (options: { noTypeManager?: boolean; }) => { const files = options.files ?? {}; - const markdownFiles = Object.keys(files).map(path => new TFile(path)); + const markdownFiles = Object.keys(files).map(path => createTFile(path)); return { vault: { diff --git a/src/Modals/menuFilter.test.ts b/src/Modals/menuFilter.test.ts index a82c39a..9c32734 100644 --- a/src/Modals/menuFilter.test.ts +++ b/src/Modals/menuFilter.test.ts @@ -116,7 +116,7 @@ describe("canStructureEditProperty", () => { }); it("withholds structure edits on nested/virtual YAML rows", () => { - expect(canStructureEditProperty({key: "a.b", content: "v", type: MetaType.YAML, isNested: true})).toBe(false); - expect(canStructureEditProperty({key: "a.b", content: "v", type: MetaType.YAML, isVirtual: true})).toBe(false); + expect(canStructureEditProperty({type: MetaType.YAML, isNested: true})).toBe(false); + expect(canStructureEditProperty({type: MetaType.YAML, isVirtual: true})).toBe(false); }); }); diff --git a/src/Settings/settingsMigration.test.ts b/src/Settings/settingsMigration.test.ts index 4972603..c8e681f 100644 --- a/src/Settings/settingsMigration.test.ts +++ b/src/Settings/settingsMigration.test.ts @@ -36,7 +36,7 @@ describe("mergeSettings", () => { SomeFutureSection: {foo: 1}, }; - const merged = mergeSettings(loaded) as typeof loaded & Record; + const merged: Record = {...mergeSettings(loaded)}; expect(merged.SomeFutureSection).toEqual({foo: 1}); }); diff --git a/src/bulk/bulkMetadataEditor.test.ts b/src/bulk/bulkMetadataEditor.test.ts index d45f907..e42465f 100644 --- a/src/bulk/bulkMetadataEditor.test.ts +++ b/src/bulk/bulkMetadataEditor.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { TFile } from "obsidian"; +import { type TFile } from "obsidian"; +import {createTFile} from "../../tests/obsidian-stub"; // MetaController and BulkMetadataEditor transitively import Svelte-backed modals // (and BulkOptionModal extends obsidian's Modal), which the node test environment @@ -154,7 +155,7 @@ describe("BulkMetadataEditor serialization through the controller write queue", const path = "race-order.md"; const harness = makeHarness({ [path]: "---\ntitle: Note\n---\nbody\n" }); - const result = await runGatedRace(harness, new TFile(path)); + const result = await runGatedRace(harness, createTFile(path)); // The bulk write must NOT have begun while the controller write held the // queue. Pre-fix (direct processFrontMatter, unqueued) this is true and the @@ -168,7 +169,7 @@ describe("BulkMetadataEditor serialization through the controller write queue", const path = "race-loss.md"; const harness = makeHarness({ [path]: "---\ntitle: Note\n---\nbody\n" }); - const result = await runGatedRace(harness, new TFile(path)); + const result = await runGatedRace(harness, createTFile(path)); // Both writes survive: the controller's appended inline field AND the bulk // frontmatter property. Pre-fix the controller's stale snapshot overwrites @@ -181,7 +182,7 @@ describe("BulkMetadataEditor serialization through the controller write queue", it("isolates a failing bulk write and keeps the per-file queue usable for the next write", async () => { const path = "race-error.md"; const harness = makeHarness({ [path]: "---\ntitle: Note\n---\nbody\n" }); - const file = new TFile(path); + const file = createTFile(path); const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); // The queued bulk write rejects. enqueueFileWrite must still propagate the @@ -223,7 +224,7 @@ describe("BulkMetadataEditor reserved-key guard", () => { for (const key of RESERVED) { const path = `reserved-apply-${key}.md`; const harness = makeHarness({ [path]: ORIGINAL }); - const file = new TFile(path); + const file = createTFile(path); // apply() refuses the whole batch once - it rejects rather than returning // a summary that overstates the refusal as N per-note write failures. @@ -243,7 +244,7 @@ describe("BulkMetadataEditor reserved-key guard", () => { // keep "__proto__" from reaching `frontmatter[key] = ...`. const path = "reserved-applyToFile.md"; const harness = makeHarness({ [path]: ORIGINAL }); - const file = new TFile(path); + const file = createTFile(path); await expect( (harness.bulkEditor as any).applyToFile(file, "__proto__", "draft", "skip", false), @@ -262,7 +263,7 @@ describe("BulkMetadataEditor reserved-key guard", () => { (GenericPrompt.Prompt as ReturnType).mockResolvedValueOnce(" __proto__ "); const applySpy = vi.spyOn(harness.bulkEditor, "apply"); - await harness.bulkEditor.run([new TFile(path)], "Folder"); + await harness.bulkEditor.run([createTFile(path)], "Folder"); // Trimmed to "__proto__", refused before the value prompt: apply (and thus // any added/success summary) is never reached, and the note is untouched. @@ -275,7 +276,7 @@ describe("BulkMetadataEditor reserved-key guard", () => { const path = "reserved-normal-key.md"; const harness = makeHarness({ [path]: ORIGINAL }); - const summary = await harness.bulkEditor.apply([new TFile(path)], "status", "draft", "skip"); + const summary = await harness.bulkEditor.apply([createTFile(path)], "status", "draft", "skip"); expect(summary.added).toBe(1); expect(summary.failed).toBe(0); diff --git a/src/metaController.test.ts b/src/metaController.test.ts index 509a119..9ef1de4 100644 --- a/src/metaController.test.ts +++ b/src/metaController.test.ts @@ -1,5 +1,6 @@ import {beforeEach, describe, expect, it, vi} from "vitest"; -import {TFile} from "obsidian"; +import {type TFile} from "obsidian"; +import {createTFile} from "../tests/obsidian-stub"; // The controller transitively imports Svelte-backed modals, which the node test // environment cannot transform. The append writer never touches them, so replace the @@ -32,7 +33,7 @@ const setup = (initial: string) => { }, }; const controller = new MetaController(app as never, {} as never); - return {controller, file: new TFile("note.md"), store, app}; + return {controller, file: createTFile("note.md"), store, app}; }; const callUpdateMultiple = (controller: MetaController, props: Property[], file: TFile) => @@ -362,7 +363,7 @@ describe("MetaController native YAML property editing", () => { }, }; const controller = new MetaController(app as never, plugin as never); - return {controller, file: new TFile("note.md"), fm, processFrontMatter, setType}; + return {controller, file: createTFile("note.md"), fm, processFrontMatter, setType}; }; beforeEach(() => { @@ -562,7 +563,7 @@ describe("MetaController reserved-key guard", () => { }, }; const controller = new MetaController(app as never, plugin as never); - return {controller, file: new TFile("note.md"), fm, processFrontMatter, vaultModify}; + return {controller, file: createTFile("note.md"), fm, processFrontMatter, vaultModify}; }; beforeEach(() => { @@ -676,7 +677,7 @@ describe("MetaController.createNativeYamlProperty (fluid creation write path)", }; const plugin = {settings: {EditMode: {mode, properties: [] as string[]}, AutoProperties: {enabled: false, properties: []}}}; const controller = new MetaController(app as never, plugin as never); - return {controller, file: new TFile("note.md"), fm, processFrontMatter}; + return {controller, file: createTFile("note.md"), fm, processFrontMatter}; }; beforeEach(() => { diff --git a/src/parser.test.ts b/src/parser.test.ts index 7a78f57..d0f7167 100644 --- a/src/parser.test.ts +++ b/src/parser.test.ts @@ -1,6 +1,6 @@ import {describe, expect, it, vi} from "vitest"; import * as obsidian from "obsidian"; -import {TFile} from "obsidian"; +import {createTFile} from "../tests/obsidian-stub"; import MetaEditParser from "./parser"; import {MetaType} from "./Types/metaType"; @@ -29,7 +29,7 @@ const replaceInline = (line: string, key: string, value: string): string => describe("MetaEditParser frontmatter parsing", () => { it("reads frontmatter from live file content when cache positions are stale", async () => { - const file = new TFile("legacy.md"); + const file = createTFile("legacy.md"); const parser = createParser( { frontmatter: { @@ -52,7 +52,7 @@ describe("MetaEditParser frontmatter parsing", () => { // getFrontMatterInfo + parseYaml read path (the cache-fallback path is // covered below). it("reads a YAML array via the live frontmatter path as a real array", async () => { - const file = new TFile("tags-list.md"); + const file = createTFile("tags-list.md"); const parser = createParser( { frontmatter: {tags: ["state/inprogress", "course/x"]}, @@ -72,7 +72,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("reads frontmatter with CRLF line endings", async () => { - const file = new TFile("crlf-frontmatter.md"); + const file = createTFile("crlf-frontmatter.md"); const parser = createParser(null, "---\r\nstatus: draft\r\n---\r\nfoo:: bar\r\n"); await expect(parser.parseFrontmatter(file)).resolves.toEqual([ @@ -84,7 +84,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("keeps the legacy dot frontmatter closer compatible", async () => { - const file = new TFile("dot-frontmatter.md"); + const file = createTFile("dot-frontmatter.md"); const parser = createParser(null, "---\nstatus: draft\n...\nfoo:: bar\n"); await expect(parser.parseFrontmatter(file)).resolves.toEqual([ @@ -96,7 +96,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("prefers a legacy dot frontmatter closer before a later thematic break", async () => { - const file = new TFile("dot-frontmatter-later-break.md"); + const file = createTFile("dot-frontmatter-later-break.md"); const parser = createParser(null, "---\nstatus: draft\n...\nfoo:: bar\n---\nbar:: baz\n"); await expect(parser.parseFrontmatter(file)).resolves.toEqual([ @@ -109,7 +109,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("falls back to cached frontmatter entries when no position metadata exists", async () => { - const file = new TFile("cache-only.md"); + const file = createTFile("cache-only.md"); const parser = createParser( { frontmatter: { @@ -127,7 +127,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("reads live frontmatter content when the metadata cache has not populated yet", async () => { - const file = new TFile("live-only.md"); + const file = createTFile("live-only.md"); const parser = createParser(null, "---\nstatus: draft\n---\nBody\n"); await expect(parser.parseFrontmatter(file)).resolves.toEqual([ @@ -136,7 +136,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); it("keeps root YAML rows and adds virtual nested scalar leaf rows", async () => { - const file = new TFile("nested.md"); + const file = createTFile("nested.md"); const parser = createParser( { frontmatter: { @@ -234,10 +234,10 @@ describe("MetaEditParser frontmatter parsing", () => { // with `for..in`, which would surface string/array indices like `0:value`. it("ignores non-mapping frontmatter instead of emitting index keys (#85)", async () => { const stringFm = createParser({frontmatter: "tags:someValue"}, ""); - await expect(stringFm.parseFrontmatter(new TFile("string-fm.md"))).resolves.toEqual([]); + await expect(stringFm.parseFrontmatter(createTFile("string-fm.md"))).resolves.toEqual([]); const arrayFm = createParser({frontmatter: ["item1", "item2"]}, ""); - await expect(arrayFm.parseFrontmatter(new TFile("array-fm.md"))).resolves.toEqual([]); + await expect(arrayFm.parseFrontmatter(createTFile("array-fm.md"))).resolves.toEqual([]); }); // #130 follow-up: a note opening with `---` but holding malformed YAML gives @@ -252,7 +252,7 @@ describe("MetaEditParser frontmatter parsing", () => { }); try { const parser = createParser(null, "---\nstatus: : :\n---\nfoo:: bar\n"); - await expect(parser.parseFrontmatter(new TFile("malformed-fm.md"))).resolves.toEqual([]); + await expect(parser.parseFrontmatter(createTFile("malformed-fm.md"))).resolves.toEqual([]); expect(spy).toHaveBeenCalled(); } finally { spy.mockRestore(); @@ -272,7 +272,7 @@ describe("MetaEditParser frontmatter parsing", () => { {frontmatter: {status: "cached"}}, "---\nstatus: : :\n---\nfoo:: bar\n", ); - await expect(parser.parseFrontmatter(new TFile("malformed-with-cache.md"))).resolves.toEqual([ + await expect(parser.parseFrontmatter(createTFile("malformed-with-cache.md"))).resolves.toEqual([ {key: "status", content: "cached", type: MetaType.YAML}, ]); } finally { @@ -285,14 +285,14 @@ describe("MetaEditParser frontmatter parsing", () => { // never calls parseYaml, so the permissive stub is sufficient here. it("still surfaces inline fields when the frontmatter is malformed", async () => { const parser = createParser(null, "---\nstatus: : :\n---\nfoo:: bar\n"); - await expect(parser.parseInlineFields(new TFile("malformed-fm.md"))).resolves.toEqual([ + await expect(parser.parseInlineFields(createTFile("malformed-fm.md"))).resolves.toEqual([ {key: "foo", content: "bar", type: MetaType.Dataview}, ]); }); it("still surfaces inline fields when the malformed frontmatter uses CRLF", async () => { const parser = createParser(null, "---\r\nstatus: : :\r\n---\r\nfoo:: bar\r\n"); - await expect(parser.parseInlineFields(new TFile("malformed-crlf.md"))).resolves.toEqual([ + await expect(parser.parseInlineFields(createTFile("malformed-crlf.md"))).resolves.toEqual([ {key: "foo", content: "bar", type: MetaType.Dataview}, ]); }); @@ -300,7 +300,7 @@ describe("MetaEditParser frontmatter parsing", () => { describe("MetaEditParser inline field parsing", () => { it("parses exact inline keys that contain regex metacharacters", async () => { - const file = new TFile("inline.md"); + const file = createTFile("inline.md"); const parser = createParser( {}, "progress (%):: old\n[[my-key:: value]]\n(status:: complete)\n", diff --git a/src/tests/uniqueQueue.test.ts b/src/tests/uniqueQueue.test.ts index 356d5e6..d5f0c35 100644 --- a/src/tests/uniqueQueue.test.ts +++ b/src/tests/uniqueQueue.test.ts @@ -1,7 +1,6 @@ +import {expect, test} from "vitest"; import {UniqueQueue} from "../uniqueQueue"; -export {} - test("UniqueQueue_enqueue_isEmptyFalseOnAdd", () => { const queue = new UniqueQueue(); queue.enqueue(1); diff --git a/tests/obsidian-stub.ts b/tests/obsidian-stub.ts index a43762d..9db7476 100644 --- a/tests/obsidian-stub.ts +++ b/tests/obsidian-stub.ts @@ -1,3 +1,5 @@ +import type * as obsidian from "obsidian"; + export class TFile { path: string; name: string; @@ -20,6 +22,12 @@ export class TFile { } } +// Typechecking resolves "obsidian" to the real API, which has no public TFile +// constructor, so specs build files through this instead of `new TFile(path)`. +export function createTFile(path: string): obsidian.TFile { + return new TFile(path) as unknown as obsidian.TFile; +} + export class Notice { static messages: string[] = []; diff --git a/tsconfig.json b/tsconfig.json index b98635c..85ada94 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src/**/*", "__tests__/**/*", "tests/**/*", "vitest.config.mts"], + "include": ["src/**/*", "__tests__/**/*", "tests/**/*", "scripts/**/*.ts", "vitest.config.mts", "vitest.e2e.config.mts"], "exclude": ["node_modules/*"], "compilerOptions": { "module": "ESNext",