fix issue
This commit is contained in:
parent
c522ff1301
commit
84a82f521a
8 changed files with 124 additions and 47 deletions
|
|
@ -6,9 +6,9 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite build --watch --mode development",
|
"dev": "vite build --watch --mode development",
|
||||||
"build": "bun run tsc && vite build --mode production",
|
"build": "bun run tsc && vite build --mode production",
|
||||||
"typecheck": "tsc -noEmit -skipLibCheck",
|
"typecheck": "tsc -p tsconfig.json -noEmit -skipLibCheck && tsc -p tests/tsconfig.json -noEmit -skipLibCheck",
|
||||||
"test": "bun test",
|
"test": "bun test --preload ./tests/setup.ts",
|
||||||
"test:log": "LOG_TESTS=true bun test",
|
"test:log": "LOG_TESTS=true bun test --preload ./tests/setup.ts",
|
||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"format:check": "prettier --check .",
|
"format:check": "prettier --check .",
|
||||||
"lint": "eslint --max-warnings=0 --no-warn-ignored packages/obsidian/src/**",
|
"lint": "eslint --max-warnings=0 --no-warn-ignored packages/obsidian/src/**",
|
||||||
|
|
|
||||||
|
|
@ -194,6 +194,9 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
);
|
);
|
||||||
loadedSettings.propertyMappingModels = migratedModels.map(m => m.toJSON());
|
loadedSettings.propertyMappingModels = migratedModels.map(m => m.toJSON());
|
||||||
this.settings = loadedSettings;
|
this.settings = loadedSettings;
|
||||||
|
this.mediaTypeManager.updateTemplates(this.settings);
|
||||||
|
this.mediaTypeManager.updateFolders(this.settings);
|
||||||
|
this.dateFormatter.setFormat(this.settings.customDateFormat);
|
||||||
|
|
||||||
const legacyEntries = this.getLegacyApiKeyEntries(diskSettings);
|
const legacyEntries = this.getLegacyApiKeyEntries(diskSettings);
|
||||||
if (legacyEntries.length > 0) {
|
if (legacyEntries.length > 0) {
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,8 @@ export class MediaTypeManager {
|
||||||
|
|
||||||
getFileName(mediaTypeModel: MediaTypeModel): string {
|
getFileName(mediaTypeModel: MediaTypeModel): string {
|
||||||
// Ignore undefined tags since some search APIs do not return all properties in the model and produce clean file names even if errors occur
|
// Ignore undefined tags since some search APIs do not return all properties in the model and produce clean file names even if errors occur
|
||||||
const fileName = replaceTags(this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType())!, mediaTypeModel, true);
|
const template = this.mediaFileNameTemplateMap.get(mediaTypeModel.getMediaType()) ?? '{{ title }}';
|
||||||
|
const fileName = replaceTags(template, mediaTypeModel, true);
|
||||||
return this.cleanFileName(fileName);
|
return this.cleanFileName(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
62
tests/media-type-manager.test.ts
Normal file
62
tests/media-type-manager.test.ts
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
import { expect, test } from 'bun:test';
|
||||||
|
import { MovieModel } from 'packages/obsidian/src/models/MovieModel';
|
||||||
|
import type { MediaDbPluginSettings } from 'packages/obsidian/src/settings/Settings';
|
||||||
|
import { MediaTypeManager } from 'packages/obsidian/src/utils/MediaTypeManager';
|
||||||
|
|
||||||
|
function createSettings(overrides: Partial<MediaDbPluginSettings> = {}): MediaDbPluginSettings {
|
||||||
|
return {
|
||||||
|
movieFileNameTemplate: '{{ title }} ({{ year }})',
|
||||||
|
seriesFileNameTemplate: '',
|
||||||
|
seasonFileNameTemplate: '',
|
||||||
|
mangaFileNameTemplate: '',
|
||||||
|
gameFileNameTemplate: '',
|
||||||
|
wikiFileNameTemplate: '',
|
||||||
|
musicReleaseFileNameTemplate: '',
|
||||||
|
boardgameFileNameTemplate: '',
|
||||||
|
bookFileNameTemplate: '',
|
||||||
|
movieTemplate: '',
|
||||||
|
seriesTemplate: '',
|
||||||
|
seasonTemplate: '',
|
||||||
|
mangaTemplate: '',
|
||||||
|
gameTemplate: '',
|
||||||
|
wikiTemplate: '',
|
||||||
|
musicReleaseTemplate: '',
|
||||||
|
boardgameTemplate: '',
|
||||||
|
bookTemplate: '',
|
||||||
|
...overrides,
|
||||||
|
} as MediaDbPluginSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('getFileName falls back to title when templates have not been initialized', () => {
|
||||||
|
const manager = new MediaTypeManager();
|
||||||
|
const movie = new MovieModel({ title: 'Arrival', year: '2016' });
|
||||||
|
|
||||||
|
expect(manager.getFileName(movie)).toBe('Arrival');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getFileName uses the configured template for the media type', () => {
|
||||||
|
const manager = new MediaTypeManager();
|
||||||
|
const movie = new MovieModel({ title: 'Arrival', year: '2016' });
|
||||||
|
|
||||||
|
manager.updateTemplates(createSettings({ movieFileNameTemplate: '{{ title }} - {{ year }}' }));
|
||||||
|
|
||||||
|
expect(manager.getFileName(movie)).toBe('Arrival - 2016');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getFileName omits undefined template values', () => {
|
||||||
|
const manager = new MediaTypeManager();
|
||||||
|
const movie = new MovieModel({ title: 'Arrival', year: undefined });
|
||||||
|
|
||||||
|
manager.updateTemplates(createSettings({ movieFileNameTemplate: '{{ title }} {{ missingField }} {{ year }}' }));
|
||||||
|
|
||||||
|
expect(manager.getFileName(movie)).toBe('Arrival ');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getFileName sanitizes illegal filename characters and duplicate spaces', () => {
|
||||||
|
const manager = new MediaTypeManager();
|
||||||
|
const movie = new MovieModel({ title: 'Star: Trek / First <Contact>', year: '1996' });
|
||||||
|
|
||||||
|
manager.updateTemplates(createSettings({ movieFileNameTemplate: '{{ title }} {{ year }}' }));
|
||||||
|
|
||||||
|
expect(manager.getFileName(movie)).toBe('Star - Trek - First Contact 1996');
|
||||||
|
});
|
||||||
|
|
@ -1,46 +1,10 @@
|
||||||
import { expect, mock, test } from 'bun:test';
|
import { expect, test } from 'bun:test';
|
||||||
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
import type MediaDbPlugin from 'packages/obsidian/src/main';
|
||||||
import type { ModalLifecycle, ModalSession } from 'packages/obsidian/src/utils/ModalHelper';
|
import { ModalHelper, type ModalLifecycle, type ModalSession } from 'packages/obsidian/src/utils/ModalHelper';
|
||||||
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
import type { MDBError } from 'packages/obsidian/src/utils/MDBError';
|
||||||
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
import { MDBErrorKind } from 'packages/obsidian/src/utils/MDBError';
|
||||||
import { OutcomeStatus } from 'packages/obsidian/src/utils/result';
|
import { OutcomeStatus } from 'packages/obsidian/src/utils/result';
|
||||||
|
|
||||||
mock.module('obsidian', () => ({
|
|
||||||
AbstractInputSuggest: class {},
|
|
||||||
Component: class {
|
|
||||||
load(): void {}
|
|
||||||
unload(): void {}
|
|
||||||
},
|
|
||||||
DropdownComponent: class {},
|
|
||||||
MarkdownRenderer: { render: async (): Promise<void> => {} },
|
|
||||||
MarkdownView: class {},
|
|
||||||
Modal: class {
|
|
||||||
app: unknown;
|
|
||||||
|
|
||||||
constructor(app: unknown) {
|
|
||||||
this.app = app;
|
|
||||||
}
|
|
||||||
|
|
||||||
open(): void {}
|
|
||||||
close(): void {}
|
|
||||||
},
|
|
||||||
Notice: class {},
|
|
||||||
normalizePath: (path: string): string => path,
|
|
||||||
moment: Object.assign((value?: unknown): unknown => value, { locale: (): void => {} }),
|
|
||||||
parseYaml: (): unknown => ({}),
|
|
||||||
Plugin: class {},
|
|
||||||
PluginSettingTab: class {},
|
|
||||||
requestUrl: async (): Promise<unknown> => ({}),
|
|
||||||
SecretComponent: class {},
|
|
||||||
Setting: class {},
|
|
||||||
SettingGroup: class {},
|
|
||||||
stringifyYaml: (value: unknown): string => String(value),
|
|
||||||
TFile: class {},
|
|
||||||
TFolder: class {},
|
|
||||||
TextComponent: class {},
|
|
||||||
ToggleComponent: class {},
|
|
||||||
}));
|
|
||||||
|
|
||||||
class FakeModal implements ModalLifecycle {
|
class FakeModal implements ModalLifecycle {
|
||||||
closeCount = 0;
|
closeCount = 0;
|
||||||
|
|
||||||
|
|
@ -64,13 +28,12 @@ function createModalSession(cancelled: () => boolean): ModalSession<undefined, F
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createHelper(): Promise<import('packages/obsidian/src/utils/ModalHelper').ModalHelper> {
|
function createHelper(): ModalHelper {
|
||||||
const { ModalHelper } = await import('packages/obsidian/src/utils/ModalHelper');
|
|
||||||
return new ModalHelper({} as MediaDbPlugin);
|
return new ModalHelper({} as MediaDbPlugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
test('runModalTask closes the modal after active task success', async () => {
|
test('runModalTask closes the modal after active task success', async () => {
|
||||||
const helper = await createHelper();
|
const helper = createHelper();
|
||||||
const session = createModalSession(() => false);
|
const session = createModalSession(() => false);
|
||||||
|
|
||||||
const outcome = await helper.runModalTask(session, async () => 'done');
|
const outcome = await helper.runModalTask(session, async () => 'done');
|
||||||
|
|
@ -80,7 +43,7 @@ test('runModalTask closes the modal after active task success', async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('runModalTask ignores late success after cancellation', async () => {
|
test('runModalTask ignores late success after cancellation', async () => {
|
||||||
const helper = await createHelper();
|
const helper = createHelper();
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const session = createModalSession(() => cancelled);
|
const session = createModalSession(() => cancelled);
|
||||||
|
|
||||||
|
|
@ -94,7 +57,7 @@ test('runModalTask ignores late success after cancellation', async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('runModalTask ignores late errors after cancellation', async () => {
|
test('runModalTask ignores late errors after cancellation', async () => {
|
||||||
const helper = await createHelper();
|
const helper = createHelper();
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
const session = createModalSession(() => cancelled);
|
const session = createModalSession(() => cancelled);
|
||||||
const fallback: MDBError = { kind: MDBErrorKind.Modal, message: 'Task failed' };
|
const fallback: MDBError = { kind: MDBErrorKind.Modal, message: 'Task failed' };
|
||||||
37
tests/setup.ts
Normal file
37
tests/setup.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { mock } from 'bun:test';
|
||||||
|
|
||||||
|
mock.module('obsidian', () => ({
|
||||||
|
AbstractInputSuggest: class {},
|
||||||
|
Component: class {
|
||||||
|
load(): void {}
|
||||||
|
unload(): void {}
|
||||||
|
},
|
||||||
|
DropdownComponent: class {},
|
||||||
|
MarkdownRenderer: { render: async (): Promise<void> => {} },
|
||||||
|
MarkdownView: class {},
|
||||||
|
Modal: class {
|
||||||
|
app: unknown;
|
||||||
|
|
||||||
|
constructor(app: unknown) {
|
||||||
|
this.app = app;
|
||||||
|
}
|
||||||
|
|
||||||
|
open(): void {}
|
||||||
|
close(): void {}
|
||||||
|
},
|
||||||
|
Notice: class {},
|
||||||
|
normalizePath: (path: string): string => path,
|
||||||
|
moment: Object.assign((value?: unknown): unknown => value, { locale: (): void => {} }),
|
||||||
|
parseYaml: (): unknown => ({}),
|
||||||
|
Plugin: class {},
|
||||||
|
PluginSettingTab: class {},
|
||||||
|
requestUrl: async (): Promise<unknown> => ({}),
|
||||||
|
SecretComponent: class {},
|
||||||
|
Setting: class {},
|
||||||
|
SettingGroup: class {},
|
||||||
|
stringifyYaml: (value: unknown): string => String(value),
|
||||||
|
TFile: class {},
|
||||||
|
TFolder: class {},
|
||||||
|
TextComponent: class {},
|
||||||
|
ToggleComponent: class {},
|
||||||
|
}));
|
||||||
11
tests/tsconfig.json
Normal file
11
tests/tsconfig.json
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"extends": "../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"paths": {
|
||||||
|
"packages/*": ["../packages/*"],
|
||||||
|
"tests/*": ["./*"]
|
||||||
|
},
|
||||||
|
"types": ["vite/client", "bun-types"]
|
||||||
|
},
|
||||||
|
"include": ["**/*.ts"]
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue