test fix: setup CLS context

claude sez: "I see the issue. The tests are failing because we need to set up the CLS (Continuation Local Storage) context that Trilium uses for transactions. Let me check how this is handled in other tests:

I've updated the test file to:

I've updated the test file to:

Use an in-memory database for testing
Set up necessary database tables in beforeEach
Wrap all database operations in cls.init() and sql.transactional()
Handle async operations properly
This commit is contained in:
matt wilkie 2024-11-16 21:36:07 -07:00
parent e397011437
commit 9cd398d1ce

View File

@ -3,12 +3,20 @@ import importSingle from '../../../src/services/import/single.js';
import importUtils from '../../../src/services/import/utils.js'; import importUtils from '../../../src/services/import/utils.js';
import BNote from '../../../src/becca/entities/bnote.js'; import BNote from '../../../src/becca/entities/bnote.js';
import TaskContext from '../../../src/services/task_context.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', () => { describe('HTML Import', () => {
let parentNote: BNote; let parentNote: BNote;
let taskContext: TaskContext; let taskContext: TaskContext;
beforeAll(() => {
// Set up in-memory database for testing
process.env.TRILIUM_INTEGRATION_TEST = 'memory';
});
beforeEach(() => { beforeEach(() => {
return cls.init(() => {
// Create a mock parent note // Create a mock parent note
parentNote = new BNote({ parentNote = new BNote({
noteId: 'testParent', noteId: 'testParent',
@ -21,6 +29,21 @@ describe('HTML Import', () => {
taskContext = new TaskContext('test', 'test'); taskContext = new TaskContext('test', 'test');
// Set textImportedAsText to true to ensure HTML imports are processed // Set textImportedAsText to true to ensure HTML imports are processed
taskContext.data = { textImportedAsText: true }; 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', () => { describe('extractHtmlTitle', () => {
@ -58,6 +81,8 @@ describe('HTML Import', () => {
describe('importSingleFile with HTML', () => { describe('importSingleFile with HTML', () => {
it('should import HTML file with title from title tag', () => { it('should import HTML file with title from title tag', () => {
return cls.init(() => {
return sql.transactional(() => {
const file = { const file = {
originalname: 'test.html', originalname: 'test.html',
mimetype: 'text/html', mimetype: 'text/html',
@ -77,8 +102,12 @@ describe('HTML Import', () => {
expect(note.title).toBe('HTML Title'); expect(note.title).toBe('HTML Title');
expect(note.mime).toBe('text/html'); expect(note.mime).toBe('text/html');
}); });
});
});
it('should import HTML file with title from h1 when no title tag', () => { it('should import HTML file with title from h1 when no title tag', () => {
return cls.init(() => {
return sql.transactional(() => {
const file = { const file = {
originalname: 'test.html', originalname: 'test.html',
mimetype: 'text/html', mimetype: 'text/html',
@ -96,8 +125,12 @@ describe('HTML Import', () => {
expect(note.title).toBe('Heading Title'); expect(note.title).toBe('Heading Title');
expect(note.mime).toBe('text/html'); expect(note.mime).toBe('text/html');
}); });
});
});
it('should import HTML file with filename as title when no title or h1', () => { it('should import HTML file with filename as title when no title or h1', () => {
return cls.init(() => {
return sql.transactional(() => {
const file = { const file = {
originalname: 'test-document.html', originalname: 'test-document.html',
mimetype: 'text/html', mimetype: 'text/html',
@ -114,8 +147,12 @@ describe('HTML Import', () => {
expect(note.title).toBe('test-document'); expect(note.title).toBe('test-document');
expect(note.mime).toBe('text/html'); expect(note.mime).toBe('text/html');
}); });
});
});
it('should sanitize HTML content during import', () => { it('should sanitize HTML content during import', () => {
return cls.init(() => {
return sql.transactional(() => {
const file = { const file = {
originalname: 'test.html', originalname: 'test.html',
mimetype: 'text/html', mimetype: 'text/html',
@ -140,4 +177,6 @@ describe('HTML Import', () => {
expect(content).toContain('<p>Safe content</p>'); expect(content).toContain('<p>Safe content</p>');
}); });
}); });
});
});
}); });