error.spec.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Unit tests for model-facing guarded-mutation diagnostics: normalized unread
  3. * failures, the stale-version remedy, code preservation, and passthrough.
  4. */
  5. import { describe, expect, it } from 'vitest'
  6. import { FsError } from '@deepseek-ai/dsh-fs'
  7. import { remediateFsError } from '../src/error.ts'
  8. describe('remediateFsError', () => {
  9. it('appends the re-read remedy to FS_STALE_VERSION, preserving the code and chaining the cause', () => {
  10. const original = new FsError('cannot edit "x": file changed since it was read', 'FS_STALE_VERSION')
  11. const remedied = remediateFsError(original, 'x') as FsError
  12. expect(remedied).toBeInstanceOf(FsError)
  13. expect(remedied.message).toBe('cannot edit "x": file changed since it was read — re-read the file, then retry')
  14. expect(remedied.code).toBe('FS_STALE_VERSION')
  15. expect(remedied.cause).toBe(original)
  16. })
  17. it('normalizes policy and provider FS_NOT_OBSERVED failures to one diagnostic', () => {
  18. const sources = [
  19. new FsError('edit requires reading "x" first', 'FS_NOT_OBSERVED'),
  20. new FsError('cannot overwrite existing "x" without reading it first', 'FS_NOT_OBSERVED'),
  21. ]
  22. const remedied = sources.map(error => remediateFsError(error, 'x') as FsError)
  23. expect(remedied.map(error => error.message)).toEqual([
  24. 'cannot modify "x": file has not been read — read the file, then retry',
  25. 'cannot modify "x": file has not been read — read the file, then retry',
  26. ])
  27. expect(remedied.map(error => error.code)).toEqual(['FS_NOT_OBSERVED', 'FS_NOT_OBSERVED'])
  28. expect(remedied.map(error => error.cause)).toEqual(sources)
  29. })
  30. it('leaves other FsError codes untouched', () => {
  31. const original = new FsError('no match anywhere', 'FS_EDIT_NOT_FOUND')
  32. expect(remediateFsError(original, 'x')).toBe(original)
  33. })
  34. it('leaves non-FsError values untouched', () => {
  35. const original = new Error('boom')
  36. expect(remediateFsError(original, 'x')).toBe(original)
  37. })
  38. })