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
293 lines
8.7 KiB
TypeScript
293 lines
8.7 KiB
TypeScript
/**
|
|
* Test file for simpleVideo strategy
|
|
*
|
|
* Tests core functionality with proper mocks to avoid depending on fs, ffmpeg, etc.
|
|
*/
|
|
|
|
import fsPromises from 'fs/promises';
|
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import logger from '../../../src/logger';
|
|
import {
|
|
addVideoToBase64,
|
|
createProgressBar,
|
|
escapeDrawtextString,
|
|
getFallbackBase64,
|
|
writeVideoFile,
|
|
} from '../../../src/redteam/strategies/simpleVideo';
|
|
|
|
import type { TestCase } from '../../../src/types/index';
|
|
|
|
// Mock for dummy video data
|
|
const DUMMY_VIDEO_BASE64 = 'AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAu1tZGF0';
|
|
|
|
// Mock video generator function
|
|
const mockVideoGenerator = vi.fn().mockImplementation(function () {
|
|
return Promise.resolve(DUMMY_VIDEO_BASE64);
|
|
});
|
|
|
|
// Mock required dependencies
|
|
vi.mock('../../../src/logger', () => ({
|
|
default: {
|
|
level: 'info',
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('../../../src/cliState', () => ({
|
|
default: {
|
|
webUI: false,
|
|
},
|
|
}));
|
|
|
|
const mockWriteFile = vi.hoisted(() => vi.fn());
|
|
vi.mock('fs/promises', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('fs/promises')>();
|
|
return {
|
|
...actual,
|
|
default: {
|
|
...actual,
|
|
writeFile: mockWriteFile,
|
|
},
|
|
writeFile: mockWriteFile,
|
|
};
|
|
});
|
|
|
|
// Mock for progress bar
|
|
vi.mock('cli-progress', async (importOriginal) => {
|
|
return {
|
|
...(await importOriginal()),
|
|
|
|
SingleBar: vi.fn().mockImplementation(function () {
|
|
return {
|
|
start: vi.fn(),
|
|
increment: vi.fn(),
|
|
stop: vi.fn(),
|
|
};
|
|
}),
|
|
|
|
Presets: { shades_classic: {} },
|
|
};
|
|
});
|
|
|
|
describe('escapeDrawtextString', () => {
|
|
it('passes plain text through unchanged', () => {
|
|
expect(escapeDrawtextString('hello world')).toBe('hello world');
|
|
});
|
|
|
|
it('escapes backslashes first', () => {
|
|
expect(escapeDrawtextString('a\\b')).toBe('a\\\\b');
|
|
});
|
|
|
|
it('escapes single quotes using close-escape-reopen pattern', () => {
|
|
// "it's" → it'\''s (no shell involved — FFmpeg filter parser handles \' as literal ')
|
|
expect(escapeDrawtextString("it's")).toBe("it'\\''s");
|
|
});
|
|
|
|
it('escapes colons as option separators', () => {
|
|
expect(escapeDrawtextString('10:30')).toBe('10\\:30');
|
|
});
|
|
|
|
it('escapes newlines as \\n', () => {
|
|
expect(escapeDrawtextString('line1\nline2')).toBe('line1\\nline2');
|
|
});
|
|
|
|
it('escapes percent signs as %% for drawtext expansion safety', () => {
|
|
expect(escapeDrawtextString('100%')).toBe('100%%');
|
|
});
|
|
|
|
it('double-escapes a backslash immediately before a single quote', () => {
|
|
// Input runtime string: it\'s (backslash + apostrophe + s)
|
|
// Step 1: backslash → \\ gives: it\\'s
|
|
// Step 2: ' → '\'' gives: it\\'\''s
|
|
expect(escapeDrawtextString("it\\'s")).toBe("it\\\\'\\''s");
|
|
});
|
|
|
|
it('handles adversarial input with multiple special characters', () => {
|
|
// Input: ';%{pts}\n[overlay]=value (apostrophe, semicolon, percent, backslash+n, brackets, equals)
|
|
const input = "';%{pts}\\n[overlay]=value";
|
|
const result = escapeDrawtextString(input);
|
|
// Single quote becomes '\'' pattern
|
|
expect(result).toContain("'\\''");
|
|
// Percent becomes %%
|
|
expect(result).toContain('%%');
|
|
// Backslash is doubled
|
|
expect(result).toContain('\\\\');
|
|
});
|
|
});
|
|
|
|
describe('simpleVideo strategy', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockWriteFile.mockReset();
|
|
mockVideoGenerator.mockClear();
|
|
});
|
|
|
|
describe('getFallbackBase64', () => {
|
|
it('converts text to base64', () => {
|
|
const input = 'Test text';
|
|
const result = getFallbackBase64(input);
|
|
|
|
// Decode the base64 and verify it matches the original text
|
|
const decoded = Buffer.from(result, 'base64').toString();
|
|
expect(decoded).toBe(input);
|
|
});
|
|
});
|
|
|
|
describe('createProgressBar', () => {
|
|
it('creates a progress bar with increment and stop methods', () => {
|
|
const progressBar = createProgressBar(10);
|
|
|
|
expect(progressBar).toHaveProperty('increment');
|
|
expect(progressBar).toHaveProperty('stop');
|
|
expect(typeof progressBar.increment).toBe('function');
|
|
expect(typeof progressBar.stop).toBe('function');
|
|
});
|
|
|
|
it('handles errors when incrementing progress', () => {
|
|
const progressBar = createProgressBar(10);
|
|
|
|
// Call increment and make sure it doesn't throw an error
|
|
expect(() => {
|
|
progressBar.increment();
|
|
}).not.toThrow();
|
|
|
|
expect(logger.warn).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('handles errors when stopping progress', () => {
|
|
const progressBar = createProgressBar(10);
|
|
|
|
// Call stop and make sure it doesn't throw an error
|
|
expect(() => {
|
|
progressBar.stop();
|
|
}).not.toThrow();
|
|
|
|
expect(logger.warn).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('writeVideoFile', () => {
|
|
it('writes a base64 video to a file', async () => {
|
|
await writeVideoFile(DUMMY_VIDEO_BASE64, 'test.mp4');
|
|
|
|
expect(fsPromises.writeFile).toHaveBeenCalledWith('test.mp4', expect.any(Buffer));
|
|
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Video file written to'));
|
|
});
|
|
|
|
it('throws an error if writing fails', async () => {
|
|
const mockError = new Error('Write failed');
|
|
vi.mocked(fsPromises.writeFile).mockRejectedValueOnce(mockError);
|
|
|
|
await expect(writeVideoFile(DUMMY_VIDEO_BASE64, 'test.mp4')).rejects.toThrow('Write failed');
|
|
expect(logger.error).toHaveBeenCalledWith(
|
|
expect.stringContaining('Failed to write video file'),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('addVideoToBase64', () => {
|
|
it('processes test cases correctly using mock generator', async () => {
|
|
// Create a test case
|
|
const testCase: TestCase = {
|
|
vars: {
|
|
prompt: 'test prompt',
|
|
},
|
|
assert: [
|
|
{
|
|
type: 'promptfoo:redteam:test',
|
|
metric: 'test-metric',
|
|
},
|
|
],
|
|
};
|
|
|
|
// Process the test case
|
|
const result = await addVideoToBase64([testCase], 'prompt', mockVideoGenerator);
|
|
|
|
// Verify the structure and content of the result
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0].vars?.prompt).toBe(DUMMY_VIDEO_BASE64);
|
|
expect(result[0].vars?.video_text).toBe('test prompt');
|
|
expect(result[0].metadata?.strategyId).toBe('video');
|
|
expect(result[0].metadata?.originalText).toBe('test prompt');
|
|
expect(result[0].assert?.[0].metric).toBe('test/Video-Encoded');
|
|
|
|
// Verify the mock generator was called
|
|
expect(mockVideoGenerator).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('throws an error if vars is missing', async () => {
|
|
const emptyTestCase = {} as TestCase;
|
|
|
|
await expect(addVideoToBase64([emptyTestCase], 'prompt', mockVideoGenerator)).rejects.toThrow(
|
|
'Video encoding: testCase.vars is required',
|
|
);
|
|
});
|
|
|
|
it('preserves non-redteam assertion metrics', async () => {
|
|
const testCase: TestCase = {
|
|
vars: {
|
|
prompt: 'test prompt',
|
|
},
|
|
assert: [
|
|
{
|
|
// @ts-expect-error: Testing non-redteam type
|
|
type: 'other',
|
|
metric: 'test-metric',
|
|
},
|
|
],
|
|
};
|
|
|
|
const result = await addVideoToBase64([testCase], 'prompt', mockVideoGenerator);
|
|
|
|
expect(result[0].assert?.[0].metric).toBe('test-metric');
|
|
});
|
|
|
|
it('handles multiple test cases', async () => {
|
|
const testCases: TestCase[] = [
|
|
{ vars: { prompt: 'test prompt 1' } },
|
|
{ vars: { prompt: 'test prompt 2' } },
|
|
{ vars: { prompt: 'test prompt 3' } },
|
|
];
|
|
|
|
const result = await addVideoToBase64(testCases, 'prompt', mockVideoGenerator);
|
|
|
|
expect(result).toHaveLength(3);
|
|
expect(mockVideoGenerator).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
it('logs debug messages when logger.level is debug', async () => {
|
|
// Set logger level to debug
|
|
Object.defineProperty(logger, 'level', { get: () => 'debug' });
|
|
|
|
const testCase: TestCase = {
|
|
vars: { prompt: 'test prompt' },
|
|
};
|
|
|
|
await addVideoToBase64([testCase], 'prompt', mockVideoGenerator);
|
|
|
|
// Verify debug was called
|
|
expect(logger.debug).toHaveBeenCalledWith('Processed 1 of 1');
|
|
});
|
|
|
|
it('handles errors during processing', async () => {
|
|
const errorCase: TestCase = {
|
|
vars: { prompt: 'error prompt' },
|
|
};
|
|
|
|
// Mock generator that throws an error
|
|
const errorGenerator = vi.fn().mockImplementation(function () {
|
|
throw new Error('Test error');
|
|
});
|
|
|
|
// Expect the function to reject with the error
|
|
await expect(addVideoToBase64([errorCase], 'prompt', errorGenerator)).rejects.toThrow(
|
|
'Test error',
|
|
);
|
|
});
|
|
});
|
|
});
|