Files
yhy-mager/frontend/src/api-health.test.ts
xiewenjun 54c318c615
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled
feat: implement license platform business flows and delivery tooling
Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
2026-10-09 15:33:22 +08:00

28 lines
1.2 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import axios from 'axios'
import { checkApiHealth } from './api-health'
describe('checkApiHealth', () => {
it('returns online for the live response', async () => {
const request = vi.fn().mockResolvedValue({ data: { status: 'ok' } })
await expect(checkApiHealth(request)).resolves.toBe('online')
expect(request).toHaveBeenCalledWith('/api/v1/health/live', { timeout: 3000 })
})
it('returns offline for invalid status', async () => {
await expect(checkApiHealth(vi.fn().mockResolvedValue({ data: { status: 'degraded' } }))).resolves.toBe('offline')
await expect(checkApiHealth(vi.fn().mockResolvedValue({ data: null }))).resolves.toBe('offline')
})
it('returns offline when the request rejects', async () => {
await expect(checkApiHealth(vi.fn().mockRejectedValue(new Error('network unavailable')))).resolves.toBe('offline')
})
it('uses Axios by default', async () => {
const request = vi.spyOn(axios, 'get').mockResolvedValue({ data: { status: 'ok' } } as never)
await expect(checkApiHealth()).resolves.toBe('online')
expect(request).toHaveBeenCalledOnce()
request.mockRestore()
})
})