|
| 1 | +import { describe, test, expect, spyOn } from 'bun:test' |
| 2 | +import { logProxyDeny, logForDebugging } from '../../src/utils/debug.js' |
| 3 | + |
| 4 | +describe('logProxyDeny', () => { |
| 5 | + // The key contract: proxy-deny messages must NOT go to stderr. |
| 6 | + // Writing to stderr from the srt host process corrupts TUI rendering |
| 7 | + // in the sandboxed child (e.g. Claude Code CLI). |
| 8 | + // Instead, on macOS the message is sent to the unified log via logger. |
| 9 | + test('does not write to stderr for HTTPS-CONNECT', () => { |
| 10 | + const saved = process.env.SRT_DEBUG |
| 11 | + delete process.env.SRT_DEBUG |
| 12 | + |
| 13 | + const writes: string[] = [] |
| 14 | + const spy = spyOn(process.stderr, 'write').mockImplementation( |
| 15 | + (chunk: string | Uint8Array) => { |
| 16 | + writes.push(typeof chunk === 'string' ? chunk : chunk.toString()) |
| 17 | + return true |
| 18 | + }, |
| 19 | + ) |
| 20 | + |
| 21 | + logProxyDeny('HTTPS-CONNECT', 'api.example.com', 443) |
| 22 | + |
| 23 | + expect(writes).toHaveLength(0) |
| 24 | + spy.mockRestore() |
| 25 | + if (saved !== undefined) process.env.SRT_DEBUG = saved |
| 26 | + }) |
| 27 | + |
| 28 | + test('does not write to stderr for HTTP', () => { |
| 29 | + const writes: string[] = [] |
| 30 | + const spy = spyOn(process.stderr, 'write').mockImplementation( |
| 31 | + (chunk: string | Uint8Array) => { |
| 32 | + writes.push(typeof chunk === 'string' ? chunk : chunk.toString()) |
| 33 | + return true |
| 34 | + }, |
| 35 | + ) |
| 36 | + |
| 37 | + logProxyDeny('HTTP', 'example.com', 80) |
| 38 | + |
| 39 | + expect(writes).toHaveLength(0) |
| 40 | + spy.mockRestore() |
| 41 | + }) |
| 42 | + |
| 43 | + test('does not write to stderr for SOCKS', () => { |
| 44 | + const writes: string[] = [] |
| 45 | + const spy = spyOn(process.stderr, 'write').mockImplementation( |
| 46 | + (chunk: string | Uint8Array) => { |
| 47 | + writes.push(typeof chunk === 'string' ? chunk : chunk.toString()) |
| 48 | + return true |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + logProxyDeny('SOCKS', 'internal.corp', 1080) |
| 53 | + |
| 54 | + expect(writes).toHaveLength(0) |
| 55 | + spy.mockRestore() |
| 56 | + }) |
| 57 | + |
| 58 | + test('does not write to stderr even when SRT_DEBUG is set', () => { |
| 59 | + const saved = process.env.SRT_DEBUG |
| 60 | + process.env.SRT_DEBUG = '1' |
| 61 | + |
| 62 | + const writes: string[] = [] |
| 63 | + const spy = spyOn(process.stderr, 'write').mockImplementation( |
| 64 | + (chunk: string | Uint8Array) => { |
| 65 | + writes.push(typeof chunk === 'string' ? chunk : chunk.toString()) |
| 66 | + return true |
| 67 | + }, |
| 68 | + ) |
| 69 | + |
| 70 | + logProxyDeny('HTTPS-CONNECT', 'blocked.example.com', 443) |
| 71 | + |
| 72 | + expect(writes).toHaveLength(0) |
| 73 | + spy.mockRestore() |
| 74 | + |
| 75 | + process.env.SRT_DEBUG = saved ?? '' |
| 76 | + if (saved === undefined) delete process.env.SRT_DEBUG |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +describe('logForDebugging', () => { |
| 81 | + test('is silent when SRT_DEBUG is not set', () => { |
| 82 | + const saved = process.env.SRT_DEBUG |
| 83 | + delete process.env.SRT_DEBUG |
| 84 | + |
| 85 | + const spy = spyOn(console, 'error').mockImplementation(() => {}) |
| 86 | + logForDebugging('should not appear', { level: 'error' }) |
| 87 | + expect(spy).not.toHaveBeenCalled() |
| 88 | + spy.mockRestore() |
| 89 | + |
| 90 | + if (saved !== undefined) process.env.SRT_DEBUG = saved |
| 91 | + }) |
| 92 | + |
| 93 | + test('logs when SRT_DEBUG is set', () => { |
| 94 | + const saved = process.env.SRT_DEBUG |
| 95 | + process.env.SRT_DEBUG = '1' |
| 96 | + |
| 97 | + const messages: string[] = [] |
| 98 | + const spy = spyOn(console, 'error').mockImplementation( |
| 99 | + (...args: unknown[]) => { |
| 100 | + messages.push(String(args[0])) |
| 101 | + }, |
| 102 | + ) |
| 103 | + logForDebugging('test message', { level: 'error' }) |
| 104 | + expect(messages.some(m => m.includes('test message'))).toBe(true) |
| 105 | + spy.mockRestore() |
| 106 | + |
| 107 | + process.env.SRT_DEBUG = saved ?? '' |
| 108 | + if (saved === undefined) delete process.env.SRT_DEBUG |
| 109 | + }) |
| 110 | +}) |
0 commit comments