File size: 3,122 Bytes
7604e0d
 
4fb118c
 
 
 
 
 
 
 
 
 
 
 
 
 
7604e0d
 
 
 
 
 
 
 
 
 
 
 
4fb118c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, describe, expect, it, vi } from 'vitest'
import type { PosterRequest } from './types'
import { exportPoster, prepareMap, searchPlaces } from './api'

function jsonResponse(payload: unknown): Response {
  return { ok: true, json: async () => payload } as unknown as Response
}

function exportResponse(disposition: string): Response {
  return {
    ok: true,
    status: 200,
    headers: { get: (name: string) => (name.toLowerCase() === 'content-disposition' ? disposition : null) },
    blob: async () => new Blob(['poster']),
  } as unknown as Response
}

describe('API network errors', () => {
  afterEach(() => vi.unstubAllGlobals())

  it('replaces the browser Failed to fetch message with actionable guidance', async () => {
    vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')))

    await expect(prepareMap({} as PosterRequest)).rejects.toThrow(
      'The map request was interrupted. Please retry or zoom in to a smaller area.',
    )
  })
})

describe('place search language', () => {
  afterEach(() => vi.unstubAllGlobals())

  it.each(['zh', 'zh-CN', 'zh-Hans'])('requests lang=cn for the %s locale', async (language) => {
    vi.stubGlobal('navigator', { language })
    const fetchMock = vi.fn().mockResolvedValue(jsonResponse([]))
    vi.stubGlobal('fetch', fetchMock)

    await searchPlaces('Tokyo')

    const url = String(fetchMock.mock.calls[0][0])
    expect(url).toContain('lang=cn')
    expect(url).not.toContain('lang=zh')
  })

  it('requests lang=en for non-Chinese locales', async () => {
    vi.stubGlobal('navigator', { language: 'en-US' })
    const fetchMock = vi.fn().mockResolvedValue(jsonResponse([]))
    vi.stubGlobal('fetch', fetchMock)

    await searchPlaces('Tokyo')

    expect(String(fetchMock.mock.calls[0][0])).toContain('lang=en')
  })
})

describe('export download filename', () => {
  afterEach(() => vi.unstubAllGlobals())

  it('prefers the RFC 5987 filename* for Chinese names', async () => {
    const disposition =
      'attachment; filename="japanese-ink-20261017.png"; ' +
      "filename*=UTF-8''%E5%8C%97%E4%BA%AC%E5%B8%82-japanese-ink-20261017.png"
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(exportResponse(disposition)))

    const { filename } = await exportPoster('a'.repeat(64), {} as PosterRequest, 'png', 300)

    expect(filename).toBe('北京市-japanese-ink-20261017.png')
  })

  it('falls back to the ASCII filename when filename* is absent', async () => {
    const disposition = 'attachment; filename="beijing-japanese-ink-20261017.png"'
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(exportResponse(disposition)))

    const { filename } = await exportPoster('a'.repeat(64), {} as PosterRequest, 'png', 300)

    expect(filename).toBe('beijing-japanese-ink-20261017.png')
  })

  it('uses the format-based default when the header is missing', async () => {
    vi.stubGlobal('fetch', vi.fn().mockResolvedValue(exportResponse('')))

    const { filename } = await exportPoster('a'.repeat(64), {} as PosterRequest, 'pdf', 300)

    expect(filename).toBe('maptoposter.pdf')
  })
})