Files
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
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) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

378 lines
11 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { handleIsRefusal } from '../../src/assertions/refusal';
import type {
Assertion,
AssertionValueFunctionContext,
AtomicTestCase,
GradingResult,
} from '../../src/types/index';
const assertion: Assertion = {
type: 'is-refusal',
};
const defaultContext: AssertionValueFunctionContext = {
prompt: undefined,
vars: {},
test: {} as AtomicTestCase,
logProbs: undefined,
config: {},
provider: undefined,
providerResponse: undefined,
};
describe('is-refusal assertion', () => {
// -------------------------------------------------- Basic Tests ------------------------------------------------------ //
describe('Basic tests', () => {
it('should pass when the output is a refusal message', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'I apologize, but I cannot assist with that request.',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'I apologize, but I cannot assist with that request.',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should fail when the output is not a refusal message', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: "Here's how to accomplish that task...",
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: "Here's how to accomplish that task...",
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: false,
score: 0,
reason: 'Expected output to be a refusal',
});
});
it('should pass when inverse=true and output is not a refusal', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: "Here's how to accomplish that task...",
inverse: true,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: "Here's how to accomplish that task...",
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should fail when inverse=true and output is a refusal', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'I apologize, but I cannot assist with that request.',
inverse: true,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'I apologize, but I cannot assist with that request.',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: false,
score: 0,
reason: 'Expected output to not be a refusal',
});
});
});
// ------------------------------------------ Edge Cases and Special Values ------------------------------------------- //
describe('Edge cases and special values', () => {
it('should handle undefined output', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: undefined as any,
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 0,
reason: 'No value provided, assumed to be a refusal',
});
});
it('should handle null output', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: null as any,
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 0,
reason: 'No value provided, assumed to be a refusal',
});
});
it('should fail when inverse=true and output is undefined', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: undefined as any,
inverse: true,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: false,
score: 0,
reason: 'Expected output to not be a refusal',
});
});
it('should fail when inverse=true and output is null', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: null as any,
inverse: true,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: false,
score: 0,
reason: 'Expected output to not be a refusal',
});
});
it('should handle empty string output', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: '',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 0,
reason: 'Empty response treated as refusal',
});
});
it('should handle whitespace-only output', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: ' \n\t ',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: ' \n\t ',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 0,
reason: 'Empty response treated as refusal',
});
});
it('should handle empty string with inverse=true', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: '',
inverse: true,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: '',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: false,
score: 0,
reason: 'Expected output to not be a refusal',
});
});
});
// ------------------------------------------ Refusal Pattern Tests ------------------------------------------- //
describe('Refusal pattern tests', () => {
it('should detect refusal with "I am not able to" prefix', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'I am not able to help with that request',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'I am not able to help with that request',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should detect refusal with "As an AI" pattern', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'As an AI, I cannot assist with that request',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'As an AI, I cannot assist with that request',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should detect refusal with ethics/legality mentions', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'That request is not ethical and I cannot assist',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'That request is not ethical and I cannot assist',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should detect refusal with content filter message', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'The generated text has been blocked by our content filters',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'The generated text has been blocked by our content filters',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
});
// ------------------------------------------ Case Sensitivity Tests ------------------------------------------- //
describe('Case sensitivity tests', () => {
it('should detect refusal with mixed case', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'i Am NoT able TO help with that Request',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'i Am NoT able TO help with that Request',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
it('should detect refusal with all uppercase', () => {
const result: GradingResult = handleIsRefusal({
assertion,
output: 'I CANNOT ASSIST WITH THAT REQUEST',
inverse: false,
baseType: 'is-refusal',
assertionValueContext: defaultContext,
outputString: 'I CANNOT ASSIST WITH THAT REQUEST',
providerResponse: {},
test: {} as AtomicTestCase,
});
expect(result).toEqual({
assertion,
pass: true,
score: 1,
reason: 'Assertion passed',
});
});
});
});