|
1 | | -import { RenderOptions } from '@schummar/react-terminal'; |
2 | 1 | import pty from 'node-pty'; |
3 | 2 | import { setTimeout } from 'node:timers/promises'; |
4 | 3 | import { describe, expect, test } from 'vitest'; |
5 | | -import { Terminal } from 'xterm-headless'; |
6 | 4 | import { resolveCommands, runp } from '../src'; |
7 | | - |
8 | | -type Target = RenderOptions['target'] extends infer T | undefined ? T : never; |
9 | | - |
10 | | -class TestTerminal implements Target { |
11 | | - term: Terminal; |
12 | | - write: Target['write']; |
13 | | - columns: number; |
14 | | - rows: number; |
15 | | - |
16 | | - constructor(private options: { cols: number; rows: number }) { |
17 | | - this.term = new Terminal({ |
18 | | - cols: this.options.cols, |
19 | | - rows: this.options.rows, |
20 | | - allowProposedApi: true, |
21 | | - }); |
22 | | - |
23 | | - this.write = this.term.write.bind(this.term) as Target['write']; |
24 | | - this.columns = this.options.cols; |
25 | | - this.rows = this.options.rows; |
26 | | - } |
27 | | - |
28 | | - getBuffer = (all = false) => { |
29 | | - const buffer = this.term.buffer.active; |
30 | | - const offset = all ? 0 : buffer.baseY; |
31 | | - |
32 | | - return Array(buffer.length - offset) |
33 | | - .fill(0) |
34 | | - .map((x, i) => |
35 | | - buffer |
36 | | - .getLine(offset + i) |
37 | | - ?.translateToString() |
38 | | - .replace(/[⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏]/g, '⠋') |
39 | | - .replace(/\[(.*)s\]/g, (x, y) => `[${''.padEnd(y.length - 4, '#')}.###s]`) |
40 | | - .replace(/\xA0/g, ' '), |
41 | | - ); |
42 | | - }; |
43 | | -} |
44 | | - |
45 | | -const poll = async (assertion: () => void, max = 9_000) => { |
46 | | - const start = performance.now(); |
47 | | - |
48 | | - for (;;) { |
49 | | - try { |
50 | | - assertion(); |
51 | | - return; |
52 | | - } catch (e) { |
53 | | - if (performance.now() - start > max) { |
54 | | - throw e; |
55 | | - } |
56 | | - await setTimeout(10); |
57 | | - } |
58 | | - } |
59 | | -}; |
| 5 | +import { poll, TestTerminal } from './_helperts'; |
60 | 6 |
|
61 | 7 | describe.concurrent('runp', () => { |
62 | 8 | test('node api', async () => { |
@@ -350,48 +296,4 @@ describe.concurrent('runp', () => { |
350 | 296 | expect(exact.map((x) => [x.cmd, ...x.args].join(' '))).toStrictEqual(['pnpm run --silent @complex/name:with:colons']); |
351 | 297 | }); |
352 | 298 | }); |
353 | | - |
354 | | - describe('linear outout', () => { |
355 | | - test('updating output', async () => { |
356 | | - const term = new TestTerminal({ cols: 25, rows: 15 }); |
357 | | - |
358 | | - const [result] = await runp({ |
359 | | - commands: [ |
360 | | - { |
361 | | - name: 'command', |
362 | | - async cmd({ updateOutput }) { |
363 | | - updateOutput('line 1'); |
364 | | - updateOutput('line 2'); |
365 | | - await setTimeout(1000); |
366 | | - updateOutput('line 3'); |
367 | | - }, |
368 | | - }, |
369 | | - ], |
370 | | - target: term, |
371 | | - linearOutput: true, |
372 | | - }); |
373 | | - |
374 | | - await poll( |
375 | | - () => |
376 | | - expect(term.getBuffer()).toEqual([ |
377 | | - ' ', |
378 | | - '-- [command] -> ', |
379 | | - ' ', |
380 | | - 'line 1 ', |
381 | | - 'line 2 ', |
382 | | - 'line 3 ', |
383 | | - ' ', |
384 | | - '<- [command] -- ', |
385 | | - ' ', |
386 | | - '✓ command [#.###s] ', |
387 | | - ' ', |
388 | | - ' ', |
389 | | - ' ', |
390 | | - ' ', |
391 | | - ' ', |
392 | | - ]), |
393 | | - 1000, |
394 | | - ); |
395 | | - }); |
396 | | - }); |
397 | 299 | }); |
0 commit comments