Files
helder/test/highlight.test.ts
2026-06-16 06:18:42 +02:00

61 lines
1.9 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { HL } from '../src/renderer/src/highlight'
describe('HL.ext', () => {
it('extracts a normal extension', () => {
expect(HL.ext('src/a/b.php')).toBe('php')
expect(HL.ext('x.TSX')).toBe('tsx')
})
it('treats dotfiles like .env specially', () => {
expect(HL.ext('.env')).toBe('env')
expect(HL.ext('config/.env.local')).toBe('env')
})
it('returns empty for no extension', () => {
expect(HL.ext('Makefile')).toBe('')
})
})
describe('HL.langFor / langLabel', () => {
it('maps known extensions to Prism languages', () => {
expect(HL.langFor('a.php')).toBe('php')
expect(HL.langFor('a.ts')).toBe('typescript')
expect(HL.langFor('a.py')).toBe('python')
expect(HL.langFor('a.unknownext')).toBeNull()
})
it('produces human labels', () => {
expect(HL.langLabel('a.php')).toBe('PHP')
expect(HL.langLabel('a.tsx')).toBe('TypeScript')
expect(HL.langLabel('Makefile')).toBe('Plain Text')
})
})
describe('HL.iconFor', () => {
it('uses name-specific icons', () => {
expect(HL.iconFor('composer.json').t).toBe('co')
expect(HL.iconFor('package.json').t).toBe('pk')
})
it('falls back to extension icons', () => {
expect(HL.iconFor('x.php').c).toBe('#a78bdb')
})
it('falls back to first two letters for unknown types', () => {
expect(HL.iconFor('weird.zzz').t).toBe('we')
})
})
describe('HL.escapeHtml', () => {
it('escapes html-significant characters', () => {
expect(HL.escapeHtml('<a> & </a>')).toBe('&lt;a&gt; &amp; &lt;/a&gt;')
})
})
describe('HL.hlText (Prism)', () => {
it('wraps php keywords in token spans (markup-templating loaded first)', () => {
const out = HL.hlText('<?php class A {}', 'php')
expect(out).toContain('token')
expect(out).toContain('class')
})
it('escapes when no grammar is available', () => {
expect(HL.hlText('<x>', null)).toBe('&lt;x&gt;')
})
})