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

selection.test.ts

95 lines2.8 KB

1import { describe, expect, it } from 'vitest'
2​
3import { globToRegExp, matchTargets, parseSelection } from './selection.ts'
4​
5const INDEX = {
6 provider: 'anthropic',
7 activities: {
8 chat: ['v1/messages', 'v1/complete'],
9 image: ['v1/images'],
10 },
12​
13describe('parseSelection', () => {
14 it('parses the full grammar', () => {
15 expect(parseSelection('anthropic')).toEqual({
16 provider: 'anthropic',
17 endpointPattern: '*',
18 kinds: ['input', 'output'],
19 explicitKind: false,
20 raw: 'anthropic',
21 })
22 expect(parseSelection('openai/chat/completions#request')).toMatchObject({
23 provider: 'openai',
24 endpointPattern: 'chat/completions',
25 kinds: ['input'],
26 explicitKind: true,
27 })
28 expect(parseSelection('gemini/*#response')).toMatchObject({
29 provider: 'gemini',
30 endpointPattern: '*',
31 kinds: ['output'],
32 })
33 })
34​
35 it('rejects malformed selections with actionable messages', () => {
36 expect(() => parseSelection('')).toThrow('Empty selection')
37 expect(() => parseSelection('anthropic#body')).toThrow(
38 '#request or #response',
39 )
40 expect(() => parseSelection('*/v1/messages')).toThrow(
41 'globs are only supported in the endpoint part',
42 )
43 expect(() => parseSelection('anthropic/')).toThrow('empty endpoint pattern')
44 expect(() => parseSelection('a#request#response')).toThrow(
45 "more than one '#'",
46 )
47 })
48})
49​
50describe('globToRegExp', () => {
51 it('anchors and escapes everything but *', () => {
52 expect(globToRegExp('v1/*').test('v1/messages')).toBe(true)
53 expect(globToRegExp('v1/*').test('v2/messages')).toBe(false)
54 expect(globToRegExp('a.b').test('axb')).toBe(false)
55 expect(globToRegExp('*messages').test('v1/messages')).toBe(true)
56 })
57})
58​
59describe('matchTargets', () => {
60 it('expands a provider-wide selection to every endpoint × both kinds', () => {
61 const targets = matchTargets(parseSelection('anthropic'), INDEX)
62 expect(targets).toHaveLength(6)
63 expect(targets[0]).toMatchObject({
64 provider: 'anthropic',
65 activity: 'chat',
66 endpointId: 'v1/messages',
67 kind: 'input',
68 explicitKind: false,
69 })
70 })
71​
72 it('filters by glob and pins kinds', () => {
73 const targets = matchTargets(
74 parseSelection('anthropic/v1/m*#request'),
75 INDEX,
76 )
77 expect(targets).toEqual([
78 expect.objectContaining({
79 endpointId: 'v1/messages',
80 kind: 'input',
81 explicitKind: true,
82 }),
83 ])
84 })
85​
86 it('throws for a concrete pattern that matches nothing, listing valid ids', () => {
87 expect(() =>
88 matchTargets(parseSelection('anthropic/v1/message'), INDEX),
89 ).toThrow(/no endpoint 'v1\/message'.*v1\/messages/)
90 })
91​
92 it('returns [] for an unmatched glob (not an error)', () => {
93 expect(matchTargets(parseSelection('anthropic/v9/*'), INDEX)).toEqual([])
94 })
95})