api/tests/app.test.ts
120 lines · 3937 bytes
1// Integration tests against the assembled app (no upstream calls)
2import { describe, it, expect } from 'vitest'
3import { buildApp } from '../src/app'
4
5const app = buildApp()
6const env = { API_TOKEN: 'test' }
7
8const hit = (path: string, init?: RequestInit) =>
9 app.fetch(new Request(`http://localhost${path}`, init), env)
10
11describe('public routes', () => {
12 it('health returns ok envelope', async () => {
13 const res = await hit('/v1/health')
14 expect(res.status).toBe(200)
15 expect(await res.json()).toMatchObject({ data: { status: 'ok' } })
16 })
17
18 it('stats has positive counts', async () => {
19 const res = await hit('/v1/stats')
20 const { data } = (await res.json()) as any
21 expect(data.categories).toBeGreaterThan(0)
22 expect(data.services).toBeGreaterThan(0)
23 })
24
25 it('categories returns wrapped list', async () => {
26 const res = await hit('/v1/categories')
27 const body = (await res.json()) as any
28 expect(Array.isArray(body.data)).toBe(true)
29 expect(body.data[0]).toHaveProperty('slug')
30 })
31
32 it('unknown service returns NOT_FOUND envelope', async () => {
33 const res = await hit('/v1/services/nope-nope')
34 expect(res.status).toBe(404)
35 expect(await res.json()).toMatchObject({ error: { code: 'NOT_FOUND' } })
36 })
37
38 it('search without q returns BAD_REQUEST envelope', async () => {
39 const res = await hit('/v1/search')
40 expect(res.status).toBe(400)
41 expect(await res.json()).toMatchObject({ error: { code: 'BAD_REQUEST' } })
42 })
43
44 it('search returns results for known term', async () => {
45 const res = await hit('/v1/search?q=password&limit=3')
46 const body = (await res.json()) as any
47 expect(body.data.length).toBeGreaterThan(0)
48 })
49
50 it('services paginates', async () => {
51 const res = await hit('/v1/services?limit=5')
52 const body = (await res.json()) as any
53 expect(body.data).toHaveLength(5)
54 expect(body.pagination).toMatchObject({ page: 1, limit: 5, hasMore: true })
55 })
56})
57
58describe('auth', () => {
59 it('private route rejects missing token', async () => {
60 const res = await hit('/v1/enrich/privacy/1')
61 expect(res.status).toBe(401)
62 })
63
64 it('private route rejects wrong token', async () => {
65 const res = await hit('/v1/enrich/privacy/1', {
66 headers: { authorization: 'Bearer wrong' },
67 })
68 expect(res.status).toBe(401)
69 })
70
71 it('security enrich route requires auth', async () => {
72 const res = await hit('/v1/enrich/security/jquery/jquery')
73 expect(res.status).toBe(401)
74 })
75
76 it('docker enrich route requires auth', async () => {
77 const res = await hit('/v1/enrich/docker/pihole')
78 expect(res.status).toBe(401)
79 })
80})
81
82describe('middleware scoping', () => {
83 it('public routes are edge cacheable', async () => {
84 const res = await hit('/v1/stats')
85 expect(res.headers.get('cache-control')).toBe('public, s-maxage=300')
86 })
87
88 it('enrich routes are not advertised as public cacheable', async () => {
89 const res = await hit('/v1/enrich/privacy/1')
90 expect(res.headers.get('cache-control') ?? '').not.toContain('public')
91 })
92})
93
94describe('mcp', () => {
95 it('lists tools', async () => {
96 const res = await hit('/v1/mcp', {
97 method: 'POST',
98 headers: { 'content-type': 'application/json' },
99 body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }),
100 })
101 const body = (await res.json()) as any
102 expect(body.result.tools.length).toBeGreaterThan(0)
103 })
104
105 it('runs get_service tool', async () => {
106 const res = await hit('/v1/mcp', {
107 method: 'POST',
108 headers: { 'content-type': 'application/json' },
109 body: JSON.stringify({
110 jsonrpc: '2.0',
111 id: 2,
112 method: 'tools/call',
113 params: { name: 'get_service', arguments: { slug: 'bitwarden' } },
114 }),
115 })
116 const body = (await res.json()) as any
117 const out = JSON.parse(body.result.content[0].text)
118 expect(out.name).toBe('Bitwarden')
119 })
120})