import { describe, expect, it } from 'vitest'
import { truncateReadToolOutput } from '../src/dimsdk/core/store/read-output.ts'

describe('truncateReadToolOutput', () => {
  it('extracts text lines from jsonish read output', () => {
    const result = truncateReadToolOutput(
      JSON.stringify([
        { type: 'text', text: 'alpha\nbeta\ngamma' },
        { type: 'image', url: 'https://example.com/demo.png' },
      ]),
      { maxLines: 2 },
    )

    expect(result).toEqual({
      output: 'alpha\nbeta\n...',
      truncated: true,
    })
  })

  it('falls back to plain text truncation when json parsing is skipped', () => {
    const result = truncateReadToolOutput('one\ntwo\nthree', {
      maxLines: 2,
      maxJsonParseChars: 0,
    })

    expect(result).toEqual({
      output: 'one\ntwo\n...',
      truncated: true,
    })
  })

  it('ignores non-text json entries and preserves non-truncated text', () => {
    const result = truncateReadToolOutput(
      JSON.stringify({
        type: 'text',
        text: 'single line',
      }),
      { maxLines: 5 },
    )

    expect(result).toEqual({
      output: 'single line',
      truncated: false,
    })
  })
})
