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

153 lines
4.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import logger from '../../src/logger';
import { CIProgressReporter } from '../../src/progress/ciProgressReporter';
import { mockConsole, mockProcessEnv } from '../util/utils';
// Mock the logger
vi.mock('../../src/logger', () => ({
default: {
info: vi.fn(),
error: vi.fn(),
},
}));
describe('CIProgressReporter', () => {
let consoleLogMock: ReturnType<typeof mockConsole>;
let restoreEnv: () => void;
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
consoleLogMock = mockConsole('log');
restoreEnv = mockProcessEnv({ GITHUB_ACTIONS: undefined });
});
afterEach(() => {
vi.useRealTimers();
consoleLogMock.mockRestore();
restoreEnv();
});
it('should log start message', () => {
const reporter = new CIProgressReporter(100);
reporter.start();
expect(logger.info).toHaveBeenCalledWith('[Evaluation] Starting 100 test cases...');
});
it('should log milestone updates at 25%, 50%, 75%', () => {
const reporter = new CIProgressReporter(100);
reporter.start();
vi.clearAllMocks();
// Update to 25%
reporter.update(25);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('[Evaluation] ✓ 25% complete (25/100)'),
);
// Update to 50%
reporter.update(50);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('[Evaluation] ✓ 50% complete (50/100)'),
);
// Update to 75%
reporter.update(75);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('[Evaluation] ✓ 75% complete (75/100)'),
);
});
it('should log periodic updates at intervals', () => {
const reporter = new CIProgressReporter(100, 1000); // 1 second interval
reporter.start();
reporter.update(30);
vi.clearAllMocks();
// Advance time by 1 second
vi.advanceTimersByTime(1000);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('[CI Progress] Evaluation running for'),
);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('Completed 30/100 tests (30%)'),
);
});
it('should log finish message', () => {
const reporter = new CIProgressReporter(100);
reporter.start();
reporter.update(100);
vi.clearAllMocks();
reporter.finish();
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('[Evaluation] ✓ Complete! 100/100 tests in'),
);
});
it('should emit GitHub Actions annotations when GITHUB_ACTIONS is set', () => {
const restoreGithubActions = mockProcessEnv({ GITHUB_ACTIONS: 'true' });
try {
const reporter = new CIProgressReporter(100);
reporter.update(25);
expect(consoleLogMock).toHaveBeenCalledWith('::notice::Evaluation 25% complete');
reporter.finish();
expect(consoleLogMock).toHaveBeenCalledWith(
expect.stringContaining('::notice::Evaluation completed: 25/100 tests in'),
);
} finally {
restoreGithubActions();
}
});
it('should handle errors', () => {
const reporter = new CIProgressReporter(100);
reporter.error('Test error message');
expect(logger.error).toHaveBeenCalledWith('[Evaluation Error] Test error message');
});
it('should emit GitHub Actions error annotation', () => {
const restoreGithubActions = mockProcessEnv({ GITHUB_ACTIONS: 'true' });
try {
const reporter = new CIProgressReporter(100);
reporter.error('Test error');
expect(consoleLogMock).toHaveBeenCalledWith('::error::Test error');
} finally {
restoreGithubActions();
}
});
it('should clear interval on finish', () => {
const reporter = new CIProgressReporter(100, 1000);
reporter.start();
// Verify interval is set
expect(vi.getTimerCount()).toBe(1);
reporter.finish();
// Verify interval is cleared
expect(vi.getTimerCount()).toBe(0);
});
it('should format elapsed time correctly', () => {
const reporter = new CIProgressReporter(100);
reporter.start();
// Advance time by 65 seconds
vi.advanceTimersByTime(65000);
reporter.finish();
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('1m 5s'));
});
});