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

key.test.ts

105 lines3.1 KB

1import { describe, expect, it } from 'vitest'
2import { computeSandboxKey, computeWorkspaceHash } from '../src/key'
3import { createSecrets } from '../src/secrets'
4import { defineWorkspace, githubRepo } from '../src/workspace'
5import type { SandboxKeyInput } from '../src/key'
6​
7const base: SandboxKeyInput = {
8 threadId: 'thread-1',
9 sandboxId: 'repo',
10 providerName: 'docker',
11 workspace: defineWorkspace({
12 source: githubRepo({ repo: 'TanStack/ai', ref: 'main' }),
13 }),
15​
16describe('computeSandboxKey', () => {
17 it('is stable for identical inputs', () => {
18 expect(computeSandboxKey(base)).toBe(computeSandboxKey({ ...base }))
19 })
20​
21 it('changes when thread, sandbox id, provider, or tenant change', () => {
22 const k = computeSandboxKey(base)
23 expect(computeSandboxKey({ ...base, threadId: 'thread-2' })).not.toBe(k)
24 expect(computeSandboxKey({ ...base, sandboxId: 'other' })).not.toBe(k)
25 expect(computeSandboxKey({ ...base, providerName: 'cloudflare' })).not.toBe(
26 k,
27 )
28 expect(computeSandboxKey({ ...base, tenant: { orgId: 'acme' } })).not.toBe(
29 k,
30 )
31 })
32​
33 it('changes when the workspace source/ref changes (busts stale env)', () => {
34 const k = computeSandboxKey(base)
35 const otherRef = computeSandboxKey({
36 ...base,
37 workspace: defineWorkspace({
38 source: githubRepo({ repo: 'TanStack/ai', ref: 'next' }),
39 }),
40 })
41 expect(otherRef).not.toBe(k)
42 })
43})
44​
45describe('computeWorkspaceHash', () => {
46 it('excludes secrets (rotating a token must not orphan the sandbox)', () => {
47 const a = computeWorkspaceHash(
48 defineWorkspace({
49 source: githubRepo({ repo: 'TanStack/ai' }),
50 secrets: createSecrets({ A: '1' }),
51 }),
52 )
53 const b = computeWorkspaceHash(
54 defineWorkspace({
55 source: githubRepo({ repo: 'TanStack/ai' }),
56 secrets: createSecrets({ A: '2' }),
57 }),
58 )
59 expect(a).toBe(b)
60 })
61​
62 it('excludes the git source token (#1468)', () => {
63 const a = computeWorkspaceHash(
64 defineWorkspace({
65 source: githubRepo({ repo: 'TanStack/ai', auth: { token: 'old' } }),
66 }),
67 )
68 const b = computeWorkspaceHash(
69 defineWorkspace({
70 source: githubRepo({ repo: 'TanStack/ai', auth: { token: 'new' } }),
71 }),
72 )
73 expect(a).toBe(b)
74 })
75​
76 it('changes when setup/scripts change', () => {
77 const a = computeWorkspaceHash(
78 defineWorkspace({
79 source: githubRepo({ repo: 'TanStack/ai' }),
80 setup: ['pnpm i'],
81 }),
82 )
83 const b = computeWorkspaceHash(
84 defineWorkspace({
85 source: githubRepo({ repo: 'TanStack/ai' }),
86 setup: ['npm i'],
87 }),
88 )
89 expect(a).not.toBe(b)
90​
91 const withTest = computeWorkspaceHash(
92 defineWorkspace({
93 source: githubRepo({ repo: 'TanStack/ai' }),
94 scripts: { test: 'pnpm test' },
95 }),
96 )
97 const withTypecheck = computeWorkspaceHash(
98 defineWorkspace({
99 source: githubRepo({ repo: 'TanStack/ai' }),
100 scripts: { typecheck: 'pnpm test:types' },
101 }),
102 )
103 expect(withTest).not.toBe(withTypecheck)
104 })
105})