From 3a7564f7332cf0070b4c7dfb9fe66cb141491581 Mon Sep 17 00:00:00 2001 From: maphew Date: Sat, 16 Nov 2024 09:45:13 -0700 Subject: [PATCH] a missed .ts file --- spec/services/import/single.spec.ts | 145 ++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 spec/services/import/single.spec.ts diff --git a/spec/services/import/single.spec.ts b/spec/services/import/single.spec.ts new file mode 100644 index 000000000..05e307a6f --- /dev/null +++ b/spec/services/import/single.spec.ts @@ -0,0 +1,145 @@ +import importSingle from '../../../src/services/import/single.js'; +import importUtils from '../../../src/services/import/utils.js'; +import BNote from '../../../src/becca/entities/bnote.js'; +import TaskContext from '../../../src/services/task_context.js'; + +describe('HTML Import', () => { + let parentNote: BNote; + let taskContext: TaskContext; + + beforeEach(() => { + // Create a mock parent note + parentNote = new BNote({ + noteId: 'testParent', + title: 'Test Parent', + type: 'text', + mime: 'text/html' + }); + + // Create a mock task context + taskContext = new TaskContext('test', 'test'); + }); + + describe('extractHtmlTitle', () => { + it('should extract title from HTML content', () => { + const html = ` + + + Test Title + + +

Content

+ + `; + + const title = importUtils.extractHtmlTitle(html); + expect(title).toBe('Test Title'); + }); + + it('should handle missing title tag', () => { + const html = ` + + + +

Content

+ + `; + + const title = importUtils.extractHtmlTitle(html); + expect(title).toBeNull(); + }); + + it('should handle title with special characters', () => { + const html = ` + + + Test/Title: With Special & Characters + + +

Content

+ + `; + + const title = importUtils.extractHtmlTitle(html); + expect(title).toBe('Test/Title: With Special & Characters'); + }); + }); + + describe('importHtml', () => { + it('should prefer title from HTML over filename', () => { + const file = { + originalname: 'test.html', + buffer: Buffer.from(` + + + HTML Title + + +

Content

+ + `), + mimetype: 'text/html' + }; + + const note = importSingle.importHtml(taskContext, file, parentNote); + expect(note.title).toBe('HTML Title'); + }); + + it('should fall back to filename when no HTML title exists', () => { + const file = { + originalname: 'test_file.html', + buffer: Buffer.from(` + + + +

Content

+ + `), + mimetype: 'text/html' + }; + + const note = importSingle.importHtml(taskContext, file, parentNote); + expect(note.title).toBe('test file'); // assuming replaceUnderscoresWithSpaces is true + }); + + it('should handle HTML with both title and H1', () => { + const file = { + originalname: 'test.html', + buffer: Buffer.from(` + + + HTML Title + + +

Different H1 Title

+

Content

+ + `), + mimetype: 'text/html' + }; + + const note = importSingle.importHtml(taskContext, file, parentNote); + expect(note.title).toBe('HTML Title'); + expect(note.content).toContain('

HTML Title

'); // H1 should be updated to match title + }); + + it('should preserve special characters in title', () => { + const file = { + originalname: 'test.html', + buffer: Buffer.from(` + + + Title/With: Special & Characters + + +

Content

+ + `), + mimetype: 'text/html' + }; + + const note = importSingle.importHtml(taskContext, file, parentNote); + expect(note.title).toBe('Title/With: Special & Characters'); + }); + }); +});