char-fork/llama.cpp

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

Aleksander Grygierrefactor: Naming (#27001)fa4ec4590

char/aliceai-t5-moe
7.2 KiB215 linesraw
1import { containsCodeSpan, isOffsetInCodeBlock, tokenizeContent } from '$lib/utils';
2import { describe, expect, it } from 'vitest';
3
4describe('tokenizeContent', () => {
5	it('tokenizes a plain text buffer with no badges', () => {
6		expect(tokenizeContent('hello world')).toEqual([{ kind: 'text', text: 'hello world' }]);
7	});
8
9	it('tokenizes a single badge', () => {
10		expect(tokenizeContent('[docs](file:///a/b)')).toEqual([
11			{ kind: 'badge', name: 'docs', path: '/a/b' }
12		]);
13	});
14
15	it('tokenizes text around a single badge', () => {
16		expect(tokenizeContent('hello [docs](file:///a/b) world')).toEqual([
17			{ kind: 'text', text: 'hello ' },
18			{ kind: 'badge', name: 'docs', path: '/a/b' },
19			{ kind: 'text', text: ' world' }
20		]);
21	});
22
23	it('tokenizes adjacent badges as separate tokens', () => {
24		expect(tokenizeContent('[a](file:///x)[b](file:///y)')).toEqual([
25			{ kind: 'badge', name: 'a', path: '/x' },
26			{ kind: 'badge', name: 'b', path: '/y' }
27		]);
28	});
29
30	it('leaves non-file links untouched in the stream', () => {
31		expect(tokenizeContent('see [foo](https://example.com) for details')).toEqual([
32			{ kind: 'text', text: 'see [foo](https://example.com) for details' }
33		]);
34	});
35
36	it('recognizes badges whose path contains spaces (macOS screenshots)', () => {
37		const path = '/Users/allozaur/Desktop/Screenshot 2026-07-28 at 17.21.50.png';
38		const source = `[Screenshot 2026-07-28 at 17.21.50.png](file://${path}) `;
39
40		expect(tokenizeContent(source)).toEqual([
41			{ kind: 'badge', name: 'Screenshot 2026-07-28 at 17.21.50.png', path },
42			{ kind: 'text', text: ' ' }
43		]);
44	});
45
46	it('recognizes badges whose path lives in the macOS temp folder', () => {
47		const path =
48			'/var/folders/78/j28m7pn57wb34bfjwlskh62h0000gn/T/TemporaryItems/NSIRD_screencaptureui_GD0A2R/Screenshot 2026-07-28 at 17.23.28.png';
49		const source = `[Screenshot 2026-07-28 at 17.23.28.png](file://${path}) `;
50
51		expect(tokenizeContent(source)).toEqual([
52			{ kind: 'badge', name: 'Screenshot 2026-07-28 at 17.23.28.png', path },
53			{ kind: 'text', text: ' ' }
54		]);
55	});
56
57	it('keeps text around a badge with spaces in the path', () => {
58		const path = '/Users/allozaur/Desktop/Screenshot 2026-07-28 at 17.21.50.png';
59		const source = `see [Screenshot 2026-07-28 at 17.21.50.png](file://${path}) done`;
60
61		expect(tokenizeContent(source)).toEqual([
62			{ kind: 'text', text: 'see ' },
63			{ kind: 'badge', name: 'Screenshot 2026-07-28 at 17.21.50.png', path },
64			{ kind: 'text', text: ' done' }
65		]);
66	});
67
68	it('recognizes badges whose path contains a close parenthesis (macOS duplicate files)', () => {
69		const path = '/Users/foo/Screenshot (1).png';
70		const source = `[Screenshot (1).png](file://${path}) `;
71
72		expect(tokenizeContent(source)).toEqual([
73			{ kind: 'badge', name: 'Screenshot (1).png', path },
74			{ kind: 'text', text: ' ' }
75		]);
76	});
77
78	it('recognizes badges whose folder name is wrapped in parentheses', () => {
79		const path = '/Users/foo/Project (Stuff)/main.rs';
80		const source = `[main.rs](file://${path}) `;
81
82		expect(tokenizeContent(source)).toEqual([
83			{ kind: 'badge', name: 'main.rs', path },
84			{ kind: 'text', text: ' ' }
85		]);
86	});
87
88	it('recognizes adjacent badges back-to-back with no separator', () => {
89		const source = '[a](file:///p)[b](file:///q)';
90
91		expect(tokenizeContent(source)).toEqual([
92			{ kind: 'badge', name: 'a', path: '/p' },
93			{ kind: 'badge', name: 'b', path: '/q' }
94		]);
95	});
96
97	it('tokenizes inline code with the backticks included', () => {
98		expect(tokenizeContent('run `npm test` now')).toEqual([
99			{ kind: 'text', text: 'run ' },
100			{ kind: 'code_inline', text: '`npm test`' },
101			{ kind: 'text', text: ' now' }
102		]);
103	});
104
105	it('tokenizes a fenced code block without a language', () => {
106		const source = 'before\n```\nconst a = 1;\n```\nafter';
107
108		expect(tokenizeContent(source)).toEqual([
109			{ kind: 'text', text: 'before\n' },
110			{ kind: 'code_block', text: '```\nconst a = 1;\n```' },
111			{ kind: 'text', text: '\nafter' }
112		]);
113	});
114
115	it('tokenizes a fenced code block with a language', () => {
116		const source = '```js\nconst a = 1;\n```';
117
118		expect(tokenizeContent(source)).toEqual([
119			{ kind: 'code_block', text: '```js\nconst a = 1;\n```' }
120		]);
121	});
122
123	it('prefers the fenced block over inline spans at triple backticks', () => {
124		expect(tokenizeContent('```a``` ```b```')).toEqual([
125			{ kind: 'code_block', text: '```a```' },
126			{ kind: 'text', text: ' ' },
127			{ kind: 'code_block', text: '```b```' }
128		]);
129	});
130
131	it('leaves an unclosed fence as plain text', () => {
132		expect(tokenizeContent('```js\nconst a = 1;')).toEqual([
133			{ kind: 'text', text: '```js\nconst a = 1;' }
134		]);
135	});
136
137	it('leaves an unclosed inline backtick as plain text', () => {
138		expect(tokenizeContent('run `npm test')).toEqual([{ kind: 'text', text: 'run `npm test' }]);
139	});
140
141	it('does not recognize badges inside code spans', () => {
142		expect(tokenizeContent('`[a](file:///p)`')).toEqual([
143			{ kind: 'code_inline', text: '`[a](file:///p)`' }
144		]);
145	});
146
147	it('tokenizes badges and code spans side by side', () => {
148		expect(tokenizeContent('[a](file:///p) `x`')).toEqual([
149			{ kind: 'badge', name: 'a', path: '/p' },
150			{ kind: 'text', text: ' ' },
151			{ kind: 'code_inline', text: '`x`' }
152		]);
153	});
154});
155
156describe('containsCodeSpan', () => {
157	it('detects inline code', () => {
158		expect(containsCodeSpan('run `npm test` now')).toBe(true);
159	});
160
161	it('detects a fenced block with a language', () => {
162		expect(containsCodeSpan('```js\nconst a = 1;\n```')).toBe(true);
163	});
164
165	it('detects a fenced block without a language', () => {
166		expect(containsCodeSpan('```\ncode\n```')).toBe(true);
167	});
168
169	it('ignores unclosed fences and lone backticks', () => {
170		expect(containsCodeSpan('```js\nconst a = 1;')).toBe(false);
171		expect(containsCodeSpan('run `npm test')).toBe(false);
172		expect(containsCodeSpan('``')).toBe(false);
173	});
174
175	it('ignores plain text and mention links', () => {
176		expect(containsCodeSpan('hello world')).toBe(false);
177		expect(containsCodeSpan('[a](file:///p)')).toBe(false);
178	});
179});
180
181describe('isOffsetInCodeBlock', () => {
182	const BLOCK = '```js\nconst a = 1;\n```';
183
184	it('is false with no fences in the buffer', () => {
185		expect(isOffsetInCodeBlock('hello world', 5)).toBe(false);
186		expect(isOffsetInCodeBlock('run `npm test` now', 10)).toBe(false);
187	});
188
189	it('is true right after the opening fence, before any content', () => {
190		expect(isOffsetInCodeBlock('```', 3)).toBe(true);
191		expect(isOffsetInCodeBlock('```js', 5)).toBe(true);
192	});
193
194	it('is true inside a still-open block while it is being typed', () => {
195		const open = '```js\nconst a = 1;';
196
197		expect(isOffsetInCodeBlock(open, open.length)).toBe(true);
198	});
199
200	it('is true inside a closed block and false outside it', () => {
201		expect(isOffsetInCodeBlock(BLOCK, 6)).toBe(true);
202		expect(isOffsetInCodeBlock(BLOCK, 0)).toBe(false);
203		expect(isOffsetInCodeBlock(BLOCK, BLOCK.length)).toBe(false);
204		expect(isOffsetInCodeBlock(BLOCK + '\nafter', BLOCK.length + 5)).toBe(false);
205	});
206
207	it('toggles per fence across multiple blocks', () => {
208		const two = BLOCK + '\ntext\n' + BLOCK;
209		const secondBlock = two.lastIndexOf(BLOCK);
210
211		expect(isOffsetInCodeBlock(two, secondBlock - 2)).toBe(false);
212		expect(isOffsetInCodeBlock(two, secondBlock + 6)).toBe(true);
213		expect(isOffsetInCodeBlock(two, two.length)).toBe(false);
214	});
215});