/ clis / uiverse / _shared.test.js
_shared.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import {
 3    UIVERSE_BASE_URL,
 4    parseComponentInput,
 5    parseHtmlRootSignature,
 6    inferLanguage,
 7    getCodeLength,
 8  } from './_shared.js';
 9  
10  describe('uiverse shared helpers', () => {
11    it('parses full URLs and author/slug identifiers', () => {
12      expect(parseComponentInput('Galahhad/strong-squid-82')).toEqual({
13        raw: 'Galahhad/strong-squid-82',
14        username: 'Galahhad',
15        slug: 'strong-squid-82',
16        url: `${UIVERSE_BASE_URL}/Galahhad/strong-squid-82`,
17      });
18  
19      expect(parseComponentInput('https://uiverse.io/Galahhad/strong-squid-82')).toEqual({
20        raw: 'https://uiverse.io/Galahhad/strong-squid-82',
21        username: 'Galahhad',
22        slug: 'strong-squid-82',
23        url: `${UIVERSE_BASE_URL}/Galahhad/strong-squid-82`,
24      });
25    });
26  
27    it('rejects unsupported hosts and malformed identifiers', () => {
28      expect(() => parseComponentInput('https://example.com/foo/bar')).toThrow('Unsupported non-Uiverse URL');
29      expect(() => parseComponentInput('only-author')).toThrow('Could not parse author/slug');
30      expect(() => parseComponentInput('a/b/c')).toThrow('Could not parse author/slug');
31    });
32  
33    it('parses the HTML root signature', () => {
34      expect(parseHtmlRootSignature('<label id="x" class="theme-switch primary"></label>')).toEqual({
35        tag: 'label',
36        id: 'x',
37        classes: ['theme-switch', 'primary'],
38      });
39      expect(parseHtmlRootSignature('')).toEqual({ tag: null, id: null, classes: [] });
40    });
41  
42    it('infers the language from target and metadata', () => {
43      expect(inferLanguage('react', {})).toBe('tsx');
44      expect(inferLanguage('vue', {})).toBe('vue');
45      expect(inferLanguage('html', { isTailwind: true })).toBe('html+tailwind');
46      expect(inferLanguage('css', {})).toBe('css');
47      expect(inferLanguage('unknown', {})).toBe('text');
48    });
49  
50    it('returns the code length safely', () => {
51      expect(getCodeLength('abc')).toBe(3);
52      expect(getCodeLength('')).toBe(0);
53      expect(getCodeLength(null)).toBe(0);
54    });
55  });