paired-markdown-derivatives.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, expect, it } from 'vitest'
  2. import { partitionPairedMarkdownDerivatives } from './paired-markdown-derivatives.ts'
  3. interface Block {
  4. doc: string
  5. kind: string
  6. code: string
  7. }
  8. const partition = (blocks: Block[]) => partitionPairedMarkdownDerivatives(
  9. blocks,
  10. block => block.doc,
  11. block => `${block.kind}\0${block.code}`,
  12. )
  13. describe('partitionPairedMarkdownDerivatives', () => {
  14. it('treats a complete byte-identical Chinese sequence as derivative', () => {
  15. const english = [
  16. { doc: 'docs/example.md', kind: 'ts', code: 'const one = 1' },
  17. { doc: 'docs/example.md', kind: 'type-equiv', code: 'interface Example {}' },
  18. ]
  19. const chinese = english.map(block => ({ ...block, doc: 'docs/example.zh.md' }))
  20. const unrelated = { doc: 'docs/other.md', kind: 'ts', code: 'const other = 2' }
  21. expect(partition([...english, ...chinese, unrelated])).toEqual({
  22. primary: [...english, unrelated],
  23. derivatives: chinese,
  24. })
  25. })
  26. it('keeps reordered, changed, partial, and orphan Chinese sequences primary', () => {
  27. const sequence = (doc: string) => [
  28. { doc, kind: 'ts', code: 'const one = 1' },
  29. { doc, kind: 'ts', code: 'const two = 2' },
  30. ]
  31. const english = sequence('docs/example.md')
  32. const changed = english.map((block, index) => ({
  33. ...block,
  34. doc: 'docs/example.zh.md',
  35. code: index === 0 ? 'const one = 0' : block.code,
  36. }))
  37. const reorderedEnglish = sequence('docs/reordered.md')
  38. const reordered = [...reorderedEnglish].reverse().map(block => ({ ...block, doc: 'docs/reordered.zh.md' }))
  39. const partialEnglish = sequence('docs/partial.md')
  40. const partial = [{ ...partialEnglish[0]!, doc: 'docs/partial.zh.md' }]
  41. const orphan = [{ doc: 'docs/orphan.zh.md', kind: 'ts', code: 'const orphan = true' }]
  42. const blocks = [
  43. ...english,
  44. ...changed,
  45. ...reorderedEnglish,
  46. ...reordered,
  47. ...partialEnglish,
  48. ...partial,
  49. ...orphan,
  50. ]
  51. expect(partition(blocks)).toEqual({ primary: blocks, derivatives: [] })
  52. })
  53. it('requires the fence kind to match as well as the body', () => {
  54. const english = { doc: 'docs/example.md', kind: 'type-equiv', code: 'interface Example {}' }
  55. const chinese = { ...english, doc: 'docs/example.zh.md', kind: 'public-api' }
  56. expect(partition([english, chinese])).toEqual({ primary: [english, chinese], derivatives: [] })
  57. })
  58. })