Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

8.4 KB
Raw Blame
import type { IBufferLine } from '@xterm/xterm'
import { describe, expect, it, vi } from 'vitest'
import { buildHardWrappedHttpLogicalLineCandidates } from './hard-wrapped-terminal-http-links'
import {
  TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS,
  TERMINAL_HTTP_URL_MAX_LENGTH
} from './terminal-http-link-limits'

function bufferLine(text: string, onTranslateWithColumns: () => void): IBufferLine {
  return {
    isWrapped: false,
    length: text.length,
    translateToString: (
      _trimRight?: boolean,
      startColumn = 0,
      endColumn = text.length,
      outColumns?: number[]
    ) => {
      if (outColumns) {
        onTranslateWithColumns()
      }
      outColumns?.push(...Array.from({ length: text.length + 1 }, (_value, index) => index))
      return text.slice(startColumn, endColumn)
    }
  } as IBufferLine
}

function bufferLineWithCellColumns(text: string): IBufferLine {
  const columns: number[] = []
  let column = 0
  for (const character of text) {
    columns.push(column)
    column += character === '你' ? 2 : 1
  }
  columns.push(column)
  return {
    isWrapped: false,
    length: column,
    translateToString: (
      _trimRight?: boolean,
      startColumn = 0,
      endColumn = text.length,
      outColumns?: number[]
    ) => {
      outColumns?.push(...columns)
      return text.slice(startColumn, endColumn)
    }
  } as IBufferLine
}

describe('hard-wrapped terminal HTTP candidate bounds', () => {
  it('builds each required column map once and rejects an overlength candidate early', () => {
    const onTranslateWithColumns = vi.fn()
    const rows = Array.from({ length: TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS }, (_value, index) =>
      bufferLine(`| ${index === 0 ? 'http://x' : 'aaaaaaaa'} |`, onTranslateWithColumns)
    )

    expect(
      buildHardWrappedHttpLogicalLineCandidates(
        { getLine: (y) => rows[y] },
        TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS
      )
    ).toEqual([])
    expect(onTranslateWithColumns).toHaveBeenCalledTimes(
      Math.floor(TERMINAL_HTTP_URL_MAX_LENGTH / 'aaaaaaaa'.length) + 1
    )
  })

  it('does not build column maps while rejecting rows without an HTTP scheme', () => {
    const onTranslateWithColumns = vi.fn()
    const rows = Array.from({ length: TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS }, () =>
      bufferLine(`| ${'a'.repeat(500)} |`, onTranslateWithColumns)
    )

    expect(
      buildHardWrappedHttpLogicalLineCandidates(
        { getLine: (y) => rows[y] },
        TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS
      )
    ).toEqual([])
    expect(onTranslateWithColumns).not.toHaveBeenCalled()
  })

  it('stops scanning back at a table border instead of walking every framed row', () => {
    const rows = Array.from({ length: TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS }, (_value, index) =>
      bufferLine(
        index % 10 === 0 ? '├──────────────────────┤' : `│ https://example.com/${index} │`,
        () => undefined
      )
    )
    const getLine = vi.fn((y: number) => rows[y])

    buildHardWrappedHttpLogicalLineCandidates({ getLine }, TERMINAL_HTTP_URL_MAX_HARD_WRAPPED_ROWS)
    expect(new Set(getLine.mock.calls.map(([y]) => y)).size).toBeLessThan(20)
  })

  it('reconstructs framed URL rows aligned by cells when a fragment has wide characters', () => {
    const rowTexts = ['|http://a/ab|', '|a你你你你你|', '|tailzzzzzzz|']
    const rows = rowTexts.map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates[0]?.text).toBe('http://a/aba你你你你你tailzzzzzzz')
  })

  it('reconstructs a framed URL while the screen underneath shows through left of the frame', () => {
    const inner = 'http://example.com/aaaa'
    const rows = [
      `      |${inner}|`,
      `todo  |${'b'.repeat(inner.length)}|`,
      `  in  |${'c'.repeat(inner.length)}|`,
      `Final |${'dddd'.padEnd(inner.length, ' ')}|`
    ].map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates[0]?.text).toBe(
      `${inner}${'b'.repeat(inner.length)}${'c'.repeat(inner.length)}dddd`
    )
  })

  it('reconstructs a URL in a nested frame whose left label repeats on every row', () => {
    const inner = 'http://example.com/aaaa'
    const rows = [
      `│ label │ ${inner} │`,
      `│ label │ ${'b'.repeat(inner.length)} │`,
      `│ label │ ${'c'.repeat(inner.length)} │`,
      `│ label │ ${'dddd'.padEnd(inner.length, ' ')} │`
    ].map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates[0]?.text).toBe(
      `${inner}${'b'.repeat(inner.length)}${'c'.repeat(inner.length)}dddd`
    )
  })

  it('reconstructs an overlay URL when a box border underneath shows at a fixed column', () => {
    const inner = 'http://example.com/aaaa'
    const rows = [
      `│      │ ${inner} │`,
      `│      │ ${'b'.repeat(inner.length)} │`,
      `│      │ ${'c'.repeat(inner.length)} │`,
      `│      │ ${'dddd'.padEnd(inner.length, ' ')} │`
    ].map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates[0]?.text).toBe(
      `${inner}${'b'.repeat(inner.length)}${'c'.repeat(inner.length)}dddd`
    )
  })

  it('does not join adjacent cells from a shared framed table', () => {
    const rows = [
      '│ Label  │ https://example.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa │',
      '│ Other  │ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb │',
      '│ Third  │ cccccccccccccccccccccccccccccccccccccccccccccccccc │'
    ].map(bufferLineWithCellColumns)

    expect(
      buildHardWrappedHttpLogicalLineCandidates({ getLine: (y) => rows[y] }, rows.length)
    ).toEqual([])
  })

  it('aligns continuation rows by the frame cell column when a gutter holds wide characters', () => {
    const inner = 'http://example.com/aaaa'
    const rows = [
      `      |${inner}|`,
      `你你你|${'b'.repeat(inner.length)}|`,
      `  in  |${'c'.repeat(inner.length)}|`,
      `Final |${'dddd'.padEnd(inner.length, ' ')}|`
    ].map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates[0]?.text).toBe(
      `${inner}${'b'.repeat(inner.length)}${'c'.repeat(inner.length)}dddd`
    )
  })

  it('stops joining when the frame moves to another column', () => {
    const inner = 'http://example.com/aaaa'
    const rows = [
      `      |${inner}|`,
      `todo  |${'b'.repeat(inner.length)}|`,
      `  in  |${'c'.repeat(inner.length)}|`,
      `Final  |${'dddd'.padEnd(inner.length - 1, ' ')}|`
    ].map(bufferLineWithCellColumns)

    const candidates = buildHardWrappedHttpLogicalLineCandidates(
      { getLine: (y) => rows[y] },
      rows.length
    )

    expect(candidates).toEqual([])
  })

  it('reconstructs a maximum-length wide-character URL beyond the ASCII row bound', () => {
    const scheme = 'http://'
    const continuation = '你'.repeat(TERMINAL_HTTP_URL_MAX_LENGTH - scheme.length)
    const rows = [
      bufferLineWithCellColumns(`|${scheme}|`),
      ...continuation
        .match(/.{1,3}/g)!
        .map((fragment) =>
          bufferLineWithCellColumns(`|${fragment}${' '.repeat(7 - fragment.length * 2)}|`)
        )
    ]

    expect(rows.length).toBeGreaterThan(Math.ceil(TERMINAL_HTTP_URL_MAX_LENGTH / scheme.length))
    expect(
      buildHardWrappedHttpLogicalLineCandidates({ getLine: (y) => rows[y] }, rows.length)[0]?.text
    ).toBe(scheme + continuation)
  })

  it('reconstructs a maximum-length URL with one-character continuation rows', () => {
    const firstFragment = 'http://a'
    const continuation = '/'.repeat(TERMINAL_HTTP_URL_MAX_LENGTH - firstFragment.length)
    const rows = [
      bufferLineWithCellColumns(`|${firstFragment}|`),
      ...continuation.split('').map((fragment) => bufferLineWithCellColumns(`|${fragment}       |`))
    ]
    const fullUrl = firstFragment + continuation

    expect(() => new URL(fullUrl)).not.toThrow()
    expect(
      buildHardWrappedHttpLogicalLineCandidates({ getLine: (y) => rows[y] }, rows.length)[0]?.text
    ).toBe(fullUrl)
  })
})