rescope-vendor.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Acceptance-path coverage for the rescope codemod's exact-edit classifier: a
  3. * duplicated insertion — what a non-idempotent apply produces — must be
  4. * rejected rather than applied again.
  5. */
  6. import { describe, expect, it } from 'vitest'
  7. import { exactEditState } from './rescope-vendor.ts'
  8. const ANCHOR = '\n## Sync procedure'
  9. const INSERTED = `\n15. **rescope**: one log entry.\n${ANCHOR}`
  10. describe('exactEditState', () => {
  11. it('classifies an insertion by its target form, so a duplicate is invalid', () => {
  12. expect(exactEditState(`log\n${ANCHOR}\n`, ANCHOR, INSERTED, 1)).toBe('pending')
  13. expect(exactEditState(`log${INSERTED}\n`, ANCHOR, INSERTED, 1)).toBe('applied')
  14. // The anchor survives an insertion, so counting the source form would have
  15. // called this pending and inserted the entry a second time.
  16. expect(exactEditState(`log${INSERTED}${INSERTED}\n`, ANCHOR, INSERTED, 1)).toBe('invalid')
  17. expect(exactEditState('log\n', ANCHOR, INSERTED, 1)).toBe('invalid')
  18. })
  19. it('classifies a deletion by its source form, and requires its remainder to survive', () => {
  20. const remainder = 'exclude:\n'
  21. const withEntries = 'exclude:\n - cordis@4\n'
  22. expect(exactEditState(withEntries, withEntries, remainder, 1)).toBe('pending')
  23. expect(exactEditState(remainder, withEntries, remainder, 1)).toBe('applied')
  24. // Upstream dropped the whole field: the source form is gone, but so is the
  25. // remainder, so this is a moved site rather than a completed deletion.
  26. expect(exactEditState('unrelated:\n', withEntries, remainder, 1)).toBe('invalid')
  27. })
  28. it('requires a replacement to leave no source form and the exact target count', () => {
  29. expect(exactEditState('a = 1\n', 'a = 1', 'b = 2', 1)).toBe('pending')
  30. expect(exactEditState('b = 2\n', 'a = 1', 'b = 2', 1)).toBe('applied')
  31. expect(exactEditState('b = 2\nb = 2\n', 'a = 1', 'b = 2', 1)).toBe('invalid')
  32. // A moved or partially applied site: neither state is complete.
  33. expect(exactEditState('a = 1\nb = 2\n', 'a = 1', 'b = 2', 1)).toBe('invalid')
  34. expect(exactEditState('x\n', 'a = 1', 'b = 2', 1)).toBe('invalid')
  35. })
  36. })