Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 335 additions & 0 deletions packages/core/src/qwen/qwenOAuth2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
qwenOAuth2Events,
QwenOAuth2Event,
QwenOAuth2Client,
showFallbackMessage,
type DeviceAuthorizationResponse,
type DeviceTokenResponse,
type ErrorData,
Expand Down Expand Up @@ -2387,3 +2388,337 @@ describe('Constants and Configuration', () => {
);
});
});

describe('showFallbackMessage', () => {
const ORIGINAL_ENV = { ...process.env };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stderrWriteSpy: any;
let originalIsTTY: boolean;

beforeEach(() => {
originalIsTTY = process.stderr.isTTY ?? false;
stderrWriteSpy = vi
.spyOn(process.stderr, 'write')
.mockImplementation(() => true);
// Reset env — clear CI and other terminal-detection vars so
// supportsOsc8Hyperlinks() doesn't short-circuit on CI runners.
process.env = { ...ORIGINAL_ENV };
delete process.env['CI'];
delete process.env['TMUX'];
delete process.env['STY'];
delete process.env['TERM_PROGRAM'];
delete process.env['TERM_PROGRAM_VERSION'];
delete process.env['WT_SESSION'];
delete process.env['KITTY_WINDOW_ID'];
delete process.env['VTE_VERSION'];
delete process.env['DOMTERM'];
delete process.env['GHOSTTY_RESOURCES_DIR'];
delete process.env['KONSOLE_VERSION'];
delete process.env['TERMINAL_EMULATOR'];
delete process.env['ALACRITTY_LOG'];
delete process.env['ALACRITTY_WINDOW_ID'];
delete process.env['ALACRITTY_SOCKET'];
delete process.env['FORCE_HYPERLINK'];
delete process.env['QWEN_DISABLE_HYPERLINKS'];
delete process.env['TEAMCITY_VERSION'];
delete process.env['NO_COLOR'];
delete process.env['FORCE_COLOR'];
// Keep TERM but clear known OSC 8 terminal names
if (
['xterm-kitty', 'xterm-ghostty', 'alacritty'].includes(
process.env['TERM'] ?? '',
)
) {
delete process.env['TERM'];
}
});

afterEach(() => {
stderrWriteSpy.mockRestore();
process.env = ORIGINAL_ENV;
// Restore isTTY
if (originalIsTTY) {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
} else {
Object.defineProperty(process.stderr, 'isTTY', {
value: false,
configurable: true,
});
}
});

function getOutput(): string {
return stderrWriteSpy.mock.calls.map((c: [string]) => c[0]).join('');
}

it('emits OSC 8 hyperlink when terminal supports it (iTerm.app)', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['TERM_PROGRAM_VERSION'] = '3.5.0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain(
'\x1b]8;;https://chat.qwen.ai/device?code=ABC123\x07',
);
expect(output).toContain('\x1b]8;;\x07');
// URL appears as a single line within the OSC 8 envelope
expect(output).toContain(
'https://chat.qwen.ai/device?code=ABC123\x1b]8;;\x07',
);
});

it('hard-wraps URL when terminal does not support OSC 8 (non-TTY)', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: false,
configurable: true,
});
delete process.env['TERM_PROGRAM'];
delete process.env['FORCE_HYPERLINK'];

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
// URL is present as plain text
expect(output).toContain('https://chat.qwen.ai/device?code=ABC123');
});

it('does not emit OSC 8 when QWEN_DISABLE_HYPERLINKS=1', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['QWEN_DISABLE_HYPERLINKS'] = '1';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('respects FORCE_HYPERLINK=1 even in tmux', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TMUX'] = '/tmp/tmux-1000/default,1234,0';
process.env['FORCE_HYPERLINK'] = '1';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1b]8;;');
// Must use DCS passthrough wrapping for tmux
expect(output).toContain('\x1bPtmux;');
expect(output).toContain('\x1b\\');
});

it('wraps OSC 8 in DCS passthrough for screen (STY)', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['STY'] = '12345.pts-0.host';
process.env['FORCE_HYPERLINK'] = '1';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1bP\x1b]8;;');
expect(output).toContain('\x1b\\');
});

it('refuses OSC 8 in tmux without FORCE_HYPERLINK', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TMUX'] = '/tmp/tmux-1000/default,1234,0';
delete process.env['FORCE_HYPERLINK'];

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('detects Windows Terminal via WT_SESSION', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['WT_SESSION'] = 'xxx';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1b]8;;');
});

it('detects Kitty via TERM=xterm-kitty', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM'] = 'xterm-kitty';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1b]8;;');
});

it('detects VS Code via TERM_PROGRAM=vscode', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'vscode';
process.env['TERM_PROGRAM_VERSION'] = '1.85.0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1b]8;;');
});

it('still renders the ASCII box with title and instructions', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('Qwen OAuth Device Authorization');
expect(output).toContain('Please visit the following URL');
expect(output).toContain('Waiting for authorization');
});

it('treats FORCE_HYPERLINK=false as disabled', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['FORCE_HYPERLINK'] = 'false';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('treats FORCE_HYPERLINK=0 as disabled', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['FORCE_HYPERLINK'] = '0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('strips control characters from URL in OSC 8 envelope', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';

showFallbackMessage('https://chat.qwen.ai/device?code=\x07ABC\x1b123');

const output = getOutput();
// Control chars stripped — the sanitized URL appears in the envelope
expect(output).toContain(
'\x1b]8;;https://chat.qwen.ai/device?code=ABC123\x07',
);
// Raw control chars must not leak into the OSC payload
expect(output).not.toContain('code=\x07');
});

it('gates mintty on version >= 3.3', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'mintty';
process.env['TERM_PROGRAM_VERSION'] = '3.3.0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).toContain('\x1b]8;;');
});

it('refuses OSC 8 for mintty < 3.3', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'mintty';
process.env['TERM_PROGRAM_VERSION'] = '3.1.0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('refuses OSC 8 for mintty without version', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'mintty';
delete process.env['TERM_PROGRAM_VERSION'];

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('does not emit OSC 8 when NO_COLOR is set', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['NO_COLOR'] = '1';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});

it('does not emit OSC 8 when FORCE_COLOR=0', () => {
Object.defineProperty(process.stderr, 'isTTY', {
value: true,
configurable: true,
});
process.env['TERM_PROGRAM'] = 'iTerm.app';
process.env['FORCE_COLOR'] = '0';

showFallbackMessage('https://chat.qwen.ai/device?code=ABC123');

const output = getOutput();
expect(output).not.toContain('\x1b]8;;');
});
});
Loading
Loading