Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

definitions.test.ts

56 lines1.6 KB

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(await resource.read()).toEqual({ text: 'hello' })
17 })
18​
19 it('accepts a uriTemplate when uri is missing', async () => {
20 const resource = resourceDefinition({
21 name: 'file',
22 mimeType: 'text/plain',
23 uriTemplate: 'file:///{path}',
24 }).read(async () => ({ text: 'body' }))
25​
26 expect(resource.uriTemplate).toBe('file:///{path}')
27 expect(await resource.read()).toEqual({ text: 'body' })
28 })
29​
30 it('throws when uri and uriTemplate are missing', () => {
31 expect(() =>
32 resourceDefinition({
33 name: 'file',
34 mimeType: 'text/plain',
35 }),
36 ).toThrow(
37 'This resource has no uri and no uriTemplate. Pass a uri or a uriTemplate.',
38 )
39 })
40})
41​
42describe('promptDefinition', () => {
43 it('returns the message built from the parsed arguments', async () => {
44 const prompt = promptDefinition({
45 name: 'summarize',
46 description: 'Summarize a topic',
47 argsSchema: z.object({
48 topic: z.string().transform((value) => value.toUpperCase()),
49 }),
50 }).render(async (args) => [{ role: 'user', content: args.topic }])
51​
52 expect(await prompt.render({ topic: 'x' })).toEqual([
53 { role: 'user', content: 'X' },
54 ])
55 })
56})