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

74 lines
2.9 KiB
TypeScript

import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import Eval from '../../src/models/eval';
import { nodeEvaluatorRuntime } from '../../src/node/evaluatorRuntime';
import type { EvaluateResult } from '../../src/types/index';
describe('nodeEvaluatorRuntime', () => {
const tempDirs: string[] = [];
afterEach(() => {
vi.clearAllMocks();
for (const tempDir of tempDirs.splice(0)) {
// On Windows, file handles can linger briefly after a stream is closed, so an
// immediate recursive delete may throw ENOTEMPTY/EBUSY/EPERM. Retry with a short
// backoff (a no-op on POSIX, where these errors don't occur).
fs.rmSync(tempDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
}
});
it('creates and closes JSONL writers for JSONL output paths only', async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'promptfoo-evaluator-runtime-'));
tempDirs.push(tempDir);
const jsonlPath = path.join(tempDir, 'results.jsonl');
const uppercaseJsonlPath = path.join(tempDir, 'uppercase.JSONL');
const csvPath = path.join(tempDir, 'results.csv');
const writers = nodeEvaluatorRuntime.createResultWriters(
[jsonlPath, uppercaseJsonlPath, csvPath],
{ append: false },
);
expect(writers).toHaveLength(2);
await writers[0].write({ output: 'hello' });
await writers[1].write({ output: 'uppercase' });
await writers[0].close();
await writers[1].close();
expect(fs.readFileSync(jsonlPath, 'utf8')).toBe('{"output":"hello"}\n');
expect(fs.readFileSync(uppercaseJsonlPath, 'utf8')).toBe('{"output":"uppercase"}\n');
});
it('truncates by default and appends when resuming', async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'promptfoo-evaluator-runtime-'));
tempDirs.push(tempDir);
const jsonlPath = path.join(tempDir, 'results.jsonl');
fs.writeFileSync(jsonlPath, 'stale row\n');
const [writer] = nodeEvaluatorRuntime.createResultWriters(jsonlPath, { append: false });
await writer.write({ output: 'fresh' });
await writer.close();
expect(fs.readFileSync(jsonlPath, 'utf8')).toBe('{"output":"fresh"}\n');
const [appender] = nodeEvaluatorRuntime.createResultWriters(jsonlPath, { append: true });
await appender.write({ output: 'resumed' });
await appender.close();
expect(fs.readFileSync(jsonlPath, 'utf8')).toBe('{"output":"fresh"}\n{"output":"resumed"}\n');
});
it('creates an Eval-backed evaluation store', async () => {
const result = { success: true } as EvaluateResult;
const evaluation = new Eval({});
const addResult = vi.spyOn(evaluation, 'addResult').mockResolvedValue(undefined);
const store = nodeEvaluatorRuntime.createEvaluationStore(evaluation);
await store.appendResult(result);
expect(store.evaluation).toBe(evaluation);
expect(addResult).toHaveBeenCalledWith(result);
});
});