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.1 KiB173 linesraw
1import { GLOB, SEARCH } from '$lib/constants';
2import {
3	buildCaseInsensitiveGlob,
4	buildGlobSearchArgs,
5	highlightMatch,
6	joinPath,
7	rankEntries,
8	splitPathQuery
9} from '$lib/utils';
10import { describe, expect, it } from 'vitest';
11
12describe('splitPathQuery', () => {
13	it('treats a plain query as a home-relative glob (not navigation)', () => {
14		expect(splitPathQuery('docs')).toBeNull();
15	});
16
17	it('navigates the root for `/`', () => {
18		expect(splitPathQuery('/')).toEqual({ last: '', parent: '/' });
19	});
20
21	it('navigates home for `~`', () => {
22		expect(splitPathQuery('~')).toEqual({ last: '', parent: '~' });
23	});
24
25	it('splits an absolute path into parent and last segment', () => {
26		expect(splitPathQuery('/Users/al/proj')).toEqual({ last: 'proj', parent: '/Users/al' });
27	});
28
29	it('navigates a Windows drive path written with backslashes', () => {
30		expect(splitPathQuery('C:\\repos\\llama.cpp')).toEqual({
31			last: 'llama.cpp',
32			parent: 'C:/repos'
33		});
34	});
35
36	it('navigates a Windows drive path written with forward slashes', () => {
37		expect(splitPathQuery('D:/repos')).toEqual({ last: 'repos', parent: 'D:/' });
38	});
39
40	it('treats a bare drive as its root', () => {
41		expect(splitPathQuery('D:')).toEqual({ last: '', parent: 'D:/' });
42		expect(splitPathQuery('D:\\')).toEqual({ last: '', parent: 'D:/' });
43	});
44
45	it('navigates a UNC share', () => {
46		expect(splitPathQuery('\\\\host\\share\\proj')).toEqual({
47			last: 'proj',
48			parent: '//host/share/'
49		});
50	});
51
52	it('keeps a backslash as a POSIX filename character', () => {
53		expect(splitPathQuery('/tmp/a\\b')).toEqual({ last: 'a\\b', parent: '/tmp' });
54	});
55
56	it('splits a home-relative path into parent and last segment', () => {
57		expect(splitPathQuery('~/Documents')).toEqual({ last: 'Documents', parent: '~' });
58	});
59
60	it('strips trailing slashes before splitting', () => {
61		expect(splitPathQuery('/Users/al/')).toEqual({ last: 'al', parent: '/Users' });
62	});
63
64	it('handles a single-segment absolute path', () => {
65		expect(splitPathQuery('/opt')).toEqual({ last: 'opt', parent: '/' });
66	});
67});
68
69describe('buildCaseInsensitiveGlob', () => {
70	it('wraps letters in case-insensitive character classes', () => {
71		expect(buildCaseInsensitiveGlob('ab')).toBe('*[aA][bB]*');
72	});
73
74	it('escapes glob metacharacters into literal fragments', () => {
75		expect(buildCaseInsensitiveGlob('a*b')).toBe('*[aA][*][bB]*');
76	});
77});
78
79describe('rankEntries', () => {
80	const entries = [
81		{ path: '/h/README', type: 'dir' },
82		{ path: '/h/read', type: 'dir' },
83		{ path: '/h/readme.txt', type: 'dir' }
84	];
85
86	it('ranks exact basename match first', () => {
87		const ranked = rankEntries(entries, 'read');
88
89		expect(ranked[0].path).toBe('/h/read');
90	});
91
92	it('breaks ties by shorter path, then alphabetically', () => {
93		const ranked = rankEntries(entries, 'read');
94
95		expect(ranked[ranked.length - 1].path).toBe('/h/readme.txt');
96	});
97
98	it('does not mutate the input', () => {
99		const snapshot = [...entries];
100
101		rankEntries(entries, 'read');
102		expect(entries).toEqual(snapshot);
103	});
104});
105
106describe('joinPath', () => {
107	it('joins base and relative avoiding a double slash', () => {
108		expect(joinPath('/home/al/', 'docs')).toBe('/home/al/docs');
109	});
110
111	it('returns the relative path when base is empty', () => {
112		expect(joinPath('', 'docs')).toBe('docs');
113	});
114});
115
116describe('highlightMatch', () => {
117	it('returns a single non-matching segment when query is empty', () => {
118		expect(highlightMatch('abc', '')).toEqual([{ match: false, text: 'abc' }]);
119	});
120
121	it('marks every case-insensitive occurrence of the query', () => {
122		expect(highlightMatch('aXa', 'ax')).toEqual([
123			{ match: true, text: 'aX' },
124			{ match: false, text: 'a' }
125		]);
126	});
127
128	it('returns non-matching text when the query is absent', () => {
129		expect(highlightMatch('abc', 'z')).toEqual([{ match: false, text: 'abc' }]);
130	});
131});
132
133describe('buildGlobSearchArgs', () => {
134	const DEPTH = 6;
135
136	it('glob-matches home-relative within the scope path', () => {
137		const args = buildGlobSearchArgs('docs', '/home', DEPTH);
138
139		expect(args.path).toBe('/home');
140		expect(args.include).toBe(buildCaseInsensitiveGlob('docs'));
141		expect(args.maxDepth).toBe(DEPTH);
142		expect(args.rankQuery).toBe('docs');
143		expect(args.last).toBeUndefined();
144	});
145
146	it('navigates home for a `~` path query', () => {
147		const args = buildGlobSearchArgs('~/proj', '/home', DEPTH);
148
149		expect(args.path).toBe('~');
150		expect(args.include).toBe(buildCaseInsensitiveGlob('proj'));
151		expect(args.maxDepth).toBe(SEARCH.PATH_NAV_MAX_DEPTH);
152		expect(args.rankQuery).toBe('proj');
153		expect(args.last).toBe('proj');
154	});
155
156	it('lists the scope root when a path query has no last segment', () => {
157		const args = buildGlobSearchArgs('~/', '/home', DEPTH);
158
159		expect(args.path).toBe('~');
160		expect(args.include).toBe(GLOB.WILDCARD);
161		expect(args.maxDepth).toBe(SEARCH.PATH_NAV_MAX_DEPTH);
162	});
163
164	it('navigates an absolute path under its root', () => {
165		const args = buildGlobSearchArgs('/usr/local/bin', '/home', DEPTH);
166
167		expect(args.path).toBe('/usr/local');
168		expect(args.include).toBe(buildCaseInsensitiveGlob('bin'));
169		expect(args.maxDepth).toBe(SEARCH.PATH_NAV_MAX_DEPTH);
170		expect(args.rankQuery).toBe('bin');
171		expect(args.last).toBe('bin');
172	});
173});