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

projection.test.ts

89 lines2.9 KB

1import { describe, expect, it } from 'vitest'
2import {
3 ProjectionCapability,
4 getWorkspaceProjection,
5 provideWorkspaceProjection,
6} from '../src/projection'
7import { createSecrets, resolveSecret } from '../src/secrets'
8import type { WorkspaceProjection } from '../src/projection'
9import type { CapabilityContext } from '@tanstack/ai'
10​
11/** Minimal capability context sufficient for testing capability round-trips. */
12function makeCtx(): CapabilityContext {
13 return {
14 capabilities: { markProvided: () => undefined },
15 } as unknown as CapabilityContext
17​
18function makeProjection(
19 overrides?: Partial<WorkspaceProjection>,
20): WorkspaceProjection {
21 return {
22 skills: [],
23 plugins: [],
24 resolveSecret: () => '',
25 markerPath: '/workspace/.tanstack-projected-abc123',
26 root: '/workspace',
27 ...overrides,
28 }
30​
31describe('ProjectionCapability', () => {
32 it('round-trips: provide then get returns the same object', () => {
33 const ctx = makeCtx()
34 const projection = makeProjection()
35​
36 provideWorkspaceProjection(ctx, projection)
37​
38 const result = getWorkspaceProjection(ctx, { optional: true })
39 expect(result).toBe(projection)
40 })
41​
42 it('resolveSecret resolves a ref built via createSecrets', () => {
43 const ctx = makeCtx()
44 const secrets = createSecrets({ GITHUB_TOKEN: 'ghp_test123' })
45 const projection = makeProjection({
46 resolveSecret: (ref) => resolveSecret(secrets, ref),
47 })
48​
49 provideWorkspaceProjection(ctx, projection)
50 const result = getWorkspaceProjection(ctx)
51 expect(result.resolveSecret(secrets.GITHUB_TOKEN)).toBe('ghp_test123')
52 })
53​
54 it('absent capability returns undefined with optional: true', () => {
55 const ctx = makeCtx()
56 const result = getWorkspaceProjection(ctx, { optional: true })
57 expect(result).toBeUndefined()
58 })
59​
60 it('absent capability throws without optional flag', () => {
61 const ctx = makeCtx()
62 expect(() => getWorkspaceProjection(ctx)).toThrow(
63 'Capability "sandbox-projection" was requested but never provided',
64 )
65 })
66​
67 it('ProjectionCapability.capabilityName is sandbox-projection', () => {
68 expect(ProjectionCapability.capabilityName).toBe('sandbox-projection')
69 })
70​
71 it('has() returns false before providing and true after', () => {
72 const ctx = makeCtx()
73 expect(ProjectionCapability.has(ctx)).toBe(false)
74 provideWorkspaceProjection(ctx, makeProjection())
75 expect(ProjectionCapability.has(ctx)).toBe(true)
76 })
77​
78 it('skills and plugins arrays are stored as-is', () => {
79 const ctx = makeCtx()
80 const skills = [{ kind: 'agent-skill' as const, name: 'my-skill' }]
81 const plugins = ['@anthropic/plugin-search']
82 const projection = makeProjection({ skills, plugins })
83​
84 provideWorkspaceProjection(ctx, projection)
85 const result = getWorkspaceProjection(ctx)
86 expect(result.skills).toBe(skills)
87 expect(result.plugins).toBe(plugins)
88 })
89})