Files

50 lines
2.0 KiB
TypeScript

import { afterEach, expect, it, vi } from 'vitest'
import { api, proxyUrl, downloadFile } from '@/infrastructure/admin-api'
import { writeAuthSession } from '@/application/auth'
import { apiOrigin } from '../../shared/api-origin.ts'
afterEach(() => {
writeAuthSession(null)
vi.unstubAllGlobals()
})
it('selects business API by host and downloads models directly from storage', async () => {
vi.stubGlobal('window', { location: { origin: 'https://cadworld-admin.robotquan.com' } })
const fetcher = vi.fn<typeof fetch>(async () =>
Response.json({ code: 'OK', data: { items: [] } }),
)
vi.stubGlobal('fetch', fetcher)
await api('/admin-api/components?page=1&size=10')
expect(fetcher.mock.calls[0][0]).toBe(
'https://cadworld-api.robotquan.com/admin-api/components?page=1&size=10',
)
expect(fetcher.mock.calls[0][1]?.credentials).toBe('omit')
expect(proxyUrl('http://127.0.0.1:8992/admin-api/component-assets/CW/a.STL?v=2')).toBe(
'https://cadworld-api.robotquan.com/admin-api/component-assets/CW/a.STL?v=2',
)
expect(proxyUrl('https://bucket.example/file?signature=x')).toBe(
'https://bucket.example/file?signature=x',
)
writeAuthSession({
token: 'admin-token',
tokenType: 'Bearer',
expiresAt: Date.now() + 60000,
user: { userCode: 'A', nickname: '管理员', email: 'admin@example.com' },
})
await downloadFile('/admin-api/component-assets/CW/a.STL', 'a.STL')
expect(fetcher.mock.calls[1][0]).toBe(
'https://cadworld-api.robotquan.com/admin-api/component-assets/CW/a.STL',
)
expect(fetcher.mock.calls[1][1]?.credentials).toBe('omit')
expect(new Headers(fetcher.mock.calls[1][1]?.headers).get('Authorization')).toBe(
'Bearer admin-token',
)
})
it('selects development API for dev websites and keeps local development proxies', () => {
expect(apiOrigin('https://dev-cadworld-admin.robotquan.com')).toBe(
'https://dev-cadworld-api.robotquan.com',
)
expect(apiOrigin('http://192.168.1.10:8092')).toBe('')
})