fonts.service.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  2. import * as fs from 'fs'
  3. import * as os from 'os'
  4. import * as path from 'path'
  5. import { addUserFonts, listUserFonts, removeUserFont, userFontDir } from './fonts.service'
  6. let work: string
  7. beforeEach(() => {
  8. work = fs.mkdtempSync(path.join(os.tmpdir(), 'we-fonts-'))
  9. process.env.LWE_FONT_DIR = path.join(work, 'fonts')
  10. })
  11. afterEach(() => {
  12. delete process.env.LWE_FONT_DIR
  13. fs.rmSync(work, { recursive: true, force: true })
  14. })
  15. describe('user fonts', () => {
  16. it('uses the folder the engine looks in', () => {
  17. expect(userFontDir()).toBe(path.join(work, 'fonts'))
  18. })
  19. it('copies font files and refuses the rest', () => {
  20. const font = path.join(work, 'Segoe UI Emoji.ttf')
  21. const text = path.join(work, 'notes.txt')
  22. fs.writeFileSync(font, 'font')
  23. fs.writeFileSync(text, 'not a font')
  24. expect(addUserFonts([font, text, path.join(work, 'missing.ttf')])).toEqual({
  25. added: ['Segoe UI Emoji.ttf'],
  26. rejected: ['notes.txt', 'missing.ttf']
  27. })
  28. expect(listUserFonts()).toEqual([{ name: 'Segoe UI Emoji.ttf', size: 4 }])
  29. })
  30. it('removes a font by its name only', () => {
  31. const font = path.join(work, 'a.otf')
  32. fs.writeFileSync(font, 'x')
  33. addUserFonts([font])
  34. expect(removeUserFont('../a.otf')).toBe(false)
  35. expect(removeUserFont('a.otf')).toBe(true)
  36. expect(listUserFonts()).toEqual([])
  37. })
  38. })