Skip to content

Commit a467781

Browse files
committed
test(core): pin the already-ended input path with a stream that finished
The first version of this test destroyed the stream, which reaches the cancel through the `close` event and therefore passed with the `readableEnded` check removed. A stream that ended before the prompt attached does not emit `end` again, which is the case the check exists for: the test now builds it with a finished `Readable` and asserts `readableEnded` first.
1 parent 8f67778 commit a467781

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

‎packages/core/test/prompts/prompt.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type } from 'arktype';
2+
import { Readable } from 'node:stream';
23
import { cursor } from 'sisteransi';
34
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
45
import { default as Prompt } from '../../src/prompts/prompt.js';
@@ -496,8 +497,16 @@ describe('Prompt with an input that ends', () => {
496497
});
497498

498499
test('cancels when the input already ended before it starts', async () => {
499-
input.destroy();
500-
const instance = new Prompt({ input, output, render: () => 'foo' });
500+
// A stream that has finished emits `end` once; a prompt attached afterwards
501+
// never sees it, which is the "stdin ended before the prompt started" case.
502+
const ended = new Readable({ read() {} });
503+
const endEvent = new Promise<void>((resolve) => ended.once('end', () => resolve()));
504+
ended.push(null);
505+
ended.resume();
506+
await endEvent;
507+
expect(ended.readableEnded).to.equal(true);
508+
509+
const instance = new Prompt({ input: ended, output, render: () => 'foo' });
501510
const result = await instance.prompt();
502511
expect(isCancel(result)).to.equal(true);
503512
expect(instance.state).to.equal('cancel');

0 commit comments

Comments
 (0)