Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { z } from 'zod'
3import {
4 promptDefinition,
5 resourceDefinition,
6} from '../../src/server/definitions'
7
8describe('resourceDefinition', () => {
9 it('returns the contents from the read function', async () => {
10 const resource = resourceDefinition({
11 uri: 'file:///readme.md',
12 name: 'readme',
13 mimeType: 'text/markdown',
14 }).read(async () => ({ text: 'hello' }))
15
16 expect(
17 await resource.read(new URL('file:///readme.md'), {}, { context: {} }),
18 ).toEqual({ text: 'hello' })
19 })
20
21 it('accepts a uriTemplate when uri is missing', async () => {
22 const resource = resourceDefinition({
23 name: 'file',
24 mimeType: 'text/plain',
25 uriTemplate: 'file:///{path}',
26 }).read(async () => ({ text: 'body' }))
27
28 expect(resource.uriTemplate).toBe('file:///{path}')
29 expect(
30 await resource.read(new URL('file:///a'), { path: 'a' }, { context: {} }),
31 ).toEqual({ text: 'body' })
32 })
33
34 it('gives the read function the variables parsed by argsSchema', async () => {
35 const resource = resourceDefinition({
36 name: 'user',
37 mimeType: 'text/plain',
38 uriTemplate: 'users://{id}',
39 argsSchema: z.object({ id: z.string().transform(Number) }),
40 }).read(async (uri, { id }) => ({ text: `${id + 1} ${uri.href}` }))
41
42 expect(
43 await resource.read(new URL('users://41'), { id: '41' }, { context: {} }),
44 ).toEqual({ text: '42 users://41' })
45 })
46
47 it('throws when uri and uriTemplate are missing', () => {
48 expect(() =>
49 // @ts-expect-error A resource needs a uri or a uriTemplate.
50 resourceDefinition({
51 name: 'file',
52 mimeType: 'text/plain',
53 }),
54 ).toThrow(
55 'This resource has no uri and no uriTemplate. Pass a uri or a uriTemplate.',
56 )
57 })
58})
59
60describe('promptDefinition', () => {
61 it('returns the message built from the parsed arguments', async () => {
62 const prompt = promptDefinition({
63 name: 'summarize',
64 description: 'Summarize a topic',
65 argsSchema: z.object({
66 topic: z.string().transform((value) => value.toUpperCase()),
67 }),
68 }).render(async (args) => [{ role: 'user', content: args.topic }])
69
70 expect(await prompt.render({ topic: 'x' })).toEqual([
71 { role: 'user', content: 'X' },
72 ])
73 })
74})