render.spec.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Shared exit-status parse contract: the inverse of the `[exit code: N]` /
  3. * `[killed by signal: X]` markers `dsh-tool-bash` and `dsh-tool-pwsh` append.
  4. * Both tools' presenter suites round-trip their own renderers through this
  5. * parse; this spec pins the parse's own edges (marker-like output, body
  6. * slicing) once, at the seam that owns it.
  7. */
  8. import { describe, expect, it } from 'vitest'
  9. import { parseExitStatus } from '../src/render.ts'
  10. describe('parseExitStatus', () => {
  11. it('recovers a clean exit 0 with the body verbatim when no marker is present', () => {
  12. expect(parseExitStatus('hi\n\n')).toEqual({ body: 'hi\n\n', exitCode: 0 })
  13. expect(parseExitStatus('')).toEqual({ body: '', exitCode: 0 })
  14. })
  15. it('recovers a non-zero exit and strips only its marker from the body', () => {
  16. expect(parseExitStatus('oops\n[exit code: 3]')).toEqual({ body: 'oops', exitCode: 3 })
  17. // The marker needs the leading newline and the end of the string, so a
  18. // clean result whose output merely ENDS in marker-like text is not read
  19. // as a failure and the text stays in the body.
  20. expect(parseExitStatus('[exit code: 5]')).toEqual({ body: '[exit code: 5]', exitCode: 0 })
  21. })
  22. it('recovers a signal kill ahead of any non-zero exit marker', () => {
  23. expect(parseExitStatus('gone\n[killed by signal: SIGKILL]')).toEqual({ body: 'gone', signal: 'SIGKILL' })
  24. // A fake signal marker with no leading newline is output, not a kill.
  25. expect(parseExitStatus('[killed by signal: SIGKILL]')).toEqual({ body: '[killed by signal: SIGKILL]', exitCode: 0 })
  26. })
  27. it('keeps markers no pill shows (timeout) in the body', () => {
  28. expect(parseExitStatus('slow\n[timed out after 100ms]\n[exit code: 143]'))
  29. .toEqual({ body: 'slow\n[timed out after 100ms]', exitCode: 143 })
  30. })
  31. })