Files
n8n-mcp/tests/integration/database/node-fts5-search.test.ts
Romuald Członkowski 7300957d13 chore: update n8n to v1.116.2 (#348)
* docs: Update CLAUDE.md with development notes

* chore: update n8n to v1.116.2

- Updated n8n from 1.115.2 to 1.116.2
- Updated n8n-core from 1.114.0 to 1.115.1
- Updated n8n-workflow from 1.112.0 to 1.113.0
- Updated @n8n/n8n-nodes-langchain from 1.114.1 to 1.115.1
- Rebuilt node database with 542 nodes
- Updated version to 2.20.7
- Updated n8n version badge in README
- All changes will be validated in CI with full test suite

Conceived by Romuald Członkowski - www.aiadvisors.pl/en

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: regenerate package-lock.json to sync with updated dependencies

Fixes CI failure caused by package-lock.json being out of sync with
the updated n8n dependencies.

- Regenerated with npm install to ensure all dependency versions match
- Resolves "npm ci" sync errors in CI pipeline

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: align FTS5 tests with production boosting logic

Tests were failing because they used raw FTS5 ranking instead of the
exact-match boosting logic that production uses. Updated both test files
to replicate production search behavior from src/mcp/server.ts.

- Updated node-fts5-search.test.ts to use production boosting
- Updated database-population.test.ts to use production boosting
- Both tests now use JOIN + CASE statement for exact-match prioritization

This makes tests more accurate and less brittle to FTS5 ranking changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: prioritize exact matches in FTS5 search with case-insensitive comparison

Root cause: SQL ORDER BY was sorting by FTS5 rank first, then CASE statement.
Since ranks are unique, the CASE boosting never applied. Additionally, the
CASE statement used case-sensitive comparison which failed to match nodes
like "Webhook" when searching for "webhook".

Changes:
- Changed ORDER BY from "rank, CASE" to "CASE, rank" in production code
- Added LOWER() for case-insensitive exact match detection
- Updated both test files to match the corrected SQL logic
- Exact matches now consistently rank first regardless of FTS5 score

Impact:
- Improves search quality by ensuring exact matches appear first
- More efficient SQL (less JavaScript sorting needed)
- Tests now accurately validate production search behavior
- Fixes 2/705 failing integration tests

Verified:
- Both tests pass locally after fix
- SQL query tested with SQLite CLI showing webhook ranks 1st

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: update CHANGELOG with FTS5 search fix details

Added comprehensive documentation for the FTS5 search ranking bug fix:
- Problem description with SQL examples showing wrong ORDER BY
- Root cause analysis explaining why CASE statement never applied
- Case-sensitivity issue details
- Complete fix description for production code and tests
- Impact section covering search quality, performance, and testing
- Verified search results showing exact matches ranking first

This documents the critical bug fix that ensures exact matches
appear first in search results (webhook, http, code, etc.) with
case-insensitive matching.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 10:28:32 +02:00

230 lines
7.6 KiB
TypeScript

/**
* Integration tests for node FTS5 search functionality
* Ensures the production search failures (Issue #296) are prevented
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { createDatabaseAdapter } from '../../../src/database/database-adapter';
import { NodeRepository } from '../../../src/database/node-repository';
import * as fs from 'fs';
import * as path from 'path';
describe('Node FTS5 Search Integration Tests', () => {
let db: any;
let repository: NodeRepository;
beforeAll(async () => {
// Use test database
const testDbPath = './data/nodes.db';
db = await createDatabaseAdapter(testDbPath);
repository = new NodeRepository(db);
});
afterAll(() => {
if (db) {
db.close();
}
});
describe('FTS5 Table Existence', () => {
it('should have nodes_fts table in schema', () => {
const schemaPath = path.join(__dirname, '../../../src/database/schema.sql');
const schema = fs.readFileSync(schemaPath, 'utf-8');
expect(schema).toContain('CREATE VIRTUAL TABLE IF NOT EXISTS nodes_fts USING fts5');
expect(schema).toContain('CREATE TRIGGER IF NOT EXISTS nodes_fts_insert');
expect(schema).toContain('CREATE TRIGGER IF NOT EXISTS nodes_fts_update');
expect(schema).toContain('CREATE TRIGGER IF NOT EXISTS nodes_fts_delete');
});
it('should have nodes_fts table in database', () => {
const result = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='table' AND name='nodes_fts'
`).get();
expect(result).toBeDefined();
expect(result.name).toBe('nodes_fts');
});
it('should have FTS5 triggers in database', () => {
const triggers = db.prepare(`
SELECT name FROM sqlite_master
WHERE type='trigger' AND name LIKE 'nodes_fts_%'
`).all();
expect(triggers).toHaveLength(3);
const triggerNames = triggers.map((t: any) => t.name);
expect(triggerNames).toContain('nodes_fts_insert');
expect(triggerNames).toContain('nodes_fts_update');
expect(triggerNames).toContain('nodes_fts_delete');
});
});
describe('FTS5 Index Population', () => {
it('should have nodes_fts count matching nodes count', () => {
const nodesCount = db.prepare('SELECT COUNT(*) as count FROM nodes').get();
const ftsCount = db.prepare('SELECT COUNT(*) as count FROM nodes_fts').get();
expect(nodesCount.count).toBeGreaterThan(500); // Should have both packages
expect(ftsCount.count).toBe(nodesCount.count);
});
it('should not have empty FTS5 index', () => {
const ftsCount = db.prepare('SELECT COUNT(*) as count FROM nodes_fts').get();
expect(ftsCount.count).toBeGreaterThan(0);
});
});
describe('Critical Node Searches (Production Failure Cases)', () => {
it('should find webhook node via FTS5', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'webhook'
`).all();
expect(results.length).toBeGreaterThan(0);
const nodeTypes = results.map((r: any) => r.node_type);
expect(nodeTypes).toContain('nodes-base.webhook');
});
it('should find merge node via FTS5', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'merge'
`).all();
expect(results.length).toBeGreaterThan(0);
const nodeTypes = results.map((r: any) => r.node_type);
expect(nodeTypes).toContain('nodes-base.merge');
});
it('should find split batch node via FTS5', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'split OR batch'
`).all();
expect(results.length).toBeGreaterThan(0);
const nodeTypes = results.map((r: any) => r.node_type);
expect(nodeTypes).toContain('nodes-base.splitInBatches');
});
it('should find code node via FTS5', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'code'
`).all();
expect(results.length).toBeGreaterThan(0);
const nodeTypes = results.map((r: any) => r.node_type);
expect(nodeTypes).toContain('nodes-base.code');
});
it('should find http request node via FTS5', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'http OR request'
`).all();
expect(results.length).toBeGreaterThan(0);
const nodeTypes = results.map((r: any) => r.node_type);
expect(nodeTypes).toContain('nodes-base.httpRequest');
});
});
describe('FTS5 Search Quality', () => {
it('should rank exact matches higher', () => {
const results = db.prepare(`
SELECT
n.node_type,
rank
FROM nodes n
JOIN nodes_fts ON n.rowid = nodes_fts.rowid
WHERE nodes_fts MATCH 'webhook'
ORDER BY
CASE
WHEN LOWER(n.display_name) = LOWER('webhook') THEN 0
WHEN LOWER(n.display_name) LIKE LOWER('%webhook%') THEN 1
WHEN LOWER(n.node_type) LIKE LOWER('%webhook%') THEN 2
ELSE 3
END,
rank
LIMIT 10
`).all();
expect(results.length).toBeGreaterThan(0);
// Exact match should be in top results (using production boosting logic with CASE-first ordering)
const topResults = results.slice(0, 3).map((r: any) => r.node_type);
expect(topResults).toContain('nodes-base.webhook');
});
it('should support phrase searches', () => {
const results = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH '"http request"'
`).all();
expect(results.length).toBeGreaterThan(0);
});
it('should support boolean operators', () => {
const andResults = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'google AND sheets'
`).all();
const orResults = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'google OR sheets'
`).all();
expect(andResults.length).toBeGreaterThan(0);
expect(orResults.length).toBeGreaterThanOrEqual(andResults.length);
});
});
describe('FTS5 Index Synchronization', () => {
it('should keep FTS5 in sync after node updates', () => {
// This test ensures triggers work properly
const beforeCount = db.prepare('SELECT COUNT(*) as count FROM nodes_fts').get();
// Insert a test node
db.prepare(`
INSERT INTO nodes (
node_type, package_name, display_name, description,
category, development_style, is_ai_tool, is_trigger,
is_webhook, is_versioned, version, properties_schema,
operations, credentials_required
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
'test.node',
'test-package',
'Test Node',
'A test node for FTS5 synchronization',
'Test',
'programmatic',
0, 0, 0, 0,
'1.0',
'[]', '[]', '[]'
);
const afterInsert = db.prepare('SELECT COUNT(*) as count FROM nodes_fts').get();
expect(afterInsert.count).toBe(beforeCount.count + 1);
// Verify the new node is searchable
const searchResults = db.prepare(`
SELECT node_type FROM nodes_fts
WHERE nodes_fts MATCH 'test synchronization'
`).all();
expect(searchResults.length).toBeGreaterThan(0);
// Clean up
db.prepare('DELETE FROM nodes WHERE node_type = ?').run('test.node');
const afterDelete = db.prepare('SELECT COUNT(*) as count FROM nodes_fts').get();
expect(afterDelete.count).toBe(beforeCount.count);
});
});
});