From eb139dc39c784d2d8edd03bca631ac8735c4e976 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Mon, 10 Aug 2026 20:21:28 +0800 Subject: [PATCH 01/10] feat: hide snooze option for snoozed alarms --- src/app/(alarm)/alarm.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/(alarm)/alarm.tsx b/src/app/(alarm)/alarm.tsx index 566e735..0d4a792 100644 --- a/src/app/(alarm)/alarm.tsx +++ b/src/app/(alarm)/alarm.tsx @@ -138,7 +138,7 @@ export default function AlarmDisplay() { Begin )} - {effectiveSnapshot.snooze ? ( + {effectiveSnapshot.snooze && !effectiveSnapshot.isSnoozed ? ( [ styles.button, From 5b01edae33f46fa679c49c644fd83089fc28f5ac Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Mon, 10 Aug 2026 22:51:50 +0800 Subject: [PATCH 02/10] style: show task icon on AlarmCard --- src/components/AlarmCard.tsx | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/components/AlarmCard.tsx b/src/components/AlarmCard.tsx index 4eb2d9e..4172196 100644 --- a/src/components/AlarmCard.tsx +++ b/src/components/AlarmCard.tsx @@ -11,6 +11,13 @@ import type { Alarm } from "@/data/types"; import { colors, radii, spacing, typography } from "@/theme"; import { formatTime, getDaysString } from "@/utils/time"; +const taskIcons = { + Math: "sigma", + Memory: "brain", + "Shake phone": "vibrate", + None: "alarm-clock", +} as const; + interface AlarmCardProps { alarm: Alarm; editEnabled: boolean; @@ -36,7 +43,14 @@ export function AlarmCard({ > {formatTime(alarm.hour, alarm.minute)} - {getDaysString(alarm.days)} + + + {getDaysString(alarm.days)} + {editEnabled ? ( @@ -82,6 +96,11 @@ const styles = StyleSheet.create({ flexDirection: "column", gap: spacing.xs, }, + row: { + flexDirection: "row", + alignItems: "center", + gap: 4, + }, time: { ...typography.numeric, color: colors.text, From 7b9d43a4964abd974e9a77a33000b313615ee306 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Mon, 10 Aug 2026 23:05:39 +0800 Subject: [PATCH 03/10] refactor: always show current time on alarm screen --- src/app/(alarm)/alarm.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/app/(alarm)/alarm.tsx b/src/app/(alarm)/alarm.tsx index 0d4a792..d39dd90 100644 --- a/src/app/(alarm)/alarm.tsx +++ b/src/app/(alarm)/alarm.tsx @@ -7,9 +7,9 @@ import { useAlarmDismissal } from "@/hooks/useAlarmDismissal"; import { useAlarmFiringStore } from "@/store/alarmFiringStore"; import { useAlarmStore } from "@/store/alarmStore"; import { colors, radii, spacing, typography } from "@/theme"; -import { formatTime } from "@/utils/time"; import { useLocalSearchParams, useRouter } from "expo-router"; -import { useEffect, useMemo } from "react"; +import dayjs from "dayjs"; +import { useEffect, useMemo, useState } from "react"; import { BackHandler, Pressable, StyleSheet, Text, View } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; @@ -54,6 +54,15 @@ export default function AlarmDisplay() { return () => backSub.remove(); }, []); + const [currentTime, setCurrentTime] = useState(() => dayjs().format("HH:mm")); + + useEffect(() => { + const timer = setInterval(() => { + setCurrentTime(dayjs().format("HH:mm")); + }, 1000); + return () => clearInterval(timer); + }, []); + if (!effectiveSnapshot) { return ( @@ -102,9 +111,7 @@ export default function AlarmDisplay() { return ( - - {formatTime(effectiveSnapshot.hour, effectiveSnapshot.minute)} - + {currentTime} {effectiveSnapshot.isSnoozed ? "Snoozed alarm" : "Alarm"} From 82bfbdb5d4599952d80c392aeadcc15a0dcd2858 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Mon, 10 Aug 2026 23:18:26 +0800 Subject: [PATCH 04/10] Revert "feat: hide snooze option for snoozed alarms" This reverts commit eb139dc39c784d2d8edd03bca631ac8735c4e976. --- src/app/(alarm)/alarm.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/(alarm)/alarm.tsx b/src/app/(alarm)/alarm.tsx index d39dd90..5c72852 100644 --- a/src/app/(alarm)/alarm.tsx +++ b/src/app/(alarm)/alarm.tsx @@ -145,7 +145,7 @@ export default function AlarmDisplay() { Begin )} - {effectiveSnapshot.snooze && !effectiveSnapshot.isSnoozed ? ( + {effectiveSnapshot.snooze ? ( [ styles.button, From 9a8fac620f4cc0cdc12b65e159d3a1a787456c51 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Tue, 11 Aug 2026 22:18:46 +0800 Subject: [PATCH 05/10] feat: show snooze alarm count and dismiss option on home screen --- src/app/(main)/index.tsx | 71 ++++++++++++++++++++++++++++++++++++++++ src/store/alarmStore.ts | 69 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/src/app/(main)/index.tsx b/src/app/(main)/index.tsx index 54b536b..fdff262 100644 --- a/src/app/(main)/index.tsx +++ b/src/app/(main)/index.tsx @@ -28,6 +28,8 @@ export default function Home() { const [selectedIds, setSelectedIds] = useState>(() => new Set()); const [now, setNow] = useState(() => new Date()); + const snoozedCount = useAlarmStore((s) => s.snoozedCount); + useEffect(() => { useAlarmStore.getState().loadAlarms(); }, []); @@ -42,6 +44,34 @@ export default function Home() { [alarms, now], ); + const dismissSnoozed = () => { + Alert.alert( + "Dismiss snoozed alarms?", + `${snoozedCount} snoozed alarm${snoozedCount > 1 ? "s" : ""} will be dismissed.`, + [ + { text: "Cancel", style: "cancel" }, + { + text: "Dismiss all", + style: "destructive", + onPress: () => { + void (async () => { + try { + await useAlarmStore.getState().dismissAllSnoozedAlarms(); + } catch (e) { + console.error("dismissSnoozed failed", e); + Alert.alert( + "Error", + "Could not dismiss snoozed alarms. Please try again.", + ); + } + })(); + }, + }, + ], + { cancelable: true }, + ); + }; + const toggleEnabled = (alarm: Alarm) => { if (!alarm.enabled) { const conflict = findConflictingAlarm(alarms, alarm, alarm.id); @@ -199,6 +229,24 @@ export default function Home() { {countdown} + {snoozedCount > 0 ? ( + [ + styles.snoozedBadge, + pressed && styles.snoozedBadgePressed, + ]} + accessibilityRole="button" + accessibilityLabel={`${snoozedCount} snoozed alarm${snoozedCount > 1 ? "s" : ""}`} + onPress={dismissSnoozed} + > + + + {snoozedCount} snoozed alarm{snoozedCount > 1 ? "s" : ""} + + + + ) : null} + void) | null; _initializing: boolean; init: () => void; @@ -25,6 +29,7 @@ interface AlarmStoreState { deleteAlarm: (alarm: { id: string }) => Promise; getAlarmById: (id: string) => Promise; getAllAlarms: () => Alarm[]; + dismissAllSnoozedAlarms: () => Promise; } export const useAlarmStore = create((set, get) => { @@ -36,11 +41,20 @@ export const useAlarmStore = create((set, get) => { set({ alarms: rows.map(rowToAlarm), loaded: true, loading: false }); }; + const syncSnoozedCount = () => { + const registryState = useAlarmRegistrationsStore.getState(); + const count = registryState.records.filter( + (r) => r.type === "snooze", + ).length; + set({ snoozedCount: count }); + }; + return { alarms: [], loaded: false, loading: false, initError: null, + snoozedCount: 0, _disposer: null, _initializing: false, @@ -60,10 +74,31 @@ export const useAlarmStore = create((set, get) => { try { await dbReady; await fetchAlarms(); - const subscription = SQLite.addDatabaseChangeListener((event) => { + + const registryStore = useAlarmRegistrationsStore.getState(); + if (!registryStore._disposer || !registryStore.loaded) { + registryStore.init(); + } + + syncSnoozedCount(); + + const dbSubscription = SQLite.addDatabaseChangeListener((event) => { if (event.tableName === "alarms") void fetchAlarms(); }); - set({ _disposer: () => subscription.remove() }); + + const unsubReg = useAlarmRegistrationsStore.subscribe((state) => { + const count = state.records.filter( + (r) => r.type === "snooze", + ).length; + set({ snoozedCount: count }); + }); + + set({ + _disposer: () => { + dbSubscription.remove(); + unsubReg(); + }, + }); } catch (e) { console.error("alarmStore.init failed", e); set({ @@ -112,5 +147,35 @@ export const useAlarmStore = create((set, get) => { }, getAllAlarms: () => get().alarms, + + dismissAllSnoozedAlarms: async () => { + const registryStore = useAlarmRegistrationsStore.getState(); + const snoozedIds = registryStore.records + .filter((r) => r.type === "snooze") + .map((r) => r.alarmId); + + if (snoozedIds.length === 0) return 0; + + const native = getAlarmScheduler(); + + const results = await Promise.allSettled( + snoozedIds.map(async (alarmId) => { + await native.cancel(snoozeIdentifierFor(alarmId)); + await registryStore.remove(alarmId, "snooze"); + }), + ); + + const failed = results.filter((r) => r.status === "rejected").length; + if (failed > 0) { + console.error( + `Failed to dismiss ${failed} snoozed alarm(s)`, + results + .filter((r) => r.status === "rejected") + .map((r) => (r as PromiseRejectedResult).reason), + ); + } + + return snoozedIds.length - failed; + }, }; }); From 1a691692e34163509f5b51bb55f103bf862650af Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Wed, 12 Aug 2026 18:57:14 +0800 Subject: [PATCH 06/10] fix: add accessibilityLabel to AlarmCard Pressable with task info --- src/components/AlarmCard.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/AlarmCard.tsx b/src/components/AlarmCard.tsx index 4172196..c007ae8 100644 --- a/src/components/AlarmCard.tsx +++ b/src/components/AlarmCard.tsx @@ -40,6 +40,7 @@ export function AlarmCard({ style={({ pressed }) => [styles.card, pressed && styles.cardPressed]} onPress={() => onPress(alarm)} onLongPress={() => onLongPress(alarm)} + accessibilityLabel={`${formatTime(alarm.hour, alarm.minute)} alarm, ${alarm.task} task, ${getDaysString(alarm.days)}`} > {formatTime(alarm.hour, alarm.minute)} From 3f8cdf599e95c57f90d832e9a9ed8218a1f9c6c7 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Wed, 12 Aug 2026 19:01:17 +0800 Subject: [PATCH 07/10] fix: add generation token guard to snooze dismiss and propagate partial failures --- drizzle/0004_striped_exiles.sql | 1 + drizzle/meta/0004_snapshot.json | 190 +++++++++++++++++++++++++++ drizzle/meta/_journal.json | 7 + drizzle/migrations.js | 2 + src/data/schema.ts | 1 + src/store/alarmRegistrationsStore.ts | 23 +++- src/store/alarmStore.ts | 30 +++-- 7 files changed, 237 insertions(+), 17 deletions(-) create mode 100644 drizzle/0004_striped_exiles.sql create mode 100644 drizzle/meta/0004_snapshot.json diff --git a/drizzle/0004_striped_exiles.sql b/drizzle/0004_striped_exiles.sql new file mode 100644 index 0000000..53f293a --- /dev/null +++ b/drizzle/0004_striped_exiles.sql @@ -0,0 +1 @@ +ALTER TABLE `alarm_registrations` ADD `generation` integer DEFAULT 0 NOT NULL; \ No newline at end of file diff --git a/drizzle/meta/0004_snapshot.json b/drizzle/meta/0004_snapshot.json new file mode 100644 index 0000000..202fd65 --- /dev/null +++ b/drizzle/meta/0004_snapshot.json @@ -0,0 +1,190 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "dcab48f9-720d-491d-9c88-0ef7fcbfeb3a", + "prevId": "c3f2a9e1-4d5b-48f6-a7c8-9b0d1e2f3a4b", + "tables": { + "active_alarm": { + "name": "active_alarm", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "payload": { + "name": "payload", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "alarm_registrations": { + "name": "alarm_registrations", + "columns": { + "alarm_id": { + "name": "alarm_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "generation": { + "name": "generation", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "alarm_registrations_alarm_id_type_pk": { + "columns": [ + "alarm_id", + "type" + ], + "name": "alarm_registrations_alarm_id_type_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "alarms": { + "name": "alarms", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "days": { + "name": "days", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "hour": { + "name": "hour", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "minute": { + "name": "minute", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "task": { + "name": "task", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "rounds": { + "name": "rounds", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "difficulty": { + "name": "difficulty", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "sound": { + "name": "sound", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "is_snooze": { + "name": "is_snooze", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "is_enabled": { + "name": "is_enabled", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 3f8259a..6fe5407 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -29,6 +29,13 @@ "when": 1785933857428, "tag": "0003_misty_moon", "breakpoints": true + }, + { + "idx": 4, + "version": "6", + "when": 1786532266738, + "tag": "0004_striped_exiles", + "breakpoints": true } ] } \ No newline at end of file diff --git a/drizzle/migrations.js b/drizzle/migrations.js index c344646..7be1ebf 100644 --- a/drizzle/migrations.js +++ b/drizzle/migrations.js @@ -5,6 +5,7 @@ import m0000 from "./0000_large_vulture.sql"; import m0001 from "./0001_equal_jetstream.sql"; import m0002 from "./0002_bent_zemo.sql"; import m0003 from "./0003_misty_moon.sql"; +import m0004 from "./0004_striped_exiles.sql"; export default { journal, @@ -13,5 +14,6 @@ export default { m0001, m0002, m0003, + m0004, }, }; diff --git a/src/data/schema.ts b/src/data/schema.ts index da2a0f0..5c50409 100644 --- a/src/data/schema.ts +++ b/src/data/schema.ts @@ -26,6 +26,7 @@ export const alarmRegistrationsTable = sqliteTable( { alarmId: text("alarm_id").notNull(), type: text("type").notNull(), + generation: integer("generation").notNull().default(0), }, (table) => ({ pk: primaryKey({ columns: [table.alarmId, table.type] }), diff --git a/src/store/alarmRegistrationsStore.ts b/src/store/alarmRegistrationsStore.ts index 9f70b56..fea17d7 100644 --- a/src/store/alarmRegistrationsStore.ts +++ b/src/store/alarmRegistrationsStore.ts @@ -9,6 +9,7 @@ export type RegistrationType = "weekly" | "snooze"; export interface RegistrationRecord { alarmId: string; type: RegistrationType; + generation: number; } interface AlarmRegistrationsStoreState { @@ -36,6 +37,7 @@ export const useAlarmRegistrationsStore = create( records: rows.map((r) => ({ alarmId: r.alarmId, type: r.type as RegistrationType, + generation: r.generation, })), loaded: true, loading: false, @@ -91,20 +93,29 @@ export const useAlarmRegistrationsStore = create( upsert: async (alarmId, type) => { await dbReady; + const existing = get().records.find( + (r) => r.alarmId === alarmId && r.type === type, + ); + const generation = existing ? existing.generation + 1 : 0; await db .insert(alarmRegistrationsTable) - .values({ alarmId, type }) + .values({ alarmId, type, generation }) .onConflictDoUpdate({ target: [ alarmRegistrationsTable.alarmId, alarmRegistrationsTable.type, ], - set: { alarmId, type }, + set: { generation }, }); - const records = get().records; - if (!records.some((r) => r.alarmId === alarmId && r.type === type)) { - set({ records: [...records, { alarmId, type }] }); - } + set({ + records: existing + ? get().records.map((r) => + r.alarmId === alarmId && r.type === type + ? { ...r, generation } + : r, + ) + : [...get().records, { alarmId, type, generation }], + }); }, remove: async (alarmId, type) => { diff --git a/src/store/alarmStore.ts b/src/store/alarmStore.ts index 25f6e3f..3dd8da1 100644 --- a/src/store/alarmStore.ts +++ b/src/store/alarmStore.ts @@ -150,32 +150,40 @@ export const useAlarmStore = create((set, get) => { dismissAllSnoozedAlarms: async () => { const registryStore = useAlarmRegistrationsStore.getState(); - const snoozedIds = registryStore.records + const snoozed = registryStore.records .filter((r) => r.type === "snooze") - .map((r) => r.alarmId); + .map((r) => ({ alarmId: r.alarmId, generation: r.generation })); - if (snoozedIds.length === 0) return 0; + if (snoozed.length === 0) return 0; const native = getAlarmScheduler(); const results = await Promise.allSettled( - snoozedIds.map(async (alarmId) => { + snoozed.map(async ({ alarmId, generation }) => { + const current = useAlarmRegistrationsStore + .getState() + .records.find((r) => r.alarmId === alarmId && r.type === "snooze"); + if (!current || current.generation !== generation) { + return; + } await native.cancel(snoozeIdentifierFor(alarmId)); await registryStore.remove(alarmId, "snooze"); }), ); - const failed = results.filter((r) => r.status === "rejected").length; + const rejected = results.filter((r) => r.status === "rejected"); + const failed = rejected.length; if (failed > 0) { - console.error( - `Failed to dismiss ${failed} snoozed alarm(s)`, - results - .filter((r) => r.status === "rejected") - .map((r) => (r as PromiseRejectedResult).reason), + const reasons = rejected.map( + (r) => (r as PromiseRejectedResult).reason, + ); + console.error(`Failed to dismiss ${failed} snoozed alarm(s)`, reasons); + throw new Error( + `Failed to dismiss ${failed} snoozed alarm(s): ${reasons.join(", ")}`, ); } - return snoozedIds.length - failed; + return results.filter((r) => r.status === "fulfilled").length; }, }; }); From e49bb5269502bf25cfef5b7e05412e39833949b5 Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Wed, 12 Aug 2026 19:19:52 +0800 Subject: [PATCH 08/10] fix: roll forward alarm countdown to next scheduled time --- src/utils/time.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/time.ts b/src/utils/time.ts index 9735790..fce67df 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -60,7 +60,7 @@ export function computeNextAlarm( .second(0) .millisecond(0); const adjusted = - candidate.valueOf() < nowDayjs.valueOf() + candidate.valueOf() <= nowDayjs.valueOf() ? candidate.add(7, "day") : candidate; const ms = adjusted.valueOf() - nowDayjs.valueOf(); From c37551ddcc25c5d0dce65c5cf4ac7180536b2fab Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Wed, 12 Aug 2026 19:32:00 +0800 Subject: [PATCH 09/10] fix: use atomic sql generation + 1 with returning() in upsert Replace pre-read of generation from Zustand state with SQL-level `generation + 1` in onConflictDoUpdate, returning the result row to eliminate a race window where concurrent upserts could compute the same generation number from stale in-memory state. --- src/store/alarmRegistrationsStore.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/store/alarmRegistrationsStore.ts b/src/store/alarmRegistrationsStore.ts index fea17d7..a6b790c 100644 --- a/src/store/alarmRegistrationsStore.ts +++ b/src/store/alarmRegistrationsStore.ts @@ -1,5 +1,5 @@ import * as SQLite from "expo-sqlite"; -import { and, eq } from "drizzle-orm"; +import { and, eq, sql } from "drizzle-orm"; import { create } from "zustand"; import { db, dbReady } from "@/data/db"; import { alarmRegistrationsTable } from "@/data/schema"; @@ -93,22 +93,23 @@ export const useAlarmRegistrationsStore = create( upsert: async (alarmId, type) => { await dbReady; - const existing = get().records.find( - (r) => r.alarmId === alarmId && r.type === type, - ); - const generation = existing ? existing.generation + 1 : 0; - await db + const [returned] = await db .insert(alarmRegistrationsTable) - .values({ alarmId, type, generation }) + .values({ alarmId, type, generation: 0 }) .onConflictDoUpdate({ target: [ alarmRegistrationsTable.alarmId, alarmRegistrationsTable.type, ], - set: { generation }, - }); + set: { generation: sql`generation + 1` }, + }) + .returning(); + const generation = returned.generation; + const exists = get().records.some( + (r) => r.alarmId === alarmId && r.type === type, + ); set({ - records: existing + records: exists ? get().records.map((r) => r.alarmId === alarmId && r.type === type ? { ...r, generation } From 799c02d0f8b2c1c9c10fa5fc95ba8e0833166d2b Mon Sep 17 00:00:00 2001 From: HoweiAY Date: Wed, 12 Aug 2026 19:32:19 +0800 Subject: [PATCH 10/10] test: add boundary tests for computeNextAlarm equality/edge cases Cover candidate == now (rolls forward 7 days), candidate one minute after now (keeps same day), and candidate one minute before now (rolls forward 6d 23h 59m). --- test/unit/utils/time.test.ts | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test/unit/utils/time.test.ts diff --git a/test/unit/utils/time.test.ts b/test/unit/utils/time.test.ts new file mode 100644 index 0000000..7e86d77 --- /dev/null +++ b/test/unit/utils/time.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from "@jest/globals"; +import type { Alarm } from "@/data/types"; +import { computeNextAlarm } from "@/utils/time"; + +function baseAlarm(over: Partial = {}): Alarm { + return { + id: "alarm-1", + days: [], + hour: 8, + minute: 0, + task: "Memory", + rounds: 1, + difficulty: "Easy", + sound: null, + snooze: true, + enabled: true, + ...over, + }; +} + +describe("computeNextAlarm", () => { + it("returns null when no alarms are enabled", () => { + const now = new Date(2024, 0, 3, 10, 0, 0, 0); + expect(computeNextAlarm([baseAlarm({ enabled: false })], now)).toBeNull(); + }); + + it("returns null when alarms list is empty", () => { + const now = new Date(2024, 0, 3, 10, 0, 0, 0); + expect(computeNextAlarm([], now)).toBeNull(); + }); + + describe("boundary conditions", () => { + const now = new Date(2024, 0, 3, 10, 0, 0, 0); + + it("rolls forward one week when candidate equals now", () => { + const result = computeNextAlarm( + [baseAlarm({ days: ["Wed"], hour: 10, minute: 0 })], + now, + ); + expect(result).toEqual({ days: 7, hours: 0, minutes: 0 }); + }); + + it("does not roll forward when candidate is one minute after now", () => { + const result = computeNextAlarm( + [baseAlarm({ days: ["Wed"], hour: 10, minute: 1 })], + now, + ); + expect(result).toEqual({ days: 0, hours: 0, minutes: 1 }); + }); + + it("rolls forward one week when candidate is one minute before now", () => { + const result = computeNextAlarm( + [baseAlarm({ days: ["Wed"], hour: 9, minute: 59 })], + now, + ); + expect(result).toEqual({ days: 6, hours: 23, minutes: 59 }); + }); + }); +});