mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
@@ -0,0 +1,63 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { cleanAmount, cleanAmountText } from '../../../src/utils/formatters';
|
||||
|
||||
/**
|
||||
* Money that has been through an uneven split (#1964).
|
||||
*
|
||||
* A total that does not divide evenly is split into parts that are each exact
|
||||
* to the cent, but adding those parts back in floating point does not return
|
||||
* the total: 81.61 + 81.60 is 163.20999999999998. The server writes the clean
|
||||
* value now, so these two are for what is already stored, and for the places
|
||||
* that print a number as it stands rather than through Intl — the expense form
|
||||
* when reopened, and the price stamped onto a linked booking.
|
||||
*/
|
||||
|
||||
describe('cleanAmount', () => {
|
||||
it('gives back the total the parts were meant to add up to', () => {
|
||||
expect(cleanAmount(81.61 + 81.60)).toBe(163.21);
|
||||
expect(cleanAmount(33.34 + 33.33 + 33.33)).toBe(100);
|
||||
});
|
||||
|
||||
it('leaves a value that was already clean untouched', () => {
|
||||
expect(cleanAmount(163.21)).toBe(163.21);
|
||||
expect(cleanAmount(25)).toBe(25);
|
||||
expect(cleanAmount(0)).toBe(0);
|
||||
});
|
||||
|
||||
it('keeps a genuine half-cent from being invented or lost', () => {
|
||||
// Rounding to cents is the point; a third of a cent is not money.
|
||||
expect(cleanAmount(0.005)).toBe(0.01);
|
||||
expect(cleanAmount(0.004)).toBe(0);
|
||||
});
|
||||
|
||||
it('handles a negative the same way, since a refund is money too', () => {
|
||||
expect(cleanAmount(-81.61 - 81.60)).toBe(-163.21);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cleanAmountText', () => {
|
||||
it('tidies a numeric price string', () => {
|
||||
expect(cleanAmountText('163.20999999999998')).toBe('163.21');
|
||||
expect(cleanAmountText(163.20999999999998)).toBe('163.21');
|
||||
});
|
||||
|
||||
/*
|
||||
* The booking price field holds whatever the confirmation said. Import writes
|
||||
* free text into it, so anything that is not a number has to come back out
|
||||
* exactly as it went in rather than as NaN.
|
||||
*/
|
||||
it('passes free text through untouched', () => {
|
||||
expect(cleanAmountText('included in fare')).toBe('included in fare');
|
||||
expect(cleanAmountText('EUR 40,00')).toBe('EUR 40,00');
|
||||
});
|
||||
|
||||
it('answers empty for nothing at all', () => {
|
||||
expect(cleanAmountText(null)).toBe('');
|
||||
expect(cleanAmountText(undefined)).toBe('');
|
||||
expect(cleanAmountText('')).toBe('');
|
||||
});
|
||||
|
||||
it('does not turn whitespace into a zero', () => {
|
||||
expect(cleanAmountText(' ')).toBe(' ');
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { formatDate, formatTime, dayTotalCost, currencyDecimals } from '../../../src/utils/formatters';
|
||||
import { formatDate, formatTime, formatClockTime, dayTotalCost, currencyDecimals, parseMeridiemTime } from '../../../src/utils/formatters';
|
||||
import type { AssignmentsMap } from '../../../src/types';
|
||||
|
||||
// dayTotalCost intentionally exercises edge-case price inputs (string / non-numeric),
|
||||
@@ -60,6 +60,68 @@ describe('formatTime', () => {
|
||||
expect(formatTime('12:00', 'en-US', '12h')).toBe('12:00 PM');
|
||||
expect(formatTime('01:00', 'en-US', '12h')).toBe('1:00 AM');
|
||||
});
|
||||
|
||||
// #1725 — a value stored with a meridiem must still follow the configured format
|
||||
it('converts a stored meridiem value to the configured format', () => {
|
||||
expect(formatTime('3:00 PM', 'en-US', '24h')).toBe('15:00');
|
||||
expect(formatTime('12:30 am', 'en-US', '24h')).toBe('00:30');
|
||||
expect(formatTime('3:00 PM', 'en-US', '12h')).toBe('3:00 PM');
|
||||
expect(formatTime('15:00', 'de-DE', '24h')).toBe('15:00 Uhr');
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseMeridiemTime', () => {
|
||||
it('returns null when there is no meridiem to convert', () => {
|
||||
expect(parseMeridiemTime(null)).toBeNull();
|
||||
expect(parseMeridiemTime(undefined)).toBeNull();
|
||||
expect(parseMeridiemTime('')).toBeNull();
|
||||
expect(parseMeridiemTime('14:30')).toBeNull();
|
||||
expect(parseMeridiemTime('later')).toBeNull();
|
||||
});
|
||||
|
||||
it('converts a meridiem time to HH:MM', () => {
|
||||
expect(parseMeridiemTime('5:30 pm')).toBe('17:30');
|
||||
expect(parseMeridiemTime('5:30 AM')).toBe('05:30');
|
||||
expect(parseMeridiemTime('530pm')).toBe('17:30');
|
||||
expect(parseMeridiemTime('5pm')).toBe('17:00');
|
||||
});
|
||||
|
||||
it('maps 12 AM to midnight and 12 PM to noon', () => {
|
||||
expect(parseMeridiemTime('12:30 AM')).toBe('00:30');
|
||||
expect(parseMeridiemTime('12:30 PM')).toBe('12:30');
|
||||
});
|
||||
});
|
||||
|
||||
// The locale-free counterpart of formatTime, shared by the time picker and the dense
|
||||
// day/booking rows so those two do not carry their own copy of the rules (#1725).
|
||||
describe('formatClockTime', () => {
|
||||
it('returns an empty string for a missing value', () => {
|
||||
expect(formatClockTime(null, false)).toBe('');
|
||||
expect(formatClockTime(undefined, true)).toBe('');
|
||||
expect(formatClockTime('', true)).toBe('');
|
||||
});
|
||||
|
||||
it('leaves a 24h value untouched in 24h and never adds a locale suffix', () => {
|
||||
expect(formatClockTime('14:30', false)).toBe('14:30');
|
||||
expect(formatClockTime('09:05', false)).toBe('09:05');
|
||||
});
|
||||
|
||||
it('converts a 24h value for a 12h user', () => {
|
||||
expect(formatClockTime('14:30', true)).toBe('2:30 PM');
|
||||
expect(formatClockTime('00:15', true)).toBe('12:15 AM');
|
||||
expect(formatClockTime('12:45', true)).toBe('12:45 PM');
|
||||
});
|
||||
|
||||
it('converts a stored meridiem value down to 24h', () => {
|
||||
expect(formatClockTime('3:00 PM', false)).toBe('15:00');
|
||||
expect(formatClockTime('12:30 am', false)).toBe('00:30');
|
||||
expect(formatClockTime('3:00 PM', true)).toBe('3:00 PM');
|
||||
});
|
||||
|
||||
it('hands back anything it cannot read', () => {
|
||||
expect(formatClockTime('later', false)).toBe('later');
|
||||
expect(formatClockTime('later', true)).toBe('later');
|
||||
});
|
||||
});
|
||||
|
||||
describe('dayTotalCost', () => {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// FE-UTIL-START-001 to FE-UTIL-START-009
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
||||
import {
|
||||
readStartDestination,
|
||||
rememberStartDestination,
|
||||
forgetStartDestination,
|
||||
tripStartPath,
|
||||
DEFAULT_START_PAGE,
|
||||
DEFAULT_START_TRIP_TAB,
|
||||
} from '../../../src/utils/startDestination'
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('readStartDestination', () => {
|
||||
// Not the dashboard: the preference lives on the server, so a device that has
|
||||
// never seen it has to say "I don't know" and let the caller wait for the
|
||||
// settings. Answering 'dashboard' here ignored the setting on every device
|
||||
// that hadn't set it — including any browser that had logged out since.
|
||||
it('FE-UTIL-START-001: reports "unknown" when nothing was ever mirrored', () => {
|
||||
expect(readStartDestination()).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-UTIL-START-002: returns what was mirrored', () => {
|
||||
rememberStartDestination({ start_page: 'active_trip', start_trip_tab: 'finanzplan' })
|
||||
expect(readStartDestination()).toEqual({ page: 'active_trip', tab: 'finanzplan' })
|
||||
})
|
||||
|
||||
it('FE-UTIL-START-002b: an explicitly mirrored dashboard is an answer, not "unknown"', () => {
|
||||
rememberStartDestination({ start_page: 'dashboard' })
|
||||
expect(readStartDestination()).toEqual({ page: DEFAULT_START_PAGE, tab: DEFAULT_START_TRIP_TAB })
|
||||
})
|
||||
|
||||
// A value a user (or an older/newer build) could have written by hand must not
|
||||
// become a route we then navigate to.
|
||||
it('FE-UTIL-START-003: ignores values that are not a known page or tab', () => {
|
||||
localStorage.setItem('trek_start_page', 'somewhere_else')
|
||||
expect(readStartDestination()).toBeNull()
|
||||
|
||||
localStorage.setItem('trek_start_page', 'active_trip')
|
||||
localStorage.setItem('trek_start_trip_tab', '../../etc/passwd')
|
||||
expect(readStartDestination()).toEqual({ page: 'active_trip', tab: DEFAULT_START_TRIP_TAB })
|
||||
})
|
||||
|
||||
it('FE-UTIL-START-004: survives a browser that refuses storage access', () => {
|
||||
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
||||
throw new Error('SecurityError')
|
||||
})
|
||||
expect(readStartDestination()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('rememberStartDestination', () => {
|
||||
it('FE-UTIL-START-005: only touches the keys present in the patch', () => {
|
||||
rememberStartDestination({ start_page: 'active_trip', start_trip_tab: 'dateien' })
|
||||
rememberStartDestination({ start_trip_tab: 'buchungen' })
|
||||
expect(readStartDestination()).toEqual({ page: 'active_trip', tab: 'buchungen' })
|
||||
})
|
||||
|
||||
it('FE-UTIL-START-006: clears a key that comes back invalid instead of keeping a stale one', () => {
|
||||
rememberStartDestination({ start_page: 'active_trip' })
|
||||
rememberStartDestination({ start_page: undefined })
|
||||
expect(localStorage.getItem('trek_start_page')).toBeNull()
|
||||
})
|
||||
|
||||
it('FE-UTIL-START-007: swallows a quota error rather than failing the settings save', () => {
|
||||
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
||||
throw new Error('QuotaExceededError')
|
||||
})
|
||||
expect(() => rememberStartDestination({ start_page: 'active_trip' })).not.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('forgetStartDestination', () => {
|
||||
it('FE-UTIL-START-008: leaves the next account on this browser knowing nothing', () => {
|
||||
rememberStartDestination({ start_page: 'active_trip', start_trip_tab: 'finanzplan' })
|
||||
forgetStartDestination()
|
||||
// "unknown", so the next login waits for its own settings instead of
|
||||
// inheriting the previous account's preference.
|
||||
expect(readStartDestination()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('tripStartPath', () => {
|
||||
it('FE-UTIL-START-009: always spells the tab out, and never a tab that does not exist', () => {
|
||||
// Explicit even for 'plan', or the last session's tab would decide instead.
|
||||
expect(tripStartPath(7, 'plan')).toBe('/trips/7?tab=plan')
|
||||
expect(tripStartPath(7, 'finanzplan')).toBe('/trips/7?tab=finanzplan')
|
||||
expect(tripStartPath(7, 'nonsense')).toBe('/trips/7?tab=plan')
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,11 @@
|
||||
/**
|
||||
* videoPoster unit tests (#823). The poster-capture path needs a real <video>
|
||||
* decoder + canvas, which jsdom does not provide, so we cover the pure file-type
|
||||
* gate here; poster capture is exercised manually / in the browser.
|
||||
* videoPoster unit tests (#823). jsdom has no video decoder and no canvas
|
||||
* rasteriser, so the capture path is driven by hand: document.createElement is
|
||||
* spied on to hand back the <video>/<canvas> the helper creates, and the media
|
||||
* events it waits for are fired from the test.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { isVideoFile } from '../../../src/utils/videoPoster'
|
||||
import { afterEach, beforeEach, describe, it, expect, vi } from 'vitest'
|
||||
import { captureVideoPoster, isVideoFile } from '../../../src/utils/videoPoster'
|
||||
|
||||
describe('isVideoFile', () => {
|
||||
it('is true for a video MIME type', () => {
|
||||
@@ -18,3 +19,182 @@ describe('isVideoFile', () => {
|
||||
expect(isVideoFile(new File([], 'noext', { type: '' }))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
interface CanvasStub {
|
||||
width: number
|
||||
height: number
|
||||
getContext: ReturnType<typeof vi.fn>
|
||||
toBlob: ReturnType<typeof vi.fn>
|
||||
}
|
||||
|
||||
const CLIP = new File(['data'], 'clip.mp4', { type: 'video/mp4' })
|
||||
const POSTER = new Blob(['jpeg'], { type: 'image/jpeg' })
|
||||
|
||||
let video: HTMLVideoElement
|
||||
let canvas: CanvasStub
|
||||
let drawImage: ReturnType<typeof vi.fn>
|
||||
let createObjectURL: ReturnType<typeof vi.fn>
|
||||
let revokeObjectURL: ReturnType<typeof vi.fn>
|
||||
|
||||
/** Fakes the read-only metadata jsdom leaves at 0/NaN. */
|
||||
function describeMedia(duration: number, width = 1280, height = 720): void {
|
||||
Object.defineProperty(video, 'duration', { value: duration, configurable: true })
|
||||
Object.defineProperty(video, 'videoWidth', { value: width, configurable: true })
|
||||
Object.defineProperty(video, 'videoHeight', { value: height, configurable: true })
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
|
||||
drawImage = vi.fn()
|
||||
canvas = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
getContext: vi.fn(() => ({ drawImage })),
|
||||
toBlob: vi.fn((cb: BlobCallback) => cb(POSTER)),
|
||||
}
|
||||
|
||||
const realCreate = document.createElement.bind(document)
|
||||
vi.spyOn(document, 'createElement').mockImplementation((tag: string) => {
|
||||
if (tag === 'canvas') return canvas as unknown as HTMLCanvasElement
|
||||
const el = realCreate(tag)
|
||||
if (tag === 'video') video = el as HTMLVideoElement
|
||||
return el
|
||||
})
|
||||
|
||||
createObjectURL = vi.fn(() => 'blob:clip')
|
||||
revokeObjectURL = vi.fn()
|
||||
vi.stubGlobal('URL', Object.assign(Object.create(URL), { createObjectURL, revokeObjectURL }))
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe('captureVideoPoster', () => {
|
||||
it('captures a frame just past the start and reports the duration', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(12.5)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
// 0.1s in, not the (often black) first frame.
|
||||
expect(video.currentTime).toBe(0.1)
|
||||
|
||||
video.onseeked!(new Event('seeked'))
|
||||
await expect(pending).resolves.toEqual({ poster: POSTER, durationMs: 12500 })
|
||||
|
||||
expect(canvas.width).toBe(1280)
|
||||
expect(canvas.height).toBe(720)
|
||||
expect(drawImage).toHaveBeenCalledWith(video, 0, 0, 1280, 720)
|
||||
expect(canvas.toBlob).toHaveBeenCalledWith(expect.any(Function), 'image/jpeg', 0.8)
|
||||
expect(revokeObjectURL).toHaveBeenCalledWith('blob:clip')
|
||||
})
|
||||
|
||||
it('seeks to the midpoint of a clip shorter than 0.2s', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(0.08)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
expect(video.currentTime).toBe(0.04)
|
||||
|
||||
video.onseeked!(new Event('seeked'))
|
||||
await expect(pending).resolves.toEqual({ poster: POSTER, durationMs: 80 })
|
||||
})
|
||||
|
||||
it('falls back to 640x360 when the stream reports no dimensions', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(4, 0, 0)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
await pending
|
||||
|
||||
expect(canvas.width).toBe(640)
|
||||
expect(canvas.height).toBe(360)
|
||||
})
|
||||
|
||||
it('reports a null duration for a stream of unknown length', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(Infinity)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
await expect(pending).resolves.toEqual({ poster: POSTER, durationMs: null })
|
||||
})
|
||||
|
||||
it('keeps the duration when no 2d context is available', async () => {
|
||||
canvas.getContext = vi.fn(() => null)
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(3)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: 3000 })
|
||||
})
|
||||
|
||||
it('keeps the duration when drawing the frame throws', async () => {
|
||||
drawImage.mockImplementation(() => { throw new Error('tainted') })
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(3)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: 3000 })
|
||||
})
|
||||
|
||||
it('passes a null poster through when toBlob produces nothing', async () => {
|
||||
canvas.toBlob = vi.fn((cb: BlobCallback) => cb(null))
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(3)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: 3000 })
|
||||
})
|
||||
|
||||
it('resolves empty when the browser cannot decode the file', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
video.onerror!(new Event('error'))
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: null })
|
||||
expect(revokeObjectURL).toHaveBeenCalledWith('blob:clip')
|
||||
})
|
||||
|
||||
it('gives up after ten seconds instead of hanging', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
await vi.advanceTimersByTimeAsync(10_000)
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: null })
|
||||
})
|
||||
|
||||
it('resolves once even if several outcomes race', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(6)
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
video.onseeked!(new Event('seeked'))
|
||||
video.onerror!(new Event('error'))
|
||||
|
||||
await expect(pending).resolves.toEqual({ poster: POSTER, durationMs: 6000 })
|
||||
expect(revokeObjectURL).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('still reports the duration when seeking is rejected', async () => {
|
||||
const pending = captureVideoPoster(CLIP)
|
||||
|
||||
describeMedia(9)
|
||||
Object.defineProperty(video, 'currentTime', {
|
||||
configurable: true,
|
||||
get: () => 0,
|
||||
set: () => { throw new Error('seek not supported') },
|
||||
})
|
||||
video.onloadedmetadata!(new Event('loadedmetadata'))
|
||||
|
||||
await expect(pending).resolves.toEqual({ poster: null, durationMs: 9000 })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user