|
| 1 | +/** |
| 2 | + * Bridge gap tests for CLI tool support: isTTY, setRawMode, HTTPS, streams. |
| 3 | + * |
| 4 | + * Exercises PTY-backed process TTY detection and raw mode toggling through |
| 5 | + * the kernel PTY line discipline. Uses openShell({ command: 'node', ... }) |
| 6 | + * to spawn Node directly on a PTY — no WasmVM shell needed. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 10 | +import { createKernel } from '../../../secure-exec-core/src/kernel/index.ts'; |
| 11 | +import type { Kernel } from '../../../secure-exec-core/src/kernel/index.ts'; |
| 12 | +import { InMemoryFileSystem } from '../../../secure-exec-browser/src/os-filesystem.ts'; |
| 13 | +import { createNodeRuntime } from '../../../secure-exec-nodejs/src/kernel-runtime.ts'; |
| 14 | + |
| 15 | +async function createNodeKernel(): Promise<{ kernel: Kernel; dispose: () => Promise<void> }> { |
| 16 | + const vfs = new InMemoryFileSystem(); |
| 17 | + const kernel = createKernel({ filesystem: vfs }); |
| 18 | + await kernel.mount(createNodeRuntime()); |
| 19 | + return { kernel, dispose: () => kernel.dispose() }; |
| 20 | +} |
| 21 | + |
| 22 | +/** Collect all output from a PTY-backed process spawned via openShell. */ |
| 23 | +async function runNodeOnPty( |
| 24 | + kernel: Kernel, |
| 25 | + code: string, |
| 26 | + timeout = 10_000, |
| 27 | +): Promise<string> { |
| 28 | + const shell = kernel.openShell({ |
| 29 | + command: 'node', |
| 30 | + args: ['-e', code], |
| 31 | + }); |
| 32 | + |
| 33 | + const chunks: Uint8Array[] = []; |
| 34 | + shell.onData = (data) => chunks.push(data); |
| 35 | + |
| 36 | + const exitCode = await Promise.race([ |
| 37 | + shell.wait(), |
| 38 | + new Promise<number>((_, reject) => |
| 39 | + setTimeout(() => reject(new Error('PTY process timed out')), timeout), |
| 40 | + ), |
| 41 | + ]); |
| 42 | + |
| 43 | + const output = new TextDecoder().decode( |
| 44 | + Buffer.concat(chunks), |
| 45 | + ); |
| 46 | + return output; |
| 47 | +} |
| 48 | + |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | +// PTY isTTY detection |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | + |
| 53 | +describe('bridge gap: isTTY via PTY', () => { |
| 54 | + let ctx: { kernel: Kernel; dispose: () => Promise<void> }; |
| 55 | + |
| 56 | + afterEach(async () => { |
| 57 | + await ctx?.dispose(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('process.stdin.isTTY returns true when spawned with PTY', async () => { |
| 61 | + ctx = await createNodeKernel(); |
| 62 | + const output = await runNodeOnPty(ctx.kernel, "console.log('STDIN_TTY:' + process.stdin.isTTY)"); |
| 63 | + expect(output).toContain('STDIN_TTY:true'); |
| 64 | + }, 15_000); |
| 65 | + |
| 66 | + it('process.stdout.isTTY returns true when spawned with PTY', async () => { |
| 67 | + ctx = await createNodeKernel(); |
| 68 | + const output = await runNodeOnPty(ctx.kernel, "console.log('STDOUT_TTY:' + process.stdout.isTTY)"); |
| 69 | + expect(output).toContain('STDOUT_TTY:true'); |
| 70 | + }, 15_000); |
| 71 | + |
| 72 | + it('process.stderr.isTTY returns true when spawned with PTY', async () => { |
| 73 | + ctx = await createNodeKernel(); |
| 74 | + const output = await runNodeOnPty(ctx.kernel, "console.log('STDERR_TTY:' + process.stderr.isTTY)"); |
| 75 | + expect(output).toContain('STDERR_TTY:true'); |
| 76 | + }, 15_000); |
| 77 | + |
| 78 | + it('isTTY remains false for non-PTY sandbox processes', async () => { |
| 79 | + ctx = await createNodeKernel(); |
| 80 | + |
| 81 | + // Spawn node directly via kernel.spawn (no PTY) |
| 82 | + const stdout: string[] = []; |
| 83 | + const proc = ctx.kernel.spawn('node', ['-e', "console.log('STDIN_TTY:' + process.stdin.isTTY + ',STDOUT_TTY:' + process.stdout.isTTY)"], { |
| 84 | + onStdout: (data) => stdout.push(new TextDecoder().decode(data)), |
| 85 | + }); |
| 86 | + const exitCode = await proc.wait(); |
| 87 | + |
| 88 | + expect(exitCode).toBe(0); |
| 89 | + const output = stdout.join(''); |
| 90 | + expect(output).toMatch(/STDIN_TTY:(false|undefined)/); |
| 91 | + expect(output).toMatch(/STDOUT_TTY:(false|undefined)/); |
| 92 | + }, 15_000); |
| 93 | +}); |
| 94 | + |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | +// PTY setRawMode |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | + |
| 99 | +describe('bridge gap: setRawMode via PTY', () => { |
| 100 | + let ctx: { kernel: Kernel; dispose: () => Promise<void> }; |
| 101 | + |
| 102 | + afterEach(async () => { |
| 103 | + await ctx?.dispose(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('setRawMode(true) succeeds when stdin is a TTY', async () => { |
| 107 | + ctx = await createNodeKernel(); |
| 108 | + const output = await runNodeOnPty(ctx.kernel, "process.stdin.setRawMode(true); console.log('RAW_OK')"); |
| 109 | + expect(output).toContain('RAW_OK'); |
| 110 | + }, 15_000); |
| 111 | + |
| 112 | + it('setRawMode(false) restores PTY defaults', async () => { |
| 113 | + ctx = await createNodeKernel(); |
| 114 | + const output = await runNodeOnPty( |
| 115 | + ctx.kernel, |
| 116 | + "process.stdin.setRawMode(true); process.stdin.setRawMode(false); console.log('RESTORE_OK')", |
| 117 | + ); |
| 118 | + expect(output).toContain('RESTORE_OK'); |
| 119 | + }, 15_000); |
| 120 | + |
| 121 | + it('setRawMode throws when stdin is not a TTY', async () => { |
| 122 | + ctx = await createNodeKernel(); |
| 123 | + |
| 124 | + // Spawn node directly via kernel.spawn (no PTY) |
| 125 | + const stderr: string[] = []; |
| 126 | + const proc = ctx.kernel.spawn('node', ['-e', ` |
| 127 | + try { |
| 128 | + process.stdin.setRawMode(true); |
| 129 | + console.log('SHOULD_NOT_REACH'); |
| 130 | + } catch (e) { |
| 131 | + console.error('ERR:' + e.message); |
| 132 | + } |
| 133 | + `], { |
| 134 | + onStderr: (data) => stderr.push(new TextDecoder().decode(data)), |
| 135 | + }); |
| 136 | + await proc.wait(); |
| 137 | + |
| 138 | + const output = stderr.join(''); |
| 139 | + expect(output).toContain('ERR:'); |
| 140 | + expect(output).toContain('not a TTY'); |
| 141 | + }, 15_000); |
| 142 | +}); |
0 commit comments