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
194 lines
7.0 KiB
TypeScript
194 lines
7.0 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
vi.mock('../../src/envars', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('../../src/envars')>()),
|
|
isCI: vi.fn(() => false),
|
|
}));
|
|
|
|
describe('verboseToggle', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it('should export initVerboseToggle function', async () => {
|
|
const { initVerboseToggle } = await import('../../src/util/verboseToggle');
|
|
expect(typeof initVerboseToggle).toBe('function');
|
|
});
|
|
|
|
it('should export disableVerboseToggle function', async () => {
|
|
const { disableVerboseToggle } = await import('../../src/util/verboseToggle');
|
|
expect(typeof disableVerboseToggle).toBe('function');
|
|
});
|
|
|
|
it('should export isVerboseToggleActive function', async () => {
|
|
const { isVerboseToggleActive } = await import('../../src/util/verboseToggle');
|
|
expect(typeof isVerboseToggleActive).toBe('function');
|
|
});
|
|
|
|
it('should return null when not in TTY mode', async () => {
|
|
// Save original values
|
|
const originalStdinIsTTY = process.stdin.isTTY;
|
|
const originalStdoutIsTTY = process.stdout.isTTY;
|
|
|
|
// Mock non-TTY mode
|
|
Object.defineProperty(process.stdin, 'isTTY', { value: false, configurable: true });
|
|
Object.defineProperty(process.stdout, 'isTTY', { value: false, configurable: true });
|
|
|
|
const { initVerboseToggle } = await import('../../src/util/verboseToggle');
|
|
const result = initVerboseToggle({ onInterrupt: vi.fn() });
|
|
|
|
expect(result).toBeNull();
|
|
|
|
// Restore original values
|
|
Object.defineProperty(process.stdin, 'isTTY', {
|
|
value: originalStdinIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdout, 'isTTY', {
|
|
value: originalStdoutIsTTY,
|
|
configurable: true,
|
|
});
|
|
});
|
|
|
|
it('should initially report toggle as inactive', async () => {
|
|
const { isVerboseToggleActive } = await import('../../src/util/verboseToggle');
|
|
// In non-TTY test environment, toggle won't be enabled
|
|
expect(isVerboseToggleActive()).toBe(false);
|
|
});
|
|
|
|
it('disableVerboseToggle should not throw when not initialized', async () => {
|
|
const { disableVerboseToggle } = await import('../../src/util/verboseToggle');
|
|
expect(() => disableVerboseToggle()).not.toThrow();
|
|
});
|
|
|
|
it('should fall back to process.exit(130) when onInterrupt throws so Ctrl+C is never broken', async () => {
|
|
const originalStdinIsTTY = process.stdin.isTTY;
|
|
const originalStdoutIsTTY = process.stdout.isTTY;
|
|
const originalSetRawMode = process.stdin.setRawMode;
|
|
const onInterrupt = vi.fn(() => {
|
|
throw new Error('caller-decided shutdown failed');
|
|
});
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);
|
|
const stderrWriteSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
|
|
Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdout, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdin, 'setRawMode', { value: vi.fn(), configurable: true });
|
|
|
|
try {
|
|
const { initVerboseToggle, isVerboseToggleActive } = await import(
|
|
'../../src/util/verboseToggle'
|
|
);
|
|
|
|
initVerboseToggle({ onInterrupt });
|
|
expect(() => process.stdin.emit('data', '\u0003')).not.toThrow();
|
|
|
|
expect(onInterrupt).toHaveBeenCalledTimes(1);
|
|
expect(exitSpy).toHaveBeenCalledWith(130);
|
|
expect(isVerboseToggleActive()).toBe(false);
|
|
} finally {
|
|
Object.defineProperty(process.stdin, 'isTTY', {
|
|
value: originalStdinIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdout, 'isTTY', {
|
|
value: originalStdoutIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdin, 'setRawMode', {
|
|
value: originalSetRawMode,
|
|
configurable: true,
|
|
});
|
|
stderrWriteSpy.mockRestore();
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('should remove its exit listener during cleanup so init/teardown cycles do not leak', async () => {
|
|
const originalStdinIsTTY = process.stdin.isTTY;
|
|
const originalStdoutIsTTY = process.stdout.isTTY;
|
|
const originalSetRawMode = process.stdin.setRawMode;
|
|
const stderrWriteSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
|
|
Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdout, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdin, 'setRawMode', { value: vi.fn(), configurable: true });
|
|
|
|
try {
|
|
const { initVerboseToggle, disableVerboseToggle } = await import(
|
|
'../../src/util/verboseToggle'
|
|
);
|
|
|
|
const before = process.listenerCount('exit');
|
|
const cleanup = initVerboseToggle({ onInterrupt: vi.fn() });
|
|
expect(cleanup).not.toBeNull();
|
|
expect(process.listenerCount('exit')).toBe(before + 1);
|
|
|
|
disableVerboseToggle();
|
|
expect(process.listenerCount('exit')).toBe(before);
|
|
} finally {
|
|
Object.defineProperty(process.stdin, 'isTTY', {
|
|
value: originalStdinIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdout, 'isTTY', {
|
|
value: originalStdoutIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdin, 'setRawMode', {
|
|
value: originalSetRawMode,
|
|
configurable: true,
|
|
});
|
|
stderrWriteSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('should delegate Ctrl+C to the caller without exiting the process', async () => {
|
|
const originalStdinIsTTY = process.stdin.isTTY;
|
|
const originalStdoutIsTTY = process.stdout.isTTY;
|
|
const originalSetRawMode = process.stdin.setRawMode;
|
|
const onInterrupt = vi.fn();
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((() => undefined) as never);
|
|
const stderrWriteSpy = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
|
|
Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdout, 'isTTY', { value: true, configurable: true });
|
|
Object.defineProperty(process.stdin, 'setRawMode', {
|
|
value: vi.fn(),
|
|
configurable: true,
|
|
});
|
|
|
|
try {
|
|
const { initVerboseToggle, isVerboseToggleActive } = await import(
|
|
'../../src/util/verboseToggle'
|
|
);
|
|
|
|
initVerboseToggle({ onInterrupt });
|
|
process.stdin.emit('data', '\u0003');
|
|
|
|
expect(onInterrupt).toHaveBeenCalledTimes(1);
|
|
expect(exitSpy).not.toHaveBeenCalled();
|
|
expect(isVerboseToggleActive()).toBe(false);
|
|
} finally {
|
|
Object.defineProperty(process.stdin, 'isTTY', {
|
|
value: originalStdinIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdout, 'isTTY', {
|
|
value: originalStdoutIsTTY,
|
|
configurable: true,
|
|
});
|
|
Object.defineProperty(process.stdin, 'setRawMode', {
|
|
value: originalSetRawMode,
|
|
configurable: true,
|
|
});
|
|
stderrWriteSpy.mockRestore();
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
});
|