Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 HARNESSES,
4 PROVIDERS,
5 buildTriagePrompt,
6 isHarness,
7 isProvider,
8 missingEnv,
9 parseIssueUrl,
10 parseVerdict,
11} from './sandbox-triage'
12
13describe('parseIssueUrl', () => {
14 it('extracts repo + issue number', () => {
15 expect(parseIssueUrl('https://github.com/TanStack/ai/issues/123')).toEqual({
16 repo: 'TanStack/ai',
17 issueNumber: 123,
18 })
19 })
20
21 it('tolerates a trailing slash, query, and hash', () => {
22 expect(parseIssueUrl('https://github.com/a/b/issues/7/?x=1#note')).toEqual({
23 repo: 'a/b',
24 issueNumber: 7,
25 })
26 })
27
28 it('throws on a pull-request URL', () => {
29 expect(() => parseIssueUrl('https://github.com/a/b/pull/7')).toThrow(
30 /issue url/i,
31 )
32 })
33
34 it('throws on a non-github / malformed URL', () => {
35 expect(() => parseIssueUrl('not a url')).toThrow(/issue url/i)
36 })
37})
38
39describe('parseVerdict', () => {
40 it('reads the first VERDICT line, case-insensitive', () => {
41 expect(parseVerdict('VERDICT: relevant\n\n## Summary')).toBe('relevant')
42 expect(parseVerdict('verdict: Not-Relevant ')).toBe('not-relevant')
43 expect(parseVerdict('Verdict: UNCERTAIN')).toBe('uncertain')
44 })
45
46 it('returns null when absent or unrecognized', () => {
47 expect(parseVerdict('## Summary\nno verdict here')).toBeNull()
48 expect(parseVerdict('VERDICT: maybe')).toBeNull()
49 })
50})
51
52describe('registries', () => {
53 it('has 5 harnesses and 4 providers with labels + required env arrays', () => {
54 expect(Object.keys(HARNESSES).sort()).toEqual([
55 'acp',
56 'claude-code',
57 'codex',
58 'grok',
59 'opencode',
60 ])
61 expect(Object.keys(PROVIDERS).sort()).toEqual([
62 'daytona',
63 'docker',
64 'local',
65 'railway',
66 'vercel',
67 ])
68 for (const spec of Object.values(HARNESSES)) {
69 expect(typeof spec.label).toBe('string')
70 expect(Array.isArray(spec.requiredEnv)).toBe(true)
71 }
72 })
73
74 it('guards narrow unknown picker values', () => {
75 expect(isHarness('codex')).toBe(true)
76 expect(isHarness('nope')).toBe(false)
77 expect(isProvider('docker')).toBe(true)
78 expect(isProvider(42)).toBe(false)
79 })
80
81 it('missingEnv skips harness keys when authMode is host', () => {
82 delete process.env.XAI_API_KEY
83 delete process.env.GROK_API_KEY
84 expect(missingEnv('grok', 'docker', 'host')).not.toContain('XAI_API_KEY')
85 expect(missingEnv('grok', 'local', 'api-key')).toContain(
86 'XAI_API_KEY (or GROK_API_KEY)',
87 )
88 })
89
90 it('missingEnv requires harness keys when authMode is omitted', () => {
91 delete process.env.XAI_API_KEY
92 delete process.env.GROK_API_KEY
93 expect(missingEnv('grok', 'local')).toContain(
94 'XAI_API_KEY (or GROK_API_KEY)',
95 )
96 })
97
98 it('missingEnv reports unset required vars', () => {
99 delete process.env.ANTHROPIC_API_KEY
100 delete process.env.DAYTONA_API_KEY
101 expect(missingEnv('claude-code', 'docker')).toContain('ANTHROPIC_API_KEY')
102 expect(missingEnv('claude-code', 'daytona')).toEqual(
103 expect.arrayContaining(['ANTHROPIC_API_KEY', 'DAYTONA_API_KEY']),
104 )
105 process.env.ANTHROPIC_API_KEY = 'x'
106 expect(missingEnv('claude-code', 'docker')).toEqual([])
107 })
108
109 it('missingEnv requires a Railway token and an environment ID', () => {
110 process.env.ANTHROPIC_API_KEY = 'x'
111 delete process.env.RAILWAY_TOKEN
112 delete process.env.RAILWAY_API_TOKEN
113 delete process.env.RAILWAY_ENVIRONMENT_ID
114 expect(missingEnv('claude-code', 'railway')).toEqual([
115 'RAILWAY_API_TOKEN (or RAILWAY_TOKEN)',
116 'RAILWAY_ENVIRONMENT_ID',
117 ])
118 // Either token kind still needs the environment.
119 for (const token of ['RAILWAY_API_TOKEN', 'RAILWAY_TOKEN']) {
120 process.env[token] = 'x'
121 expect(missingEnv('claude-code', 'railway')).toEqual([
122 'RAILWAY_ENVIRONMENT_ID',
123 ])
124 process.env.RAILWAY_ENVIRONMENT_ID = 'x'
125 expect(missingEnv('claude-code', 'railway')).toEqual([])
126 delete process.env[token]
127 delete process.env.RAILWAY_ENVIRONMENT_ID
128 }
129 })
130})
131
132describe('buildTriagePrompt', () => {
133 it('includes the issue + the required VERDICT-line instruction', () => {
134 const prompt = buildTriagePrompt(
135 { number: 9, title: 'Boom', body: 'crashes on save', url: 'u' },
136 'a/b',
137 )
138 expect(prompt).toContain('#9')
139 expect(prompt).toContain('crashes on save')
140 expect(prompt).toMatch(/VERDICT:/)
141 expect(prompt).toMatch(/relevant \| not-relevant \| uncertain/)
142 })
143})