forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth-validation.test.ts
More file actions
82 lines (65 loc) · 3.32 KB
/
Copy pathhealth-validation.test.ts
File metadata and controls
82 lines (65 loc) · 3.32 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
import { describe, it, expect } from 'vitest';
import { validateHealthStatus } from '../src/validation/health-status';
import { LilyValidationError } from '../src/errors/sdk-error';
import { SystemClient } from '../src/clients/system-client';
import { resolveLilySdkConfig } from '../src/config/types';
import { vi } from 'vitest';
describe('validateHealthStatus (issue #422)', () => {
it('passes through a valid HealthStatus payload unchanged', () => {
const data = { status: 'ok', version: '1.0.0', uptime: 42 };
const result = validateHealthStatus(data);
expect(result).toEqual(data);
});
it('throws VALIDATION_ERROR for null input', () => {
expect(() => validateHealthStatus(null)).toThrow(LilyValidationError);
expect(() => validateHealthStatus(null)).toThrow('VALIDATION_ERROR');
});
it('throws VALIDATION_ERROR for non-object input', () => {
expect(() => validateHealthStatus('string')).toThrow(LilyValidationError);
expect(() => validateHealthStatus(42)).toThrow(LilyValidationError);
expect(() => validateHealthStatus(undefined)).toThrow(LilyValidationError);
});
it('throws VALIDATION_ERROR when status is missing', () => {
expect(() => validateHealthStatus({ version: '1.0' })).toThrow('status');
});
it('throws VALIDATION_ERROR when status is not a string', () => {
expect(() => validateHealthStatus({ status: 123 })).toThrow('status must be a string');
});
it('throws VALIDATION_ERROR for unknown status values', () => {
expect(() => validateHealthStatus({ status: 'unknown' })).toThrow('one of');
});
it('throws VALIDATION_ERROR when version is not a string', () => {
expect(() => validateHealthStatus({ status: 'ok', version: 123 })).toThrow('version');
});
it('throws VALIDATION_ERROR when uptime is not a number', () => {
expect(() => validateHealthStatus({ status: 'ok', uptime: 'fourty-two' })).toThrow('uptime');
});
it('accepts status "ok"', () => {
expect(validateHealthStatus({ status: 'ok' })).toEqual({ status: 'ok' });
});
it('accepts status "degraded"', () => {
expect(validateHealthStatus({ status: 'degraded' })).toEqual({ status: 'degraded' });
});
it('accepts status "down"', () => {
expect(validateHealthStatus({ status: 'down' })).toEqual({ status: 'down' });
});
});
describe('SystemClient with validateResponses=true (issue #422)', () => {
it('validates health response when validateResponses is enabled', async () => {
const config = resolveLilySdkConfig({ baseUrl: 'https://test.example.com', validateResponses: true });
const client = new SystemClient(config);
// Mock the parent request method
const mockRequest = vi.fn().mockResolvedValue({ status: 'ok', version: '2.0', uptime: 100 });
client.request = mockRequest as any;
const result = await client.health();
expect(result.status).toBe('ok');
expect(result.version).toBe('2.0');
});
it('rejects invalid health response when validateResponses is enabled', async () => {
const config = resolveLilySdkConfig({ baseUrl: 'https://test.example.com', validateResponses: true });
const client = new SystemClient(config);
const mockRequest = vi.fn().mockResolvedValue({ status: 'invalid_status' });
client.request = mockRequest as any;
await expect(client.health()).rejects.toThrow('VALIDATION_ERROR');
});
});