mirror of
https://github.com/stablyai/orca.git
synced 2026-11-03 02:27:43 +00:00
test: consolidate discovery coverage and replace copied oracles (#32211)
Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
createCommandCodeOutputStatusDetector,
|
||||
stripTerminalControl
|
||||
} from './command-code-output-status'
|
||||
import { createCommandCodeOutputStatusDetector } from './command-code-output-status'
|
||||
import { stripTerminalControl } from './terminal-control-stripping'
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
@@ -389,44 +387,6 @@ function maxStringContextLength(contexts: unknown[]): number {
|
||||
}
|
||||
|
||||
describe('terminal control stripping', () => {
|
||||
const esc = String.fromCharCode(0x1b)
|
||||
const bel = String.fromCharCode(0x07)
|
||||
const ansiEscape = new RegExp(
|
||||
`${esc}(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~]|\\][^${bel}]*(?:${bel}|${esc}\\\\))`,
|
||||
'g'
|
||||
)
|
||||
const incompleteAnsiEscape = new RegExp(
|
||||
`${esc}(?:\\[[0-?]*[ -/]*|\\][^${bel}${esc}]*|\\S?)?$`,
|
||||
'g'
|
||||
)
|
||||
|
||||
function legacyStripTerminalControl(data: string): string {
|
||||
const withoutAnsi = data.replace(ansiEscape, '').replace(incompleteAnsiEscape, '')
|
||||
let output = ''
|
||||
for (let index = 0; index < withoutAnsi.length; index += 1) {
|
||||
const code = withoutAnsi.charCodeAt(index)
|
||||
if ((code <= 0x1f && code !== 0x0a && code !== 0x0d) || (code >= 0x7f && code <= 0x9f)) {
|
||||
continue
|
||||
}
|
||||
output += withoutAnsi[index]
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
function makeRandom(seed: number): () => number {
|
||||
let state = seed >>> 0
|
||||
return () => {
|
||||
state ^= state << 13
|
||||
state ^= state >>> 17
|
||||
state ^= state << 5
|
||||
return (state >>> 0) / 0x1_0000_0000
|
||||
}
|
||||
}
|
||||
|
||||
function expectLegacyEquivalent(data: string): void {
|
||||
expect(stripTerminalControl(data)).toBe(legacyStripTerminalControl(data))
|
||||
}
|
||||
|
||||
function promptFrom(raw: string): string | null {
|
||||
let captured: string | null = null
|
||||
const detector = createCommandCodeOutputStatusDetector({
|
||||
@@ -458,77 +418,26 @@ describe('terminal control stripping', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('matches the legacy filter across ANSI, C0/C1, line breaks, and Unicode', () => {
|
||||
for (const data of [
|
||||
'',
|
||||
'plain text',
|
||||
'\x1b[35mstyled\x1b[0m',
|
||||
'\x1b[unterminated',
|
||||
'\x00leading',
|
||||
'trailing\x9f',
|
||||
'\x01\x02adjacent\x7f\x80',
|
||||
'keep\r\nline breaks',
|
||||
'日本語 \u{1f389} \ud83d \ude00',
|
||||
'\x1b]0;title\x07prompt',
|
||||
'\x1b]0;title\x1b\\prompt'
|
||||
]) {
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('matches legacy output at density thresholds and block resets', () => {
|
||||
const control = '\x01'
|
||||
const fixtures = [
|
||||
`${control.repeat(31)}${'a'.repeat(33)}`,
|
||||
`${control.repeat(32)}${'a'.repeat(32)}`,
|
||||
`${'a'.repeat(32)}${control.repeat(31)}a`,
|
||||
`${control.repeat(31)}${'a'.repeat(33)}${control.repeat(31)}z`,
|
||||
`${control.repeat(31)}${'a'.repeat(33)}${control.repeat(32)}z`,
|
||||
`${'a'.repeat(64 * 3)}${control.repeat(32)}tail`,
|
||||
`${'a\x01'.repeat(2048)}tail`
|
||||
]
|
||||
for (const data of fixtures) {
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('exhaustively matches short strings over control and Unicode code units', () => {
|
||||
const alphabet = ['a', '\r', '\n', '\x01', '\x7f', '\x80', '\u20ac', '\ud83d']
|
||||
for (let encoded = 0; encoded < alphabet.length ** 4; encoded += 1) {
|
||||
let cursor = encoded
|
||||
let data = ''
|
||||
for (let position = 0; position < 4; position += 1) {
|
||||
data += alphabet[cursor % alphabet.length]
|
||||
cursor = Math.floor(cursor / alphabet.length)
|
||||
}
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('matches seeded random terminal text', () => {
|
||||
const random = makeRandom(0xc0de_0727)
|
||||
const alphabet = [
|
||||
'a',
|
||||
'Z',
|
||||
'\r',
|
||||
'\n',
|
||||
'\x00',
|
||||
'\x1f',
|
||||
'\x7f',
|
||||
'\x9f',
|
||||
'\u00e9',
|
||||
'\u20ac',
|
||||
'\u{1f389}',
|
||||
'\x1b[35m',
|
||||
'\x1b[0m'
|
||||
]
|
||||
for (let trial = 0; trial < 2_000; trial += 1) {
|
||||
let data = ''
|
||||
const parts = Math.floor(random() * 256)
|
||||
for (let index = 0; index < parts; index += 1) {
|
||||
data += alphabet[Math.floor(random() * alphabet.length)]
|
||||
}
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
it.each([
|
||||
{ data: '', expected: '' },
|
||||
{ data: 'plain text', expected: 'plain text' },
|
||||
{ data: '\x1b[35mstyled\x1b[0m', expected: 'styled' },
|
||||
{ data: 'prompt\x1b[35', expected: 'prompt' },
|
||||
{ data: '\x00leading', expected: 'leading' },
|
||||
{ data: 'trailing\x9f', expected: 'trailing' },
|
||||
{ data: '\x01\x02adjacent\x7f\x80', expected: 'adjacent' },
|
||||
{ data: 'keep\r\nline breaks', expected: 'keep\r\nline breaks' },
|
||||
{ data: '日本語 🎉 \ud83d \ude00', expected: '日本語 🎉 \ud83d \ude00' },
|
||||
{ data: '\x01'.repeat(31) + 'a'.repeat(33), expected: 'a'.repeat(33) },
|
||||
{ data: '\x01'.repeat(32) + 'a'.repeat(32), expected: 'a'.repeat(32) },
|
||||
{ data: `${'a'.repeat(32) + '\x01'.repeat(31)}a`, expected: 'a'.repeat(33) },
|
||||
{
|
||||
data: `${'\x01'.repeat(31) + 'a'.repeat(33) + '\x01'.repeat(32)}z`,
|
||||
expected: `${'a'.repeat(33)}z`
|
||||
},
|
||||
{ data: `${'a'.repeat(192) + '\x01'.repeat(32)}tail`, expected: `${'a'.repeat(192)}tail` },
|
||||
{ data: `${'a\x01'.repeat(2048)}tail`, expected: `${'a'.repeat(2048)}tail` }
|
||||
])('strips controls while preserving printable text (case %#)', ({ data, expected }) => {
|
||||
expect(stripTerminalControl(data)).toBe(expected)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user