mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add system prompt test
This commit is contained in:
1 file changed
+450
@@ -0,0 +1,450 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { makeSystemPrompt, MakeSystemPromptOpts } from './system';
|
||||
import { JSRuntimeFS } from './JSRuntime';
|
||||
import { NUser } from '@nostrify/react/login';
|
||||
import { NostrMetadata } from '@nostrify/nostrify';
|
||||
import OpenAI from 'openai';
|
||||
|
||||
// Mock the plugins module
|
||||
vi.mock('./plugins', () => ({
|
||||
getAllSkills: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock path-browserify
|
||||
vi.mock('path-browserify', () => ({
|
||||
join: (...args: string[]) => args.join('/').replace(/\/+/g, '/'),
|
||||
}));
|
||||
|
||||
import { getAllSkills } from './plugins';
|
||||
|
||||
describe('makeSystemPrompt', () => {
|
||||
let mockFs: JSRuntimeFS;
|
||||
let baseOpts: MakeSystemPromptOpts;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Mock filesystem
|
||||
mockFs = {
|
||||
readFile: vi.fn(),
|
||||
writeFile: vi.fn(),
|
||||
readdir: vi.fn(),
|
||||
stat: vi.fn(),
|
||||
mkdir: vi.fn(),
|
||||
rmdir: vi.fn(),
|
||||
unlink: vi.fn(),
|
||||
rename: vi.fn(),
|
||||
exists: vi.fn(),
|
||||
} as unknown as JSRuntimeFS;
|
||||
|
||||
// Base options for tests
|
||||
baseOpts = {
|
||||
name: 'TestBot',
|
||||
profession: 'software developer',
|
||||
tools: [],
|
||||
mode: 'agent',
|
||||
fs: mockFs,
|
||||
cwd: '/projects/test-project',
|
||||
};
|
||||
|
||||
// Mock getAllSkills to return empty array by default
|
||||
vi.mocked(getAllSkills).mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('basic system prompt generation', () => {
|
||||
it('should generate a basic system prompt in agent mode', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('You are TestBot, an expert software developer');
|
||||
expect(result).toContain('Your goal is to work on the project in the current directory');
|
||||
expect(result).toContain('explore and understand the project structure');
|
||||
});
|
||||
|
||||
it('should generate a basic system prompt in init mode', async () => {
|
||||
const result = await makeSystemPrompt({ ...baseOpts, mode: 'init' });
|
||||
|
||||
expect(result).toContain('You are TestBot, an expert software developer');
|
||||
expect(result).toContain('The files in the current directory are a template');
|
||||
expect(result).toContain('transform this template into a working project');
|
||||
});
|
||||
|
||||
it('should include current date', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
const currentDate = new Date().toLocaleDateString('en-US', {
|
||||
weekday: 'long',
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
});
|
||||
|
||||
expect(result).toContain(`**Current Date**: ${currentDate}`);
|
||||
});
|
||||
|
||||
it('should include current working directory', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('**Current Working Directory**: /projects/test-project');
|
||||
});
|
||||
});
|
||||
|
||||
describe('user information', () => {
|
||||
it('should include logged-out user message when no user is provided', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('The user is not logged in');
|
||||
expect(result).toContain('can log into Nostr by clicking the "Login" button');
|
||||
});
|
||||
|
||||
it('should include basic user information when user is provided', async () => {
|
||||
const user = new NUser(
|
||||
'extension',
|
||||
'abc123def456',
|
||||
{} as NUser['signer']
|
||||
);
|
||||
|
||||
const result = await makeSystemPrompt({ ...baseOpts, user });
|
||||
|
||||
expect(result).toContain('The user is logged into Nostr');
|
||||
expect(result).toContain('**Nostr pubkey (hex)**: abc123def456');
|
||||
expect(result).toContain('**Nostr npub**: npub1');
|
||||
});
|
||||
|
||||
it('should include user metadata when provided', async () => {
|
||||
const user = new NUser(
|
||||
'extension',
|
||||
'abc123def456',
|
||||
{} as NUser['signer']
|
||||
);
|
||||
|
||||
const metadata: NostrMetadata = {
|
||||
name: 'Alice',
|
||||
about: 'A passionate developer',
|
||||
website: 'https://alice.example.com',
|
||||
picture: 'https://alice.example.com/avatar.jpg',
|
||||
banner: 'https://alice.example.com/banner.jpg',
|
||||
nip05: '[email protected]',
|
||||
lud16: '[email protected]',
|
||||
};
|
||||
|
||||
const result = await makeSystemPrompt({ ...baseOpts, user, metadata });
|
||||
|
||||
expect(result).toContain('**Name**: Alice');
|
||||
expect(result).toContain('**About**: A passionate developer');
|
||||
expect(result).toContain('**Website**: https://alice.example.com');
|
||||
expect(result).toContain('**Avatar**: https://alice.example.com/avatar.jpg');
|
||||
expect(result).toContain('**Banner**: https://alice.example.com/banner.jpg');
|
||||
expect(result).toContain('**NIP-05**: [email protected]');
|
||||
expect(result).toContain('**Lightning Address**: [email protected]');
|
||||
});
|
||||
|
||||
it('should sanitize multiline about text', async () => {
|
||||
const user = new NUser(
|
||||
'extension',
|
||||
'abc123def456',
|
||||
{} as NUser['signer']
|
||||
);
|
||||
|
||||
const metadata: NostrMetadata = {
|
||||
about: 'Line 1\nLine 2\r\nLine 3',
|
||||
};
|
||||
|
||||
const result = await makeSystemPrompt({ ...baseOpts, user, metadata });
|
||||
|
||||
expect(result).toContain('**About**: Line 1 Line 2 Line 3');
|
||||
expect(result).not.toContain('\n**About**');
|
||||
});
|
||||
});
|
||||
|
||||
describe('tools section', () => {
|
||||
it('should not include tools section when no tools are provided', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).not.toContain('## Available Tools');
|
||||
});
|
||||
|
||||
it('should list available tools with descriptions', async () => {
|
||||
const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
|
||||
{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'read_file',
|
||||
description: 'Read contents of a file',
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'write_file',
|
||||
description: 'Write contents to a file',
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = await makeSystemPrompt({ ...baseOpts, tools });
|
||||
|
||||
expect(result).toContain('## Available Tools');
|
||||
expect(result).toContain('**read_file**: Read contents of a file');
|
||||
expect(result).toContain('**write_file**: Write contents to a file');
|
||||
});
|
||||
|
||||
it('should handle tools without descriptions', async () => {
|
||||
const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
|
||||
{
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'some_tool',
|
||||
parameters: {},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = await makeSystemPrompt({ ...baseOpts, tools });
|
||||
|
||||
expect(result).toContain('**some_tool**: No description available');
|
||||
});
|
||||
});
|
||||
|
||||
describe('skills section', () => {
|
||||
it('should show no skills message when pluginsPath is not provided', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('## Skills');
|
||||
expect(result).toContain('No skills are currently configured');
|
||||
});
|
||||
|
||||
it('should show no skills message when getAllSkills returns empty array', async () => {
|
||||
vi.mocked(getAllSkills).mockResolvedValue([]);
|
||||
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
pluginsPath: '/plugins',
|
||||
});
|
||||
|
||||
expect(result).toContain('No skills are currently configured');
|
||||
expect(getAllSkills).toHaveBeenCalledWith(mockFs, '/plugins');
|
||||
});
|
||||
|
||||
it('should list available skills when they exist', async () => {
|
||||
vi.mocked(getAllSkills).mockResolvedValue([
|
||||
{
|
||||
name: 'create_component',
|
||||
description: 'Create a new React component',
|
||||
plugin: 'react-plugin',
|
||||
path: '/plugins/react-plugin/skills/create_component.md',
|
||||
},
|
||||
{
|
||||
name: 'setup_routing',
|
||||
description: 'Setup React Router',
|
||||
plugin: 'react-plugin',
|
||||
path: '/plugins/react-plugin/skills/setup_routing.md',
|
||||
},
|
||||
]);
|
||||
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
pluginsPath: '/plugins',
|
||||
});
|
||||
|
||||
expect(result).toContain('## Skills');
|
||||
expect(result).toContain('**create_component**: Create a new React component');
|
||||
expect(result).toContain('Plugin: react-plugin');
|
||||
expect(result).toContain('**setup_routing**: Setup React Router');
|
||||
expect(result).toContain('Skills MUST be used whenever applicable');
|
||||
});
|
||||
|
||||
it('should handle getAllSkills errors gracefully', async () => {
|
||||
vi.mocked(getAllSkills).mockRejectedValue(new Error('Plugin error'));
|
||||
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
pluginsPath: '/plugins',
|
||||
});
|
||||
|
||||
expect(result).toContain('No skills are currently configured');
|
||||
});
|
||||
});
|
||||
|
||||
describe('CORS proxy section', () => {
|
||||
it('should not include CORS proxy section when not provided', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).not.toContain('## Working Around CORS Issues');
|
||||
});
|
||||
|
||||
it('should include CORS proxy information when provided', async () => {
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
corsProxy: 'https://cors-proxy.example.com/{href}',
|
||||
});
|
||||
|
||||
expect(result).toContain('## Working Around CORS Issues');
|
||||
expect(result).toContain('**CORS Proxy URL Template**: `https://cors-proxy.example.com/{href}`');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Edit with Shakespeare section', () => {
|
||||
it('should show message when no repository URL is configured', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('## Edit with Shakespeare');
|
||||
expect(result).toContain('This project does not currently have a repository URL configured');
|
||||
});
|
||||
|
||||
it('should include badge code snippets when repository URL is provided', async () => {
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
repositoryUrl: 'https://github.com/user/repo.git',
|
||||
});
|
||||
|
||||
expect(result).toContain('## Edit with Shakespeare');
|
||||
expect(result).toContain('[![Edit with Shakespeare]');
|
||||
expect(result).toContain('/badge.svg');
|
||||
expect(result).toContain('/clone');
|
||||
// URL is encoded, so check for the encoded version
|
||||
expect(result).toContain('url=https%3A%2F%2Fgithub.com%2Fuser%2Frepo.git');
|
||||
});
|
||||
});
|
||||
|
||||
describe('README.md inclusion', () => {
|
||||
it('should include README.md content when it exists', async () => {
|
||||
vi.mocked(mockFs.readFile).mockImplementation(async (path: string) => {
|
||||
if (path === '/projects/test-project/README.md') {
|
||||
return '# My Project\n\nThis is a test project.';
|
||||
}
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('# My Project');
|
||||
expect(result).toContain('This is a test project.');
|
||||
});
|
||||
|
||||
it('should continue when README.md does not exist', async () => {
|
||||
vi.mocked(mockFs.readFile).mockRejectedValue(new Error('File not found'));
|
||||
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toBeTruthy();
|
||||
expect(result).not.toContain('# My Project');
|
||||
});
|
||||
});
|
||||
|
||||
describe('AGENTS.md and context files inclusion', () => {
|
||||
it('should include AGENTS.md content when it exists', async () => {
|
||||
vi.mocked(mockFs.readFile).mockImplementation(async (path: string) => {
|
||||
if (path === '/projects/test-project/AGENTS.md') {
|
||||
return '# Agent Instructions\n\nFollow these guidelines.';
|
||||
}
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('# Agent Instructions');
|
||||
expect(result).toContain('Follow these guidelines.');
|
||||
});
|
||||
|
||||
it('should try alternative context files in order', async () => {
|
||||
vi.mocked(mockFs.readFile).mockImplementation(async (path: string) => {
|
||||
if (path === '/projects/test-project/CONTEXT.md') {
|
||||
return '# Context File\n\nProject context.';
|
||||
}
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('# Context File');
|
||||
expect(result).toContain('Project context.');
|
||||
});
|
||||
|
||||
it('should continue when no context files exist', async () => {
|
||||
vi.mocked(mockFs.readFile).mockRejectedValue(new Error('File not found'));
|
||||
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should try context files in correct order', async () => {
|
||||
const readFileCalls: string[] = [];
|
||||
|
||||
vi.mocked(mockFs.readFile).mockImplementation(async (path: string) => {
|
||||
readFileCalls.push(path);
|
||||
throw new Error('File not found');
|
||||
});
|
||||
|
||||
await makeSystemPrompt(baseOpts);
|
||||
|
||||
// Check that AGENTS.md is tried first
|
||||
expect(readFileCalls).toContain('/projects/test-project/AGENTS.md');
|
||||
});
|
||||
});
|
||||
|
||||
describe('complete system prompt structure', () => {
|
||||
it('should include all major sections', async () => {
|
||||
// Use a valid hex pubkey (64 characters)
|
||||
const validPubkey = '0'.repeat(64);
|
||||
const user = new NUser('extension', validPubkey, {} as NUser['signer']);
|
||||
const result = await makeSystemPrompt({
|
||||
...baseOpts,
|
||||
user,
|
||||
corsProxy: 'https://cors.example.com/{href}',
|
||||
repositoryUrl: 'https://github.com/test/repo.git',
|
||||
});
|
||||
|
||||
expect(result).toContain('# Your Environment');
|
||||
expect(result).toContain('## What Shakespeare Is');
|
||||
expect(result).toContain('## The User');
|
||||
expect(result).toContain('## User Interface');
|
||||
expect(result).toContain('## User Actions');
|
||||
expect(result).toContain('## Virtual Filesystem Structure');
|
||||
expect(result).toContain('## Your Role');
|
||||
expect(result).toContain('## Skills');
|
||||
expect(result).toContain('## Working Around CORS Issues');
|
||||
expect(result).toContain('## Edit with Shakespeare');
|
||||
});
|
||||
|
||||
it('should include Shakespeare architecture information', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('Browser-Based Storage');
|
||||
expect(result).toContain('AI Provider Independence');
|
||||
expect(result).toContain('Shakespeare AI Credits');
|
||||
expect(result).toContain('Cross-Browser Access');
|
||||
expect(result).toContain('No Central Shakespeare Server');
|
||||
});
|
||||
|
||||
it('should include user interface description', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('**Homepage**');
|
||||
expect(result).toContain('**Project View**');
|
||||
expect(result).toContain('**Settings**');
|
||||
expect(result).toContain('**Left Pane**: AI chat interface');
|
||||
expect(result).toContain('**Right Pane**: Toggles between two views');
|
||||
});
|
||||
|
||||
it('should include virtual filesystem structure', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('/projects/');
|
||||
// The config/ and tmp/ paths are in a code block, so check for the exact format
|
||||
expect(result).toContain('├── config/');
|
||||
expect(result).toContain('└── tmp/');
|
||||
expect(result).toContain('Project Isolation');
|
||||
expect(result).toContain('Persistent Storage');
|
||||
});
|
||||
|
||||
it('should include role description with commit reminder', async () => {
|
||||
const result = await makeSystemPrompt(baseOpts);
|
||||
|
||||
expect(result).toContain('## Your Role');
|
||||
expect(result).toContain('Always commit your code changes');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user