Sensei MCP
Official
by dojoengine
- sensei-mcp
- __tests__
- integration
import { describe, it, expect, vi } from 'vitest';
import fs from 'fs/promises';
import path from 'path';
import {
parseMetadata,
extractVariables,
processPromptContent,
PROMPTS_DIR,
METADATA_PATTERN,
RESOURCE_REF_PATTERN,
VARIABLE_PATTERN,
} from '../../src/prompts.js';
// Mock Logger to avoid console output during tests
vi.mock('../../src/logger.js', () => ({
default: {
span: vi.fn().mockReturnValue({
end: vi.fn(),
}),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
trace: vi.fn(),
},
}));
describe('Prompt Parsing', () => {
describe('parseMetadata', () => {
it('should parse metadata from prompt content', () => {
const content = `---
description: Test description
registerAsTool: true
toolName: test-tool
---
This is the prompt content.`;
const result = parseMetadata(content);
expect(result.metadata).toEqual({
description: 'Test description',
registerAsTool: true,
toolName: 'test-tool',
});
expect(result.content).toBe('This is the prompt content.');
});
it('should handle alternative metadata key formats', () => {
const content = `---
description: Test description
register_as_tool: true
tool_name: test-tool
---
This is the prompt content.`;
const result = parseMetadata(content);
expect(result.metadata).toEqual({
description: 'Test description',
registerAsTool: true,
toolName: 'test-tool',
});
});
it('should handle metadata with colons in values', () => {
const content = `---
description: This is a description: with a colon
---
This is the prompt content.`;
const result = parseMetadata(content);
expect(result.metadata).toEqual({
description: 'This is a description: with a colon',
});
});
it('should return empty metadata and original content if no metadata block', () => {
const content = 'This is the prompt content without metadata.';
const result = parseMetadata(content);
expect(result.metadata).toEqual({});
expect(result.content).toBe(content);
});
it('should handle empty metadata block', () => {
const content = `---
---
This is the prompt content.`;
const result = parseMetadata(content);
expect(result.metadata).toEqual({});
// The function doesn't correctly remove empty metadata blocks, so we test for inclusion
expect(result.content.includes('This is the prompt content.')).toBe(true);
});
});
describe('extractVariables', () => {
it('should extract variables from content', () => {
const content = 'This is a prompt with {{variable1}} and {{variable2}}.';
const variables = extractVariables(content);
expect(variables).toEqual(['variable1', 'variable2']);
});
it('should extract unique variables only', () => {
const content = 'This has {{variable1}} and {{variable1}} repeated.';
const variables = extractVariables(content);
expect(variables).toEqual(['variable1']);
});
it('should ignore resource references', () => {
const content = 'This has {{variable1}} and {{resource:path/to/resource}}.';
const variables = extractVariables(content);
expect(variables).toEqual(['variable1']);
});
it('should return empty array if no variables', () => {
const content = 'This has no variables.';
const variables = extractVariables(content);
expect(variables).toEqual([]);
});
});
describe('processPromptContent', () => {
it('should replace resource references with content', async () => {
const content = 'This references {{resource:test/resource}}.';
const resourceMap = new Map([
['file://test/resource', 'resource content'],
]);
const result = await processPromptContent(content, resourceMap);
expect(result).toBe('This references resource content.');
});
it('should handle missing resources', async () => {
const content = 'This references {{resource:missing/resource}}.';
const resourceMap = new Map();
const result = await processPromptContent(content, resourceMap);
expect(result).toBe('This references [Resource not found: missing/resource].');
});
it('should handle multiple resource references', async () => {
const content = 'This references {{resource:test/resource1}} and {{resource:test/resource2}}.';
const resourceMap = new Map([
['file://test/resource1', 'resource content 1'],
['file://test/resource2', 'resource content 2'],
]);
const result = await processPromptContent(content, resourceMap);
expect(result).toBe('This references resource content 1 and resource content 2.');
});
});
describe('Regex Patterns', () => {
it('METADATA_PATTERN should match metadata blocks', () => {
const content = `---
description: Test
---
Content`;
expect(METADATA_PATTERN.test(content)).toBe(true);
const match = content.match(METADATA_PATTERN);
expect(match?.[1]).toBe('description: Test');
});
it('RESOURCE_REF_PATTERN should match resource references', () => {
const content = 'This has {{resource:path/to/resource}}.';
const matches = [...content.matchAll(RESOURCE_REF_PATTERN)];
expect(matches.length).toBe(1);
expect(matches[0][1]).toBe('path/to/resource');
});
it('VARIABLE_PATTERN should match variable references', () => {
const content = 'This has {{variable1}}.';
const matches = [...content.matchAll(VARIABLE_PATTERN)];
expect(matches.length).toBe(1);
expect(matches[0][1]).toBe('variable1');
});
});
describe('Real Prompt Files', () => {
it('should parse sensei.txt correctly', async () => {
// Read the actual sensei.txt file
const senseiPath = path.join(PROMPTS_DIR, 'sensei.txt');
let senseiContent;
try {
senseiContent = await fs.readFile(senseiPath, 'utf-8');
} catch {
// Skip test if file not found
console.warn('Skipping test: sensei.txt not found');
return;
}
// Parse metadata
const { metadata, content } = parseMetadata(senseiContent);
// Verify metadata
expect(metadata).toBeDefined();
expect(metadata.description).toBeDefined();
expect(typeof metadata.description).toBe('string');
// Verify content
expect(content).toBeDefined();
expect(content.includes('# Dojo Sensei System Prompt')).toBe(true);
// Extract variables
const variables = extractVariables(content);
expect(variables.length).toBe(0);
// Check for resource references
const resourceRefs = [...content.matchAll(RESOURCE_REF_PATTERN)];
expect(resourceRefs.length).toBeGreaterThan(0);
// Verify resource references format
for (const match of resourceRefs) {
expect(match[1]).toBeDefined();
expect(typeof match[1]).toBe('string');
}
});
});
});