Repository navigation
Expand file tree
/
Copy pathliveSpecValidation.test.ts
More file actions
280 lines (249 loc) · 8.56 KB
/
Copy pathliveSpecValidation.test.ts
File metadata and controls
280 lines (249 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { describe, it, expect, vi, beforeEach } from 'vitest'
import {
loadOpenApiSpec,
buildSpecAjv,
resolveTestRequest,
getResponseSchema,
validateResponseAgainstSchema,
checkResponseAgainstSpec,
formatMarkdownReport,
postOrUpdateGitHubReport,
type ValidationSummary,
} from '../openapi/liveValidator'
describe('Live Spec Validator', () => {
const spec = loadOpenApiSpec()
const ajv = buildSpecAjv(spec)
describe('Route and Request Resolution', () => {
it('loads openapi.yaml and finds paths', () => {
expect(spec).toBeDefined()
expect(spec.paths).toBeDefined()
expect(Object.keys(spec.paths).length).toBeGreaterThan(20)
})
it('resolves parameterized path templates into valid test URLs', () => {
const priceReq = resolveTestRequest('/v1/price/{assetA}/{assetB}', 'get')
expect(priceReq.urlPath).toBe('/v1/price/XLM/USDC')
expect(priceReq.method).toBe('GET')
const routeReq = resolveTestRequest('/v1/price/{assetA}/{assetB}/route', 'get')
expect(routeReq.urlPath).toBe('/v1/price/XLM/USDC/route?amount=100')
const benchmarkReq = resolveTestRequest('/v1/benchmark/{asset}', 'get')
expect(benchmarkReq.urlPath).toBe('/v1/benchmark/XLM?target=USD')
const basketReq = resolveTestRequest('/v1/basket', 'get')
expect(basketReq.urlPath).toBe('/v1/basket?asset=XLM&weight=1')
})
it('supplies appropriate request bodies for POST operations', () => {
const gqlReq = resolveTestRequest('/v1/graphql', 'post')
expect(gqlReq.method).toBe('POST')
expect(gqlReq.body).toContain('__typename')
const webhookReq = resolveTestRequest('/v1/webhooks', 'post')
expect(webhookReq.method).toBe('POST')
expect(JSON.parse(webhookReq.body!)).toMatchObject({
url: 'https://example.com/webhook',
assetA: 'XLM',
assetB: 'USDC',
})
})
it('resolves response schemas from operations', () => {
const statusOp = spec.paths['/v1/status'].get
const statusSchema = getResponseSchema(statusOp, 200)
expect(statusSchema).toBeDefined()
expect(statusSchema.properties?.ok).toBeDefined()
})
})
describe('Schema Conformance & Deliberate Mismatch Detection', () => {
it('validates a schema-conforming response payload as valid', () => {
const validStatus = {
ok: true,
network: 'testnet',
watchedPairs: ['XLM:native/USDC:GBBD'],
lastIndexedLedger: 1000,
lastProcessedAt: '2026-10-16T10:00:00Z',
ingestLagSeconds: 2,
}
const result = checkResponseAgainstSpec(ajv, spec, '/v1/status', 'get', 200, validStatus)
expect(result.valid).toBe(true)
expect(result.errors).toBeUndefined()
})
it('detects a deliberate schema mismatch: invalid field type', () => {
// In AggregatedPrice, 'price' allows number or string, so we deliberately pass a boolean
const mismatchedPrice = {
assetA: 'XLM',
assetB: 'USDC',
pairKey: 'XLM/USDC',
price: true,
sdexPrice: 0.12,
ammPrice: 0.12,
bestRoute: 'SDEX',
volume24h: 1000,
sdexVolume24h: 500,
ammVolume24h: 500,
vwap1m: 0.12,
vwap5m: 0.12,
vwap1h: 0.12,
vwap24h: 0.12,
priceChange24h: 0.01,
lastUpdated: '2026-10-16T12:00:00Z',
sources: 2,
confidence: 'high',
}
const result = checkResponseAgainstSpec(
ajv,
spec,
'/v1/price/{assetA}/{assetB}',
'get',
200,
mismatchedPrice
)
expect(result.valid).toBe(false)
expect(result.errors).toBeDefined()
expect(result.errors!.length).toBeGreaterThan(0)
const priceError = result.errors!.find(e => e.path === '/price')
expect(priceError).toBeDefined()
expect(priceError!.keyword).toBe('type')
expect(priceError!.message).toContain('must be number')
})
it('detects a deliberate schema mismatch: invalid enum value', () => {
const invalidEnumPrice = {
assetA: 'XLM',
assetB: 'USDC',
pairKey: 'XLM/USDC',
price: 0.15,
sdexPrice: 0.15,
ammPrice: 0.15,
bestRoute: 'SOME_NONEXISTENT_ROUTE', // Invalid enum
volume24h: 1000,
sdexVolume24h: 500,
ammVolume24h: 500,
vwap1m: 0.15,
vwap5m: 0.15,
vwap1h: 0.15,
vwap24h: 0.15,
priceChange24h: 0,
lastUpdated: '2026-10-16T12:00:00Z',
sources: 2,
confidence: 'high',
}
const result = checkResponseAgainstSpec(
ajv,
spec,
'/v1/price/{assetA}/{assetB}',
'get',
200,
invalidEnumPrice
)
expect(result.valid).toBe(false)
const enumError = result.errors!.find(e => e.path === '/bestRoute')
expect(enumError).toBeDefined()
expect(enumError!.keyword).toBe('enum')
})
it('detects a deliberate schema mismatch: missing required property', () => {
// SpreadResponse requires: asset, network, window, startTime, endTime, venues, tightest
const missingRequired = {
asset: 'XLM',
network: 'testnet',
window: '5m',
// missing startTime, endTime, venues, tightest
}
const result = checkResponseAgainstSpec(
ajv,
spec,
'/v1/spreads/{asset}',
'get',
200,
missingRequired
)
expect(result.valid).toBe(false)
const reqErrors = result.errors!.filter(e => e.keyword === 'required')
expect(reqErrors.length).toBeGreaterThan(0)
})
})
describe('Report Formatting', () => {
it('formats a clean Markdown report with metrics and mismatch details', () => {
const summary: ValidationSummary = {
timestamp: '2026-10-16T12:00:00Z',
baseUrl: 'https://lens.stellar.org',
totalDocumented: 25,
checked: 24,
valid: 23,
mismatched: 1,
unreachable: 0,
results: [
{
path: '/status',
method: 'get',
resolvedUrl: '/status',
status: 200,
valid: true,
skipped: false,
durationMs: 45,
},
{
path: '/price/{assetA}/{assetB}',
method: 'get',
resolvedUrl: '/price/XLM/USDC',
status: 200,
valid: false,
skipped: false,
errors: [
{
path: '/price',
keyword: 'type',
message: 'must be number',
},
],
durationMs: 120,
},
],
}
const md = formatMarkdownReport(summary)
expect(md).toContain('Live Service Spec Drift Report')
expect(md).toContain('https://lens.stellar.org')
expect(md).toContain('Schema Mismatches Detected (1)')
expect(md).toContain('/price')
expect(md).toContain('must be number')
expect(md).toContain('/status')
expect(md).toContain('VALID')
})
})
describe('Single-Issue Update Logic', () => {
it('appends to existing tracking issue rather than creating duplicates', async () => {
const fetchMock = vi.fn()
globalThis.fetch = fetchMock
// Mock finding an existing issue
fetchMock
.mockResolvedValueOnce({
ok: true,
json: async () => [{ number: 99, title: 'Live Service Spec Drift Report' }],
}) // Search call
.mockResolvedValueOnce({ ok: true, json: async () => ({ id: 101 }) }) // Post comment
.mockResolvedValueOnce({ ok: true, json: async () => ({ id: 99 }) }) // Update body
const res = await postOrUpdateGitHubReport('# Test Report', {
token: 'fake-token',
repo: 'Divinity-Labs/Lens',
})
expect(res.action).toBe('appended')
expect(res.issueNumber).toBe(99)
expect(fetchMock).toHaveBeenCalledTimes(3)
})
it('creates a new tracking issue only when no existing report issue exists', async () => {
const fetchMock = vi.fn()
globalThis.fetch = fetchMock
// Mock empty search results
fetchMock
.mockResolvedValueOnce({
ok: true,
json: async () => [],
}) // Search call returns empty
.mockResolvedValueOnce({
ok: true,
json: async () => ({ number: 150 }),
}) // Create issue call
const res = await postOrUpdateGitHubReport('# Test Report', {
token: 'fake-token',
repo: 'Divinity-Labs/Lens',
})
expect(res.action).toBe('created')
expect(res.issueNumber).toBe(150)
expect(fetchMock).toHaveBeenCalledTimes(2)
})
})
})