|
| 1 | +/** |
| 2 | + * Cross-runtime terminal tests — node -e and python3 -c from brush-shell. |
| 3 | + * |
| 4 | + * Mounts WasmVM + Node + Python into the same kernel and verifies |
| 5 | + * interactive output through TerminalHarness. |
| 6 | + * |
| 7 | + * Gated: WasmVM binary required for all tests, Pyodide import for Python. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 11 | +import { TerminalHarness } from '../../../kernel/test/terminal-harness.ts'; |
| 12 | +import { |
| 13 | + createIntegrationKernel, |
| 14 | + skipUnlessWasmBuilt, |
| 15 | + type IntegrationKernelResult, |
| 16 | +} from './helpers.ts'; |
| 17 | + |
| 18 | +/** brush-shell interactive prompt. */ |
| 19 | +const PROMPT = 'sh-0.4$ '; |
| 20 | + |
| 21 | +const wasmSkip = skipUnlessWasmBuilt(); |
| 22 | + |
| 23 | +// Dynamic import check — require.resolve finds pyodide but ESM import may fail |
| 24 | +let pyodideImportable = false; |
| 25 | +try { |
| 26 | + await import('pyodide'); |
| 27 | + pyodideImportable = true; |
| 28 | +} catch { |
| 29 | + // pyodide can't be imported as ESM — skip Python tests |
| 30 | +} |
| 31 | + |
| 32 | +// --------------------------------------------------------------------------- |
| 33 | +// Node cross-runtime terminal tests |
| 34 | +// --------------------------------------------------------------------------- |
| 35 | + |
| 36 | +describe.skipIf(wasmSkip)('cross-runtime terminal: node', () => { |
| 37 | + let harness: TerminalHarness; |
| 38 | + let ctx: IntegrationKernelResult; |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + await harness?.dispose(); |
| 42 | + await ctx?.dispose(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('node -e "console.log(42)" → 42 appears on screen', async () => { |
| 46 | + ctx = await createIntegrationKernel({ runtimes: ['wasmvm', 'node'] }); |
| 47 | + harness = new TerminalHarness(ctx.kernel); |
| 48 | + |
| 49 | + await harness.waitFor(PROMPT); |
| 50 | + await harness.type('node -e "console.log(42)"\n'); |
| 51 | + await harness.waitFor(PROMPT, 2, 10_000); |
| 52 | + |
| 53 | + const screen = harness.screenshotTrimmed(); |
| 54 | + expect(screen).toContain('42'); |
| 55 | + // Verify prompt returned |
| 56 | + const lines = screen.split('\n'); |
| 57 | + expect(lines[lines.length - 1]).toBe(PROMPT); |
| 58 | + }, 15_000); |
| 59 | + |
| 60 | + it('^C during node -e — shell survives and prompt returns', async () => { |
| 61 | + ctx = await createIntegrationKernel({ runtimes: ['wasmvm', 'node'] }); |
| 62 | + harness = new TerminalHarness(ctx.kernel); |
| 63 | + |
| 64 | + await harness.waitFor(PROMPT); |
| 65 | + // Start a long-running node process |
| 66 | + harness.shell.write('node -e "setTimeout(() => {}, 60000)"\n'); |
| 67 | + |
| 68 | + // Give it a moment to start, then send ^C |
| 69 | + await new Promise((r) => setTimeout(r, 500)); |
| 70 | + harness.shell.write('\x03'); |
| 71 | + |
| 72 | + // Wait for prompt to return |
| 73 | + await harness.waitFor(PROMPT, 2, 10_000); |
| 74 | + |
| 75 | + // Verify shell is still alive — type another command |
| 76 | + await harness.type('echo alive\n'); |
| 77 | + await harness.waitFor('alive', 1, 5_000); |
| 78 | + |
| 79 | + const screen = harness.screenshotTrimmed(); |
| 80 | + expect(screen).toContain('alive'); |
| 81 | + }, 20_000); |
| 82 | +}); |
| 83 | + |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | +// Python cross-runtime terminal tests |
| 86 | +// --------------------------------------------------------------------------- |
| 87 | + |
| 88 | +describe.skipIf(wasmSkip || !pyodideImportable)('cross-runtime terminal: python', () => { |
| 89 | + let harness: TerminalHarness; |
| 90 | + let ctx: IntegrationKernelResult; |
| 91 | + |
| 92 | + afterEach(async () => { |
| 93 | + await harness?.dispose(); |
| 94 | + await ctx?.dispose(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('python3 -c "print(99)" → 99 appears on screen', async () => { |
| 98 | + ctx = await createIntegrationKernel({ |
| 99 | + runtimes: ['wasmvm', 'python'], |
| 100 | + }); |
| 101 | + harness = new TerminalHarness(ctx.kernel); |
| 102 | + |
| 103 | + await harness.waitFor(PROMPT); |
| 104 | + await harness.type('python3 -c "print(99)"\n'); |
| 105 | + await harness.waitFor(PROMPT, 2, 30_000); |
| 106 | + |
| 107 | + const screen = harness.screenshotTrimmed(); |
| 108 | + expect(screen).toContain('99'); |
| 109 | + // Verify prompt returned |
| 110 | + const lines = screen.split('\n'); |
| 111 | + expect(lines[lines.length - 1]).toBe(PROMPT); |
| 112 | + }, 45_000); |
| 113 | +}); |
0 commit comments