Files
wehub-resource-sync f9447f8e5f
CI / typecheck · build · test · bundle-size (push) Failing after 1s
CI / clean-room dependency closure smoke (push) Failing after 1s
CI / server-runtime e2e (docker · pg + valkey) (push) Failing after 2s
Deploy Install Scripts / deploy (push) Failing after 2s
Windows / build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:07:03 +08:00

52 lines
1.8 KiB
TypeScript

import { describe, it, expect, afterEach } from 'bun:test';
import { Readable } from 'stream';
import { readJsonFromStdin } from '../../src/cli/stdin-reader.js';
const realStdin = process.stdin;
const realStdinDescriptor = Object.getOwnPropertyDescriptor(process, 'stdin');
function installFakeStdin(payload: string): void {
const fake = Readable.from([payload], { objectMode: false }) as unknown as NodeJS.ReadStream;
Object.defineProperty(fake, 'isTTY', { value: false, configurable: true });
Object.defineProperty(process, 'stdin', {
configurable: true,
enumerable: realStdinDescriptor?.enumerable ?? true,
writable: true,
value: fake,
});
}
afterEach(() => {
if (realStdinDescriptor) {
Object.defineProperty(process, 'stdin', realStdinDescriptor);
} else {
Object.defineProperty(process, 'stdin', { value: realStdin, configurable: true, writable: true });
}
});
describe('readJsonFromStdin — onEnd contract (#2089)', () => {
it('resolves with parsed JSON when stdin yields a complete object', async () => {
installFakeStdin('{"hello":"world"}');
const result = await readJsonFromStdin();
expect(result).toEqual({ hello: 'world' });
});
it('resolves with undefined when stdin closes empty', async () => {
installFakeStdin('');
const result = await readJsonFromStdin();
expect(result).toBeUndefined();
});
it('rejects when stdin closes with non-empty but unparseable bytes', async () => {
installFakeStdin('{"truncated":');
await expect(readJsonFromStdin()).rejects.toThrow(/Malformed JSON at stdin EOF/);
});
it('rejects when stdin closes with junk that is clearly not JSON', async () => {
installFakeStdin('not json at all');
await expect(readJsonFromStdin()).rejects.toThrow(/Malformed JSON at stdin EOF/);
});
});