Skip to content

Commit ad0414e

Browse files
committed
add unit tests
1 parent 0cb3684 commit ad0414e

File tree

1 file changed

+174
-78
lines changed

1 file changed

+174
-78
lines changed

src/vs/workbench/contrib/chat/test/common/promptSyntax/parsers/textModelPromptParser.test.ts

Lines changed: 174 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ILogService, NullLogService } from '../../../../../../../platform/log/c
2222
import { TextModelPromptParser } from '../../../../common/promptSyntax/parsers/textModelPromptParser.js';
2323
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../../base/test/common/utils.js';
2424
import { IInstantiationService } from '../../../../../../../platform/instantiation/common/instantiation.js';
25-
import { INSTRUCTIONS_LANGUAGE_ID, PROMPT_LANGUAGE_ID } from '../../../../common/promptSyntax/constants.js';
25+
import { INSTRUCTIONS_LANGUAGE_ID, MODE_LANGUAGE_ID, PROMPT_LANGUAGE_ID } from '../../../../common/promptSyntax/constants.js';
2626
import { InMemoryFileSystemProvider } from '../../../../../../../platform/files/common/inMemoryFilesystemProvider.js';
2727
import { ExpectedDiagnosticError, ExpectedDiagnosticWarning, TExpectedDiagnostic } from '../testUtils/expectedDiagnostic.js';
2828
import { TestInstantiationService } from '../../../../../../../platform/instantiation/test/common/instantiationServiceMock.js';
@@ -334,6 +334,62 @@ suite('TextModelPromptParser', () => {
334334
'Must have empty metadata.',
335335
);
336336
});
337+
338+
test(`• has correct 'instructions' metadata`, async () => {
339+
const test = createTest(
340+
URI.file('/absolute/folder/and/a/filename.instructions.md'),
341+
[
342+
/* 01 */"---",
343+
/* 02 */"description: 'My prompt.'\t\t",
344+
/* 03 */" something: true", /* unknown metadata record */
345+
/* 04 */" tools: [ 'tool_name1', \"tool_name2\", 'tool_name1', true, false, '', 'tool_name2' ]\t\t",
346+
/* 05 */" tools: [ 'tool_name3', \"tool_name4\" ]", /* duplicate `tools` record is ignored */
347+
/* 06 */" tools: 'tool_name5'", /* duplicate `tools` record with invalid value is ignored */
348+
/* 07 */" mode: 'agent'",
349+
/* 07 */" applyTo: 'frontend/**/*spec.ts'",
350+
/* 08 */"---",
351+
/* 09 */"The cactus on my desk has a thriving Instagram account.",
352+
/* 10 */"Midnight snacks are the secret to eternal [text](./foo-bar-baz/another-file.ts) happiness.",
353+
/* 11 */"In an alternate universe, pigeons deliver sushi by drone.",
354+
/* 12 */"Lunar rainbows only appear when you sing in falsetto.",
355+
/* 13 */"Carrots have secret telepathic abilities, but only on Tuesdays.",
356+
],
357+
INSTRUCTIONS_LANGUAGE_ID,
358+
);
359+
360+
await test.validateReferences([
361+
new ExpectedReference({
362+
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
363+
text: '[text](./foo-bar-baz/another-file.ts)',
364+
path: './foo-bar-baz/another-file.ts',
365+
startLine: 11,
366+
startColumn: 43,
367+
pathStartColumn: 50,
368+
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
369+
}),
370+
]);
371+
372+
const { header, metadata } = test.parser;
373+
assertDefined(
374+
header,
375+
'Prompt header must be defined.',
376+
);
377+
378+
assert(
379+
metadata?.promptType === PromptsType.instructions,
380+
`Must be a 'instructions' metadata, got '${JSON.stringify(metadata)}'.`,
381+
);
382+
383+
assert.deepStrictEqual(
384+
metadata,
385+
{
386+
promptType: PromptsType.instructions,
387+
description: 'My prompt.',
388+
applyTo: 'frontend/**/*spec.ts',
389+
},
390+
'Must have correct metadata.',
391+
);
392+
});
337393
});
338394

339395
suite(' • prompts', () => {
@@ -379,12 +435,11 @@ suite('TextModelPromptParser', () => {
379435
'Must have empty metadata.',
380436
);
381437
});
382-
});
383438

384-
test(`• has correct 'prompt' metadata`, async () => {
385-
const test = createTest(
386-
URI.file('/absolute/folder/and/a/filename.txt'),
387-
[
439+
test(`• has correct 'prompt' metadata`, async () => {
440+
const test = createTest(
441+
URI.file('/absolute/folder/and/a/filename.txt'),
442+
[
388443
/* 01 */"---",
389444
/* 02 */"description: 'My prompt.'\t\t",
390445
/* 03 */" something: true", /* unknown metadata record */
@@ -399,99 +454,140 @@ suite('TextModelPromptParser', () => {
399454
/* 11 */"In an alternate universe, pigeons deliver sushi by drone.",
400455
/* 12 */"Lunar rainbows only appear when you sing in falsetto.",
401456
/* 13 */"Carrots have secret telepathic abilities, but only on Tuesdays.",
402-
],
403-
PROMPT_LANGUAGE_ID,
404-
);
457+
],
458+
PROMPT_LANGUAGE_ID,
459+
);
405460

406-
await test.validateReferences([
407-
new ExpectedReference({
408-
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
409-
text: '[text](./foo-bar-baz/another-file.ts)',
410-
path: './foo-bar-baz/another-file.ts',
411-
startLine: 11,
412-
startColumn: 43,
413-
pathStartColumn: 50,
414-
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
415-
}),
416-
]);
461+
await test.validateReferences([
462+
new ExpectedReference({
463+
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
464+
text: '[text](./foo-bar-baz/another-file.ts)',
465+
path: './foo-bar-baz/another-file.ts',
466+
startLine: 11,
467+
startColumn: 43,
468+
pathStartColumn: 50,
469+
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
470+
}),
471+
]);
417472

418-
const { header, metadata } = test.parser;
419-
assertDefined(
420-
header,
421-
'Prompt header must be defined.',
422-
);
473+
const { header, metadata } = test.parser;
474+
assertDefined(
475+
header,
476+
'Prompt header must be defined.',
477+
);
423478

424-
assert(
425-
metadata?.promptType === PromptsType.prompt,
426-
`Must be a 'prompt' metadata, got '${JSON.stringify(metadata)}'.`,
427-
);
479+
assert(
480+
metadata?.promptType === PromptsType.prompt,
481+
`Must be a 'prompt' metadata, got '${JSON.stringify(metadata)}'.`,
482+
);
428483

429-
assert.deepStrictEqual(
430-
metadata,
431-
{
432-
promptType: PromptsType.prompt,
433-
mode: 'agent',
434-
description: 'My prompt.',
435-
tools: ['tool_name1', 'tool_name2'],
436-
},
437-
'Must have correct metadata.',
438-
);
484+
assert.deepStrictEqual(
485+
metadata,
486+
{
487+
promptType: PromptsType.prompt,
488+
mode: 'agent',
489+
description: 'My prompt.',
490+
tools: ['tool_name1', 'tool_name2'],
491+
},
492+
'Must have correct metadata.',
493+
);
494+
});
439495
});
440496

441-
test(`• has correct 'instructions' metadata`, async () => {
442-
const test = createTest(
443-
URI.file('/absolute/folder/and/a/filename.instructions.md'),
444-
[
497+
suite(' • modes', () => {
498+
test(`• empty header`, async () => {
499+
const test = createTest(
500+
URI.file('/absolute/folder/and/a/filename.txt'),
501+
[
445502
/* 01 */"---",
446-
/* 02 */"description: 'My prompt.'\t\t",
503+
/* 02 */"",
504+
/* 03 */"---",
505+
/* 04 */"The cactus on my desk has a thriving Instagram account.",
506+
/* 05 */"Midnight snacks are the secret to eternal [text](./foo-bar-baz/another-file.ts) happiness.",
507+
/* 06 */"In an alternate universe, pigeons deliver sushi by drone.",
508+
/* 07 */"Lunar rainbows only appear when you sing in falsetto.",
509+
/* 08 */"Carrots have secret telepathic abilities, but only on Tuesdays.",
510+
],
511+
MODE_LANGUAGE_ID,
512+
);
513+
514+
await test.validateReferences([
515+
new ExpectedReference({
516+
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
517+
text: '[text](./foo-bar-baz/another-file.ts)',
518+
path: './foo-bar-baz/another-file.ts',
519+
startLine: 5,
520+
startColumn: 43,
521+
pathStartColumn: 50,
522+
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
523+
}),
524+
]);
525+
526+
const { header, metadata } = test.parser;
527+
assertDefined(
528+
header,
529+
'Prompt header must be defined.',
530+
);
531+
532+
assert.deepStrictEqual(
533+
metadata,
534+
{
535+
promptType: PromptsType.mode,
536+
},
537+
'Must have empty metadata.',
538+
);
539+
});
540+
541+
test(`• has correct metadata`, async () => {
542+
const test = createTest(
543+
URI.file('/absolute/folder/and/a/filename.txt'),
544+
[
545+
/* 01 */"---",
546+
/* 02 */"description: 'My mode.'\t\t",
447547
/* 03 */" something: true", /* unknown metadata record */
448548
/* 04 */" tools: [ 'tool_name1', \"tool_name2\", 'tool_name1', true, false, '', 'tool_name2' ]\t\t",
449549
/* 05 */" tools: [ 'tool_name3', \"tool_name4\" ]", /* duplicate `tools` record is ignored */
450550
/* 06 */" tools: 'tool_name5'", /* duplicate `tools` record with invalid value is ignored */
451-
/* 07 */" mode: 'agent'",
452551
/* 07 */" applyTo: 'frontend/**/*spec.ts'",
453552
/* 08 */"---",
454553
/* 09 */"The cactus on my desk has a thriving Instagram account.",
455554
/* 10 */"Midnight snacks are the secret to eternal [text](./foo-bar-baz/another-file.ts) happiness.",
456555
/* 11 */"In an alternate universe, pigeons deliver sushi by drone.",
457556
/* 12 */"Lunar rainbows only appear when you sing in falsetto.",
458557
/* 13 */"Carrots have secret telepathic abilities, but only on Tuesdays.",
459-
],
460-
INSTRUCTIONS_LANGUAGE_ID,
461-
);
462-
463-
await test.validateReferences([
464-
new ExpectedReference({
465-
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
466-
text: '[text](./foo-bar-baz/another-file.ts)',
467-
path: './foo-bar-baz/another-file.ts',
468-
startLine: 11,
469-
startColumn: 43,
470-
pathStartColumn: 50,
471-
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
472-
}),
473-
]);
558+
],
559+
MODE_LANGUAGE_ID,
560+
);
474561

475-
const { header, metadata } = test.parser;
476-
assertDefined(
477-
header,
478-
'Prompt header must be defined.',
479-
);
562+
await test.validateReferences([
563+
new ExpectedReference({
564+
uri: URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'),
565+
text: '[text](./foo-bar-baz/another-file.ts)',
566+
path: './foo-bar-baz/another-file.ts',
567+
startLine: 10,
568+
startColumn: 43,
569+
pathStartColumn: 50,
570+
childrenOrError: new OpenFailed(URI.file('/absolute/folder/and/a/foo-bar-baz/another-file.ts'), 'File not found.'),
571+
}),
572+
]);
480573

481-
assert(
482-
metadata?.promptType === PromptsType.instructions,
483-
`Must be a 'instructions' metadata, got '${JSON.stringify(metadata)}'.`,
484-
);
574+
const { header, metadata } = test.parser;
575+
assertDefined(
576+
header,
577+
'Mode header must be defined.',
578+
);
485579

486-
assert.deepStrictEqual(
487-
metadata,
488-
{
489-
promptType: PromptsType.instructions,
490-
description: 'My prompt.',
491-
applyTo: 'frontend/**/*spec.ts',
492-
},
493-
'Must have correct metadata.',
494-
);
580+
assert.deepStrictEqual(
581+
metadata,
582+
{
583+
promptType: PromptsType.mode,
584+
mode: 'agent',
585+
description: 'My mode.',
586+
tools: ['tool_name1', 'tool_name2'],
587+
},
588+
'Must have correct metadata.',
589+
);
590+
});
495591
});
496592
});
497593

0 commit comments

Comments
 (0)