// Reproduces an issue where write is called while the stream is flowing but a
// destination cannot accept all of the buffered chunks.  minipass should add
// the chunk to the end of the buffer instead of emitting it before the buffer
// is cleared.
//
// This caused issues when piping make-fetch-happen stream to tar.extract
// https://github.com/npm/cli/issues/3884
import t from 'tap'
import { Minipass } from '../src/index.js'

class Pauser extends Minipass<string> {
  write(
    chunk: Minipass.ContiguousData,
    cb?: (() => void) | undefined
  ): boolean
  write(
    chunk: Minipass.ContiguousData,
    encoding?: Minipass.Encoding | undefined,
    cb?: (() => void) | undefined
  ): boolean
  write(
    chunk: Minipass.ContiguousData,
    encoding?: Minipass.Encoding | (() => void) | undefined,
    cb?: (() => void) | undefined
  ): boolean {
    if (typeof encoding === 'function') super.write(chunk, encoding)
    else super.write(chunk, encoding, cb)
    return false
  }
}

const src = new Minipass<string>({ encoding: 'utf8' })
const pauser = new Pauser({ encoding: 'utf8' })

// queue up two chunks while the src is buffering
src.write('1')
src.write('2')

// when the src starts flowing write a third chunk
src.once('resume', () => src.write('3'))

// pipe the src to the pauser which will request the src stops after the first
// chunk.
src.pipe(pauser)

src.end()

// we should expect the chunks in the original order.
t.resolveMatch(pauser.collect(), ['1', '2', '3'], '123')
