Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.7 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { deriveLongRecap, deriveTurnSummary, extendShortRecap, LONG_RECAP_MAX_BYTES } from './deviceRecap.js'

describe('device recap minimum', () => {
  it.each(['Yes.', 'No.', 'Done.', 'Tests pass.', "It isn't external."])(
    'extends %s with the following explanation', opening => {
      const source = `${opening} The remaining explanation is much longer than the space available below the companion on this display.`
      const recap = deriveTurnSummary(source)!.split('\n\n')[0]
      expect(recap.startsWith(opening + ' ')).toBe(true)
      expect(recap.length).toBeGreaterThanOrEqual(20)
      expect(recap.length).toBeLessThanOrEqual(180)
    })
  it('keeps complete sentences once their combined length is at least 20', () => {
    const opening = 'Fixed. All tests pass.'
    expect(deriveTurnSummary(opening + ' ' + 'Additional explanation '.repeat(8))!.split('\n\n')[0]).toBe(opening)
  })
  it('does not invent text when the entire answer is short', () => {
    expect(deriveTurnSummary('Yes.')!.split('\n\n')[0]).toBe('Yes.')
  })
  it('uses the larger reading area without cutting a complete sentence', () => {
    const source = 'The update is installed. Voice input now sends to the selected agent. The new layout keeps the result in the center.'
    expect(source.length).toBeGreaterThan(80)
    expect(deriveTurnSummary(source)!.split('\n\n')[0]).toBe(source)
    expect(extendShortRecap('The update is installed. Voice input now sends to the selected agent.', source)).toBe(source)
    expect(extendShortRecap('The update is installed. Voice input now sends to the +', source)).toBe(source)
    expect(extendShortRecap('The update is installed.', 'An unrelated update is installed.')).toBe('The update is installed.')
  })
  it('repairs a cached short recap only from its own matching body', () => {
    expect(extendShortRecap('Yes.', 'Yes. The fix is installed.')).toBe('Yes. The fix is installed.')
    expect(extendShortRecap('No.', 'Yes. The fix is installed.')).toBe('No.')
    expect(extendShortRecap('The fix is installed.', 'The fix is installed. All checks passed.')).toBe('The fix is installed. All checks passed.')
  })
})

describe('deriveLongRecap', () => {
  it('strips markdown and collapses whitespace', () => {
    expect(deriveLongRecap('## Kết quả\n\n- **SJC**: 149 triệu\n\n```sh\nls\n```\nDone.')).toBe('Kết quả SJC: 149 triệu Done.')
  })
  it('keeps code and the underscores inside words, and drops only the emphasis around them', () => {
    expect(deriveLongRecap('Name it `MAX_SIZE`, not *max_size*; see __init__ and _this_ word.'))
      .toBe('Name it MAX_SIZE, not max_size; see init and this word.')
    expect(deriveLongRecap('Set AI_AGENT and `__init__.py` stays as it is.')).toBe('Set AI_AGENT and __init__.py stays as it is.')
  })
  it('clips long text at a word boundary with an ellipsis, within the char and byte caps', () => {
    const long = Array.from({ length: 600 }, (_, i) => `từ${i}`).join(' ')
    const out = deriveLongRecap(long)!
    expect(out.length).toBeLessThanOrEqual(1200)
    expect(out.endsWith('…')).toBe(true)
    expect(out.slice(0, -1).endsWith(' ')).toBe(false)
    expect(Buffer.byteLength(out)).toBeLessThanOrEqual(LONG_RECAP_MAX_BYTES)
  })
  it('drops control characters and lone surrogates, which JSON writes as six bytes each', () => {
    expect(deriveLongRecap('a\x00\x1b\x07b \ud800c\x7f d')).toBe('ab c d')
    expect(deriveLongRecap('\x00\x01')).toBeNull()
    expect(deriveLongRecap('ok 😀 fine')).toBe('ok 😀 fine')
  })
  it('keeps short text whole and returns null for nothing', () => {
    expect(deriveLongRecap('All good.')).toBe('All good.')
    expect(deriveLongRecap('  ')).toBeNull()
  })
})