fix(build): tsc --noEmit зелёный, добавлен npm run typecheck
- @types/node@20 в devDependencies (без него TS2688) - skipLibCheck: obsidian.d.ts несовместим с TS 5.9 (HistoryHandler) - настройки модулей: двойной cast Record<string, unknown> -> Settings - legacy Vault API (getConfig, getAvailablePath) вынесен в типизированный интерфейс - тестовые моки типизированы (VaultMock, makeFile) - vitest.config: удалён неподдерживаемый transformMode - main.js пересобран, поведение не менялось
This commit is contained in:
1 parent
d6048e5956
commit
7cc89afbf7
10 files changed
+113
-125
No files matched your search
@@ -27,7 +27,10 @@ vi.mock('obsidian', () => ({
|
||||
// Minimal normalizePath: collapses backslashes, collapses duplicate slashes,
|
||||
// strips leading/trailing slash. Sufficient for the inputs in these tests.
|
||||
normalizePath: (p: string): string =>
|
||||
p.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/^\/|\/$/g, ''),
|
||||
p
|
||||
.replace(/\\/g, '/')
|
||||
.replace(/\/+/g, '/')
|
||||
.replace(/^\/|\/$/g, ''),
|
||||
Plugin: class {},
|
||||
}));
|
||||
|
||||
@@ -38,8 +41,7 @@ import { AttachmentCleanPasteModule } from '../../src/modules/attachment-clean-p
|
||||
function createFile(name: string, content: string = 'data'): File {
|
||||
return {
|
||||
name,
|
||||
arrayBuffer: async () =>
|
||||
new TextEncoder().encode(content).buffer as ArrayBuffer,
|
||||
arrayBuffer: async () => new TextEncoder().encode(content).buffer as ArrayBuffer,
|
||||
} as unknown as File;
|
||||
}
|
||||
|
||||
@@ -58,11 +60,8 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
createBinary: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let pasteHandler:
|
||||
| ((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise<void>)
|
||||
| null;
|
||||
let dropHandler:
|
||||
| ((evt: DragEvent, editor: unknown, info: unknown) => Promise<void>)
|
||||
| null;
|
||||
((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise<void>) | null;
|
||||
let dropHandler: ((evt: DragEvent, editor: unknown, info: unknown) => Promise<void>) | null;
|
||||
let settings: { extensions: string };
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -92,9 +91,7 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
adapter: mockAdapter,
|
||||
createFolder: vi.fn(async () => undefined),
|
||||
// Mirrors the real Obsidian contract: returns "<basePath>.<ext>".
|
||||
getAvailablePath: vi.fn(
|
||||
async (basePath: string, ext: string) => `${basePath}.${ext}`,
|
||||
),
|
||||
getAvailablePath: vi.fn(async (basePath: string, ext: string) => `${basePath}.${ext}`),
|
||||
createBinary: vi.fn(async () => undefined),
|
||||
};
|
||||
|
||||
@@ -109,9 +106,7 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
|
||||
module = new AttachmentCleanPasteModule();
|
||||
module.onload(
|
||||
mockPlugin as unknown as Parameters<
|
||||
AttachmentCleanPasteModule['onload']
|
||||
>[0],
|
||||
mockPlugin as unknown as Parameters<AttachmentCleanPasteModule['onload']>[0],
|
||||
settings,
|
||||
);
|
||||
});
|
||||
@@ -124,10 +119,7 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
): boolean =>
|
||||
(
|
||||
module as unknown as {
|
||||
shouldProcess: (
|
||||
f: string,
|
||||
s: { extensions: string },
|
||||
) => boolean;
|
||||
shouldProcess: (f: string, s: { extensions: string }) => boolean;
|
||||
}
|
||||
).shouldProcess(filename, cfg);
|
||||
|
||||
@@ -182,9 +174,7 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
expect(stopPropagation).toHaveBeenCalledTimes(1);
|
||||
// The link was inserted into the editor.
|
||||
expect(editor.replaceSelection).toHaveBeenCalledTimes(1);
|
||||
expect(editor.replaceSelection).toHaveBeenCalledWith(
|
||||
'[[attachments/image.png|image]]',
|
||||
);
|
||||
expect(editor.replaceSelection).toHaveBeenCalledWith('[[attachments/image.png|image]]');
|
||||
});
|
||||
|
||||
it('НЕ вызывает preventDefault, если ни один файл не подходит по расширению', async () => {
|
||||
@@ -214,19 +204,14 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
|
||||
const result = await (
|
||||
module as unknown as {
|
||||
processFile: (
|
||||
f: File,
|
||||
s: { extensions: string },
|
||||
) => Promise<string | null>;
|
||||
processFile: (f: File, s: { extensions: string }) => Promise<string | null>;
|
||||
}
|
||||
).processFile(file, settings);
|
||||
|
||||
expect(result).toBe('[[attachments/image.png|image]]');
|
||||
expect(mockVault.createBinary).toHaveBeenCalledTimes(1);
|
||||
// The binary payload was passed to the vault.
|
||||
const [, payload] = (
|
||||
mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer]
|
||||
);
|
||||
const [, payload] = mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer];
|
||||
expect(payload).toBeInstanceOf(ArrayBuffer);
|
||||
});
|
||||
|
||||
@@ -235,10 +220,7 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
|
||||
const result = await (
|
||||
module as unknown as {
|
||||
processFile: (
|
||||
f: File,
|
||||
s: { extensions: string },
|
||||
) => Promise<string | null>;
|
||||
processFile: (f: File, s: { extensions: string }) => Promise<string | null>;
|
||||
}
|
||||
).processFile(file, settings);
|
||||
|
||||
@@ -256,21 +238,13 @@ describe('AttachmentCleanPasteModule', () => {
|
||||
|
||||
describe('onload lifecycle', () => {
|
||||
it('регистрирует оба обработчика (editor-paste и editor-drop) в workspace', () => {
|
||||
expect(mockWorkspace.on).toHaveBeenCalledWith(
|
||||
'editor-paste',
|
||||
expect.any(Function),
|
||||
);
|
||||
expect(mockWorkspace.on).toHaveBeenCalledWith(
|
||||
'editor-drop',
|
||||
expect.any(Function),
|
||||
);
|
||||
expect(mockWorkspace.on).toHaveBeenCalledWith('editor-paste', expect.any(Function));
|
||||
expect(mockWorkspace.on).toHaveBeenCalledWith('editor-drop', expect.any(Function));
|
||||
});
|
||||
|
||||
it('onunload отключает оба зарегистрированных обработчика', () => {
|
||||
module.onunload(
|
||||
mockPlugin as unknown as Parameters<
|
||||
AttachmentCleanPasteModule['onunload']
|
||||
>[0],
|
||||
mockPlugin as unknown as Parameters<AttachmentCleanPasteModule['onunload']>[0],
|
||||
);
|
||||
|
||||
expect(mockWorkspace.offref).toHaveBeenCalledTimes(2);
|
||||
|
||||
@@ -7,6 +7,21 @@ interface PreserveLinkAliasesSettings {
|
||||
processCanvas: boolean;
|
||||
}
|
||||
|
||||
// `obsidian` is aliased to __mocks__/obsidian.ts at runtime, but tsc types the
|
||||
// import against the real package. These helpers keep the mock's extra surface
|
||||
// visible without weakening the types of the module under test.
|
||||
type VaultMock = Vault & {
|
||||
mdFiles: TFile[];
|
||||
allFiles: TFile[];
|
||||
storage: Map<TFile, string>;
|
||||
readCalls: TFile[];
|
||||
modifyCalls: Array<{ file: TFile; content: string }>;
|
||||
};
|
||||
|
||||
function makeFile(path: string): TFile {
|
||||
return new (TFile as unknown as new (path: string) => TFile)(path);
|
||||
}
|
||||
|
||||
// TestablePreserveLinkAliasesModule: surface the private members so tests
|
||||
// can invoke processContent / getFilesToProcess / handleRename directly.
|
||||
class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
||||
@@ -17,21 +32,25 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
||||
oldBasename: string,
|
||||
settings: PreserveLinkAliasesSettings,
|
||||
): string {
|
||||
return (this as unknown as {
|
||||
processContent: (
|
||||
c: string,
|
||||
nf: string,
|
||||
nb: string,
|
||||
ob: string,
|
||||
s: PreserveLinkAliasesSettings,
|
||||
) => string;
|
||||
}).processContent(content, newFileName, newBasename, oldBasename, settings);
|
||||
return (
|
||||
this as unknown as {
|
||||
processContent: (
|
||||
c: string,
|
||||
nf: string,
|
||||
nb: string,
|
||||
ob: string,
|
||||
s: PreserveLinkAliasesSettings,
|
||||
) => string;
|
||||
}
|
||||
).processContent(content, newFileName, newBasename, oldBasename, settings);
|
||||
}
|
||||
|
||||
public callGetFilesToProcess(settings: PreserveLinkAliasesSettings): TFile[] {
|
||||
return (this as unknown as {
|
||||
getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[];
|
||||
}).getFilesToProcess(settings);
|
||||
return (
|
||||
this as unknown as {
|
||||
getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[];
|
||||
}
|
||||
).getFilesToProcess(settings);
|
||||
}
|
||||
|
||||
public async callHandleRename(
|
||||
@@ -39,9 +58,15 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
||||
oldPath: string,
|
||||
settings: PreserveLinkAliasesSettings,
|
||||
): Promise<void> {
|
||||
await (this as unknown as {
|
||||
handleRename: (f: TFile, op: string, s: PreserveLinkAliasesSettings) => Promise<void>;
|
||||
}).handleRename(file, oldPath, settings);
|
||||
await (
|
||||
this as unknown as {
|
||||
handleRename: (
|
||||
f: TFile,
|
||||
op: string,
|
||||
s: PreserveLinkAliasesSettings,
|
||||
) => Promise<void>;
|
||||
}
|
||||
).handleRename(file, oldPath, settings);
|
||||
}
|
||||
|
||||
public setVault(vault: Vault): void {
|
||||
@@ -57,12 +82,14 @@ function makeModule(vault?: Vault): TestablePreserveLinkAliasesModule {
|
||||
return module;
|
||||
}
|
||||
|
||||
function makeVault(opts: {
|
||||
mdFiles?: TFile[];
|
||||
allFiles?: TFile[];
|
||||
storage?: Map<TFile, string>;
|
||||
} = {}): Vault {
|
||||
const vault = new Vault();
|
||||
function makeVault(
|
||||
opts: {
|
||||
mdFiles?: TFile[];
|
||||
allFiles?: TFile[];
|
||||
storage?: Map<TFile, string>;
|
||||
} = {},
|
||||
): VaultMock {
|
||||
const vault = new Vault() as VaultMock;
|
||||
vault.mdFiles = opts.mdFiles ?? [];
|
||||
vault.allFiles = opts.allFiles ?? [];
|
||||
if (opts.storage) {
|
||||
@@ -178,10 +205,10 @@ describe('PreserveLinkAliasesModule', () => {
|
||||
// 5. getFilesToProcess включает canvas-файлы при processCanvas=true
|
||||
// 6. getFilesToProcess НЕ включает canvas-файлы при processCanvas=false
|
||||
describe('getFilesToProcess', () => {
|
||||
const mdA = new TFile('notes/a.md');
|
||||
const mdB = new TFile('notes/b.md');
|
||||
const canvasX = new TFile('boards/x.canvas');
|
||||
const pdfF = new TFile('papers/f.pdf');
|
||||
const mdA = makeFile('notes/a.md');
|
||||
const mdB = makeFile('notes/b.md');
|
||||
const canvasX = makeFile('boards/x.canvas');
|
||||
const pdfF = makeFile('papers/f.pdf');
|
||||
|
||||
it('включает canvas-файлы при processCanvas=true', () => {
|
||||
const vault = makeVault({
|
||||
@@ -217,7 +244,7 @@ describe('PreserveLinkAliasesModule', () => {
|
||||
// 7. handleRename не вызывает processContent если oldBasename === newBasename
|
||||
describe('handleRename', () => {
|
||||
it('не вызывает processContent если oldBasename === newBasename', async () => {
|
||||
const file = new TFile('notes/note.md');
|
||||
const file = makeFile('notes/note.md');
|
||||
const vault = makeVault({
|
||||
mdFiles: [file],
|
||||
storage: new Map([[file, 'See [[other.md|note]] here.']]),
|
||||
@@ -233,4 +260,4 @@ describe('PreserveLinkAliasesModule', () => {
|
||||
expect(vault.modifyCalls).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user