add a renewal notification when all the search JWTs have been used

This commit is contained in:
2026-11-09 20:17:49 -07:00
parent 149ca5b6e5
commit 965f5c0db5
15 changed files with 943 additions and 13 deletions
+8
View File
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- A renewal reminder on each channel. Each scheduler pass ends with a sweep for users whose newest batch on that channel has no JWT dated today or later; from the day after the last JWT day, at the user's hour, each gets one push (`Keep your alerts going`, data `type: alert_search` with `reason: renewal`) or one text (`Gift Economies: your text alerts have ended. Open the app to renew them: https://giftopia.me/deep-link/new-activity Reply STOP to end.`) pointing at New Activity, where the app re-authorizes. Once per uploaded batch; a user whose inventory was already spent gets it on the first pass. Before this a user who never opened New Activity used their last JWT and went silent.
### Changed in DB or environment
- New column `renewal_reminded_at` on `alert_authorization_batches` and `sms_alert_authorization_batches`, added on startup to an existing database.
- New `sms_phone_log` actions `renewal-sent` and `renewal-send-failed`.
## [0.2.7]
### Added
- `scripts/alert-search-tap.ts` (`pnpm alert-search:tap`): sends one `alert_search` FCM to a registered device (`--device-id`) so a notification tap can be tested without a daily run. Omitting `--device-id` lists registrations and sends nothing. It does not consume a JWT or move a cursor.
+40 -4
View File
@@ -113,10 +113,45 @@ The **delegated** JWT is sent as `Authorization: Bearer`. Each source takes its
`runDailyAlertSearch(userId, now?)` picks the unused delegated JWT for the current **UTC** day, runs `runAlertSearchCycle` with that JWT, and marks that specific JWT consumed only when both Endorser and Partner complete (`success` or `empty`, including both empty). Pagination or source failures leave the JWT unused so the same day can be retried. The result reports the day it used as `utcDay`. After a retrieve, the result includes `digest` from `buildAlertSearchDigest` (eight bucket records and counts: the five endorser lists plus `profilesNearby`, `profilesSimilar` and `plansSimilar`), and `startCursors`, the cursors the run counted from (absent on a first run). `digest` is `null` when there is no batch or no unused JWT for today. Consumption does not depend on `digest.hasUpdates`.
`startAlertSearchScheduler()` (started from `src/index.ts` next to the FCM scheduler) is a **separate** user-level job. One query per pass asks for the users who hold an unused JWT for the current UTC day, flagged by whether their `notify_hour_min_utc` has arrived ([Scheduler selection](#scheduler-selection)); it calls `runDailyAlertSearch` on the due ones, up to `ALERT_SEARCH_USER_CONCURRENCY` at a time. After each run, if the digest is complete with updates **and** today's JWT was consumed, it sends a user-visible FCM message (`title: TimeSafari`, body `You have N new updates.`, data `type: alert_search` and `c`, the start cursors joined by `.`) to that user's registered tokens. It does not call `sendPushToDevice` or change `WAKEUP_PING`. Subsequent ticks the same UTC day see no unused JWT (`digest: null`) and do not resend. Underneath that, an `alert_push_sends` count caps alert pushes at one per identity per UTC day, to all its devices at once; for a DID on `PUSH_SAME_DAY_RESEND_DIDS` it counts only pushes since the current batch was stored, so a `rerunToday` reset can push again. A push that reached no device is not counted. FCM send failures are logged and do not roll back cursors or JWT consumption. A process-local in-flight flag skips a tick if a pass is still running.
`startAlertSearchScheduler()` (started from `src/index.ts` next to the FCM scheduler) is a **separate** user-level job. One query per pass asks for the users who hold an unused JWT for the current UTC day, flagged by whether their `notify_hour_min_utc` has arrived ([Scheduler selection](#scheduler-selection)); it calls `runDailyAlertSearch` on the due ones, up to `ALERT_SEARCH_USER_CONCURRENCY` at a time. After each run, if the digest is complete with updates **and** today's JWT was consumed, it sends a user-visible FCM message (`title: TimeSafari`, body `You have N new updates.`, data `type: alert_search` and `c`, the start cursors joined by `.`) to that user's registered tokens. After every user, the pass runs the [renewal reminder](#renewal-reminder) sweep. It does not call `sendPushToDevice` or change `WAKEUP_PING`. Subsequent ticks the same UTC day see no unused JWT (`digest: null`) and do not resend. Underneath that, an `alert_push_sends` count caps alert pushes at one per identity per UTC day, to all its devices at once; for a DID on `PUSH_SAME_DAY_RESEND_DIDS` it counts only pushes since the current batch was stored, so a `rerunToday` reset can push again. A push that reached no device is not counted. FCM send failures are logged and do not roll back cursors or JWT consumption. A process-local in-flight flag skips a tick if a pass is still running.
`buildAlertSearchDigest` maps a retrieve result into structured payload data: per-bucket record arrays, counts, `totalCount`, `hasUpdates`, and Endorser/Partner completion status. It does not invent a notification string; FCM uses only `totalCount` for the short body. Incomplete outcomes (`pagination`, auth, network, etc.) yield `completed: false` and `hasUpdates: false`. Empty successful retrieves are complete with `hasUpdates: false` and do not send FCM.
### Renewal reminder
A batch is 100 JWTs, one per UTC day, so an inventory runs out about 100 days
after it was uploaded. The app refills it when the person opens New Activity;
nothing on the server mints a JWT. A user with no JWT for today is not listed by
the [scheduler selection](#scheduler-selection) at all, so once the last one is
consumed the daily runs stop without anything observing it.
The renewal reminder is a sweep at the end of every scheduler pass, on each
channel over that channel's inventory, rather than a step of a user's daily
run, because the user it needs to reach has no run. A user is a candidate on a
UTC day when the newest batch has not been reminded (`renewal_reminded_at` is
null), none of the user's JWTs on that channel is dated that day or later, and
the batch's hour has arrived, read exactly as the daily selection reads it. JWT
status is not consulted: an unused JWT for a past day is a run the scheduler
skipped and will never make up, not reserve.
So the reminder goes out from the day after the batch's last JWT day, at the
user's usual hour, and never on that last day, whose digest may be on its
way. From that day on nothing else can reach the user on that channel, so
one message a day holds without a further check. A user whose inventory was
already spent is reminded on the first pass that finds them.
- Push: title `Keep your alerts going`, body `Your daily alerts have ended. Tap to open New Activity and renew them.` The data carries `type: alert_search`, because that is the only type the app routes a tap on, plus `reason: renewal` and no `c`, so a later app version can tell it from a digest.
- Text: `Gift Economies: your text alerts have ended. Open the app to renew them: https://giftopia.me/deep-link/new-activity Reply STOP to end.`, inside one GSM-7 segment, logged as `renewal-sent` / `renewal-send-failed`.
`renewal_reminded_at` is set when a send reached a device or a handset, and a
fresh upload starts a fresh batch row, so each batch gets one reminder. A send
that reached nobody leaves the batch unmarked, and the user stays a candidate
for the next pass. The reminder is not counted in `alert_push_sends` or as an
`alert-sent` row. The SMS reminder honors the block list,
`SMS_ALLOWED_RECIPIENT_DIDS`, and Twilio `21610` exactly as the digest does.
The sweep logs `[AlertSearchRenewal] Sweep` with candidate and reminded counts
on any pass that had a candidate.
## SMS
`/notify-sms` delivers the daily alertSearch digest by text as well as by push.
@@ -435,7 +470,8 @@ charge. `c` is the endorser and partner cursors the run counted from, joined by
handset per identity per UTC calendar day. For a number on
`SMS_SAME_DAY_RESEND_NUMBERS` it counts only sends since the current batch was
stored, so a `rerunToday` reset can text it again. Send failures are logged and do not roll back
cursor advancement or JWT consumption.
cursor advancement or JWT consumption. After every user, the pass runs the
[renewal reminder](#renewal-reminder) sweep over the SMS inventory.
The SMS channel keeps its own JWT inventory and its own cursor table. A user on
both channels produces two Endorser and two Partner queries per day and needs
@@ -665,7 +701,7 @@ Unique on `(user_id, device_id)`. Indexes also exist on `user_id`, `device_id`,
Tables `alert_authorization_batches` and `alert_authorization_jwts` hold a user's delegated notification-JWT inventory (separate from device registration):
- Batch: `id`, `user_id` (authenticated DID), `batch_id`, `notify_hour_min_utc` (zero-padded `HH:MM` UTC, nullable), `timezone` (IANA name, nullable, recorded but unread), `created_at`
- Batch: `id`, `user_id` (authenticated DID), `batch_id`, `notify_hour_min_utc` (zero-padded `HH:MM` UTC, nullable), `timezone` (IANA name, nullable, recorded but unread), `renewal_reminded_at` (when the [renewal reminder](#renewal-reminder) for this batch went out, nullable), `created_at`
- JWT: `batch_pk`, `sequence`, `day` (`YYYY-MM-DD`), `jwt`, `nbf`, `exp`, `status` (`unused` / `consumed`), `consumed_at`, timestamps
Unique on `(batch_pk, sequence)` and on `(user_id, day)` for unused rows. Indexes also exist on `(user_id, status)`, `(user_id, day)`, and `batch_pk`.
@@ -693,7 +729,7 @@ Table `sms_phone_log` records every phone action: `user_id`, `phone_e164`
`code-sent`, `code-send-failed`, `verify-succeeded`, `verify-failed`,
`did-limit-blocked`, `did-limit-disclosed`, `deleted`,
`alert-authorization-stored`, `alert-authorization-deleted`, `alert-sent`,
`alert-send-failed`,
`alert-send-failed`, `renewal-sent`, `renewal-send-failed`,
`recipient-not-allowed`, `number-blocked`, `number-unblocked`, and `opt-out`.
Indexes on `(user_id, created_at)`, `(phone_hash, created_at)`, and
`(action, created_at)`; the throttle counts read the second.
+3 -2
View File
@@ -145,7 +145,8 @@ export function isAlertSearchNotificationEligible(
);
}
async function defaultListTokens(userId: string): Promise<string[]> {
/** This identity's registered FCM tokens, each once. */
export async function listRegisteredTokens(userId: string): Promise<string[]> {
const rows = await db.getByUserId(userId);
const seen = new Set<string>();
const tokens: string[] = [];
@@ -207,7 +208,7 @@ export async function deliverAlertSearchNotification(
return { eligible: true, sent: 0, failed: 0, capped: true };
}
const listTokens = deps.listTokens ?? defaultListTokens;
const listTokens = deps.listTokens ?? listRegisteredTokens;
const send = deps.send ?? defaultSend;
const tokens = await listTokens(result.userId);
let sent = 0;
+291
View File
@@ -0,0 +1,291 @@
import {
alertAuthorizationDb,
type AlertAuthorizationStore,
} from "../db/alertAuthorizationSqlite.js";
import { smsAlertAuthorizationDb } from "../db/smsAlertAuthorizationSqlite.js";
import { smsPhoneLogDb } from "../db/smsPhoneLogSqlite.js";
import { smsConfig } from "../env.js";
import {
utcCalendarDay,
utcHourMinute,
} from "../services/alertAuthorization.js";
import {
ALERT_SEARCH_FCM_REASON_RENEWAL,
type AlertSearchFcmContent,
} from "../services/alertSearchFcmPayload.js";
import {
TWILIO_UNSUBSCRIBED_CODE,
isPhoneNumberBlocked,
isSmsRecipientAllowed,
sendSms,
type SmsSender,
} from "../services/smsService.js";
import { errorMessage } from "../util/formatElapsed.js";
import { maskPhoneNumber } from "../util/smsPhoneNumber.js";
import { hashPhoneNumber } from "../util/smsVerificationCode.js";
import { listRegisteredTokens } from "./notify.js";
import {
ALERT_SEARCH_SMS_LINK,
listVerifiedPhones,
recordProviderOptOut,
} from "./smsNotify.js";
import type { AlertSearchChannel } from "./types.js";
import { log } from "../util/log.js";
/**
* The renewal reminder: one message per uploaded batch, on the channel whose
* inventory is spent, pointing at New Activity, where the app re-authorizes
* every enabled channel. It is a sweep at the end of each scheduler pass, not
* a step of a user's daily run, because the user it needs to reach has no JWT
* for today and therefore no daily run at all.
*
* It goes out from the day after the batch's last JWT day, never on that day:
* the last day may still carry a digest, and a person hears from the service
* once a day. On any later day nothing else can reach them on this channel, so
* no further check is needed.
*/
const JWT_INVENTORIES: Record<AlertSearchChannel, AlertAuthorizationStore> = {
fcm: alertAuthorizationDb,
sms: smsAlertAuthorizationDb,
};
/** The push: title and body. The data type stays `alert_search` so a tap opens New Activity. */
export const RENEWAL_PUSH_CONTENT: AlertSearchFcmContent = {
title: "Keep your alerts going",
body: "Your daily alerts have ended. Tap to open New Activity and renew them.",
reason: ALERT_SEARCH_FCM_REASON_RENEWAL,
};
/** The text: one GSM-7 segment with the New Activity link and the opt-out. */
export const RENEWAL_SMS_BODY =
"Gift Economies: your text alerts have ended. " +
`Open the app to renew them: ${ALERT_SEARCH_SMS_LINK} Reply STOP to end.`;
export type AlertSearchRenewalPushSender = (
fcmToken: string,
content: AlertSearchFcmContent
) => Promise<"sent" | "failed">;
export type AlertSearchRenewalDeps = {
channel: AlertSearchChannel;
/** The instant this sweep represents: its UTC day, hour, and reminder time. */
now?: Date;
/** Overrides the channel's inventory. Tests only. */
inventory?: AlertAuthorizationStore;
listCandidates?: (input: {
day: string;
hourMinute: string;
}) => Promise<string[]>;
listTokens?: (userId: string) => Promise<string[]>;
sendPush?: AlertSearchRenewalPushSender;
listPhones?: (userId: string) => Promise<string[]>;
sendSms?: SmsSender;
};
export type AlertSearchRenewalUserResult = {
userId: string;
sent: number;
failed: number;
/** Recipients withheld: blocked numbers or a DID off the SMS allowlist. */
blocked: number;
};
export type AlertSearchRenewalSweepResult = {
/** The UTC day swept. */
day: string;
candidates: number;
/** Users for whom at least one reminder was delivered and the batch marked. */
reminded: number;
users: AlertSearchRenewalUserResult[];
};
async function deliverPush(
userId: string,
deps: AlertSearchRenewalDeps
): Promise<{ sent: number; failed: number; blocked: number }> {
const listTokens = deps.listTokens ?? listRegisteredTokens;
const send =
deps.sendPush ??
(async (token: string, c: AlertSearchFcmContent) => {
const { sendAlertSearchPushToDevice } = await import(
"../services/pushService.js"
);
return sendAlertSearchPushToDevice(token, c);
});
let sent = 0;
let failed = 0;
for (const token of await listTokens(userId)) {
try {
if ((await send(token, RENEWAL_PUSH_CONTENT)) === "sent") sent += 1;
else failed += 1;
} catch (err) {
failed += 1;
log.error("[AlertSearchRenewal] Push threw:", errorMessage(err));
}
}
return { sent, failed, blocked: 0 };
}
async function deliverText(
userId: string,
deps: AlertSearchRenewalDeps
): Promise<{ sent: number; failed: number; blocked: number }> {
const secret = smsConfig().codeSecret;
if (secret === undefined) {
log.error("[AlertSearchRenewal] SMS_CODE_SECRET is not set; skipping");
return { sent: 0, failed: 0, blocked: 0 };
}
const listPhones = deps.listPhones ?? listVerifiedPhones;
const send = deps.sendSms ?? sendSms;
const phones = await listPhones(userId);
if (!isSmsRecipientAllowed(userId)) {
log.info(
"[AlertSearchRenewal] Recipient not on SMS_ALLOWED_RECIPIENT_DIDS:",
userId + ",",
"withheld=" + String(phones.length)
);
return { sent: 0, failed: 0, blocked: phones.length };
}
let sent = 0;
let failed = 0;
let blocked = 0;
for (const phoneE164 of phones) {
const phoneHash = hashPhoneNumber(phoneE164, secret);
if (await isPhoneNumberBlocked({ phoneE164, phoneHash })) {
blocked += 1;
log.info(
"[AlertSearchRenewal] Number is blocked, withholding:",
maskPhoneNumber(phoneE164)
);
continue;
}
try {
const outcome = await send(phoneE164, RENEWAL_SMS_BODY);
if (outcome.status === "sent") {
sent += 1;
await smsPhoneLogDb.append({
userId,
phoneE164,
phoneHash,
action: "renewal-sent",
result: "ok",
providerMessageId: outcome.messageId,
});
} else {
failed += 1;
await smsPhoneLogDb.append({
userId,
phoneE164,
phoneHash,
action: "renewal-send-failed",
result: "failed",
detail: outcome.error,
});
if (outcome.code === TWILIO_UNSUBSCRIBED_CODE) {
await recordProviderOptOut(phoneHash, phoneE164, userId);
}
}
} catch (err) {
failed += 1;
const detail = errorMessage(err);
log.error(
"[AlertSearchRenewal] Text threw for",
maskPhoneNumber(phoneE164) + ":",
detail
);
await smsPhoneLogDb
.append({
userId,
phoneE164,
phoneHash,
action: "renewal-send-failed",
result: "failed",
detail,
})
.catch(() => undefined);
}
}
return { sent, failed, blocked };
}
/**
* Remind one candidate. The batch is marked only after a send reached someone,
* so a failed or withheld send is tried again on a later pass while the user
* stays a candidate.
*/
export async function remindRenewalCandidate(
userId: string,
deps: AlertSearchRenewalDeps
): Promise<AlertSearchRenewalUserResult> {
log.info(
"[AlertSearchRenewal] Reminding",
userId + ",",
`channel=${deps.channel}`
);
const counts =
deps.channel === "sms"
? await deliverText(userId, deps)
: await deliverPush(userId, deps);
if (counts.sent > 0) {
const inventory = deps.inventory ?? JWT_INVENTORIES[deps.channel];
await inventory.markRenewalReminded(
userId,
(deps.now ?? new Date()).toISOString()
);
}
return { userId, ...counts };
}
/**
* One channel's renewal sweep for the instant `now`: remind every user whose
* newest batch has no JWT dated today or later and has not been reminded. Each
* user is independent; one failure is logged and the sweep goes on.
*/
export async function runAlertSearchRenewalSweep(
deps: AlertSearchRenewalDeps
): Promise<AlertSearchRenewalSweepResult> {
const now = deps.now ?? new Date();
const nowSec = Math.floor(now.getTime() / 1000);
const day = utcCalendarDay(nowSec);
const inventory = deps.inventory ?? JWT_INVENTORIES[deps.channel];
const listCandidates =
deps.listCandidates ??
((input: { day: string; hourMinute: string }) =>
inventory.listRenewalCandidates(input));
const candidates = await listCandidates({
day,
hourMinute: utcHourMinute(nowSec),
});
const users: AlertSearchRenewalUserResult[] = [];
for (const userId of candidates) {
try {
users.push(await remindRenewalCandidate(userId, { ...deps, now }));
} catch (err) {
log.error(
"[AlertSearchRenewal] Reminder failed",
userId + ":",
errorMessage(err)
);
}
}
const result = {
day,
candidates: candidates.length,
reminded: users.filter((u) => u.sent > 0).length,
users,
};
if (candidates.length > 0) {
log.info(
"[AlertSearchRenewal] Sweep",
`channel=${deps.channel}, candidates ${result.candidates}, reminded ${result.reminded}`
);
}
return result;
}
+15
View File
@@ -16,6 +16,7 @@ import {
type DailyAlertSearchResult,
} from "./daily.js";
import { deliverAlertSearchNotification } from "./notify.js";
import { runAlertSearchRenewalSweep } from "./renewal.js";
import { log } from "../util/log.js";
/** Independent of the FCM wakeup interval; does not share that timer. */
@@ -36,6 +37,8 @@ export type AlertSearchSchedulerPassInput = {
}) => Promise<PendingDayUser[]>;
runDaily?: AlertSearchUserRunner;
notify?: AlertSearchNotifyRunner;
/** Runs once after every user: the renewal reminder sweep for this channel. */
remindRenewal?: () => Promise<unknown>;
/** The instant this pass represents. Defaults to now. */
now?: Date;
/** Users worked on at once. Set to 1 for a deterministic order in tests. */
@@ -82,6 +85,9 @@ export async function runAlertSearchSchedulerPass(
const runDaily =
input.runDaily ?? ((userId: string) => runDailyAlertSearch(userId, now));
const notify = input.notify ?? deliverAlertSearchNotification;
const remindRenewal =
input.remindRenewal ??
(() => runAlertSearchRenewalSweep({ channel: "fcm", now }));
const listPending =
input.listPending ??
((query: { day: string; hourMinute: string }) =>
@@ -122,6 +128,15 @@ export async function runAlertSearchSchedulerPass(
}
);
try {
await remindRenewal();
} catch (err) {
log.error(
"[AlertSearchScheduler] Renewal sweep failed:",
errorMessage(err)
);
}
log.info(
"[AlertSearchScheduler] Pass completed in",
formatElapsedMs(Date.now() - passStarted) + ",",
+4 -3
View File
@@ -130,7 +130,7 @@ export function isAlertSearchSmsEligible(
}
/** Syncs Twilio's opt-out list into ours, one refusal at a time. */
async function recordProviderOptOut(
export async function recordProviderOptOut(
phoneHash: string,
phoneE164: string,
userId: string
@@ -157,7 +157,8 @@ async function recordProviderOptOut(
}
}
async function defaultListPhones(userId: string): Promise<string[]> {
/** This identity's verified numbers, each once. */
export async function listVerifiedPhones(userId: string): Promise<string[]> {
const rows = await smsRegistrationsDb.listVerifiedByUserId(userId);
const seen = new Set<string>();
const numbers: string[] = [];
@@ -193,7 +194,7 @@ export async function deliverAlertSearchSms(
digest.totalCount,
alertSearchLinkCursorParam(result.startCursors)
);
const listPhones = deps.listPhones ?? defaultListPhones;
const listPhones = deps.listPhones ?? listVerifiedPhones;
const send = deps.send ?? sendSms;
const now = deps.now ?? new Date();
const dayStartIso = utcDayStartIso(now);
+15
View File
@@ -20,6 +20,7 @@ import {
runDailyAlertSearch,
type DailyAlertSearchResult,
} from "./daily.js";
import { runAlertSearchRenewalSweep } from "./renewal.js";
import {
deliverAlertSearchSms,
type AlertSearchSmsNotifyResult,
@@ -39,6 +40,8 @@ export type SmsAlertSearchSchedulerPassInput = {
}) => Promise<PendingDayUser[]>;
runDaily?: (userId: string) => Promise<DailyAlertSearchResult>;
notify?: (result: DailyAlertSearchResult) => Promise<unknown>;
/** Runs once after every user: the renewal reminder sweep for this channel. */
remindRenewal?: () => Promise<unknown>;
prune?: () => Promise<unknown>;
/** Stores what one user's run found. Failures are logged, never fatal. */
recordRun?: (run: SmsAlertSearchRunInput) => Promise<unknown>;
@@ -153,6 +156,9 @@ export async function runSmsAlertSearchSchedulerPass(
input.runDaily ??
((userId: string) => runDailyAlertSearch(userId, now, {}, "sms"));
const notify = input.notify ?? deliverAlertSearchSms;
const remindRenewal =
input.remindRenewal ??
(() => runAlertSearchRenewalSweep({ channel: "sms", now }));
const prune = input.prune ?? defaultPrune;
const recordRun =
input.recordRun ??
@@ -239,6 +245,15 @@ export async function runSmsAlertSearchSchedulerPass(
}
);
try {
await remindRenewal();
} catch (err) {
log.error(
"[SmsAlertSearchScheduler] Renewal sweep failed:",
errorMessage(err)
);
}
log.info(
"[SmsAlertSearchScheduler] Pass completed in",
formatElapsedMs(Date.now() - passStarted) + ",",
+85 -1
View File
@@ -23,6 +23,8 @@ export type AlertAuthorizationBatchRecord = {
notifyMinuteUtc?: number;
/** IANA zone the caller's offset came from. Recorded only; nothing reads it. */
timezone?: string;
/** When this batch's renewal reminder went out; absent until it has. */
renewalRemindedAt?: string;
createdAt: string;
};
@@ -47,6 +49,7 @@ export type PendingDayUser = {
due: boolean;
};
export type AlertAuthorizationJwtInput = {
sequence: number;
day: string;
@@ -61,6 +64,7 @@ type BatchDbRow = {
batch_id: string;
notify_hour_min_utc: string | null;
timezone: string | null;
renewal_reminded_at: string | null;
created_at: string;
};
@@ -100,7 +104,7 @@ function toJwtRecord(row: JwtDbRow): AlertAuthorizationJwtRecord {
}
const BATCH_COLUMNS =
"id, user_id, batch_id, notify_hour_min_utc, timezone, created_at";
"id, user_id, batch_id, notify_hour_min_utc, timezone, renewal_reminded_at, created_at";
/** The stored column value for a batch, or null when it names no hour. */
function storedHourMinute(input: {
@@ -133,6 +137,7 @@ function toBatchRecord(row: BatchDbRow): AlertAuthorizationBatchRecord {
batchId: row.batch_id,
...splitStoredHourMinute(row.notify_hour_min_utc),
timezone: row.timezone ?? undefined,
renewalRemindedAt: row.renewal_reminded_at ?? undefined,
createdAt: row.created_at,
};
}
@@ -317,6 +322,85 @@ export function createAlertAuthorizationStore(tables: AlertAuthorizationTables)
return row.n;
},
/**
* The users whose renewal reminder may go out on `day`: the newest batch is
* unreminded, none of the user's JWTs is dated `day` or later, and the
* batch's hour has arrived (read as in `listPendingForDay`). Status is not
* consulted: an unused JWT for a past day is a run the scheduler skipped
* and will never make up, not reserve.
*
* Dating the test on the JWT days rather than on consumption keeps the
* reminder off the batch's last day, whose digest may still be coming, and
* finds a user with nothing left, whom the daily selection never lists.
*/
async listRenewalCandidates(input: {
day: string;
hourMinute: string;
}): Promise<string[]> {
const rows = getDatabase()
.prepare(
`
WITH latest AS (
SELECT
user_id,
notify_hour_min_utc,
renewal_reminded_at,
ROW_NUMBER() OVER (
PARTITION BY user_id ORDER BY created_at DESC, rowid DESC
) AS rn
FROM ${tables.batches}
)
SELECT latest.user_id AS user_id
FROM latest
WHERE latest.rn = 1
AND latest.renewal_reminded_at IS NULL
AND NOT EXISTS (
SELECT 1 FROM ${tables.jwts} AS jwts
WHERE jwts.user_id = latest.user_id
AND jwts.day >= @day
)
AND (
latest.notify_hour_min_utc IS NULL
OR latest.notify_hour_min_utc NOT GLOB '[0-2][0-9]:[0-5][0-9]'
OR latest.notify_hour_min_utc <= @hourMinute
)
ORDER BY latest.user_id
`
)
.all({ day: input.day, hourMinute: input.hourMinute }) as {
user_id: string;
}[];
return rows.map((row) => row.user_id);
},
/**
* Record that the renewal reminder for this user's newest batch went out.
* Only the newest batch can hold unused JWTs, because an upload replaces
* every unused row, so that is the batch a reminder is about. Returns false
* when there is no batch or it was already marked.
*/
async markRenewalReminded(
userId: string,
at: string = new Date().toISOString()
): Promise<boolean> {
const result = getDatabase()
.prepare(
`
UPDATE ${tables.batches}
SET renewal_reminded_at = ?
WHERE renewal_reminded_at IS NULL
AND id = (
SELECT id FROM ${tables.batches}
WHERE user_id = ?
ORDER BY created_at DESC, rowid DESC
LIMIT 1
)
`
)
.run(at, userId);
return result.changes === 1;
},
/**
* The users this channel has work for on `day`, each flagged with whether
* their chosen hour has arrived. One query for the whole pass: a scheduler
+28
View File
@@ -52,6 +52,10 @@ CREATE TABLE IF NOT EXISTS alert_authorization_batches (
-- DST rules move the offset out from under the stored time.
timezone TEXT,
-- When the renewal reminder for this batch went out, or NULL. Set once per
-- batch by src/alertSearch/renewal.ts, so the reminder cannot repeat.
renewal_reminded_at TEXT,
created_at TEXT NOT NULL
);
@@ -200,6 +204,10 @@ CREATE TABLE IF NOT EXISTS sms_alert_authorization_batches (
-- DST rules move the offset out from under the stored time.
timezone TEXT,
-- When the renewal reminder for this batch went out, or NULL. Set once per
-- batch by src/alertSearch/renewal.ts, so the reminder cannot repeat.
renewal_reminded_at TEXT,
created_at TEXT NOT NULL
);
@@ -364,10 +372,30 @@ function addCursorSearchInputColumns(connection: Database.Database): void {
}
}
/** Batch tables from 0.2.7 and earlier predate the renewal reminder column. */
const BATCH_TABLES = [
"alert_authorization_batches",
"sms_alert_authorization_batches",
];
function addRenewalRemindedColumn(connection: Database.Database): void {
for (const table of BATCH_TABLES) {
const columns = connection
.prepare(`PRAGMA table_info(${table})`)
.all() as Array<{ name: string }>;
if (!columns.some((c) => c.name === "renewal_reminded_at")) {
connection.exec(
`ALTER TABLE ${table} ADD COLUMN renewal_reminded_at TEXT`
);
}
}
}
function ensureSchema(connection: Database.Database): void {
dropIdPathCursorTables(connection);
connection.exec(SCHEMA_SQL);
addCursorSearchInputColumns(connection);
addRenewalRemindedColumn(connection);
}
/**
+2
View File
@@ -31,6 +31,8 @@ export type SmsPhoneLogAction =
| "alert-authorization-deleted"
| "alert-sent"
| "alert-send-failed"
| "renewal-sent"
| "renewal-send-failed"
| "recipient-not-allowed"
| "number-blocked"
| "number-unblocked"
+21 -2
View File
@@ -2,16 +2,34 @@ import { withEnvLabel } from "../env.js";
export const ALERT_SEARCH_FCM_TYPE = "alert_search";
/**
* Why an `alert_search` message was sent, when it is not the daily digest. The
* app routes a tap on any `alert_search` message to New Activity; the reason
* lets a later app version tell a reminder from a digest.
*/
export const ALERT_SEARCH_FCM_REASON_RENEWAL = "renewal";
export type AlertSearchFcmMessage = {
token: string;
notification: { title: string; body: string };
data: { type: typeof ALERT_SEARCH_FCM_TYPE; c?: string };
data: {
type: typeof ALERT_SEARCH_FCM_TYPE;
c?: string;
reason?: typeof ALERT_SEARCH_FCM_REASON_RENEWAL;
};
};
export type AlertSearchFcmContent = {
title: string;
body: string;
cursorParam?: string;
reason?: typeof ALERT_SEARCH_FCM_REASON_RENEWAL;
};
/** The FCM message a tap on the daily New Activity notification carries. */
export function buildAlertSearchFcmMessage(
fcmToken: string,
content: { title: string; body: string; cursorParam?: string }
content: AlertSearchFcmContent
): AlertSearchFcmMessage {
return {
token: fcmToken,
@@ -25,6 +43,7 @@ export function buildAlertSearchFcmMessage(
...(content.cursorParam !== undefined
? { c: content.cursorParam }
: {}),
...(content.reason !== undefined ? { reason: content.reason } : {}),
},
};
}
+2 -1
View File
@@ -3,6 +3,7 @@ import { maskToken } from "../util/maskToken.js";
import {
ALERT_SEARCH_FCM_TYPE,
buildAlertSearchFcmMessage,
type AlertSearchFcmContent,
} from "./alertSearchFcmPayload.js";
import { messaging } from "./firebase.js";
import { log } from "../util/log.js";
@@ -14,7 +15,7 @@ export { ALERT_SEARCH_FCM_TYPE };
*/
export async function sendAlertSearchPushToDevice(
fcmToken: string,
content: { title: string; body: string; cursorParam?: string }
content: AlertSearchFcmContent
): Promise<"sent" | "failed"> {
const suffix = maskToken(fcmToken);
const sendStarted = Date.now();
+368
View File
@@ -0,0 +1,368 @@
import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import {
RENEWAL_PUSH_CONTENT,
RENEWAL_SMS_BODY,
runAlertSearchRenewalSweep,
} from "../../src/alertSearch/renewal.js";
import {
ALERT_SEARCH_SMS_LINK,
SMS_SINGLE_SEGMENT_LIMIT,
} from "../../src/alertSearch/smsNotify.js";
import { alertAuthorizationDb } from "../../src/db/alertAuthorizationSqlite.js";
import { db as fcmDb } from "../../src/db/fcmTokensSqlite.js";
import { smsAlertAuthorizationDb } from "../../src/db/smsAlertAuthorizationSqlite.js";
import { smsBlockedNumbersDb } from "../../src/db/smsBlockedNumbersSqlite.js";
import { smsPhoneLogDb } from "../../src/db/smsPhoneLogSqlite.js";
import { smsRegistrationsDb } from "../../src/db/smsRegistrationsSqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
import { ALERT_SEARCH_FCM_REASON_RENEWAL } from "../../src/services/alertSearchFcmPayload.js";
import type { AlertSearchFcmContent } from "../../src/services/alertSearchFcmPayload.js";
import type { SmsSendResult } from "../../src/services/smsService.js";
import { hashPhoneNumber } from "../../src/util/smsVerificationCode.js";
const USER = "did:ethr:0xrenewal";
const OTHER = "did:ethr:0xrenewalother";
const PHONE = "+15555550123";
const SECRET = "renewal-secret";
/** The last JWT day of a spent batch, and instants on it and after it. */
const LAST_DAY = "2026-09-10";
const LAST_DAY_NOON = new Date(`${LAST_DAY}T12:00:00.000Z`);
const NEXT_DAY = "2026-09-11";
const NEXT_DAY_NOON = new Date(`${NEXT_DAY}T12:00:00.000Z`);
const ENV_KEYS = [
"NOTIFY_DATA_DIR",
"SMS_CODE_SECRET",
"SMS_ALLOWED_RECIPIENT_DIDS",
"SMS_BLOCKED_NUMBERS",
] as const;
let dir: string;
let saved: Record<string, string | undefined>;
beforeEach(async () => {
saved = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
for (const k of ENV_KEYS) delete process.env[k];
dir = await mkdtemp(path.join(tmpdir(), "renewal-"));
process.env.NOTIFY_DATA_DIR = dir;
process.env.SMS_CODE_SECRET = SECRET;
closeDatabase();
});
afterEach(async () => {
closeDatabase();
for (const k of ENV_KEYS) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
await rm(dir, { recursive: true, force: true });
});
/** Consecutive UTC days starting at `first`, one JWT each. */
function jwtsFrom(first: string, count: number) {
const start = Date.parse(`${first}T00:00:00.000Z`);
return Array.from({ length: count }, (_, i) => {
const day = new Date(start + i * 86_400_000).toISOString().slice(0, 10);
return { sequence: i, day, jwt: `jwt-${day}`, nbf: 1, exp: 2 };
});
}
type Store = typeof alertAuthorizationDb;
/** Store a batch whose JWTs run from `first` for `count` days. */
async function storeBatch(
store: Store,
userId: string,
first: string,
count: number,
extra: { batchId?: string; hour?: number; minute?: number } = {}
) {
await store.replaceUnusedBatch({
userId,
batchId: extra.batchId ?? "b1",
notifyHourUtc: extra.hour,
notifyMinuteUtc: extra.minute,
jwts: jwtsFrom(first, count),
});
}
/** Consume the JWT for `day`, as that day's run would. */
async function consume(store: Store, userId: string, day: string) {
const row = await store.getUnusedForDay(userId, day);
assert.ok(row, `no unused JWT for ${day}`);
assert.equal(await store.consumeUnusedJwt({ id: row.id, userId }), true);
}
/** A three-day batch ending on LAST_DAY, every day consumed. */
async function spentBatch(store: Store, userId = USER) {
await storeBatch(store, userId, "2026-09-08", 3);
for (const j of jwtsFrom("2026-09-08", 3)) await consume(store, userId, j.day);
}
async function registerDevice(token: string, userId = USER) {
await fcmDb.upsert({
userId,
deviceId: `dev-${token}`,
fcmToken: token,
platform: "ios",
updatedAt: new Date(),
});
}
async function registerVerifiedPhone(phone = PHONE, userId = USER) {
const now = new Date().toISOString();
await smsRegistrationsDb.upsertPendingCode({
userId,
phoneE164: phone,
codeHash: "hash",
codeExpiresAt: now,
sentAt: now,
});
await smsRegistrationsDb.markVerified(userId, phone);
}
function pushRecorder() {
const sends: Array<{ token: string; content: AlertSearchFcmContent }> = [];
const sendPush = async (token: string, content: AlertSearchFcmContent) => {
sends.push({ token, content });
return "sent" as const;
};
return { sends, sendPush };
}
function smsRecorder(outcome?: SmsSendResult) {
const texts: Array<{ to: string; body: string }> = [];
const sendSms = async (to: string, body: string): Promise<SmsSendResult> => {
texts.push({ to, body });
return outcome ?? { status: "sent", messageId: "SM1" };
};
return { texts, sendSms };
}
const refuse = async () => {
throw new Error("must not send");
};
describe("inventory: listRenewalCandidates", () => {
const query = (day: string, hourMinute = "23:59") =>
alertAuthorizationDb.listRenewalCandidates({ day, hourMinute });
it("lists a user from the day after the batch's last JWT day, not on it", async () => {
await spentBatch(alertAuthorizationDb);
assert.deepEqual(await query(LAST_DAY), []);
assert.deepEqual(await query(NEXT_DAY), [USER]);
assert.deepEqual(await query("2026-12-01"), [USER]);
});
it("ignores status: an unused JWT for a past day is not reserve", async () => {
await storeBatch(alertAuthorizationDb, USER, "2026-09-08", 3);
await consume(alertAuthorizationDb, USER, LAST_DAY); // 08 and 09 never ran
assert.deepEqual(await query(NEXT_DAY), [USER]);
});
it("does not list a user with a JWT dated today or later, used or not", async () => {
await storeBatch(alertAuthorizationDb, USER, LAST_DAY, 2); // 10, 11
assert.deepEqual(await query(NEXT_DAY), []);
await consume(alertAuthorizationDb, USER, LAST_DAY);
await consume(alertAuthorizationDb, USER, NEXT_DAY);
assert.deepEqual(await query(NEXT_DAY), []);
assert.deepEqual(await query("2026-09-12"), [USER]);
});
it("does not list a user who has been reminded, until a fresh upload", async () => {
await spentBatch(alertAuthorizationDb);
await alertAuthorizationDb.markRenewalReminded(USER);
assert.deepEqual(await query(NEXT_DAY), []);
await storeBatch(alertAuthorizationDb, USER, "2026-09-12", 100, { batchId: "b2" });
assert.equal((await alertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, undefined);
assert.deepEqual(await query(NEXT_DAY), []); // the fresh batch has days ahead
assert.deepEqual(await query("2027-01-01"), [USER]); // and runs out in turn
});
it("waits for the batch's hour, and reads an unreadable hour as due", async () => {
await spentBatch(alertAuthorizationDb);
getDatabase()
.prepare(`UPDATE alert_authorization_batches SET notify_hour_min_utc = '18:30' WHERE user_id = ?`)
.run(USER);
assert.deepEqual(await query(NEXT_DAY, "18:29"), []);
assert.deepEqual(await query(NEXT_DAY, "18:30"), [USER]);
await spentBatch(alertAuthorizationDb, OTHER);
getDatabase()
.prepare(`UPDATE alert_authorization_batches SET notify_hour_min_utc = 'soon' WHERE user_id = ?`)
.run(OTHER);
assert.deepEqual(await query(NEXT_DAY, "00:00"), [OTHER]);
});
it("does not list a deleted authorization, and keeps the channels apart", async () => {
await spentBatch(alertAuthorizationDb);
await spentBatch(smsAlertAuthorizationDb, OTHER);
assert.deepEqual(await query(NEXT_DAY), [USER]);
assert.deepEqual(
await smsAlertAuthorizationDb.listRenewalCandidates({ day: NEXT_DAY, hourMinute: "23:59" }),
[OTHER]
);
await alertAuthorizationDb.deleteAllForUser(USER);
assert.deepEqual(await query(NEXT_DAY), []);
});
});
describe("inventory: markRenewalReminded", () => {
it("marks the newest batch once and reports it on getLatestBatch", async () => {
await storeBatch(alertAuthorizationDb, USER, LAST_DAY, 1);
assert.equal((await alertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, undefined);
const at = NEXT_DAY_NOON.toISOString();
assert.equal(await alertAuthorizationDb.markRenewalReminded(USER, at), true);
assert.equal((await alertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, at);
assert.equal(await alertAuthorizationDb.markRenewalReminded(USER), false);
assert.equal(await alertAuthorizationDb.markRenewalReminded("did:ethr:0xnobody"), false);
assert.equal(await smsAlertAuthorizationDb.markRenewalReminded(USER), false);
});
});
describe("renewal reminder wording", () => {
it("push keeps the alert_search tap route and names the reason", () => {
assert.equal(RENEWAL_PUSH_CONTENT.reason, ALERT_SEARCH_FCM_REASON_RENEWAL);
assert.equal(RENEWAL_PUSH_CONTENT.cursorParam, undefined);
assert.match(RENEWAL_PUSH_CONTENT.body, /have ended/);
});
it("text fits one segment and carries the link and the opt-out", () => {
assert.ok(RENEWAL_SMS_BODY.length <= SMS_SINGLE_SEGMENT_LIMIT, `${RENEWAL_SMS_BODY.length} chars`);
assert.ok(RENEWAL_SMS_BODY.includes(ALERT_SEARCH_SMS_LINK));
assert.ok(RENEWAL_SMS_BODY.endsWith("Reply STOP to end."));
});
});
describe("renewal sweep (push)", () => {
it("stays quiet on the last day and reminds every device the day after, once", async () => {
await spentBatch(alertAuthorizationDb);
await registerDevice("tok-a");
await registerDevice("tok-b");
const lastDay = await runAlertSearchRenewalSweep({ channel: "fcm", now: LAST_DAY_NOON, sendPush: refuse });
assert.equal(lastDay.candidates, 0);
const { sends, sendPush } = pushRecorder();
const nextDay = await runAlertSearchRenewalSweep({ channel: "fcm", now: NEXT_DAY_NOON, sendPush });
assert.deepEqual({ candidates: nextDay.candidates, reminded: nextDay.reminded }, { candidates: 1, reminded: 1 });
assert.deepEqual(sends.map((s) => s.token), ["tok-a", "tok-b"]);
assert.equal(sends[0].content, RENEWAL_PUSH_CONTENT);
assert.equal(
(await alertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt,
NEXT_DAY_NOON.toISOString()
);
const dayAfter = await runAlertSearchRenewalSweep({
channel: "fcm",
now: new Date("2026-09-12T12:00:00.000Z"),
sendPush: refuse,
});
assert.equal(dayAfter.candidates, 0);
});
it("finds a user whose inventory ran out long ago and who has no daily run", async () => {
await spentBatch(alertAuthorizationDb);
await registerDevice("tok-a");
const { sends, sendPush } = pushRecorder();
const result = await runAlertSearchRenewalSweep({
channel: "fcm",
now: new Date("2026-11-30T12:00:00.000Z"),
sendPush,
});
assert.equal(result.reminded, 1);
assert.equal(sends.length, 1);
});
it("leaves the batch unmarked when no device took the push, so it retries", async () => {
await spentBatch(alertAuthorizationDb);
await registerDevice("tok-a");
const failed = await runAlertSearchRenewalSweep({
channel: "fcm",
now: NEXT_DAY_NOON,
sendPush: async () => "failed",
});
assert.equal(failed.reminded, 0);
assert.equal(failed.users[0].failed, 1);
assert.equal((await alertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, undefined);
const { sendPush } = pushRecorder();
const retried = await runAlertSearchRenewalSweep({ channel: "fcm", now: NEXT_DAY_NOON, sendPush });
assert.equal(retried.reminded, 1);
});
it("keeps going when one user's reminder throws", async () => {
await spentBatch(alertAuthorizationDb, USER);
await spentBatch(alertAuthorizationDb, OTHER);
const result = await runAlertSearchRenewalSweep({
channel: "fcm",
now: NEXT_DAY_NOON,
listTokens: async (userId) => {
if (userId === USER) throw new Error("token store down");
return ["tok-b"];
},
sendPush: async () => "sent",
});
assert.equal(result.candidates, 2);
assert.equal(result.reminded, 1);
assert.deepEqual(result.users.map((u) => u.userId), [OTHER]);
});
});
describe("renewal sweep (sms)", () => {
it("texts each verified number, logs it, and marks the SMS batch only", async () => {
await spentBatch(smsAlertAuthorizationDb);
await registerVerifiedPhone();
const { texts, sendSms } = smsRecorder();
const result = await runAlertSearchRenewalSweep({ channel: "sms", now: NEXT_DAY_NOON, sendSms });
assert.equal(result.reminded, 1);
assert.deepEqual(texts, [{ to: PHONE, body: RENEWAL_SMS_BODY }]);
const log = await smsPhoneLogDb.listByUserId(USER);
const row = log.find((r) => r.action === "renewal-sent");
assert.ok(row);
assert.equal(row.providerMessageId, "SM1");
assert.equal(row.phoneHash, hashPhoneNumber(PHONE, SECRET));
assert.equal(
(await smsAlertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt,
NEXT_DAY_NOON.toISOString()
);
assert.equal(await alertAuthorizationDb.getLatestBatch(USER), undefined);
});
it("logs a failure, does not mark the batch, and blocks on Twilio 21610", async () => {
await spentBatch(smsAlertAuthorizationDb);
await registerVerifiedPhone();
const { sendSms } = smsRecorder({ status: "failed", error: "unsubscribed", code: 21610 });
const result = await runAlertSearchRenewalSweep({ channel: "sms", now: NEXT_DAY_NOON, sendSms });
assert.equal(result.reminded, 0);
assert.equal(result.users[0].failed, 1);
assert.equal((await smsAlertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, undefined);
const log = await smsPhoneLogDb.listByUserId(USER);
assert.ok(log.some((r) => r.action === "renewal-send-failed"));
assert.ok(await smsBlockedNumbersDb.isBlocked(hashPhoneNumber(PHONE, SECRET)));
});
it("withholds from a blocked number and from a DID off the allowlist", async () => {
await spentBatch(smsAlertAuthorizationDb);
await registerVerifiedPhone();
process.env.SMS_BLOCKED_NUMBERS = PHONE;
const blocked = await runAlertSearchRenewalSweep({ channel: "sms", now: NEXT_DAY_NOON, sendSms: refuse });
assert.deepEqual(
{ sent: blocked.users[0].sent, blocked: blocked.users[0].blocked },
{ sent: 0, blocked: 1 }
);
delete process.env.SMS_BLOCKED_NUMBERS;
process.env.SMS_ALLOWED_RECIPIENT_DIDS = "did:ethr:0xsomeoneelse";
const notAllowed = await runAlertSearchRenewalSweep({ channel: "sms", now: NEXT_DAY_NOON, sendSms: refuse });
assert.deepEqual(
{ sent: notAllowed.users[0].sent, blocked: notAllowed.users[0].blocked },
{ sent: 0, blocked: 1 }
);
assert.equal((await smsAlertAuthorizationDb.getLatestBatch(USER))?.renewalRemindedAt, undefined);
});
});
+42
View File
@@ -314,6 +314,8 @@ describe("alertSearch scheduler pass", () => {
notify: async () => {
notifyCalls.push(1);
},
// Keep the sweep off the real FCM sender; this test is about the run count.
remindRenewal: async () => undefined,
});
assert.deepEqual(ran, [USER_A]);
assert.equal(notifyCalls.length, 1);
@@ -397,6 +399,46 @@ describe("alertSearch scheduler pass", () => {
});
});
describe("alertSearch scheduler renewal sweep", () => {
it("runs the sweep once, after every user's digest", async () => {
const order: string[] = [];
const result = await runAlertSearchSchedulerPass({
concurrency: 1,
listPending: async () => [
{ userId: USER_A, due: true },
{ userId: USER_B, due: true },
],
runDaily: async (userId) => eligibleDaily(userId),
notify: async (daily) => {
order.push(`notify:${daily.userId}`);
},
remindRenewal: async () => {
order.push("sweep");
},
});
assert.deepEqual(order, [`notify:${USER_A}`, `notify:${USER_B}`, "sweep"]);
assert.equal(result.failed, 0);
assert.equal(result.attempted, 2);
});
it("runs the sweep even with nobody due, and its failure does not fail the pass", async () => {
let swept = 0;
const result = await runAlertSearchSchedulerPass({
concurrency: 1,
listPending: async () => [],
runDaily: async (userId) => stubDailyResult(userId),
notify: async () => undefined,
remindRenewal: async () => {
swept += 1;
throw new Error("FCM down");
},
});
assert.equal(swept, 1);
assert.equal(result.skipped, false);
assert.equal(result.failed, 0);
});
});
describe("alertSearch scheduler isolation from FCM", () => {
it("does not reference the retired WAKEUP_PING producer", () => {
const alertSched = readFileSync(
+19
View File
@@ -87,6 +87,25 @@ describe("runSmsAlertSearchSchedulerPass", () => {
assert.equal(result.skipped, false);
});
it("runs the renewal sweep once after every text, and its failure is not a user failure", async () => {
const order: string[] = [];
const result = await runSmsAlertSearchSchedulerPass({
concurrency: 1,
listPending: async () => due([USER, OTHER]),
runDaily: async (userId) => daily(userId),
notify: async (input) => {
order.push(`notify:${input.userId}`);
},
remindRenewal: async () => {
order.push("sweep");
throw new Error("Twilio down");
},
prune: async () => undefined,
});
assert.deepEqual(order, [`notify:${USER}`, `notify:${OTHER}`, "sweep"]);
assert.equal(result.failed, 0);
});
it("lists users from the SMS batches, not the FCM ones", async () => {
await smsAlertAuthorizationDb.replaceUnusedBatch({
userId: USER,