/**
* HTML Generation Functions Tests
*
* Tests for server-side HTML generation functions that create
* dynamic content for the home page.
*/
import { describe, it, expect } from 'vitest';
import type { ToolDefinition } from '../../src/domain/types.js';
// Mock tool definitions for testing
const mockToolWithArray: ToolDefinition = {
name: 'test-tool',
description: 'A test tool for FIPS state codes',
inputSchema: {
type: 'object',
properties: {
states: {
type: 'array',
description: 'Array of FIPS state codes',
items: {
type: 'number'
}
}
},
required: ['states']
}
};
const mockToolWithString: ToolDefinition = {
name: 'another-tool',
description: 'A tool with string parameter',
inputSchema: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'Search query'
}
},
required: ['query']
}
};
describe('HTML Generation Functions', () => {
describe('Tool HTML Generation Logic', () => {
it('should handle empty tool list', () => {
const tools: ToolDefinition[] = [];
expect(tools.length).toBe(0);
});
it('should handle single tool with array parameter', () => {
const tools = [mockToolWithArray];
expect(tools.length).toBe(1);
expect(tools[0].name).toBe('test-tool');
expect(tools[0].inputSchema.properties.states.type).toBe('array');
});
it('should handle multiple tools', () => {
const tools = [mockToolWithArray, mockToolWithString];
expect(tools.length).toBe(2);
expect(tools[0].name).toBe('test-tool');
expect(tools[1].name).toBe('another-tool');
});
it('should identify FIPS code parameters', () => {
const tool = mockToolWithArray;
const statesProp = tool.inputSchema.properties.states;
expect(statesProp.description).toContain('FIPS');
expect(statesProp.type).toBe('array');
});
it('should identify required parameters', () => {
const tool = mockToolWithArray;
expect(tool.inputSchema.required).toContain('states');
});
it('should handle optional parameters', () => {
const optionalTool: ToolDefinition = {
name: 'optional-tool',
description: 'Tool with optional param',
inputSchema: {
type: 'object',
properties: {
optional: {
type: 'string',
description: 'Optional parameter'
}
},
required: []
}
};
expect(optionalTool.inputSchema.required.length).toBe(0);
});
});
describe('Example Generation Logic', () => {
it('should generate array example for FIPS codes', () => {
const exampleParams: any = {};
const properties = mockToolWithArray.inputSchema.properties;
Object.keys(properties).forEach(key => {
const prop = properties[key];
if (prop.type === 'array' && prop.description?.includes('FIPS')) {
exampleParams[key] = [48, 36]; // Texas and New York
}
});
expect(exampleParams.states).toEqual([48, 36]);
});
it('should generate string example', () => {
const exampleParams: any = {};
const properties = mockToolWithString.inputSchema.properties;
Object.keys(properties).forEach(key => {
const prop = properties[key];
if (prop.type === 'string') {
exampleParams[key] = 'example';
}
});
expect(exampleParams.query).toBe('example');
});
it('should handle enum properties', () => {
const enumTool: ToolDefinition = {
name: 'enum-tool',
description: 'Tool with enum',
inputSchema: {
type: 'object',
properties: {
choice: {
type: 'string',
enum: ['option1', 'option2', 'option3']
}
},
required: ['choice']
}
};
const exampleParams: any = {};
const properties = enumTool.inputSchema.properties;
Object.keys(properties).forEach(key => {
const prop = properties[key];
if (prop.enum && prop.enum.length > 0) {
exampleParams[key] = prop.enum[0];
}
});
expect(exampleParams.choice).toBe('option1');
});
it('should handle number properties', () => {
const numberTool: ToolDefinition = {
name: 'number-tool',
description: 'Tool with number',
inputSchema: {
type: 'object',
properties: {
count: {
type: 'number',
description: 'A count'
}
},
required: []
}
};
const exampleParams: any = {};
const properties = numberTool.inputSchema.properties;
Object.keys(properties).forEach(key => {
const prop = properties[key];
if (prop.type === 'number') {
exampleParams[key] = 0;
}
});
expect(exampleParams.count).toBe(0);
});
it('should handle boolean properties', () => {
const boolTool: ToolDefinition = {
name: 'bool-tool',
description: 'Tool with boolean',
inputSchema: {
type: 'object',
properties: {
enabled: {
type: 'boolean',
description: 'Enable feature'
}
},
required: []
}
};
const exampleParams: any = {};
const properties = boolTool.inputSchema.properties;
Object.keys(properties).forEach(key => {
const prop = properties[key];
if (prop.type === 'boolean') {
exampleParams[key] = true;
}
});
expect(exampleParams.enabled).toBe(true);
});
});
describe('HTML Escaping Logic', () => {
it('should escape quotes for HTML attributes', () => {
const json = JSON.stringify({ states: [48, 36] }, null, 2);
const escaped = json.replace(/"/g, '"');
expect(escaped).not.toContain('"');
expect(escaped).toContain('"');
});
it('should handle JSON stringify with formatting', () => {
const obj = { states: [48, 36] };
const json = JSON.stringify(obj, null, 2);
expect(json).toContain('[\n');
expect(json).toContain(' 48');
expect(json).toContain(' 36');
});
});
describe('Port Configuration', () => {
it('should use port 5008 as default', () => {
const defaultPort = 5008;
expect(defaultPort).toBe(5008);
});
it('should generate correct URLs with port', () => {
const port = 5008;
const mcpUrl = `http://localhost:${port}/mcp`;
const healthUrl = `http://localhost:${port}/health`;
const homeUrl = `http://localhost:${port}/`;
expect(mcpUrl).toBe('http://localhost:5008/mcp');
expect(healthUrl).toBe('http://localhost:5008/health');
expect(homeUrl).toBe('http://localhost:5008/');
});
});
describe('FIPS Code Reference', () => {
it('should include common FIPS codes', () => {
const fipsCodes = {
CA: 6,
TX: 48,
FL: 12,
NY: 36,
PA: 42,
IL: 17,
OH: 39,
GA: 13,
NC: 37,
MI: 26
};
expect(fipsCodes.CA).toBe(6);
expect(fipsCodes.TX).toBe(48);
expect(fipsCodes.NY).toBe(36);
});
it('should include all states code', () => {
const allStatesCode = 0;
expect(allStatesCode).toBe(0);
});
});
});