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

230 lines
6.7 KiB
TypeScript

import confirm from '@inquirer/confirm';
import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { deleteCommand, handleEvalDelete, handleEvalDeleteAll } from '../../src/commands/delete';
import logger from '../../src/logger';
import Eval from '../../src/models/eval';
import * as database from '../../src/util/database';
import type { EvalWithMetadata } from '../../src/types/index';
vi.mock('@inquirer/confirm');
vi.mock('../../src/util/database');
vi.mock('../../src/logger');
vi.mock('../../src/models/eval');
describe('delete command', () => {
let program: Command;
beforeEach(() => {
program = new Command();
vi.resetAllMocks();
process.exitCode = undefined;
});
describe('handleEvalDelete', () => {
it('should successfully delete evaluation', async () => {
await handleEvalDelete('test-id');
expect(database.deleteEval).toHaveBeenCalledWith('test-id');
expect(logger.info).toHaveBeenCalledWith(
'Evaluation with ID test-id has been successfully deleted.',
);
});
it('should handle error when deleting evaluation', async () => {
const error = new Error('Delete failed');
vi.mocked(database.deleteEval).mockRejectedValueOnce(error);
await handleEvalDelete('test-id');
expect(logger.error).toHaveBeenCalledWith(
'Could not delete evaluation with ID test-id:\nError: Delete failed',
);
expect(process.exitCode).toBe(1);
});
});
describe('handleEvalDeleteAll', () => {
it('should delete all evaluations when confirmed', async () => {
vi.mocked(confirm).mockResolvedValueOnce(true);
await handleEvalDeleteAll();
expect(database.deleteAllEvals).toHaveBeenCalledWith();
expect(logger.info).toHaveBeenCalledWith('All evaluations have been deleted.');
});
it('should not delete evaluations when not confirmed', async () => {
vi.mocked(confirm).mockResolvedValueOnce(false);
await handleEvalDeleteAll();
expect(database.deleteAllEvals).not.toHaveBeenCalled();
});
});
describe('delete command', () => {
it('should handle eval deletion when resource exists', async () => {
const mockEval = {
id: 'test-id',
date: new Date(),
config: {},
version: 3,
timestamp: new Date().toISOString(),
results: {
version: 3,
timestamp: new Date().toISOString(),
results: [],
prompts: [],
stats: {
successes: 0,
failures: 0,
errors: 0,
duration: 0,
tokenUsage: {
total: 0,
prompt: 0,
completion: 0,
cached: 0,
numRequests: 0,
completionDetails: {
reasoning: 0,
acceptedPrediction: 0,
rejectedPrediction: 0,
},
assertions: {} as any,
},
},
},
prompts: [],
stats: {
successes: 0,
failures: 0,
errors: 0,
duration: 0,
tokenUsage: {
total: 0,
prompt: 0,
completion: 0,
cached: 0,
numRequests: 0,
completionDetails: {
reasoning: 0,
acceptedPrediction: 0,
rejectedPrediction: 0,
},
assertions: {} as any,
},
},
} as EvalWithMetadata;
vi.mocked(database.getEvalFromId).mockResolvedValueOnce(mockEval);
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'test-id']);
expect(database.getEvalFromId).toHaveBeenCalledWith('test-id');
expect(database.deleteEval).toHaveBeenCalledWith('test-id');
});
it('should handle when resource does not exist', async () => {
vi.mocked(database.getEvalFromId).mockResolvedValueOnce(undefined);
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'test-id']);
expect(logger.error).toHaveBeenCalledWith('No resource found with ID test-id');
expect(process.exitCode).toBe(1);
});
describe('eval subcommand', () => {
it('should handle latest eval deletion', async () => {
const mockLatestEval = {
createdAt: new Date(),
config: {},
results: [],
prompts: [],
assertions: [],
vars: {},
providers: [],
tests: [],
outputs: [],
sharing: false,
description: '',
nunjucksTemplates: {},
version: 3,
grading: {},
metrics: {},
stats: {
successes: 0,
failures: 0,
errors: 0,
duration: 0,
tokenUsage: {
total: 0,
prompt: 0,
completion: 0,
cached: 0,
numRequests: 0,
completionDetails: {
reasoning: 0,
acceptedPrediction: 0,
rejectedPrediction: 0,
},
assertions: {} as any,
},
},
duration: 0,
summary: '',
status: 'completed',
error: null,
testSuites: [],
maxConcurrency: 1,
repeat: 1,
table: [],
views: [],
isUnfinished: false,
isShared: false,
latestGrade: null,
latestScore: null,
id: 'latest-id',
} as any;
vi.mocked(Eval.latest).mockResolvedValueOnce(mockLatestEval);
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'eval', 'latest']);
expect(database.deleteEval).toHaveBeenCalledWith('latest-id');
});
it('should handle when no latest eval exists', async () => {
vi.mocked(Eval.latest).mockResolvedValueOnce(undefined);
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'eval', 'latest']);
expect(logger.error).toHaveBeenCalledWith('No eval found.');
expect(process.exitCode).toBe(1);
});
it('should handle all evals deletion', async () => {
vi.mocked(confirm).mockResolvedValueOnce(true);
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'eval', 'all']);
expect(database.deleteAllEvals).toHaveBeenCalledWith();
});
it('should handle specific eval deletion', async () => {
deleteCommand(program);
await program.parseAsync(['node', 'test', 'delete', 'eval', 'specific-id']);
expect(database.deleteEval).toHaveBeenCalledWith('specific-id');
});
});
});
});