fix: update saveNode test mocks for docs preservation pattern

Tests now account for the SELECT query that reads existing docs
before INSERT OR REPLACE, and the 3 extra params (npm_readme,
ai_documentation_summary, ai_summary_generated_at).

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2026-03-26 15:02:20 +01:00
parent ab2dc9824e
commit af2cfec668
3 changed files with 36 additions and 7 deletions

View File

@@ -119,8 +119,13 @@ class MockPreparedStatement implements PreparedStatement {
});
}
// saveNode - SELECT existing doc fields before upsert
if (this.sql.includes('SELECT npm_readme, ai_documentation_summary, ai_summary_generated_at FROM nodes')) {
this.get = vi.fn(() => undefined); // No existing row by default
}
// saveNode - INSERT OR REPLACE
if (this.sql.includes('INSERT INTO nodes')) {
if (this.sql.includes('INSERT OR REPLACE INTO nodes')) {
this.run = vi.fn((...params: any[]): RunResult => {
const nodes = this.mockData.get('community_nodes') || [];
const nodeType = params[0];

View File

@@ -49,7 +49,12 @@ class MockPreparedStatement implements PreparedStatement {
if (sql.includes('SELECT * FROM nodes WHERE node_type = ?')) {
this.get = vi.fn((nodeType: string) => this.mockData.get(`node:${nodeType}`));
}
// Configure get() for saveNode's SELECT to preserve existing doc fields
if (sql.includes('SELECT npm_readme, ai_documentation_summary, ai_summary_generated_at FROM nodes')) {
this.get = vi.fn(() => undefined); // No existing row by default
}
// Configure all() for getAITools
if (sql.includes('WHERE is_ai_tool = 1')) {
this.all = vi.fn(() => this.mockData.get('ai_tools') || []);
@@ -91,7 +96,7 @@ describe('NodeRepository - Core Functionality', () => {
repository.saveNode(parsedNode);
// Verify prepare was called with correct SQL
expect(mockAdapter.prepare).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO nodes'));
expect(mockAdapter.prepare).toHaveBeenCalledWith(expect.stringContaining('INSERT OR REPLACE INTO nodes'));
// Get the prepared statement and verify run was called
const stmt = mockAdapter._getStatement(mockAdapter.prepare.mock.lastCall?.[0] || '');
@@ -123,7 +128,10 @@ describe('NodeRepository - Core Functionality', () => {
null, // npmPackageName
null, // npmVersion
0, // npmDownloads
null // communityFetchedAt
null, // communityFetchedAt
null, // npm_readme (preserved from existing)
null, // ai_documentation_summary (preserved from existing)
null // ai_summary_generated_at (preserved from existing)
);
});

View File

@@ -15,8 +15,21 @@ describe('NodeRepository - Outputs Handling', () => {
all: vi.fn()
};
// saveNode now calls prepare twice: first a SELECT (returns get), then INSERT (returns run).
// We create a separate mock for the SELECT statement that returns undefined (no existing row).
const selectStatement = {
run: vi.fn(),
get: vi.fn().mockReturnValue(undefined),
all: vi.fn()
};
mockDb = {
prepare: vi.fn().mockReturnValue(mockStatement),
prepare: vi.fn((sql: string) => {
if (sql.includes('SELECT npm_readme')) {
return selectStatement;
}
return mockStatement;
}),
transaction: vi.fn(),
exec: vi.fn(),
close: vi.fn(),
@@ -56,7 +69,7 @@ describe('NodeRepository - Outputs Handling', () => {
repository.saveNode(node);
expect(mockDb.prepare).toHaveBeenCalledWith(
expect.stringContaining('INSERT INTO nodes')
expect.stringContaining('INSERT OR REPLACE INTO nodes')
);
expect(mockStatement.run).toHaveBeenCalledWith(
@@ -87,7 +100,10 @@ describe('NodeRepository - Outputs Handling', () => {
null, // npm_package_name
null, // npm_version
0, // npm_downloads
null // community_fetched_at
null, // community_fetched_at
null, // npm_readme (preserved from existing)
null, // ai_documentation_summary (preserved from existing)
null // ai_summary_generated_at (preserved from existing)
);
});