mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-29 19:12:27 +08:00
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:
parent
e397011437
commit
9cd398d1ce
@ -3,24 +3,47 @@ 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;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeAll(() => {
|
||||||
// Create a mock parent note
|
// Set up in-memory database for testing
|
||||||
parentNote = new BNote({
|
process.env.TRILIUM_INTEGRATION_TEST = 'memory';
|
||||||
noteId: 'testParent',
|
});
|
||||||
title: 'Test Parent',
|
|
||||||
type: 'text',
|
|
||||||
mime: 'text/html'
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create a mock task context
|
beforeEach(() => {
|
||||||
taskContext = new TaskContext('test', 'test');
|
return cls.init(() => {
|
||||||
// Set textImportedAsText to true to ensure HTML imports are processed
|
// Create a mock parent note
|
||||||
taskContext.data = { textImportedAsText: true };
|
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', () => {
|
describe('extractHtmlTitle', () => {
|
||||||
@ -58,86 +81,102 @@ 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', () => {
|
||||||
const file = {
|
return cls.init(() => {
|
||||||
originalname: 'test.html',
|
return sql.transactional(() => {
|
||||||
mimetype: 'text/html',
|
const file = {
|
||||||
buffer: Buffer.from(`
|
originalname: 'test.html',
|
||||||
<html>
|
mimetype: 'text/html',
|
||||||
<head>
|
buffer: Buffer.from(`
|
||||||
<title>HTML Title</title>
|
<html>
|
||||||
</head>
|
<head>
|
||||||
<body>
|
<title>HTML Title</title>
|
||||||
<p>Test content</p>
|
</head>
|
||||||
</body>
|
<body>
|
||||||
</html>
|
<p>Test content</p>
|
||||||
`)
|
</body>
|
||||||
};
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
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', () => {
|
||||||
const file = {
|
return cls.init(() => {
|
||||||
originalname: 'test.html',
|
return sql.transactional(() => {
|
||||||
mimetype: 'text/html',
|
const file = {
|
||||||
buffer: Buffer.from(`
|
originalname: 'test.html',
|
||||||
<html>
|
mimetype: 'text/html',
|
||||||
<body>
|
buffer: Buffer.from(`
|
||||||
<h1>Heading Title</h1>
|
<html>
|
||||||
<p>Test content</p>
|
<body>
|
||||||
</body>
|
<h1>Heading Title</h1>
|
||||||
</html>
|
<p>Test content</p>
|
||||||
`)
|
</body>
|
||||||
};
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
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', () => {
|
||||||
const file = {
|
return cls.init(() => {
|
||||||
originalname: 'test-document.html',
|
return sql.transactional(() => {
|
||||||
mimetype: 'text/html',
|
const file = {
|
||||||
buffer: Buffer.from(`
|
originalname: 'test-document.html',
|
||||||
<html>
|
mimetype: 'text/html',
|
||||||
<body>
|
buffer: Buffer.from(`
|
||||||
<p>Test content without title</p>
|
<html>
|
||||||
</body>
|
<body>
|
||||||
</html>
|
<p>Test content without title</p>
|
||||||
`)
|
</body>
|
||||||
};
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
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', () => {
|
||||||
const file = {
|
return cls.init(() => {
|
||||||
originalname: 'test.html',
|
return sql.transactional(() => {
|
||||||
mimetype: 'text/html',
|
const file = {
|
||||||
buffer: Buffer.from(`
|
originalname: 'test.html',
|
||||||
<html>
|
mimetype: 'text/html',
|
||||||
<head>
|
buffer: Buffer.from(`
|
||||||
<title>Test Title</title>
|
<html>
|
||||||
<script>alert('xss');</script>
|
<head>
|
||||||
</head>
|
<title>Test Title</title>
|
||||||
<body>
|
<script>alert('xss');</script>
|
||||||
<p>Safe content</p>
|
</head>
|
||||||
<script>alert('xss');</script>
|
<body>
|
||||||
</body>
|
<p>Safe content</p>
|
||||||
</html>
|
<script>alert('xss');</script>
|
||||||
`)
|
</body>
|
||||||
};
|
</html>
|
||||||
|
`)
|
||||||
|
};
|
||||||
|
|
||||||
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
const note = importSingle.importSingleFile(taskContext, file, parentNote);
|
||||||
expect(note.title).toBe('Test Title');
|
expect(note.title).toBe('Test Title');
|
||||||
const content = note.getContent();
|
const content = note.getContent();
|
||||||
expect(content).not.toContain('<script>');
|
expect(content).not.toContain('<script>');
|
||||||
expect(content).toContain('<p>Safe content</p>');
|
expect(content).toContain('<p>Safe content</p>');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user