fix(native): widgets turn to the next trip day by themselves

TestFlight feedback: during a trip the widgets stayed on day 1 until the app
was opened again. The app wrote only today's day into the widget snapshot,
and only while it ran, so every timeline entry after midnight still showed
the day it last ran, with its stops all ahead again.

The snapshot now also carries the trip's days from today on (a month at
most, sixteen stops a day), each with its date. On iOS every timeline entry
takes the day of its own date, so the quarter-hour entry at midnight turns
the widget to the next day, and the bookings list drops the days behind; the
Android widgets do the same on each update. A snapshot from an app without
the days reads as it was written, and an older app binary ignores them.
This commit is contained in:
Maurice
2026-11-18 21:28:00 +01:00
parent fd35f79c36
commit 3e0a2ef8ed
10 changed files with 377 additions and 33 deletions
@@ -0,0 +1,87 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { render, waitFor } from '../../../tests/helpers/render';
import { offlineDb } from '../../db/offlineDb';
import type { Day, Trip } from '../../types';
const published = vi.hoisted(() => ({ calls: [] as unknown[][] }));
vi.mock('@capacitor/app', () => ({ App: { addListener: vi.fn(async () => ({ remove: vi.fn() })) } }));
vi.mock('../../native/capabilities', () => ({ useNativeCapabilities: () => ({ caps: {} }) }));
vi.mock('../../native/watch', () => ({
applyWatchActions: vi.fn(async () => []),
buildWatchSnapshot: vi.fn(),
publishWatch: vi.fn(async () => {}),
takeWatchActions: vi.fn(async () => []),
watchActive: () => false,
}));
vi.mock('../../native/widgetMedia', () => ({
widgetCover: vi.fn(async () => null),
widgetWeather: vi.fn(async () => null),
}));
vi.mock('../../native/widgetOnline', () => ({
atlasSummary: vi.fn(async () => null),
collabSummary: vi.fn(async () => null),
packingDetail: vi.fn(async () => null),
}));
vi.mock('../../native/widgets', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../native/widgets')>();
return {
...actual,
widgetsActive: () => true,
publishWidgets: vi.fn(async (...args: unknown[]) => {
published.calls.push(args);
}),
};
});
import NativeWidgetsSync from './NativeWidgetsSync';
const trip = {
id: 7,
title: 'Lisbon',
start_date: '2026-11-06',
end_date: '2026-11-08',
is_archived: 0,
} as unknown as Trip;
const day = (id: number, n: number, date: string, name: string) =>
({
id,
trip_id: 7,
day_number: n,
date,
assignments: [{ id: id * 10, order_index: 0, place: { name, place_time: '10:00' } }],
}) as unknown as Day;
describe('NativeWidgetsSync', () => {
beforeEach(async () => {
vi.useFakeTimers({ toFake: ['Date'] });
vi.setSystemTime(new Date(2026, 10, 7, 9, 0));
published.calls = [];
await offlineDb.trips.put(trip as never);
await offlineDb.days.bulkPut([
day(1, 1, '2026-11-06', 'Castle'),
day(2, 2, '2026-11-07', 'Harbour'),
day(3, 3, '2026-11-08', 'Market'),
] as never);
});
afterEach(async () => {
vi.useRealTimers();
await offlineDb.trips.clear();
await offlineDb.days.clear();
});
it('FE-NATIVE-WSYNC-001: writes today and every day ahead, so the widgets turn the day without the app', async () => {
render(<NativeWidgetsSync />);
await waitFor(() => expect(published.calls.length).toBeGreaterThan(0));
const snapshot = published.calls[0][0] as {
today: { date: string; stops: { name: string }[] } | null;
days: { date: string; stops: { name: string }[] }[];
};
expect(snapshot.today).toMatchObject({ date: '2026-11-07', stops: [{ name: 'Harbour' }] });
expect(snapshot.days.map((d) => [d.date, d.stops[0]?.name])).toEqual([
['2026-11-07', 'Harbour'],
['2026-11-08', 'Market'],
]);
});
});
@@ -25,11 +25,10 @@ import {
liveFlightOf,
pickTrip,
publishWidgets,
todayOf,
snapshotDays,
tripDayState,
widgetTripChoices,
widgetsActive,
withBookings,
type TripWidgetSnapshot,
type WidgetLabels,
} from '../../native/widgets';
@@ -191,11 +190,15 @@ export default function NativeWidgetsSync(): null {
const tripSnapshot = async (trip: Trip | null, context: SyncContext, atlas: WidgetAtlas | null) => {
const days = trip ? await offlineDb.days.where('trip_id').equals(trip.id).toArray() : [];
const extra = trip ? await tripExtras(trip, days, context) : null;
const plain = trip
? todayOf(trip, days, context.today, (n, m) => t('native.widget.dayOf', { n, m }), context.time)
: null;
const day = plain ? withBookings(plain, extra?.reservations ?? [], context.today, context.time) : null;
return { day, days, extra, snapshot: buildSnapshot(trip, day, labels(t), { ...extra?.extras, atlas }) };
// Today and the days after it: the widgets turn to the next day at midnight by themselves.
const { today: day, ahead } = snapshotDays(trip, days, {
reservations: extra?.reservations ?? [],
from: context.today,
dayLabel: (n, m) => t('native.widget.dayOf', { n, m }),
formatTime: context.time,
});
const snapshot = buildSnapshot(trip, day, labels(t), { ...extra?.extras, atlas, days: ahead });
return { day, days, extra, snapshot };
};
/** The hero trip in full for the watch, with the ticks from it applied so far. */