diff --git a/web-admin/src/utils/oa-trip-days.test.ts b/web-admin/src/utils/oa-trip-days.test.ts new file mode 100644 index 00000000..1b7ada0e --- /dev/null +++ b/web-admin/src/utils/oa-trip-days.test.ts @@ -0,0 +1,47 @@ +import { describe, it } from 'node:test' +import assert from 'node:assert/strict' +import { + calcTripLegDays, + sumTripLegDays, + isHalfDayMultiple +} from './oa-trip-days' + +describe('calcTripLegDays', () => { + it('same day morning->morning => 0.5', () => { + assert.equal(calcTripLegDays('2026-08-01', '上午', '2026-08-01', '上午'), 0.5) + }) + it('same day morning->afternoon => 1', () => { + assert.equal(calcTripLegDays('2026-08-01', '上午', '2026-08-01', '下午'), 1) + }) + it('same day afternoon->afternoon => 0.5', () => { + assert.equal(calcTripLegDays('2026-08-01', '下午', '2026-08-01', '下午'), 0.5) + }) + it('same day afternoon->morning => null', () => { + assert.equal(calcTripLegDays('2026-08-01', '下午', '2026-08-01', '上午'), null) + }) + it('D1 morning -> D2 afternoon => 2', () => { + assert.equal(calcTripLegDays('2026-08-01', '上午', '2026-08-02', '下午'), 2) + }) + it('D1 afternoon -> D2 morning => 1', () => { + assert.equal(calcTripLegDays('2026-08-01', '下午', '2026-08-02', '上午'), 1) + }) + it('invalid date => null', () => { + assert.equal(calcTripLegDays('', '上午', '2026-08-01', '下午'), null) + }) +}) + +describe('sumTripLegDays', () => { + it('sums durations', () => { + assert.equal(sumTripLegDays([{ duration: 0.5 }, { duration: 1 }, { duration: 2 }]), 3.5) + }) + it('ignores non-number', () => { + assert.equal(sumTripLegDays([{ duration: 1 }, { duration: null }]), 1) + }) +}) + +describe('isHalfDayMultiple', () => { + it('accepts 0.5 steps', () => { + assert.equal(isHalfDayMultiple(1.5), true) + assert.equal(isHalfDayMultiple(1.25), false) + }) +}) diff --git a/web-admin/src/utils/oa-trip-days.ts b/web-admin/src/utils/oa-trip-days.ts new file mode 100644 index 00000000..a1901ed4 --- /dev/null +++ b/web-admin/src/utils/oa-trip-days.ts @@ -0,0 +1,73 @@ +const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/ + +export type TripHalf = '上午' | '下午' + +function dayIndex(dateStr: string): number | null { + const m = DATE_RE.exec(String(dateStr || '').trim()) + if (!m) return null + const y = Number(m[1]), + mo = Number(m[2]), + d = Number(m[3]) + const t = Date.UTC(y, mo - 1, d) + if (Number.isNaN(t)) return null + return t / 86400000 +} + +function startOffset(half: string): number | null { + if (half === '上午') return 0 + if (half === '下午') return 0.5 + return null +} + +function endOffset(half: string): number | null { + if (half === '上午') return 0.5 + if (half === '下午') return 1 + return null +} + +/** 非法或 endPoint <= startPoint 返回 null */ +export function calcTripLegDays( + startDate: string, + startHalf: string, + endDate: string, + endHalf: string +): number | null { + const ds = dayIndex(startDate) + const de = dayIndex(endDate) + const so = startOffset(String(startHalf || '').trim()) + const eo = endOffset(String(endHalf || '').trim()) + if (ds == null || de == null || so == null || eo == null) return null + const duration = de + eo - (ds + so) + if (!(duration > 0)) return null + return Math.round(duration * 2) / 2 +} + +export function sumTripLegDays(legs: Array<{ duration?: unknown }>): number { + let sum = 0 + for (const leg of legs || []) { + const n = Number(leg?.duration) + if (Number.isFinite(n) && n >= 0) sum += n + } + return Math.round(sum * 2) / 2 +} + +export function isHalfDayMultiple(n: unknown): boolean { + const v = Number(n) + if (!Number.isFinite(v) || v < 0) return false + return Math.abs(v * 2 - Math.round(v * 2)) < 1e-9 +} + +export function emptyTripLeg(): Record { + return { + transport: '', + tripType: '', + fromCity: '', + toCity: '', + startDate: '', + startHalf: '上午', + endDate: '', + endHalf: '下午', + duration: null, + durationManual: false + } +}