From af2cfec668d4f305a2fb9efd18e293bb652038aa Mon Sep 17 00:00:00 2001 From: czlonkowski Date: Thu, 26 Mar 2026 15:02:20 +0100 Subject: [PATCH] fix: update saveNode test mocks for docs preservation pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../node-repository-community.test.ts | 7 +++++- .../database/node-repository-core.test.ts | 14 +++++++++--- .../database/node-repository-outputs.test.ts | 22 ++++++++++++++++--- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/tests/unit/database/node-repository-community.test.ts b/tests/unit/database/node-repository-community.test.ts index 624cbd3..7eaa8f7 100644 --- a/tests/unit/database/node-repository-community.test.ts +++ b/tests/unit/database/node-repository-community.test.ts @@ -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]; diff --git a/tests/unit/database/node-repository-core.test.ts b/tests/unit/database/node-repository-core.test.ts index ce8a311..e13a187 100644 --- a/tests/unit/database/node-repository-core.test.ts +++ b/tests/unit/database/node-repository-core.test.ts @@ -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) ); }); diff --git a/tests/unit/database/node-repository-outputs.test.ts b/tests/unit/database/node-repository-outputs.test.ts index 53402de..b7c50e4 100644 --- a/tests/unit/database/node-repository-outputs.test.ts +++ b/tests/unit/database/node-repository-outputs.test.ts @@ -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) ); });