char-fork/llama.cpp

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

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
5.7 KiB180 linesraw
1import { FileMentionEntryType } from '$lib/enums';
2import {
3	buildMentionInsertion,
4	containsFileMentionLink,
5	decodeFileLinkPath,
6	encodeFileLinkPath,
7	fileMentionLinkRe,
8	getMentionBadgeIconPaths,
9	getMentionBadgeLabel,
10	MENTION_BADGE_FILE_ICON_PATHS,
11	MENTION_BADGE_FOLDER_ICON_PATHS
12} from '$lib/utils';
13import { describe, expect, it } from 'vitest';
14
15describe('encodeFileLinkPath', () => {
16	it('leaves a clean path unchanged', () => {
17		expect(encodeFileLinkPath('/Users/foo/bar.txt')).toBe('/Users/foo/bar.txt');
18	});
19
20	it('encodes spaces per path segment', () => {
21		expect(
22			encodeFileLinkPath('/Users/allozaur/Desktop/Screenshot 2026-08-05 at 11.33.45.png')
23		).toBe('/Users/allozaur/Desktop/Screenshot%202026-08-05%20at%2011.33.45.png');
24	});
25
26	it('preserves the leading and trailing slash (directory marker)', () => {
27		expect(encodeFileLinkPath('/Users/foo/bar/')).toBe('/Users/foo/bar/');
28	});
29
30	it('encodes parentheses in macOS screenshot names', () => {
31		expect(encodeFileLinkPath('/Users/foo/Pic (1).png')).toBe('/Users/foo/Pic%20(1).png');
32	});
33});
34
35describe('fileMentionLinkRe', () => {
36	it('matches a standard mention link', () => {
37		expect(fileMentionLinkRe().test('[docs](file:///a/b)')).toBe(true);
38		expect(containsFileMentionLink('[docs](file:///a/b)')).toBe(true);
39	});
40
41	it('does not match non-file links', () => {
42		expect(fileMentionLinkRe().test('[foo](https://example.com)')).toBe(false);
43		expect(fileMentionLinkRe().test('plain text')).toBe(false);
44	});
45
46	it('admits a close paren in a macOS-style file name', () => {
47		const match = fileMentionLinkRe().exec(
48			'[Screenshot (1).png](file:///Users/foo/Screenshot (1).png)'
49		);
50
51		expect(match).not.toBeNull();
52		expect(match?.[1]).toBe('Screenshot (1).png');
53		expect(match?.[2]).toBe('/Users/foo/Screenshot (1).png');
54	});
55
56	it('admits a parenthesized folder segment', () => {
57		expect(
58			fileMentionLinkRe().exec('[main.rs](file:///Users/foo/Project (Stuff)/main.rs)')?.[2]
59		).toBe('/Users/foo/Project (Stuff)/main.rs');
60	});
61
62	it('stops at the closing paren of an adjacent badge', () => {
63		expect(fileMentionLinkRe().exec('[a](file:///p)[b](file:///q)')?.[0]).toBe('[a](file:///p)');
64	});
65});
66
67describe('getMentionBadgeIconPaths', () => {
68	it('returns the folder glyphs for a trailing-separator path', () => {
69		expect(getMentionBadgeIconPaths('/Users/foo/bar/')).toBe(MENTION_BADGE_FOLDER_ICON_PATHS);
70	});
71
72	it('returns the file glyphs otherwise', () => {
73		expect(getMentionBadgeIconPaths('/Users/foo/bar.txt')).toBe(MENTION_BADGE_FILE_ICON_PATHS);
74	});
75});
76
77describe('getMentionBadgeLabel', () => {
78	it('returns the name by default', () => {
79		expect(getMentionBadgeLabel('bar', '/Users/foo/bar/', false)).toBe('bar');
80	});
81
82	it('renders the decoded full path without the trailing separator', () => {
83		expect(getMentionBadgeLabel('bar', '/Users/foo/bar/', true)).toBe('/Users/foo/bar');
84		expect(getMentionBadgeLabel('shot', '/Users/foo/Screenshot%20(1).png', true)).toBe(
85			'/Users/foo/Screenshot (1).png'
86		);
87	});
88
89	it('abbreviates a known home prefix to a tilde', () => {
90		expect(getMentionBadgeLabel('main.rs', '/home/user/src/main.rs', true, '/home/user')).toBe(
91			'~/src/main.rs'
92		);
93	});
94
95	it('falls back to the name when the decoded path is empty', () => {
96		expect(getMentionBadgeLabel('root', '/', true)).toBe('root');
97	});
98});
99
100describe('decodeFileLinkPath', () => {
101	it('decodes encoded segments back to the original path', () => {
102		expect(
103			decodeFileLinkPath('/Users/allozaur/Desktop/Screenshot%202026-08-05%20at%2011.33.45.png')
104		).toBe('/Users/allozaur/Desktop/Screenshot 2026-08-05 at 11.33.45.png');
105	});
106
107	it('is the inverse of encodeFileLinkPath', () => {
108		for (const path of [
109			'/a/b.txt',
110			'/Users/foo/Desktop/Screenshot 2026-08-05 at 11.33.45.png',
111			'/Users/foo/bar (1)/dir/',
112			'/sp ace/pa%th.txt'
113		]) {
114			expect(decodeFileLinkPath(encodeFileLinkPath(path))).toBe(path);
115		}
116	});
117
118	it('falls back to the input on malformed percent sequences', () => {
119		expect(decodeFileLinkPath('/a/%zz.txt')).toBe('/a/%zz.txt');
120	});
121});
122
123describe('buildMentionInsertion', () => {
124	const file = (path: string, name: string) => ({
125		name,
126		path,
127		type: FileMentionEntryType.FILE
128	});
129	const dir = (path: string, name: string) => ({
130		name,
131		path,
132		type: FileMentionEntryType.DIRECTORY
133	});
134
135	it('splices a root-anchored file link in place of the token', () => {
136		const value = 'hello @repo';
137		const result = buildMentionInsertion(file('/Users/foo/myRepo', 'myRepo'), value, {
138			end: 11,
139			start: 6
140		});
141
142		expect(result).not.toBeNull();
143		const { caretOffset, newValue } = result!;
144
145		expect(newValue).toBe('hello [myRepo](file:///Users/foo/myRepo) ');
146		expect(caretOffset).toBe(6 + '[myRepo](file:///Users/foo/myRepo) '.length);
147	});
148
149	it('keeps the trailing slash on the directory marker', () => {
150		const value = 'see @src';
151		const { newValue } = buildMentionInsertion(dir('/Users/foo/myRepo/src/', 'src'), value, {
152			end: 8,
153			start: 4
154		})!;
155
156		expect(newValue).toBe('see [src](file:///Users/foo/myRepo/src/) ');
157	});
158
159	it('escapes spaces and parens in the target', () => {
160		const value = '@pic';
161		const { newValue } = buildMentionInsertion(
162			file('/Users/foo/Desktop/Pic (1).png', 'Pic (1).png'),
163			value,
164			{ end: 4, start: 0 }
165		)!;
166
167		expect(newValue).toBe('[Pic (1).png](file:///Users/foo/Desktop/Pic%20(1).png) ');
168	});
169
170	it('re-adds the directory marker when the cleaned path empties', () => {
171		const { newValue } = buildMentionInsertion(dir('/', 'root'), '/', { end: 1, start: 0 })!;
172
173		expect(newValue).toBe('[root](file:///) ');
174	});
175
176	it('returns null for an out-of-range token', () => {
177		expect(buildMentionInsertion(file('/a/b.txt', 'b.txt'), 'x', { end: 5, start: 0 })).toBeNull();
178		expect(buildMentionInsertion(file('/a/b.txt', 'b.txt'), 'x', { end: 1, start: 2 })).toBeNull();
179	});
180});