Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

fix: fence stale reminder reconciliation

Amruth Pillai08bbf9a07d5c
diff --git a/apps/mobile/locales/lt-LT.po b/apps/mobile/locales/lt-LT.po
index 7e860409d..3980c781e 100644
--- a/apps/mobile/locales/lt-LT.po
+++ b/apps/mobile/locales/lt-LT.po
@@ -10337,7 +10337,7 @@ msgstr "Atsakymas nukopijuotas ir užregistruotas"
#: src/features/workspace/tabs/messages.tsx
msgid "Reply copied. Logging wasn't confirmed. Check the timeline before trying again."
-msgstr "Atsakymas nukopijuotas. Registravimas nepatvirtintas. Prieš bandydami dar kartą patikrinkite laiko juostą."
+msgstr "Atsakymas nukopijuotas. Registravimas nepatvirtintas. Prieš bandydami dar kartą patikrinkite veiklos istoriją."
#: src/features/workspace/tabs/messages.tsx
msgid "Reply copied. Nothing was logged because the application changed. Review it again before logging."
diff --git a/apps/mobile/scripts/reminder-cancellation.test.mjs b/apps/mobile/scripts/reminder-cancellation.test.mjs
index a68081598..7eae72727 100644
--- a/apps/mobile/scripts/reminder-cancellation.test.mjs
+++ b/apps/mobile/scripts/reminder-cancellation.test.mjs
@@ -31,6 +31,8 @@ let failChannel = false;
const platform = { OS: "ios" };
let hold;
let entered = () => {};
+let readHold;
+let readEntered = () => {};
const notifications = {
getPermissionsAsync: async () => ({ granted: true }),
SchedulableTriggerInputTypes: { DATE: "date" },
@@ -53,9 +55,14 @@ const notifications = {
}
scheduled.delete(id);
},
- getAllScheduledNotificationsAsync: async () => [...scheduled.values()],
+ getAllScheduledNotificationsAsync: async () => {
+ const snapshot = [...scheduled.values()];
+ readEntered();
+ if (readHold) await readHold;
+ return snapshot;
+ },
};
-const { useReminders } = load("../src/features/reminders/index.ts", {
+const { useReminders, syncReminders } = load("../src/features/reminders/index.ts", {
"@lingui/core/macro": { t: (parts) => parts.join("") },
"@tanstack/react-query": {
useQuery: () => ({ data: new Map() }),
@@ -83,6 +90,75 @@ beforeEach(() => {
platform.OS = "ios";
hold = undefined;
entered = () => {};
+ readHold = undefined;
+ readEntered = () => {};
+});
+
+for (const timing of ["before", "during"]) {
+ for (const action of ["clear", "move"]) {
+ test(`reconciliation ${action} respects a reminder request begun ${timing} its notification read`, async () => {
+ const old = target(`sync-${timing}-${action}`);
+ const reminders = useReminders(old.applicationId);
+ await reminders.turnOn(old, 5);
+ let releaseSchedule;
+ hold = new Promise((resolve) => {
+ releaseSchedule = resolve;
+ });
+ const scheduleArrival = new Promise((resolve) => {
+ entered = resolve;
+ });
+ let releaseRead;
+ readHold = new Promise((resolve) => {
+ releaseRead = resolve;
+ });
+ const readArrival = new Promise((resolve) => {
+ readEntered = resolve;
+ });
+ const newer = target(old.applicationId, "2035-01-09T10:00:00Z");
+ let replacing;
+ if (timing === "before") {
+ replacing = reminders.turnOn(newer, 10);
+ await scheduleArrival;
+ }
+ const syncing = syncReminders(
+ old.applicationId,
+ action === "clear" ? [] : [target(old.applicationId, "2035-01-03T11:00:00Z")],
+ );
+ await readArrival;
+ if (timing === "during") {
+ replacing = reminders.turnOn(newer, 10);
+ await scheduleArrival;
+ }
+ releaseRead();
+ releaseSchedule();
+ await replacing;
+ await syncing;
+ const saved = scheduled.get(schedule.reminderId(old.kind, old.applicationId));
+ if (timing === "during") {
+ assert.equal(saved?.content.data.start, newer.start.toISOString());
+ assert.equal(saved?.content.data.lead, 10);
+ } else if (action === "clear") {
+ assert.equal(saved, undefined);
+ } else {
+ assert.equal(saved?.content.data.start, "2035-01-03T11:00:00.000Z");
+ assert.equal(saved?.content.data.lead, 5);
+ }
+ });
+ }
+}
+
+test("unchanged reconciliation still moves and cancels the observed reminder", async () => {
+ const old = target("sync-current");
+ const reminders = useReminders(old.applicationId);
+ await reminders.turnOn(old, 5);
+ const moved = target(old.applicationId, "2035-01-06T10:00:00Z");
+ await syncReminders(old.applicationId, [moved]);
+ assert.equal(
+ scheduled.get(schedule.reminderId(old.kind, old.applicationId))?.content.data.start,
+ moved.start.toISOString(),
+ );
+ await syncReminders(old.applicationId, []);
+ assert.equal(scheduled.has(schedule.reminderId(old.kind, old.applicationId)), false);
});
for (const failure of ["channel", "schedule"]) {
diff --git a/apps/mobile/src/features/reminders/index.ts b/apps/mobile/src/features/reminders/index.ts
index f9eeb437d..d6a22de9c 100644
--- a/apps/mobile/src/features/reminders/index.ts
+++ b/apps/mobile/src/features/reminders/index.ts
@@ -37,8 +37,19 @@ const completed = new Map<string, number>();
const changes = new Map<string, Promise<unknown>>();
// The platform cannot compare-and-cancel: serialize mutations so a delayed retry cannot delete a replacement.
-function changeReminder<T>(id: string, change: () => Promise<T>): Promise<T> {
- const next = (changes.get(id) ?? Promise.resolve()).catch(() => undefined).then(change);
+function changeReminder<T>(id: string, change: () => Promise<T>): Promise<T>;
+function changeReminder<T>(id: string, change: () => Promise<T>, expected: number | undefined): Promise<T | undefined>;
+function changeReminder<T>(id: string, change: () => Promise<T>, expected?: number): Promise<T | undefined> {
+ const revision = (revisions.get(id) ?? 0) + 1;
+ revisions.set(id, revision);
+ const next = (changes.get(id) ?? Promise.resolve())
+ .catch(() => undefined)
+ .then(async () => {
+ if (expected !== undefined && (completed.get(id) ?? 0) > expected) return;
+ const result = await change();
+ completed.set(id, revision);
+ return result;
+ });
changes.set(id, next);
const settled = () => {
if (changes.get(id) === next) changes.delete(id);
@@ -48,13 +59,7 @@ function changeReminder<T>(id: string, change: () => Promise<T>): Promise<T> {
}
function cancelReminder(id: string, expected?: number) {
- const revision = (revisions.get(id) ?? 0) + 1;
- revisions.set(id, revision);
- return changeReminder(id, async () => {
- if (expected !== undefined && (completed.get(id) ?? 0) > expected) return;
- await Notifications.cancelScheduledNotificationAsync(id);
- completed.set(id, revision);
- });
+ return changeReminder(id, () => Notifications.cancelScheduledNotificationAsync(id), expected);
}
/** Asks for permission only if it hasn't been answered; false when notifications are off. */
@@ -94,14 +99,7 @@ async function scheduleNative(target: ReminderTarget, lead: number): Promise<Sch
}
function schedule(target: ReminderTarget, lead: number): Promise<ScheduleResult> {
- const id = idOf(target);
- const revision = (revisions.get(id) ?? 0) + 1;
- revisions.set(id, revision);
- return changeReminder(id, async () => {
- const result = await scheduleNative(target, lead);
- completed.set(id, revision);
- return result;
- });
+ return changeReminder(idOf(target), () => scheduleNative(target, lead));
}
/** Reminders scheduled on this device for one application: notification identifier → lead minutes. */
@@ -139,7 +137,7 @@ export function useReminders(applicationId: string) {
await cancelReminder(reminderId(target.kind, target.applicationId, target.entryId));
await refresh();
},
- /** Retain this cancellation across retries; a later local schedule makes it inert. */
+ /** Retain this cancellation across retries; a later successful local change makes it inert. */
captureTurnOff: (target: Pick<ReminderTarget, "kind" | "applicationId" | "entryId">) => {
const id = reminderId(target.kind, target.applicationId, target.entryId);
const expected = revisions.get(id) ?? 0;
@@ -162,12 +160,13 @@ export const releaseReminder = (entryId: string) => held.delete(reminderId("inte
*/
export async function syncReminders(applicationId: string, targets: ReminderTarget[]) {
const byId = new Map(targets.map((target) => [idOf(target), target]));
+ const readAt = new Map(revisions);
for (const { id, data } of await scheduledFor(applicationId)) {
if (held.has(id)) continue;
const target = byId.get(id);
- if (!target) await cancelReminder(id);
+ if (!target) await cancelReminder(id, readAt.get(id) ?? 0);
else if (data.start !== target.start.toISOString() || data.title !== target.title || data.body !== target.body)
- await schedule(target, data.lead);
+ await changeReminder(id, () => scheduleNative(target, data.lead), readAt.get(id) ?? 0);
}
}
diff --git a/apps/web/locales/lt-LT.po b/apps/web/locales/lt-LT.po
index 8eb97beb6..645f02165 100644
--- a/apps/web/locales/lt-LT.po
+++ b/apps/web/locales/lt-LT.po
@@ -13040,7 +13040,7 @@ msgstr "Atsakymas nukopijuotas ir užregistruotas"
#: src/features/career/workspace/messages.tsx
msgid "Reply copied. Logging wasn't confirmed. Check the timeline before trying again."
-msgstr "Atsakymas nukopijuotas. Registravimas nepatvirtintas. Prieš bandydami dar kartą patikrinkite laiko juostą."
+msgstr "Atsakymas nukopijuotas. Registravimas nepatvirtintas. Prieš bandydami dar kartą patikrinkite veiklos istoriją."
#: src/features/career/workspace/messages.tsx
msgid "Reply copied. Nothing was logged because the application changed. Review it again before logging."
diff --git a/apps/web/src/features/applications/components/detail/activity.test.tsx b/apps/web/src/features/applications/components/detail/activity.test.tsx
index 9e53b3ef3..95fb8e730 100644
--- a/apps/web/src/features/applications/components/detail/activity.test.tsx
+++ b/apps/web/src/features/applications/components/detail/activity.test.tsx
@@ -41,6 +41,13 @@ it("does not repeat a localized reply label and keeps a custom note's canonical
text: "Custom note",
followedUp: { kind: "reply", stageEntryId: null },
},
+ {
+ id: "legacy",
+ type: "note",
+ at: new Date(),
+ text: "Legacy note",
+ followedUp: { stageEntryId: null },
+ },
],
} as Application;
render(
@@ -52,4 +59,5 @@ it("does not repeat a localized reply label and keeps a custom note's canonical
);
expect(screen.getByText("Réponse donnée", { exact: true })).toBeVisible();
expect(screen.getByText("Réponse donnée · Custom note", { exact: true })).toBeVisible();
+ expect(screen.getByText("Followed up · Legacy note", { exact: true })).toBeVisible();
});