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

index.ts

92 lines3.6 KB

1/**
2 * `runSkillSourceConformance` — the real deliverable for third-party adapters.
3 * Since adapter code is frequently LLM-generated, this suite (not prose) is what
4 * makes a new `SkillSource` safe to ship.
5 *
6 * The factory must return a source seeded with this fixed fixture contract:
7 *
8 * - skill `alpha`: description non-empty; resource `references/note.md` whose
9 * contents are exactly `hello`; script `scripts/run.py` whose bytes decode
10 * to `print(1)` (only if the source supports scripts).
11 * - skill `beta`: description non-empty; no resources required.
12 *
13 * Sources that cannot represent a tier (resources/scripts) simply omit the
14 * corresponding methods — those cases are skipped, not failed.
15 */
16import { describe, expect, it } from 'vitest'
17import type { SkillSource } from '../types'
18​
19const dec = (v: string | Uint8Array) =>
20 typeof v === 'string' ? v : new TextDecoder().decode(v)
21​
22export function runSkillSourceConformance(
23 factory: () => SkillSource | Promise<SkillSource>,
24 label = 'SkillSource',
25): void {
26 describe(`conformance: ${label}`, () => {
27 it('lists skills with a name and description', async () => {
28 const source = await factory()
29 const skills = await source.list()
30 const names = skills.map((s) => s.name)
31 expect(names).toContain('alpha')
32 expect(names).toContain('beta')
33 for (const s of skills) {
34 expect(s.name).toBeTruthy()
35 expect(s.description).toBeTruthy()
36 }
37 })
38​
39 it('loads a known skill body', async () => {
40 const source = await factory()
41 const body = await source.load('alpha')
42 expect(typeof body).toBe('string')
43 expect(body.length).toBeGreaterThan(0)
44 })
45​
46 it('throws (not returns empty) for a missing skill name', async () => {
47 const source = await factory()
48 await expect(source.load('does-not-exist')).rejects.toThrow()
49 })
50​
51 it('has a stable revision across identical content', async () => {
52 const source = await factory()
53 const rev = source.revision
54 if (!rev) return
55 const a = await rev()
56 const b = await rev()
57 expect(a).toBe(b)
58 const other = await factory()
59 if (other.revision) expect(await other.revision()).toBe(a)
60 })
61​
62 it('serves concurrent list() consistently', async () => {
63 const source = await factory()
64 const [a, b] = await Promise.all([source.list(), source.list()])
65 expect(a.map((s) => s.name).sort()).toEqual(b.map((s) => s.name).sort())
66 })
67​
68 it('reads a bundled resource and rejects path traversal', async () => {
69 const source = await factory()
70 const { listResources, readResource } = source
71 if (!listResources || !readResource) return
72 const resources = await listResources('alpha')
73 expect(resources).toContain('references/note.md')
74 const value = await readResource('alpha', 'references/note.md')
75 // trimEnd: a file-backed source keeps the fixture's trailing newline (formatters add one); the payload is what matters.
76 expect(dec(value).trimEnd()).toBe('hello')
77 await expect(readResource('alpha', '../../etc/passwd')).rejects.toThrow()
78 })
79​
80 it('returns script bytes correctly', async () => {
81 const source = await factory()
82 if (!source.listScripts || !source.readScript) return
83 const scripts = await source.listScripts('alpha')
84 const ref = scripts.find((s) => s.path === 'scripts/run.py')
85 if (!ref) return
86 expect(ref.executable).toBe(false)
87 const bytes = await source.readScript('alpha', 'scripts/run.py')
88 expect(bytes).toBeInstanceOf(Uint8Array)
89 expect(dec(bytes)).toContain('print(1)')
90 })
91 })