From 14d7e3e1ce812c50b6e8fdbfa5a96c6306421f2c Mon Sep 17 00:00:00 2001 From: matt wilkie Date: Sat, 16 Nov 2024 21:17:50 -0700 Subject: [PATCH] test: attempt fix test import error by using importSinglefile instead of importHtml --- spec/services/import/single.spec.ts | 138 ++++++++++++++-------------- 1 file changed, 67 insertions(+), 71 deletions(-) diff --git a/spec/services/import/single.spec.ts b/spec/services/import/single.spec.ts index 05e307a6f..a4d3b9131 100644 --- a/spec/services/import/single.spec.ts +++ b/spec/services/import/single.spec.ts @@ -18,6 +18,8 @@ describe('HTML Import', () => { // Create a mock task context taskContext = new TaskContext('test', 'test'); + // Set textImportedAsText to true to ensure HTML imports are processed + taskContext.data = { textImportedAsText: true }; }); describe('extractHtmlTitle', () => { @@ -30,116 +32,110 @@ describe('HTML Import', () => {

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', () => { + it('should return null if no title tag is present', () => { const html = ` - Test/Title: With Special & Characters

Content

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

Content

+

Test content

- `), - mimetype: 'text/html' + + `) }; - const note = importSingle.importHtml(taskContext, file, parentNote); + const note = importSingle.importSingleFile(taskContext, file, parentNote); expect(note.title).toBe('HTML Title'); + expect(note.mime).toBe('text/html'); }); - 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', () => { + it('should import HTML file with title from h1 when no title tag', () => { const file = { originalname: 'test.html', + mimetype: 'text/html', + buffer: Buffer.from(` + + +

Heading Title

+

Test content

+ + + `) + }; + + const note = importSingle.importSingleFile(taskContext, file, parentNote); + expect(note.title).toBe('Heading Title'); + expect(note.mime).toBe('text/html'); + }); + + it('should import HTML file with filename as title when no title or h1', () => { + const file = { + originalname: 'test-document.html', + mimetype: 'text/html', + buffer: Buffer.from(` + + +

Test content without title

+ + + `) + }; + + const note = importSingle.importSingleFile(taskContext, file, parentNote); + expect(note.title).toBe('test-document'); + expect(note.mime).toBe('text/html'); + }); + + it('should sanitize HTML content during import', () => { + const file = { + originalname: 'test.html', + mimetype: 'text/html', buffer: Buffer.from(` - HTML Title + Test Title + -

Different H1 Title

-

Content

+

Safe 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'); + const note = importSingle.importSingleFile(taskContext, file, parentNote); + expect(note.title).toBe('Test Title'); + expect(note.content).not.toContain('