mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
test(pages): cover the page hooks, stores and the api client
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// FE-W4UTL-001 to FE-W4UTL-006
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { getApiErrorMessage } from './apiError'
|
||||
|
||||
describe('getApiErrorMessage', () => {
|
||||
it('FE-W4UTL-001: returns the server-provided error string', () => {
|
||||
const err = { response: { data: { error: 'Places API (New) has not been used in project 42' } } }
|
||||
expect(getApiErrorMessage(err, 'fallback')).toBe('Places API (New) has not been used in project 42')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-002: falls back when the error field is missing', () => {
|
||||
expect(getApiErrorMessage({ response: { data: {} } }, 'fallback')).toBe('fallback')
|
||||
expect(getApiErrorMessage({ response: {} }, 'fallback')).toBe('fallback')
|
||||
expect(getApiErrorMessage({}, 'fallback')).toBe('fallback')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-003: falls back for null/undefined errors', () => {
|
||||
expect(getApiErrorMessage(null, 'fallback')).toBe('fallback')
|
||||
expect(getApiErrorMessage(undefined, 'fallback')).toBe('fallback')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-004: falls back for a whitespace-only server message', () => {
|
||||
expect(getApiErrorMessage({ response: { data: { error: ' ' } } }, 'fallback')).toBe('fallback')
|
||||
expect(getApiErrorMessage({ response: { data: { error: '' } } }, 'fallback')).toBe('fallback')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-005: falls back for a non-string server message', () => {
|
||||
expect(getApiErrorMessage({ response: { data: { error: { code: 500 } } } }, 'fallback')).toBe('fallback')
|
||||
expect(getApiErrorMessage({ response: { data: { error: 42 } } }, 'fallback')).toBe('fallback')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-006: keeps surrounding whitespace of a real message', () => {
|
||||
expect(getApiErrorMessage({ response: { data: { error: ' boom ' } } }, 'fallback')).toBe(' boom ')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,79 @@
|
||||
// FE-W4HEIC-001 to FE-W4HEIC-007
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const isHeic = vi.fn(async (_f: File) => true)
|
||||
const heicTo = vi.fn(async (_opts: unknown) => new Blob(['jpeg'], { type: 'image/jpeg' }))
|
||||
|
||||
vi.mock('heic-to', () => ({
|
||||
isHeic: (f: File) => isHeic(f),
|
||||
heicTo: (opts: unknown) => heicTo(opts),
|
||||
}))
|
||||
|
||||
import { normalizeImageFile, normalizeImageFiles } from './convertHeic'
|
||||
|
||||
beforeEach(() => {
|
||||
isHeic.mockClear()
|
||||
heicTo.mockClear()
|
||||
isHeic.mockResolvedValue(true)
|
||||
})
|
||||
|
||||
describe('normalizeImageFile', () => {
|
||||
it('FE-W4HEIC-001: passes a plain jpeg straight through', async () => {
|
||||
const f = new File(['x'], 'photo.jpg', { type: 'image/jpeg' })
|
||||
await expect(normalizeImageFile(f)).resolves.toBe(f)
|
||||
expect(isHeic).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-W4HEIC-002: converts a .heic file to jpeg and renames it', async () => {
|
||||
const f = new File(['x'], 'IMG_0042.HEIC', { type: '' })
|
||||
const out = await normalizeImageFile(f)
|
||||
|
||||
expect(out).not.toBe(f)
|
||||
expect(out.name).toBe('IMG_0042.jpg')
|
||||
expect(out.type).toBe('image/jpeg')
|
||||
expect(heicTo).toHaveBeenCalledWith({ blob: f, type: 'image/jpeg', quality: 0.92 })
|
||||
})
|
||||
|
||||
it('FE-W4HEIC-003: converts a .heif file too', async () => {
|
||||
const out = await normalizeImageFile(new File(['x'], 'scan.heif'))
|
||||
expect(out.name).toBe('scan.jpg')
|
||||
})
|
||||
|
||||
it('FE-W4HEIC-004: detects heic by mime type when the name has no extension', async () => {
|
||||
const f = new File(['x'], 'clipboard', { type: 'image/heic' })
|
||||
const out = await normalizeImageFile(f)
|
||||
|
||||
expect(heicTo).toHaveBeenCalled()
|
||||
// No .heic/.heif suffix to strip, so the name stays as-is.
|
||||
expect(out.name).toBe('clipboard')
|
||||
expect(out.type).toBe('image/jpeg')
|
||||
})
|
||||
|
||||
it('FE-W4HEIC-005: returns the original when the magic bytes say it is not heic', async () => {
|
||||
isHeic.mockResolvedValue(false)
|
||||
const f = new File(['x'], 'mislabelled.heic')
|
||||
|
||||
await expect(normalizeImageFile(f)).resolves.toBe(f)
|
||||
expect(heicTo).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('normalizeImageFiles', () => {
|
||||
it('FE-W4HEIC-006: normalizes a mixed list and keeps the order', async () => {
|
||||
const jpg = new File(['x'], 'a.jpg', { type: 'image/jpeg' })
|
||||
const heic = new File(['x'], 'b.heic')
|
||||
|
||||
const out = await normalizeImageFiles([jpg, heic])
|
||||
|
||||
expect(out).toHaveLength(2)
|
||||
expect(out[0]).toBe(jpg)
|
||||
expect(out[1].name).toBe('b.jpg')
|
||||
})
|
||||
|
||||
it('FE-W4HEIC-007: accepts a FileList-like object', async () => {
|
||||
const jpg = new File(['x'], 'a.jpg', { type: 'image/jpeg' })
|
||||
const fakeList = { 0: jpg, length: 1 } as unknown as FileList
|
||||
|
||||
await expect(normalizeImageFiles(fakeList)).resolves.toEqual([jpg])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,88 @@
|
||||
// FE-W4LOC-001 to FE-W4LOC-014
|
||||
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
||||
import { formatLocationName, formatDate, formatTime } from './formatters'
|
||||
|
||||
describe('formatLocationName', () => {
|
||||
it('FE-W4LOC-001: returns an empty string for a missing name', () => {
|
||||
expect(formatLocationName(null)).toBe('')
|
||||
expect(formatLocationName(undefined)).toBe('')
|
||||
expect(formatLocationName('')).toBe('')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-002: passes a short name through untouched', () => {
|
||||
expect(formatLocationName(' Blue Lagoon, Grindavík, Iceland ')).toBe('Blue Lagoon, Grindavík, Iceland')
|
||||
expect(formatLocationName('Reykjavik')).toBe('Reykjavik')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-003: collapses a verbose Nominatim string to name, postcode, country', () => {
|
||||
const raw = 'Hallgrimskirkja, 1, Skolavorduholt, Midborg, Reykjavik, Capital Region, Iceland, 101'
|
||||
expect(formatLocationName(raw)).toBe('Hallgrimskirkja, 101, Iceland')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-004: drops the postcode when the tail is not one', () => {
|
||||
const raw = 'Blue Lagoon, Norðurljósavegur, Grindavík, Southern Peninsula, Iceland'
|
||||
expect(formatLocationName(raw)).toBe('Blue Lagoon, Iceland')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-005: deduplicates repeated parts before shortening', () => {
|
||||
expect(formatLocationName('Paris, Paris, Paris, France')).toBe('Paris, France')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-006: keeps a deduplicated list of three or fewer parts intact', () => {
|
||||
expect(formatLocationName('A, A, B, C')).toBe('A, B, C')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-007: never repeats the name as country or postcode', () => {
|
||||
expect(formatLocationName('101, Skolavorduholt, Midborg, Reykjavik, 101')).toBe('101, Reykjavik')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-008: rejects an over-long tail as a postcode', () => {
|
||||
const raw = 'Museum, Street, District, City, Region, Country, 1234567890AB'
|
||||
expect(formatLocationName(raw)).toBe('Museum, 1234567890AB')
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatDate', () => {
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(new Date('2026-06-15T12:00:00Z'))
|
||||
})
|
||||
afterAll(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('FE-W4LOC-009: omits the year for a date in the current year', () => {
|
||||
const out = formatDate('2026-08-01', 'en-US')
|
||||
|
||||
expect(out).toContain('Aug')
|
||||
expect(out).not.toContain('2026')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-010: adds the year for another year and returns null without a date', () => {
|
||||
expect(formatDate('2025-08-01', 'en-US')).toContain('2025')
|
||||
expect(formatDate(null, 'en-US')).toBeNull()
|
||||
expect(formatDate('', 'en-US')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatTime', () => {
|
||||
it('FE-W4LOC-011: renders a padded 24h time', () => {
|
||||
expect(formatTime('9:05', 'en-US', '24h')).toBe('09:05')
|
||||
expect(formatTime('', 'en-US', '24h')).toBe('')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-012: appends Uhr for German locales', () => {
|
||||
expect(formatTime('18:30', 'de-DE', '24h')).toBe('18:30 Uhr')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-013: renders a 12h time with the right period', () => {
|
||||
expect(formatTime('00:15', 'en-US', '12h')).toBe('12:15 AM')
|
||||
expect(formatTime('12:00', 'en-US', '12h')).toBe('12:00 PM')
|
||||
expect(formatTime('13:05', 'en-US', '12h')).toBe('1:05 PM')
|
||||
expect(formatTime('09:00', 'en-US', '12h')).toBe('9:00 AM')
|
||||
})
|
||||
|
||||
it('FE-W4LOC-014: treats unparseable parts as zero', () => {
|
||||
expect(formatTime('abc', 'en-US', '24h')).toBe('00:00')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
// FE-W4UTL-010 to FE-W4UTL-014
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { GRADIENTS, entityGradient } from './gradients'
|
||||
|
||||
describe('entityGradient', () => {
|
||||
it('FE-W4UTL-010: exposes eight two-stop gradients', () => {
|
||||
expect(GRADIENTS).toHaveLength(8)
|
||||
GRADIENTS.forEach(g => expect(g).toMatch(/^linear-gradient\(135deg, #[0-9a-f]{6} 0%, #[0-9a-f]{6} 100%\)$/))
|
||||
})
|
||||
|
||||
it('FE-W4UTL-011: maps an id onto its slot', () => {
|
||||
expect(entityGradient(0)).toBe(GRADIENTS[0])
|
||||
expect(entityGradient(3)).toBe(GRADIENTS[3])
|
||||
expect(entityGradient(7)).toBe(GRADIENTS[7])
|
||||
})
|
||||
|
||||
it('FE-W4UTL-012: wraps around past the palette length', () => {
|
||||
expect(entityGradient(8)).toBe(GRADIENTS[0])
|
||||
expect(entityGradient(11)).toBe(GRADIENTS[3])
|
||||
})
|
||||
|
||||
it('FE-W4UTL-013: handles negative ids without going out of range', () => {
|
||||
expect(entityGradient(-1)).toBe(GRADIENTS[7])
|
||||
expect(entityGradient(-8)).toBe(GRADIENTS[0])
|
||||
expect(entityGradient(-11)).toBe(GRADIENTS[5])
|
||||
})
|
||||
|
||||
it('FE-W4UTL-014: is stable for the same id', () => {
|
||||
expect(entityGradient(42)).toBe(entityGradient(42))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,61 @@
|
||||
// FE-W4UTL-020 to FE-W4UTL-029
|
||||
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
||||
import { computeJourneyLifecycle } from './journeyLifecycle'
|
||||
|
||||
const TODAY = '2026-06-15'
|
||||
|
||||
beforeAll(() => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(new Date(`${TODAY}T12:00:00Z`))
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('computeJourneyLifecycle', () => {
|
||||
it('FE-W4UTL-020: archived wins over any date range', () => {
|
||||
expect(computeJourneyLifecycle('archived', '2026-06-01', '2026-06-30')).toBe('archived')
|
||||
expect(computeJourneyLifecycle('archived', null, null)).toBe('archived')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-021: a range containing today is live', () => {
|
||||
expect(computeJourneyLifecycle('published', '2026-06-01', '2026-06-30')).toBe('live')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-022: today on either boundary is still live', () => {
|
||||
expect(computeJourneyLifecycle('published', TODAY, '2026-06-30')).toBe('live')
|
||||
expect(computeJourneyLifecycle('published', '2026-06-01', TODAY)).toBe('live')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-023: a future range is upcoming', () => {
|
||||
expect(computeJourneyLifecycle('published', '2026-07-01', '2026-07-10')).toBe('upcoming')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-024: a past range is completed', () => {
|
||||
expect(computeJourneyLifecycle('published', '2026-05-01', '2026-05-10')).toBe('completed')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-025: no dates at all is a draft', () => {
|
||||
expect(computeJourneyLifecycle('published', null, null)).toBe('draft')
|
||||
expect(computeJourneyLifecycle('published', undefined, undefined)).toBe('draft')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-026: start only, in the future, is upcoming', () => {
|
||||
expect(computeJourneyLifecycle('published', '2026-07-01', null)).toBe('upcoming')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-027: start only, already reached, is live', () => {
|
||||
expect(computeJourneyLifecycle('published', '2026-06-01', null)).toBe('live')
|
||||
expect(computeJourneyLifecycle('published', TODAY, null)).toBe('live')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-028: end only, in the past, is completed', () => {
|
||||
expect(computeJourneyLifecycle('published', null, '2026-05-10')).toBe('completed')
|
||||
})
|
||||
|
||||
it('FE-W4UTL-029: end only, not yet reached, is live', () => {
|
||||
expect(computeJourneyLifecycle('published', null, '2026-06-30')).toBe('live')
|
||||
expect(computeJourneyLifecycle('published', null, TODAY)).toBe('live')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,142 @@
|
||||
// FE-W4UPQ-001 to FE-W4UPQ-010
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { uploadFilesResilient, type UploadOpts, type UploadProgress } from './uploadQueue'
|
||||
|
||||
function file(name: string, size = 100): File {
|
||||
const f = new File(['x'], name, { type: 'text/plain' })
|
||||
Object.defineProperty(f, 'size', { value: size })
|
||||
return f
|
||||
}
|
||||
|
||||
describe('uploadFilesResilient', () => {
|
||||
it('FE-W4UPQ-001: returns every uploaded item and no failures', async () => {
|
||||
const files = [file('a.txt'), file('b.txt')]
|
||||
const uploadOne = vi.fn(async (f: File) => [{ name: f.name }])
|
||||
|
||||
const res = await uploadFilesResilient(files, uploadOne)
|
||||
|
||||
expect(res.failed).toEqual([])
|
||||
expect(res.succeeded).toEqual([{ name: 'a.txt' }, { name: 'b.txt' }])
|
||||
expect(uploadOne).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-002: passes a fresh idempotency key per file', async () => {
|
||||
const keys: string[] = []
|
||||
const uploadOne = async (_f: File, opts: UploadOpts) => { keys.push(opts.idempotencyKey); return [] }
|
||||
|
||||
await uploadFilesResilient([file('a.txt'), file('b.txt')], uploadOne)
|
||||
|
||||
expect(keys).toHaveLength(2)
|
||||
expect(keys[0]).not.toBe(keys[1])
|
||||
expect(keys[0]).toMatch(/^[0-9a-f-]{36}$/)
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-003: streams onUploaded per file as it lands', async () => {
|
||||
const onUploaded = vi.fn()
|
||||
await uploadFilesResilient(
|
||||
[file('a.txt'), file('b.txt')],
|
||||
async (f: File) => [f.name],
|
||||
{ concurrency: 1, onUploaded },
|
||||
)
|
||||
|
||||
expect(onUploaded).toHaveBeenNthCalledWith(1, ['a.txt'])
|
||||
expect(onUploaded).toHaveBeenNthCalledWith(2, ['b.txt'])
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-004: reports byte progress from onUploadProgress and finishes at 100%', async () => {
|
||||
const seen: UploadProgress[] = []
|
||||
const uploadOne = async (f: File, opts: UploadOpts) => {
|
||||
opts.onUploadProgress({ loaded: f.size / 2, total: f.size } as never)
|
||||
return [f.name]
|
||||
}
|
||||
|
||||
await uploadFilesResilient([file('a.txt', 200)], uploadOne, { onProgress: p => { seen.push(p) } })
|
||||
|
||||
expect(seen[0]).toEqual({ done: 0, total: 1, failed: 0, percent: 50 })
|
||||
expect(seen[seen.length - 1]).toEqual({ done: 1, total: 1, failed: 0, percent: 100 })
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-005: reports 0% when the files carry no bytes', async () => {
|
||||
const seen: UploadProgress[] = []
|
||||
await uploadFilesResilient([file('a.txt', 0)], async () => [], { onProgress: p => { seen.push(p) } })
|
||||
|
||||
expect(seen[seen.length - 1]).toEqual({ done: 1, total: 1, failed: 0, percent: 0 })
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-006: retries a transient failure and keeps the file', async () => {
|
||||
let calls = 0
|
||||
const uploadOne = vi.fn(async (f: File) => {
|
||||
calls++
|
||||
if (calls === 1) throw new Error('network down')
|
||||
return [f.name]
|
||||
})
|
||||
|
||||
const res = await uploadFilesResilient([file('a.txt')], uploadOne, { retries: 1 })
|
||||
|
||||
expect(calls).toBe(2)
|
||||
expect(res.succeeded).toEqual(['a.txt'])
|
||||
expect(res.failed).toEqual([])
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-007: does not retry a 4xx and reports the file as failed', async () => {
|
||||
const uploadOne = vi.fn(async () => { throw { response: { status: 413 } } })
|
||||
const f = file('big.txt')
|
||||
|
||||
const res = await uploadFilesResilient([f], uploadOne, { retries: 2 })
|
||||
|
||||
expect(uploadOne).toHaveBeenCalledTimes(1)
|
||||
expect(res.succeeded).toEqual([])
|
||||
expect(res.failed).toEqual([f])
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-008: retries a 5xx up to the limit then gives up', async () => {
|
||||
const uploadOne = vi.fn(async () => { throw { response: { status: 502 } } })
|
||||
const f = file('a.txt')
|
||||
|
||||
const res = await uploadFilesResilient([f], uploadOne, { retries: 1 })
|
||||
|
||||
expect(uploadOne).toHaveBeenCalledTimes(2)
|
||||
expect(res.failed).toEqual([f])
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-009: a failed file counts into the progress failure tally', async () => {
|
||||
const seen: UploadProgress[] = []
|
||||
const ok = file('ok.txt')
|
||||
const bad = file('bad.txt')
|
||||
const uploadOne = async (f: File) => {
|
||||
if (f.name === 'bad.txt') throw { response: { status: 400 } }
|
||||
return [f.name]
|
||||
}
|
||||
|
||||
const res = await uploadFilesResilient([ok, bad], uploadOne, { concurrency: 1, onProgress: p => { seen.push(p) } })
|
||||
|
||||
expect(res.succeeded).toEqual(['ok.txt'])
|
||||
expect(res.failed).toEqual([bad])
|
||||
expect(seen[seen.length - 1]).toMatchObject({ done: 1, failed: 1, total: 2 })
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-010: never spawns more workers than files', async () => {
|
||||
let inFlight = 0
|
||||
let peak = 0
|
||||
const uploadOne = async (f: File) => {
|
||||
inFlight++
|
||||
peak = Math.max(peak, inFlight)
|
||||
await Promise.resolve()
|
||||
inFlight--
|
||||
return [f.name]
|
||||
}
|
||||
|
||||
const res = await uploadFilesResilient([file('a.txt'), file('b.txt')], uploadOne, { concurrency: 10 })
|
||||
|
||||
expect(peak).toBeLessThanOrEqual(2)
|
||||
expect(res.succeeded).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('FE-W4UPQ-011: an empty file list resolves without calling the uploader', async () => {
|
||||
const uploadOne = vi.fn(async () => [])
|
||||
const res = await uploadFilesResilient([], uploadOne)
|
||||
|
||||
expect(uploadOne).not.toHaveBeenCalled()
|
||||
expect(res).toEqual({ succeeded: [], failed: [] })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user