import { extractClaudeAuthUrl, extractClaudeSetupToken, normalizeTerminalScreenText, squashPromptText, textContainsCallbackPastePrompt } from '../services/oauthFlowManager';

describe('oauthFlowManager terminal parsing', () => {
  test('squashes screen-control fragments that render prompts one glyph per line', () => {
    const raw = 'P\r[2m\na\r[2m\ns\r[2m\nt\r[2m\ne\r[2m\n';
    expect(normalizeTerminalScreenText(raw)).toContain('P');
    expect(squashPromptText(raw)).toBe('paste');
  });

  test('detects OpenClaw Codex callback prompts rendered one glyph per line', () => {
    const raw = [
      'P', 'a', 's', 't', 'e', ' ', 't', 'h', 'e', ' ',
      'a', 'u', 't', 'h', 'o', 'r', 'i', 'z', 'a', 't', 'i', 'o', 'n', ' ',
      'c', 'o', 'd', 'e', ' ', '(', 'o', 'r', ' ', 'f', 'u', 'l', 'l', ' ',
      'r', 'e', 'd', 'i', 'r', 'e', 'c', 't', ' ', 'U', 'R', 'L', ')', ':',
    ].join('\n');

    expect(textContainsCallbackPastePrompt(raw)).toBe(true);
  });

  test('extracts Claude setup tokens from screen-normalized PTY output', () => {
    const fakeToken = ['sk', 'ant', 'oat01', 'abcdefghijklmnopqrstuvwxyz1234567890+/='].join('-');
    const raw = `Done!\r\nsetup token:\r\n${fakeToken}\r\n`;
    expect(extractClaudeSetupToken(raw)).toBe(fakeToken);
  });

  test('extracts wrapped Claude auth URLs from PTY output', () => {
    const raw = [
      'Open this URL in your browser:',
      'https://claude.ai/oauth/authorize?code=true&',
      'state=abc123',
      'Paste code here if prompted >',
    ].join('\r\n');

    expect(extractClaudeAuthUrl(raw)).toBe('https://claude.ai/oauth/authorize?code=true&state=abc123');
  });
});
