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
109 lines
3.6 KiB
TypeScript
109 lines
3.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import logger from '../../src/logger';
|
|
import { sanitizeScriptContext } from '../../src/providers/scriptContext';
|
|
import { createMockProvider } from '../factories/provider';
|
|
|
|
import type { CallApiContextParams } from '../../src/types/index';
|
|
|
|
vi.mock('../../src/logger', () => ({
|
|
default: {
|
|
debug: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('sanitizeScriptContext', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(logger.debug).mockClear();
|
|
});
|
|
|
|
it('returns undefined when context is undefined', () => {
|
|
expect(sanitizeScriptContext('PythonProvider', undefined)).toBeUndefined();
|
|
expect(logger.debug).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('strips non-serializable keys and logs them', () => {
|
|
const originalProvider = createMockProvider({ id: 'x' });
|
|
const context = {
|
|
vars: { foo: 'bar' },
|
|
getCache: vi.fn(),
|
|
logger: { debug: vi.fn() },
|
|
filters: { uppercase: () => '' },
|
|
originalProvider,
|
|
} as unknown as CallApiContextParams;
|
|
|
|
const sanitized = sanitizeScriptContext('PythonProvider', context);
|
|
|
|
expect(sanitized).toEqual({ vars: { foo: 'bar' } });
|
|
expect(sanitized).not.toHaveProperty('getCache');
|
|
expect(sanitized).not.toHaveProperty('logger');
|
|
expect(sanitized).not.toHaveProperty('filters');
|
|
expect(sanitized).not.toHaveProperty('originalProvider');
|
|
|
|
expect(logger.debug).toHaveBeenCalledTimes(1);
|
|
const message = vi.mocked(logger.debug).mock.calls[0][0] as string;
|
|
expect(message).toBe(
|
|
'PythonProvider sanitized context: stripped non-serializable keys [getCache, logger, filters, originalProvider]',
|
|
);
|
|
});
|
|
|
|
it('does not mutate the caller-owned context', () => {
|
|
const originalProvider = createMockProvider({ id: 'x' });
|
|
const context = {
|
|
vars: { foo: 'bar' },
|
|
getCache: vi.fn(),
|
|
logger: { debug: vi.fn() },
|
|
filters: { uppercase: () => '' },
|
|
originalProvider,
|
|
} as unknown as CallApiContextParams;
|
|
|
|
sanitizeScriptContext('RubyProvider', context);
|
|
|
|
expect(context.getCache).toBeDefined();
|
|
expect(context.logger).toBeDefined();
|
|
expect(context.filters).toBeDefined();
|
|
expect(context.originalProvider).toBe(originalProvider);
|
|
});
|
|
|
|
it('does not log when no non-serializable keys are present', () => {
|
|
const context = { vars: { foo: 'bar' } } as unknown as CallApiContextParams;
|
|
|
|
const sanitized = sanitizeScriptContext('PythonProvider', context);
|
|
|
|
expect(sanitized).toEqual({ vars: { foo: 'bar' } });
|
|
expect(logger.debug).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('uses the provided provider label in the log message', () => {
|
|
const context = {
|
|
vars: {},
|
|
logger: { debug: vi.fn() },
|
|
} as unknown as CallApiContextParams;
|
|
|
|
sanitizeScriptContext('RubyProvider', context);
|
|
|
|
expect(logger.debug).toHaveBeenCalledTimes(1);
|
|
expect(vi.mocked(logger.debug).mock.calls[0][0]).toContain('RubyProvider');
|
|
});
|
|
|
|
it('only strips keys that are actually present', () => {
|
|
const context = {
|
|
vars: { foo: 'bar' },
|
|
getCache: vi.fn(),
|
|
} as unknown as CallApiContextParams;
|
|
|
|
const sanitized = sanitizeScriptContext('PythonProvider', context);
|
|
|
|
expect(sanitized).toEqual({ vars: { foo: 'bar' } });
|
|
expect(sanitized).not.toHaveProperty('getCache');
|
|
expect(logger.debug).toHaveBeenCalledTimes(1);
|
|
const message = vi.mocked(logger.debug).mock.calls[0][0] as string;
|
|
expect(message).toContain('getCache');
|
|
expect(message).not.toContain('logger');
|
|
expect(message).not.toContain('filters');
|
|
expect(message).not.toContain('originalProvider');
|
|
});
|
|
});
|