code-languages.client.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { describe, expect, it } from 'vitest'
  2. import { supportsHighlighting } from '../../ui-primitives/src/markdown/highlight.ts'
  3. import { CODE_EXTENSIONS, languageForPath } from '../src/client/code/languages.ts'
  4. describe('code preview languages', () => {
  5. it.each([
  6. ['component.TSX', 'typescript'], ['module.mts', 'typescript'], ['module.cts', 'typescript'],
  7. ['client.jsx', 'javascript'], ['module.mjs', 'javascript'], ['module.cjs', 'javascript'],
  8. ['script.sh', 'shellscript'], ['script.zsh', 'shellscript'], ['data.jsonc', 'json'],
  9. ['events.jsonl', 'json'], ['model.pyi', 'python'], ['task.rake', 'ruby'],
  10. ['main.go', 'go'], ['main.rs', 'rust'], ['Main.java', 'java'], ['main.c', 'c'],
  11. ['header.h', 'c'], ['header.hpp', 'cpp'], ['main.cxx', 'cpp'], ['Main.cs', 'csharp'],
  12. ['build.kts', 'kotlin'], ['main.swift', 'swift'], ['index.php', 'php'],
  13. ['config.yml', 'yaml'], ['config.toml', 'toml'], ['config.ini', 'ini'],
  14. ['README.md', 'markdown'], ['page.mdx', 'mdx'], ['index.HTML', 'html'],
  15. ['index.htm', 'html'], ['style.css', 'css'], ['style.scss', 'scss'],
  16. ['style.less', 'less'], ['query.sql', 'sql'], ['schema.xsd', 'xml'], ['init.lua', 'lua'],
  17. ])('uses the existing %s grammar hint %s', (path, language) => {
  18. expect(languageForPath(path)).toBe(language)
  19. })
  20. it('keeps every registered suffix highlightable by the shared primitive', () => {
  21. expect(new Set(CODE_EXTENSIONS).size).toBe(CODE_EXTENSIONS.length)
  22. for (const extension of CODE_EXTENSIONS) {
  23. expect(supportsHighlighting(languageForPath(`file.${extension}`)), extension).toBe(true)
  24. }
  25. })
  26. it('uses the filename suffix for both path separators', () => {
  27. expect(languageForPath('/project/archive.old/source.d.ts')).toBe('typescript')
  28. expect(languageForPath('C:\\project\\source.CPP')).toBe('cpp')
  29. })
  30. it.each(['README', 'dir.ts/README', 'notes.txt', 'main.ts.backup', 'file.unknown', 'file.constructor', 'file.__proto__'])('does not claim %s', (path) => {
  31. expect(languageForPath(path)).toBeUndefined()
  32. })
  33. })