char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Constants refactor (#26908)e21152dc9

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