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

265 lines
7.7 KiB
TypeScript

import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { handleDataset, handleEval, handlePrompt, showCommand } from '../../src/commands/show';
import logger from '../../src/logger';
import Eval from '../../src/models/eval';
import { getDatasetFromHash, getEvalFromId, getPromptFromHash } from '../../src/util/database';
import type { Prompt } from '../../src/types/prompts';
vi.mock('../../src/logger');
vi.mock('../../src/models/eval');
vi.mock('../../src/util/database');
describe('show command', () => {
let program: Command;
beforeEach(() => {
program = new Command();
vi.resetAllMocks();
process.exitCode = undefined;
});
describe('show [id]', () => {
it('should show latest eval if no id provided', async () => {
const mockLatestEval = {
id: 'latest123',
createdAt: new Date(),
config: {},
results: [],
prompts: [],
vars: [],
testResults: [],
metrics: {},
getTable: vi.fn().mockResolvedValue({
head: { prompts: [], vars: [] },
body: [],
}),
};
vi.mocked(Eval.latest).mockResolvedValue(mockLatestEval as any);
vi.mocked(Eval.findById).mockResolvedValue(mockLatestEval as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show']);
expect(Eval.latest).toHaveBeenCalledWith();
});
it('should show error if no eval found and no id provided', async () => {
vi.mocked(Eval.latest).mockResolvedValue(undefined);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show']);
expect(logger.error).toHaveBeenCalledWith('No eval found');
expect(process.exitCode).toBe(1);
});
it('should show eval if id matches eval', async () => {
const evalId = 'eval123';
const mockEval = {
id: evalId,
date: new Date(),
config: {},
results: [],
getTable: vi.fn().mockResolvedValue({
head: { prompts: [], vars: [] },
body: [],
}),
};
vi.mocked(getEvalFromId).mockResolvedValue(mockEval as any);
vi.mocked(Eval.findById).mockResolvedValue(mockEval as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', evalId]);
expect(getEvalFromId).toHaveBeenCalledWith(evalId);
});
it('should show prompt if id matches prompt', async () => {
const promptId = 'prompt123';
const mockPrompt: Prompt = {
raw: 'test',
label: 'Test Prompt',
};
vi.mocked(getPromptFromHash).mockResolvedValue({
id: promptId,
prompt: mockPrompt,
evals: [],
} as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', promptId]);
expect(getPromptFromHash).toHaveBeenCalledWith(promptId);
});
it('should show dataset if id matches dataset', async () => {
const datasetId = 'dataset123';
vi.mocked(getDatasetFromHash).mockResolvedValue({
id: datasetId,
prompts: [],
recentEvalDate: new Date(),
recentEvalId: 'eval123',
count: 0,
testCases: [],
} as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', datasetId]);
expect(getDatasetFromHash).toHaveBeenCalledWith(datasetId);
});
it('should show error if no resource found with id', async () => {
const invalidId = 'invalid123';
vi.mocked(getEvalFromId).mockResolvedValue(undefined);
vi.mocked(getPromptFromHash).mockResolvedValue(undefined);
vi.mocked(getDatasetFromHash).mockResolvedValue(undefined);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', invalidId]);
expect(logger.error).toHaveBeenCalledWith(`No resource found with ID ${invalidId}`);
});
});
describe('show eval [id]', () => {
it('should show latest eval if no id provided', async () => {
const mockLatestEval = {
id: 'latest123',
createdAt: new Date(),
config: {},
results: [],
prompts: [],
vars: [],
testResults: [],
metrics: {},
getTable: vi.fn().mockResolvedValue({
head: { prompts: [], vars: [] },
body: [],
}),
};
vi.mocked(Eval.latest).mockResolvedValue(mockLatestEval as any);
vi.mocked(Eval.findById).mockResolvedValue(mockLatestEval as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', 'eval']);
expect(Eval.latest).toHaveBeenCalledWith();
});
it('should show error if no eval found and no id provided', async () => {
vi.mocked(Eval.latest).mockResolvedValue(undefined);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', 'eval']);
expect(logger.error).toHaveBeenCalledWith('No eval found');
expect(process.exitCode).toBe(1);
});
it('should show eval details for provided id', async () => {
const evalId = 'eval123';
vi.mocked(Eval.findById).mockResolvedValue({
id: evalId,
createdAt: new Date(),
config: {},
results: [],
prompts: [],
vars: [],
testResults: [],
metrics: {},
getTable: vi.fn().mockResolvedValue({
head: { prompts: [], vars: [] },
body: [],
}),
} as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', 'eval', evalId]);
expect(Eval.findById).toHaveBeenCalledWith(evalId);
});
});
describe('show prompt', () => {
it('should show prompt details', async () => {
const promptId = 'prompt123';
const mockPrompt: Prompt = {
raw: 'test',
label: 'Test Prompt',
};
vi.mocked(getPromptFromHash).mockResolvedValue({
id: promptId,
prompt: mockPrompt,
evals: [],
} as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', 'prompt', promptId]);
expect(getPromptFromHash).toHaveBeenCalledWith(promptId);
});
});
describe('show dataset', () => {
it('should show dataset details', async () => {
const datasetId = 'dataset123';
vi.mocked(getDatasetFromHash).mockResolvedValue({
id: datasetId,
prompts: [],
recentEvalDate: new Date(),
recentEvalId: 'eval123',
count: 0,
testCases: [],
} as any);
await showCommand(program);
await program.parseAsync(['node', 'test', 'show', 'dataset', datasetId]);
expect(getDatasetFromHash).toHaveBeenCalledWith(datasetId);
});
});
});
describe('handlers', () => {
beforeEach(() => {
vi.resetAllMocks();
process.exitCode = undefined;
});
describe('handlePrompt', () => {
it('should handle prompt not found', async () => {
const promptId = 'nonexistent';
vi.mocked(getPromptFromHash).mockResolvedValue(undefined);
await handlePrompt(promptId);
expect(logger.error).toHaveBeenCalledWith(`Prompt with ID ${promptId} not found.`);
});
});
describe('handleEval', () => {
it('should handle eval not found', async () => {
const evalId = 'nonexistent';
vi.mocked(Eval.findById).mockResolvedValue(undefined);
await handleEval(evalId);
expect(logger.error).toHaveBeenCalledWith(`No evaluation found with ID ${evalId}`);
});
});
describe('handleDataset', () => {
it('should handle dataset not found', async () => {
const datasetId = 'nonexistent';
vi.mocked(getDatasetFromHash).mockResolvedValue(undefined);
await handleDataset(datasetId);
expect(logger.error).toHaveBeenCalledWith(`Dataset with ID ${datasetId} not found.`);
});
});
});