File size: 5,537 Bytes
2517be1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | import { describe, it, expect } from 'vitest';
import {
extractTemplateVariables,
expandTemplate,
isTemplateComplete,
normalizeResourceUri
} from '../../src/lib/utils/uri-template';
import { URI_TEMPLATE_OPERATORS } from '../../src/lib/constants/uri-template';
describe('extractTemplateVariables', () => {
it('extracts simple variables', () => {
const vars = extractTemplateVariables('file:///{path}');
expect(vars).toEqual([{ name: 'path', operator: '' }]);
});
it('extracts multiple variables', () => {
const vars = extractTemplateVariables('db://{schema}/{table}');
expect(vars).toEqual([
{ name: 'schema', operator: '' },
{ name: 'table', operator: '' }
]);
});
it('extracts variables with operators', () => {
const vars = extractTemplateVariables('http://example.com{+path}');
expect(vars).toEqual([{ name: 'path', operator: URI_TEMPLATE_OPERATORS.RESERVED }]);
});
it('extracts comma-separated variable lists', () => {
const vars = extractTemplateVariables('{x,y,z}');
expect(vars).toEqual([
{ name: 'x', operator: '' },
{ name: 'y', operator: '' },
{ name: 'z', operator: '' }
]);
});
it('deduplicates variable names', () => {
const vars = extractTemplateVariables('{name}/{name}');
expect(vars).toEqual([{ name: 'name', operator: '' }]);
});
it('handles fragment expansion', () => {
const vars = extractTemplateVariables('http://example.com/page{#section}');
expect(vars).toEqual([{ name: 'section', operator: URI_TEMPLATE_OPERATORS.FRAGMENT }]);
});
it('handles path segment expansion', () => {
const vars = extractTemplateVariables('http://example.com{/path}');
expect(vars).toEqual([{ name: 'path', operator: URI_TEMPLATE_OPERATORS.PATH_SEGMENT }]);
});
it('returns empty array for template without variables', () => {
const vars = extractTemplateVariables('http://example.com/static');
expect(vars).toEqual([]);
});
it('strips explode modifier', () => {
const vars = extractTemplateVariables('{list*}');
expect(vars).toEqual([{ name: 'list', operator: '' }]);
});
it('strips prefix modifier', () => {
const vars = extractTemplateVariables('{value:5}');
expect(vars).toEqual([{ name: 'value', operator: '' }]);
});
});
describe('expandTemplate', () => {
it('expands simple variable', () => {
const result = expandTemplate('file:///{path}', { path: 'src/main.rs' });
expect(result).toBe('file:///src%2Fmain.rs');
});
it('expands reserved variable (no encoding)', () => {
const result = expandTemplate('file:///{+path}', { path: 'src/main.rs' });
expect(result).toBe('file:///src/main.rs');
});
it('expands multiple variables', () => {
const result = expandTemplate('db://{schema}/{table}', {
schema: 'public',
table: 'users'
});
expect(result).toBe('db://public/users');
});
it('leaves empty for missing variables', () => {
const result = expandTemplate('{missing}', {});
expect(result).toBe('');
});
it('expands fragment', () => {
const result = expandTemplate('http://example.com/page{#section}', {
section: 'intro'
});
expect(result).toBe('http://example.com/page#intro');
});
it('expands path segments', () => {
const result = expandTemplate('http://example.com{/path}', { path: 'docs' });
expect(result).toBe('http://example.com/docs');
});
it('expands query parameters', () => {
const result = expandTemplate('http://example.com{?q}', { q: 'search term' });
expect(result).toBe('http://example.com?q=search%20term');
});
it('keeps static parts unchanged', () => {
const result = expandTemplate('http://example.com/static', {});
expect(result).toBe('http://example.com/static');
});
});
describe('isTemplateComplete', () => {
it('returns true when all variables are filled', () => {
expect(isTemplateComplete('file:///{path}', { path: 'test.txt' })).toBe(true);
});
it('returns false when a variable is missing', () => {
expect(isTemplateComplete('db://{schema}/{table}', { schema: 'public' })).toBe(false);
});
it('returns false when a variable is empty', () => {
expect(isTemplateComplete('file:///{path}', { path: '' })).toBe(false);
});
it('returns false when a variable is whitespace only', () => {
expect(isTemplateComplete('file:///{path}', { path: ' ' })).toBe(false);
});
it('returns true for template without variables', () => {
expect(isTemplateComplete('http://example.com/static', {})).toBe(true);
});
it('returns true when all multiple variables are filled', () => {
expect(isTemplateComplete('db://{schema}/{table}', { schema: 'public', table: 'users' })).toBe(
true
);
});
});
describe('normalizeResourceUri', () => {
it('passes through a normal URI unchanged', () => {
expect(normalizeResourceUri('svelte://svelte/$effect.md')).toBe('svelte://svelte/$effect.md');
});
it('normalizes triple-slash URIs from path-style template expansion', () => {
expect(normalizeResourceUri('svelte:///svelte/$effect.md')).toBe('svelte://svelte/$effect.md');
});
it('normalizes quadruple-slash URIs', () => {
expect(normalizeResourceUri('svelte:////svelte/$effect.md')).toBe('svelte://svelte/$effect.md');
});
it('handles file:// URIs', () => {
expect(normalizeResourceUri('file:///home/user/doc.txt')).toBe('file://home/user/doc.txt');
});
it('handles http URIs unchanged', () => {
expect(normalizeResourceUri('http://example.com/path')).toBe('http://example.com/path');
});
it('returns non-URI strings unchanged', () => {
expect(normalizeResourceUri('not-a-uri')).toBe('not-a-uri');
});
});
|