diff --git a/spec/services/import/single.spec.ts b/spec/services/import/single.spec.ts index d30bfb630..34131220e 100644 --- a/spec/services/import/single.spec.ts +++ b/spec/services/import/single.spec.ts @@ -3,24 +3,47 @@ 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'; +import sql from '../../../src/services/sql.js'; +import cls from '../../../src/services/cls.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' - }); + beforeAll(() => { + // Set up in-memory database for testing + process.env.TRILIUM_INTEGRATION_TEST = 'memory'; + }); - // Create a mock task context - taskContext = new TaskContext('test', 'test'); - // Set textImportedAsText to true to ensure HTML imports are processed - taskContext.data = { textImportedAsText: true }; + beforeEach(() => { + return cls.init(() => { + // 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'); + // Set textImportedAsText to true to ensure HTML imports are processed + taskContext.data = { textImportedAsText: true }; + + // Wrap in transaction to handle database operations + return sql.transactional(() => { + sql.execute('CREATE TABLE IF NOT EXISTS notes (noteId TEXT PRIMARY KEY, title TEXT, type TEXT, mime TEXT, isProtected INTEGER DEFAULT 0)'); + sql.execute('CREATE TABLE IF NOT EXISTS note_contents (noteId TEXT PRIMARY KEY, content TEXT, hash TEXT, utcDateModified TEXT)'); + + // Insert parent note + sql.insert('notes', { + noteId: parentNote.noteId, + title: parentNote.title, + type: parentNote.type, + mime: parentNote.mime + }); + }); + }); }); describe('extractHtmlTitle', () => { @@ -58,86 +81,102 @@ describe('HTML Import', () => { 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 - - -

Test content

- - - `) - }; + return cls.init(() => { + return sql.transactional(() => { + const file = { + originalname: 'test.html', + mimetype: 'text/html', + buffer: Buffer.from(` + + + HTML Title + + +

Test content

+ + + `) + }; - const note = importSingle.importSingleFile(taskContext, file, parentNote); - expect(note.title).toBe('HTML Title'); - expect(note.mime).toBe('text/html'); + const note = importSingle.importSingleFile(taskContext, file, parentNote); + expect(note.title).toBe('HTML Title'); + expect(note.mime).toBe('text/html'); + }); + }); }); 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

- - - `) - }; + return cls.init(() => { + return sql.transactional(() => { + 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'); + 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

- - - `) - }; + return cls.init(() => { + return sql.transactional(() => { + 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'); + 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(` - - - Test Title - - - -

Safe content

- - - - `) - }; + return cls.init(() => { + return sql.transactional(() => { + const file = { + originalname: 'test.html', + mimetype: 'text/html', + buffer: Buffer.from(` + + + Test Title + + + +

Safe content

+ + + + `) + }; - const note = importSingle.importSingleFile(taskContext, file, parentNote); - expect(note.title).toBe('Test Title'); - const content = note.getContent(); - expect(content).not.toContain('