-
Notifications
You must be signed in to change notification settings - Fork 5
Update server version handling and add tests #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wpak-ai
merged 4 commits into
cppalliance:main
from
leostar0412:fix/mcp-server-version-sync
May 5, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0529f6
Update server version handling and add tests
leostar0412 506f89c
Add error handling for package.json version parsing
leostar0412 868d386
Refactor server version handling to remove error classes and implemen…
leostar0412 8031f9d
Refactor error handling in server version functions to use console.er…
leostar0412 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ coverage/ | |
| *.swo | ||
| *~ | ||
| .DS_Store | ||
| ._* | ||
|
|
||
| # Runtime | ||
| *.pid | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { readFileSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { parsePackageJsonVersion, SERVER_VERSION } from './server-version.js'; | ||
|
|
||
| function readRootPackageJson(): string { | ||
| const packageJsonPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'package.json'); | ||
| return readFileSync(packageJsonPath, 'utf8'); | ||
| } | ||
|
|
||
| /** | ||
| * True when the server-reported version matches the package.json version. | ||
| * Returns false when the two strings differ (stale hardcoding or wrong file). | ||
| */ | ||
| function isServerVersionAligned(serverVersion: string, packageVersion: string): boolean { | ||
| return serverVersion === packageVersion; | ||
| } | ||
|
|
||
| /** Synthetic package.json bodies — only the `version` field matters for parsing. */ | ||
| const PACKAGE_JSON_FIXTURES: readonly string[] = [ | ||
| JSON.stringify({ name: 'a', version: '0.1.0' }), | ||
| JSON.stringify({ name: 'b', version: '1.0.0' }), | ||
| JSON.stringify({ version: '0.1.6', type: 'module' }), | ||
| JSON.stringify({ version: '2.3.4', private: true }), | ||
| ]; | ||
|
|
||
| describe('parsePackageJsonVersion', () => { | ||
| it('extracts version from several package.json shapes', () => { | ||
| expect(parsePackageJsonVersion(PACKAGE_JSON_FIXTURES[0])).toBe('0.1.0'); | ||
| expect(parsePackageJsonVersion(PACKAGE_JSON_FIXTURES[1])).toBe('1.0.0'); | ||
| expect(parsePackageJsonVersion(PACKAGE_JSON_FIXTURES[2])).toBe('0.1.6'); | ||
| expect(parsePackageJsonVersion(PACKAGE_JSON_FIXTURES[3])).toBe('2.3.4'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isServerVersionAligned', () => { | ||
| it('returns true when server and package versions are the same string', () => { | ||
| for (const raw of PACKAGE_JSON_FIXTURES) { | ||
| const v = parsePackageJsonVersion(raw); | ||
| expect(isServerVersionAligned(v, v)).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it('returns false when server and package versions differ', () => { | ||
| expect(isServerVersionAligned('0.1.0', '0.2.0')).toBe(false); | ||
| expect(isServerVersionAligned('1.0.0', '2.0.0')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SERVER_VERSION', () => { | ||
| it('matches the root package.json version (live module read)', () => { | ||
| const packageVersion = parsePackageJsonVersion(readRootPackageJson()); | ||
| expect(isServerVersionAligned(SERVER_VERSION, packageVersion)).toBe(true); | ||
| expect(SERVER_VERSION).toBe(packageVersion); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * MCP server version — always matches the root package.json "version" field. | ||
| */ | ||
| import { readFileSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const packageJsonPath = join(__dirname, '..', 'package.json'); | ||
|
|
||
| /** | ||
| * Read `version` from package.json text (same rules as the live server). | ||
| */ | ||
| export function parsePackageJsonVersion(raw: string, pathForErrors = 'package.json'): string { | ||
| const parsed = JSON.parse(raw) as { version?: unknown }; | ||
| if (typeof parsed.version !== 'string' || parsed.version.length === 0) { | ||
| throw new Error(`Invalid or missing "version" in ${pathForErrors}`); | ||
| } | ||
| return parsed.version; | ||
| } | ||
|
|
||
| const raw = readFileSync(packageJsonPath, 'utf8'); | ||
|
leostar0412 marked this conversation as resolved.
Outdated
|
||
| export const SERVER_VERSION = parsePackageJsonVersion(raw, packageJsonPath); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.