🐛 Fix calculation of expected hours

This commit is contained in:
2026-12-02 19:47:35 +01:00
parent 2c2c7a97c6
commit 6b20cd9591
7 changed files with 110 additions and 19 deletions
+20 -1
View File
@@ -10,7 +10,7 @@ import {
type Entry,
type Settings
} from '$lib/types';
import { dayKeyForMs, endOfDayMs } from '$lib/utils';
import { dayKeyForMs, endOfDayMs, isWorkday, weekDayKeys } from '$lib/utils';
const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
const LAST_DAY = '9999-12-31';
@@ -44,6 +44,25 @@ export function currentSettings(dayKey: string = todayKey()): Doc<Settings> {
return settingsAt(dayKey);
}
export function dayExpectedMs(dayKey: string): number {
const s = currentSettings(dayKey);
if (!isWorkday(dayKey, s.workdays_mask)) return 0;
// Truncated to whole milliseconds like the server does, so that days add up
// to exactly the same week total on both sides.
return Math.trunc((s.hours_per_week * 3_600_000) / workdayCount(s.workdays_mask));
}
/** Expected time of the week: the sum over its days, each with its own settings. */
export function weekExpectedMs(weekKey: string): number {
return weekDayKeys(weekKey).reduce((sum, dayKey) => sum + dayExpectedMs(dayKey), 0);
}
function workdayCount(mask: number): number {
let count = 0;
for (let m = mask; m; m >>= 1) count += m & 1;
return count;
}
export function settingsHistory(): Doc<Settings>[] {
return all('settings_history').sort((a, b) => {
if (a.effective_from !== b.effective_from) return a.effective_from < b.effective_from ? 1 : -1;
+18
View File
@@ -7,10 +7,12 @@ import {
currentSettings,
createInterval,
deleteSettings,
dayExpectedMs,
entriesForDay,
runningEntry,
settingsHistory,
startEntry,
weekExpectedMs,
stopEntry,
todayKey,
updateEntry
@@ -91,6 +93,22 @@ describe('settings', () => {
expect(settingsHistory().map((s) => s.effective_from)).toEqual(['2026-07-01', '2026-06-01', '2026-01-01']);
});
it('expected hours follow the settings effective on each day of the week', async () => {
await addSettings(row('2000-01-01', 32));
await addSettings(row('2026-11-30', 32));
await addSettings(row('2026-12-01', 36));
const h = 3_600_000;
// Monday 30.11. counts 32h / 5, Tuesday to Friday 36h / 5.
expect(weekExpectedMs('2026-W49')).toBe(6.4 * h + 4 * 7.2 * h);
expect(dayExpectedMs('2026-11-30')).toBe(6.4 * h);
expect(dayExpectedMs('2026-12-01')).toBe(7.2 * h);
expect(weekExpectedMs('2026-W48')).toBe(32 * h);
expect(weekExpectedMs('2026-W50')).toBe(36 * h);
// Weekends never count.
expect(dayExpectedMs('2026-12-05')).toBe(0);
expect(weekExpectedMs('2026-W53')).toBe(36 * h);
});
it('addSettings rejects rows outside the bounds of the server', async () => {
for (const hours of [0, 0.009, 168.01, NaN]) {
await expect(addSettings(row('2026-01-01', hours))).rejects.toThrow('hours_per_week must be between 0.01 and 168');
+6 -11
View File
@@ -5,9 +5,11 @@
closedWeek as findClosedWeek,
closeWeek,
currentSettings as getSettings,
dayExpectedMs,
entriesForRange,
reopenWeek,
todayKey
todayKey,
weekExpectedMs
} from '$lib/app.svelte';
import type { Doc } from '$lib/db.svelte';
import type { ClosedDay } from '$lib/types';
@@ -113,17 +115,10 @@
.reduce((sum, e) => sum + (e.end_time! - e.start_time), 0);
}
function dailyExpectedMs(dk: string): number {
const mask = currentSettings.workdays_mask;
if (!isWorkday(dk, mask)) return 0;
const workdayCount = [1, 2, 4, 8, 16, 32, 64].filter((b) => mask & b).length;
return (currentSettings.hours_per_week * 3_600_000) / workdayCount;
}
const totalWorkedMs = $derived(
dayKeys.reduce((sum, dk) => sum + dayWorkedMs(dk), 0)
);
const expectedMs = $derived(currentSettings.hours_per_week * 3_600_000);
const expectedMs = $derived(weekExpectedMs(weekKey));
const daysWithEntries = $derived(new Set(weekEntries.map((e) => e.day_key)));
@@ -166,12 +161,12 @@
dayKey={dk}
weekdayLabel={DAY_NAMES[i]}
workedMs={dayWorkedMs(dk)}
expectedMs={dailyExpectedMs(dk)}
expectedMs={dayExpectedMs(dk)}
kind={closedDaysMap[dk]?.kind ?? null}
closed={!!closedDaysMap[dk]}
isToday={dk === todayKey()}
selected={dk === selectedDay}
isWorkday={isWorkday(dk, currentSettings.workdays_mask)}
isWorkday={isWorkday(dk, getSettings(dk).workdays_mask)}
onclick={() => ondaychange(dk)}
/>
</div>