Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

us-egress.test.ts

48 lines1.5 KB

1import { describe, expect, it, vi } from 'vitest'
2​
3import type { ProviderEnvironment } from './types.ts'
4import { usEgressFetch } from './us-egress.ts'
5​
6describe('usEgressFetch', () => {
7 it('routes through the enam-hinted egress object', async () => {
8 const calls: Array<unknown> = []
9 const ns = {
10 idFromName(name: string) {
11 calls.push(['idFromName', name])
12 return 'id'
13 },
14 get(id: string, opts: unknown) {
15 calls.push(['get', id, opts])
16 return {
17 fetch(url: string, init?: RequestInit) {
18 calls.push(['fetch', url, init?.headers])
19 return Promise.resolve(Response.json({ ok: true }))
20 },
21 }
22 },
23 }
24 const env = { US_EGRESS: ns } as unknown as ProviderEnvironment
25 const res = await usEgressFetch(env)('https://api.example/v1/models', {
26 headers: { 'x-api-key': 'k' },
27 })
28 expect(await res.json()).toEqual({ ok: true })
29 expect(calls).toEqual([
30 ['idFromName', 'provider-egress'],
31 ['get', 'id', { locationHint: 'enam' }],
32 ['fetch', 'https://api.example/v1/models', { 'x-api-key': 'k' }],
33 ])
34 })
35​
36 it('uses the global fetch outside the Workers runtime', () => {
37 expect(usEgressFetch({})).toBe(fetch)
38 })
39​
40 it('throws inside the Workers runtime when the binding is missing', () => {
41 vi.stubGlobal('navigator', { userAgent: 'Cloudflare-Workers' })
42 try {
43 expect(() => usEgressFetch({})).toThrow(/US_EGRESS binding missing/)
44 } finally {
45 vi.unstubAllGlobals()
46 }
47 })
48})