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
177 lines
6.0 KiB
TypeScript
177 lines
6.0 KiB
TypeScript
import { EventEmitter } from 'events';
|
|
import type { WriteStream } from 'fs';
|
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import { JsonlFileWriter } from '../../src/util/exportToFile/writeToFile';
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
createWriteStream: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('fs', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('fs')>()),
|
|
createWriteStream: mocks.createWriteStream,
|
|
}));
|
|
|
|
function createMockWriteStream({ destroyed = false }: { destroyed?: boolean } = {}) {
|
|
const stream = Object.assign(new EventEmitter(), {
|
|
closed: false,
|
|
destroyed,
|
|
end: vi.fn(),
|
|
// Mirror Node semantics: destroy() marks the stream destroyed and emits 'close'.
|
|
destroy: vi.fn(() => {
|
|
stream.destroyed = true;
|
|
stream.emit('close');
|
|
return stream;
|
|
}),
|
|
write: vi.fn((_data: string, callback?: (error?: Error | null) => void) => {
|
|
callback?.();
|
|
return true;
|
|
}),
|
|
});
|
|
mocks.createWriteStream.mockReturnValue(stream as unknown as WriteStream);
|
|
return stream;
|
|
}
|
|
|
|
describe('JsonlFileWriter', () => {
|
|
afterEach(() => {
|
|
vi.resetAllMocks();
|
|
});
|
|
|
|
it('opens the stream lazily and truncates by default, appending only when requested', async () => {
|
|
createMockWriteStream();
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
// Constructing the writer must not open (and thus truncate) the destination file.
|
|
expect(mocks.createWriteStream).not.toHaveBeenCalled();
|
|
|
|
await writer.write({ a: 1 });
|
|
expect(mocks.createWriteStream).toHaveBeenCalledWith('/tmp/results.jsonl', { flags: 'w' });
|
|
|
|
mocks.createWriteStream.mockClear();
|
|
createMockWriteStream();
|
|
const appender = new JsonlFileWriter('/tmp/results.jsonl', { append: true });
|
|
await appender.write({ a: 1 });
|
|
expect(mocks.createWriteStream).toHaveBeenCalledWith('/tmp/results.jsonl', { flags: 'a' });
|
|
});
|
|
|
|
it('leaves the destination untouched when nothing is written', async () => {
|
|
createMockWriteStream();
|
|
await expect(new JsonlFileWriter('/tmp/results.jsonl').close()).resolves.toBeUndefined();
|
|
expect(mocks.createWriteStream).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('waits for the underlying file descriptor to close', async () => {
|
|
const stream = createMockWriteStream();
|
|
stream.end.mockImplementation(() => stream);
|
|
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
const closePromise = writer.close();
|
|
let settled = false;
|
|
closePromise.finally(() => {
|
|
settled = true;
|
|
});
|
|
|
|
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
expect(settled).toBe(false);
|
|
|
|
stream.emit('close');
|
|
await expect(closePromise).resolves.toBeUndefined();
|
|
// Only the persistent recorder remains; close()'s temporary error listener was removed.
|
|
expect(stream.listenerCount('error')).toBe(1);
|
|
});
|
|
|
|
it('rejects errors emitted while the file descriptor is closing', async () => {
|
|
const stream = createMockWriteStream();
|
|
stream.end.mockImplementation(() => stream);
|
|
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
const closePromise = writer.close();
|
|
|
|
stream.emit('error', new Error('late close error'));
|
|
stream.emit('close');
|
|
|
|
await expect(closePromise).rejects.toThrow(
|
|
'Failed to close JSONL output /tmp/results.jsonl: late close error',
|
|
);
|
|
});
|
|
|
|
it('resolves immediately when the stream already closed cleanly', async () => {
|
|
const stream = createMockWriteStream();
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
|
|
stream.closed = true;
|
|
await expect(writer.close()).resolves.toBeUndefined();
|
|
expect(stream.end).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects with a previously recorded error when the stream already closed', async () => {
|
|
const stream = createMockWriteStream();
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
|
|
// A real stream that fails mid-run auto-destroys: 'error' and 'close' fire
|
|
// before close() is ever called. The recorded error must still reject close()
|
|
// rather than be swallowed (the output file is truncated/incomplete).
|
|
stream.emit('error', new Error('EBADF: bad file descriptor'));
|
|
stream.closed = true;
|
|
|
|
await expect(writer.close()).rejects.toThrow(
|
|
'Failed to close JSONL output /tmp/results.jsonl: EBADF: bad file descriptor',
|
|
);
|
|
expect(stream.end).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('waits for a destroyed stream to close without calling end()', async () => {
|
|
const stream = createMockWriteStream();
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
|
|
stream.destroyed = true;
|
|
const closePromise = writer.close();
|
|
let settled = false;
|
|
closePromise.finally(() => {
|
|
settled = true;
|
|
});
|
|
|
|
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
expect(settled).toBe(false);
|
|
expect(stream.end).not.toHaveBeenCalled();
|
|
|
|
stream.emit('close');
|
|
await expect(closePromise).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('destroys the stream and rejects when end() throws synchronously', async () => {
|
|
const stream = createMockWriteStream();
|
|
stream.end.mockImplementation(() => {
|
|
throw new Error('end failed');
|
|
});
|
|
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
|
|
await expect(writer.close()).rejects.toThrow(
|
|
'Failed to close JSONL output /tmp/results.jsonl: end failed',
|
|
);
|
|
expect(stream.destroy).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('rejects final flush errors with the output path', async () => {
|
|
const stream = createMockWriteStream();
|
|
stream.end.mockImplementation(() => {
|
|
stream.emit('error', new Error('disk full'));
|
|
stream.emit('close');
|
|
return stream;
|
|
});
|
|
|
|
const writer = new JsonlFileWriter('/tmp/results.jsonl');
|
|
await writer.write({ a: 1 });
|
|
await expect(writer.close()).rejects.toThrow(
|
|
'Failed to close JSONL output /tmp/results.jsonl: disk full',
|
|
);
|
|
});
|
|
});
|