Files
promptfoo--promptfoo/test/prompts/wildcard.test.ts
T
wehub-resource-sync 0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

152 lines
4.3 KiB
TypeScript

import * as fs from 'fs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { processPrompts } from '../../src/prompts/index';
// Mock the python execution
vi.mock('../../src/python/pythonUtils', () => ({
runPython: vi.fn((filePath: string, _functionName: string, _args: string[]) => {
// Return different responses based on file path
if (filePath.includes('marketing')) {
return Promise.resolve(
JSON.stringify({
output: `Marketing prompt from ${filePath}`,
}),
);
} else if (filePath.includes('technical')) {
return Promise.resolve(
JSON.stringify({
output: `Technical prompt from ${filePath}`,
}),
);
}
return Promise.resolve(
JSON.stringify({
output: 'Default prompt response',
}),
);
}),
}));
// Mock fs operations
vi.mock('fs', () => ({
existsSync: vi.fn(() => true),
readFileSync: vi.fn((path: string) => {
if (path.includes('.py')) {
return 'def get_prompt(context):\n return "Test prompt"';
}
if (path.includes('.js')) {
return 'module.exports = function() { return "JS prompt"; };';
}
return '';
}),
statSync: vi.fn(() => ({
size: 1000,
isDirectory: () => false,
})),
writeFileSync: vi.fn(),
}));
// Mock glob results
vi.mock('glob', () => ({
globSync: vi.fn((pattern: string) => {
if (pattern.includes('test/prompts/**/*.py')) {
return [
'test/prompts/marketing/email.py',
'test/prompts/marketing/social.py',
'test/prompts/technical/review.py',
];
}
if (pattern.includes('test/prompts/**/*.js')) {
return ['test/prompts/ui/button.js', 'test/prompts/ui/form.js'];
}
return [];
}),
hasMagic: vi.fn((pattern: string | string[]) => {
const p = Array.isArray(pattern) ? pattern.join('') : pattern;
return p.includes('*') || p.includes('?') || p.includes('[') || p.includes('{');
}),
}));
// Mock importModule for JavaScript files
vi.mock('../../src/esm', () => ({
importModule: vi.fn((filePath: string) => {
return Promise.resolve(function (_context: any) {
return `JS prompt from ${filePath}`;
});
}),
}));
describe('Wildcard prompt support', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('Python wildcards', () => {
it('should expand wildcard patterns and preserve function names', async () => {
const prompts = await processPrompts(['file://test/prompts/**/*.py:get_prompt']);
// Should create one prompt for each matched file
expect(prompts.length).toBe(3);
// Each prompt should have the function name preserved
prompts.forEach((prompt) => {
expect(prompt.label).toMatch(/\.py:get_prompt$/);
});
});
it('should work without function names', async () => {
const prompts = await processPrompts(['file://test/prompts/**/*.py']);
expect(prompts.length).toBe(3);
// For Python files without function names, labels include file content
prompts.forEach((prompt) => {
expect(prompt.label).toContain('.py: ');
});
});
});
describe('JavaScript wildcards', () => {
it('should expand wildcard patterns for JavaScript files', async () => {
const prompts = await processPrompts(['file://test/prompts/**/*.js']);
expect(prompts.length).toBe(2);
prompts.forEach((prompt) => {
expect(prompt.label).toMatch(/\.js$/);
});
});
it('should preserve function names for JavaScript', async () => {
const prompts = await processPrompts(['file://test/prompts/**/*.js:myFunction']);
expect(prompts.length).toBe(2);
prompts.forEach((prompt) => {
expect(prompt.label).toMatch(/\.js:myFunction$/);
});
});
});
describe('Mixed patterns', () => {
it('should handle both wildcards and explicit paths', async () => {
// Mock for the explicit path
vi.mocked(fs.existsSync).mockImplementation((path) => {
if (String(path).includes('explicit.py')) {
return true;
}
return true;
});
const prompts = await processPrompts([
'file://test/prompts/**/*.py:get_prompt',
'file://test/explicit.py:another_function',
]);
// 3 from wildcard + 1 explicit
expect(prompts.length).toBe(4);
});
});
});