Merge pull request 'Add push:watch diagnostic tool' (#7) from push-watch-tool into master

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-11-18 09:32:08 +00:00
31 changed files with 8489 additions and 41 deletions
+11
View File
@@ -31,6 +31,17 @@
# "production" everywhere. Unset or blank appends nothing.
# MESSAGE_ENV_LABEL=test
# Diagnostic test push (pnpm push:test). Test and production share one Firebase
# project, so a matching project_id does not distinguish them. It only confirms
# the loaded service account. A send also requires MESSAGE_ENV_LABEL=test
# above. There is no production override. Leave these unset in production.
# PUSH_TEST_FIREBASE_PROJECT_ID=
# Only the exact string true enables a send. TRUE, 1, and "true " do not.
# PUSH_DIAGNOSTIC_TEST_SEND_ENABLED=true
# One recipient. The command's --did and --device-id must match exactly.
# PUSH_DIAGNOSTIC_ALLOWED_DID=did:example:one
# PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID=phone-1
# --- SMS notifications (/notify-sms) ---
# Master switch. While false, every /notify-sms route returns 503 SMS_DISABLED
+4 -1
View File
@@ -15,7 +15,10 @@
"twilio:whoami": "tsx scripts/twilio-whoami.ts",
"sms:blocks": "tsx scripts/sms-blocks.ts",
"alert-search:tap": "tsx scripts/alert-search-tap.ts",
"sms:watch": "tsx src/tools/smsWatch.ts"
"sms:watch": "tsx src/tools/smsWatch.ts",
"push:watch": "tsx src/tools/pushWatch.ts",
"push:preview": "tsx src/tools/pushPreview.ts",
"push:test": "tsx src/tools/pushTest.ts"
},
"dependencies": {
"@peculiar/asn1-ecc": "^2.7.0",
+3 -3
View File
@@ -65,13 +65,13 @@ if (matches.length !== 1) {
}
const row = matches[0];
const status = await sendAlertSearchPushToDevice(row.fcmToken, {
const outcome = await sendAlertSearchPushToDevice(row.fcmToken, {
title: TITLE,
body: BODY,
});
console.log(
JSON.stringify({
status,
status: outcome.status,
deviceId: row.deviceId,
platform: row.platform,
tokenSuffix: maskToken(row.fcmToken),
@@ -80,4 +80,4 @@ console.log(
dataType: "alert_search",
})
);
process.exit(status === "sent" ? 0 : 1);
process.exit(outcome.status === "sent" ? 0 : 1);
+137 -15
View File
@@ -1,8 +1,20 @@
import {
alertPushDeviceSendsDb,
type AlertPushDeviceSendInput,
} from "../db/alertPushDeviceSendsSqlite.js";
import { alertPushSendsDb } from "../db/alertPushSendsSqlite.js";
import { db } from "../db/fcmTokensSqlite.js";
import { pushSameDayResendDids } from "../env.js";
import { utcCalendarDay } from "../services/alertAuthorization.js";
import { errorMessage } from "../util/formatElapsed.js";
import {
FCM_ERROR_CODE_MAX_CHARS,
FCM_ERROR_DETAIL_MAX_CHARS,
FCM_MESSAGE_ID_MAX_CHARS,
formatFcmFailureLog,
sanitizeFcmDiagnostic,
type FcmSendResult,
} from "../util/fcmOutcome.js";
import {
ALERT_SEARCH_UPDATE_CATEGORIES,
countAlertSearchUpdates,
@@ -26,16 +38,24 @@ export type AlertSearchNotificationContent = {
cursorParam?: string;
};
/** A plain status, or the FCM message id / error captured from that send. */
export type AlertSearchSendOutcome = "sent" | "failed" | FcmSendResult;
export type AlertSearchTokenSender = (
fcmToken: string,
content: AlertSearchNotificationContent
) => Promise<"sent" | "failed">;
) => Promise<AlertSearchSendOutcome>;
export type AlertSearchNotifyDeps = {
listTokens?: (userId: string) => Promise<string[]>;
send?: AlertSearchTokenSender;
/** Cap window uses this instant's UTC day. Tests pass a fixed clock. */
now?: Date;
/**
* Writes the per-device diagnostic rows. A failure here is logged and does
* not change the send result or the daily-cap row. Tests can make it throw.
*/
recordDeviceAttempts?: (rows: readonly AlertPushDeviceSendInput[]) => Promise<void>;
};
export type AlertSearchNotifyResult = {
@@ -145,23 +165,90 @@ export function isAlertSearchNotificationEligible(
);
}
/** This identity's registered FCM tokens, each once. */
export async function listRegisteredTokens(userId: string): Promise<string[]> {
type AttemptedDevice = {
token: string;
deviceId: string;
platform: string;
};
/**
* Registrations actually eligible for a send, one per distinct token.
* The first row returned for a token wins, matching `getByUserId` order.
* A later row that repeats the token is not attempted.
*/
export async function listRegisteredDevices(userId: string): Promise<AttemptedDevice[]> {
const rows = await db.getByUserId(userId);
const seen = new Set<string>();
const tokens: string[] = [];
const devices: AttemptedDevice[] = [];
for (const row of rows) {
if (seen.has(row.fcmToken)) continue;
seen.add(row.fcmToken);
tokens.push(row.fcmToken);
devices.push({ token: row.fcmToken, deviceId: row.deviceId, platform: row.platform });
}
return tokens;
return devices;
}
/** This identity's registered FCM tokens, each once. */
export async function listRegisteredTokens(userId: string): Promise<string[]> {
return (await listRegisteredDevices(userId)).map((row) => row.token);
}
/**
* Map an overridden token list back onto the registration that owns each
* token. The same first-row-wins rule decides which device id is logged.
* A token with no registration is still sent, and is not logged: there is
* no device row to attach.
*/
async function resolveListedTokens(
userId: string,
tokens: readonly string[]
): Promise<Array<{ token: string; deviceId: string | null; platform: string | null }>> {
const rows = await db.getByUserId(userId);
const byToken = new Map<string, { deviceId: string; platform: string }>();
for (const row of rows) {
if (byToken.has(row.fcmToken)) continue;
byToken.set(row.fcmToken, { deviceId: row.deviceId, platform: row.platform });
}
return tokens.map((token) => {
const row = byToken.get(token);
return {
token,
deviceId: row?.deviceId ?? null,
platform: row?.platform ?? null,
};
});
}
function asOutcome(value: AlertSearchSendOutcome): FcmSendResult {
if (value === "sent" || value === "failed") return { status: value };
return value;
}
function cleanField(value: string | undefined, token: string, maxChars: number): string | undefined {
if (value === undefined) return undefined;
const text = sanitizeFcmDiagnostic(value, token, maxChars);
return text.length === 0 ? undefined : text;
}
/** Keep success ids and failure text, and strip the token that was sent. */
function persistedOutcome(outcome: FcmSendResult, token: string): FcmSendResult {
if (outcome.status === "sent") {
const messageId = cleanField(outcome.messageId, token, FCM_MESSAGE_ID_MAX_CHARS);
return messageId === undefined ? { status: "sent" } : { status: "sent", messageId };
}
const errorCode = cleanField(outcome.errorCode, token, FCM_ERROR_CODE_MAX_CHARS);
const errorDetail = cleanField(outcome.errorDetail, token, FCM_ERROR_DETAIL_MAX_CHARS);
return {
status: "failed",
...(errorCode === undefined ? {} : { errorCode }),
...(errorDetail === undefined ? {} : { errorDetail }),
};
}
async function defaultSend(
fcmToken: string,
content: AlertSearchNotificationContent
): Promise<"sent" | "failed"> {
): Promise<FcmSendResult> {
const { sendAlertSearchPushToDevice } = await import(
"../services/pushService.js"
);
@@ -208,29 +295,64 @@ export async function deliverAlertSearchNotification(
return { eligible: true, sent: 0, failed: 0, capped: true };
}
const listTokens = deps.listTokens ?? listRegisteredTokens;
const send = deps.send ?? defaultSend;
const tokens = await listTokens(result.userId);
const targets = deps.listTokens
? await resolveListedTokens(result.userId, await deps.listTokens(result.userId))
: await listRegisteredDevices(result.userId);
let sent = 0;
let failed = 0;
const attemptedAt = now.toISOString();
const attempts: AlertPushDeviceSendInput[] = [];
for (const token of tokens) {
for (const target of targets) {
let outcome: FcmSendResult;
try {
const status = await send(token, content);
if (status === "sent") sent += 1;
outcome = persistedOutcome(asOutcome(await send(target.token, content)), target.token);
if (outcome.status === "sent") sent += 1;
else failed += 1;
} catch (err) {
failed += 1;
log.error("[AlertSearchNotify] Send threw:", errorMessage(err));
log.error("[AlertSearchNotify] Send threw:", formatFcmFailureLog(err, target.token));
outcome = persistedOutcome(
{ status: "failed", errorDetail: errorMessage(err) },
target.token
);
}
if (target.deviceId === null) continue;
attempts.push({
userId: result.userId,
deviceId: target.deviceId,
platform: target.platform,
pushSendId: null,
attemptedAt,
outcome: outcome.status,
fcmMessageId: outcome.messageId ?? null,
fcmErrorCode: outcome.errorCode ?? null,
errorDetail: outcome.errorDetail ?? null,
});
}
// The DID-level row is still written only when Firebase accepted at least
// one device, and only after those sends. Device rows do not count toward
// the cap. A diagnostic-write failure must not undo this row or resend.
let pushSendId: string | null = null;
if (sent > 0) {
await alertPushSendsDb.append({
pushSendId = await alertPushSendsDb.append({
userId: result.userId,
batchId: result.batchId,
sentAt: now.toISOString(),
sentAt: attemptedAt,
});
}
if (pushSendId !== null) {
for (const attempt of attempts) attempt.pushSendId = pushSendId;
}
if (attempts.length > 0) {
const record = deps.recordDeviceAttempts ?? ((rows) => alertPushDeviceSendsDb.appendMany(rows));
try {
await record(attempts);
} catch (err) {
log.error("[AlertSearchNotify] Per-device outcome log failed:", errorMessage(err));
}
}
return { eligible: true, sent, failed };
}
+2 -1
View File
@@ -111,7 +111,8 @@ async function deliverPush(
const { sendAlertSearchPushToDevice } = await import(
"../services/pushService.js"
);
return sendAlertSearchPushToDevice(token, c);
const outcome = await sendAlertSearchPushToDevice(token, c);
return outcome.status;
});
let sent = 0;
let failed = 0;
+166 -10
View File
@@ -1,7 +1,14 @@
import { randomUUID } from "node:crypto";
import {
alertAuthorizationDb,
type PendingDayUser,
} from "../db/alertAuthorizationSqlite.js";
import {
ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS,
alertSearchSchedulerHistoryDb,
type AlertSearchSchedulerHistory,
type SchedulerRenewalStatus,
} from "../db/alertSearchSchedulerHistorySqlite.js";
import {
utcCalendarDay,
utcHourMinute,
@@ -17,6 +24,12 @@ import {
} from "./daily.js";
import { deliverAlertSearchNotification } from "./notify.js";
import { runAlertSearchRenewalSweep } from "./renewal.js";
import {
deferredRunRow,
dueRunRow,
safeErrorCode,
type DueRunOutcome,
} from "./schedulerHistory.js";
import { log } from "../util/log.js";
/** Independent of the FCM wakeup interval; does not share that timer. */
@@ -43,6 +56,11 @@ export type AlertSearchSchedulerPassInput = {
now?: Date;
/** Users worked on at once. Set to 1 for a deterministic order in tests. */
concurrency?: number;
/**
* Where pass and per-user diagnostics are written. A write that fails is
* logged and never changes the pass, its result, or a user's run.
*/
history?: AlertSearchSchedulerHistory;
};
export type AlertSearchSchedulerPassResult = {
@@ -58,6 +76,22 @@ export type AlertSearchSchedulerPassResult = {
let intervalId: ReturnType<typeof setInterval> | undefined;
let passInFlight = false;
/** History is diagnostics: a failed write is logged and nothing else changes. */
async function writeHistory(
label: string,
write: () => Promise<unknown>
): Promise<void> {
try {
await write();
} catch (err) {
log.error(
"[AlertSearchScheduler] History write failed",
label + ":",
errorMessage(err)
);
}
}
export function isAlertSearchSchedulerPassInFlight(): boolean {
return passInFlight;
}
@@ -71,13 +105,34 @@ export function isAlertSearchSchedulerPassInFlight(): boolean {
export async function runAlertSearchSchedulerPass(
input: AlertSearchSchedulerPassInput = {}
): Promise<AlertSearchSchedulerPassResult> {
const history = input.history ?? alertSearchSchedulerHistoryDb;
if (passInFlight) {
log.info("[AlertSearchScheduler] Pass skipped (already in flight)");
const skippedAt = input.now ?? new Date();
const skippedSec = Math.floor(skippedAt.getTime() / 1000);
await writeHistory("skipped pass", () =>
history.startPass({
id: randomUUID(),
startedAt: new Date().toISOString(),
utcDay: utcCalendarDay(skippedSec),
hourMinute: utcHourMinute(skippedSec),
status: "skipped",
})
);
return { skipped: true, userIds: [], attempted: 0, failed: 0, deferred: 0 };
}
passInFlight = true;
const passStarted = Date.now();
const passId = randomUUID();
/** Where a pass-level failure came from, for the history row. */
let stage = "pass";
// Outside the try so a pass-level failure can record the counts so far.
// A notification error increments notificationErrors, never failed.
let attempted = 0;
let failed = 0;
let notificationErrors = 0;
log.info("[AlertSearchScheduler] Pass started");
try {
@@ -94,49 +149,136 @@ export async function runAlertSearchSchedulerPass(
alertAuthorizationDb.listPendingForDay(query));
const nowSec = Math.floor(now.getTime() / 1000);
const pending = await listPending({
day: utcCalendarDay(nowSec),
hourMinute: utcHourMinute(nowSec),
});
const day = utcCalendarDay(nowSec);
const hourMinute = utcHourMinute(nowSec);
await writeHistory("pass start", () =>
history.startPass({
id: passId,
startedAt: new Date(passStarted).toISOString(),
utcDay: day,
hourMinute,
})
);
// Best-effort housekeeping: a failed prune never stops the pass.
await writeHistory("prune", () =>
history.pruneOlderThan(
new Date(
Date.now() -
ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS * 24 * 60 * 60 * 1000
).toISOString()
)
);
stage = "list_pending";
const pending = await listPending({ day, hourMinute });
stage = "pass";
const userIds = pending.filter((row) => row.due).map((row) => row.userId);
const deferred = pending.length - userIds.length;
let failed = 0;
await writeHistory("pending counts", () =>
history.recordPendingCounts(passId, {
pendingUsers: pending.length,
dueUsers: userIds.length,
deferredUsers: deferred,
})
);
const deferredAt = new Date().toISOString();
await writeHistory("deferred users", () =>
history.appendRuns(
pending
.filter((row) => !row.due)
.map((row) =>
deferredRunRow({
passId,
userId: row.userId,
utcDay: day,
recordedAt: deferredAt,
})
)
)
);
await forEachWithConcurrency(
userIds,
input.concurrency ?? ALERT_SEARCH_USER_CONCURRENCY,
async (userId) => {
attempted += 1;
const startedAt = new Date().toISOString();
const userStarted = Date.now();
let outcome: DueRunOutcome;
try {
const daily = await runDaily(userId);
let notified: unknown;
let notifyErrorCode: string | undefined;
try {
await notify(daily);
notified = await notify(daily);
} catch (err) {
notificationErrors += 1;
notifyErrorCode = safeErrorCode(err);
log.error(
"[AlertSearchScheduler] Notification failed",
userId + ":",
errorMessage(err)
"notify",
notifyErrorCode
);
}
outcome = { dailyThrew: false, daily, notified, notifyErrorCode };
} catch (err) {
failed += 1;
const errorCode = safeErrorCode(err);
outcome = { dailyThrew: true, errorCode };
log.error(
"[AlertSearchScheduler] User failed",
userId + ":",
errorMessage(err)
"daily_run",
errorCode
);
}
await writeHistory("user run", () =>
history.appendRuns([
dueRunRow(
{
passId,
userId,
utcDay: day,
recordedAt: new Date().toISOString(),
startedAt,
elapsedMs: Date.now() - userStarted,
},
outcome
),
])
);
}
);
let renewalStatus: SchedulerRenewalStatus = "ok";
let renewalErrorCode: string | undefined;
try {
await remindRenewal();
} catch (err) {
renewalStatus = "failed";
renewalErrorCode = safeErrorCode(err);
log.error(
"[AlertSearchScheduler] Renewal sweep failed:",
errorMessage(err)
"renewal",
renewalErrorCode
);
}
await writeHistory("pass finish", () =>
history.finishPass(passId, {
finishedAt: new Date().toISOString(),
status: "completed",
attemptedUsers: attempted,
failedUsers: failed,
notificationErrors,
renewalStatus,
renewalErrorCode,
})
);
log.info(
"[AlertSearchScheduler] Pass completed in",
formatElapsedMs(Date.now() - passStarted) + ",",
@@ -150,10 +292,24 @@ export async function runAlertSearchSchedulerPass(
deferred,
};
} catch (err) {
const errorCode = safeErrorCode(err);
log.error(
"[AlertSearchScheduler] Pass failed in",
formatElapsedMs(Date.now() - passStarted) + ":",
errorMessage(err)
stage,
errorCode
);
await writeHistory("pass failure", () =>
history.finishPass(passId, {
finishedAt: new Date().toISOString(),
status: "failed",
attemptedUsers: attempted,
failedUsers: failed,
notificationErrors,
renewalStatus: "not_run",
errorStage: stage,
errorCode,
})
);
throw err;
} finally {
+126
View File
@@ -0,0 +1,126 @@
import type { SchedulerRunInput } from "../db/alertSearchSchedulerHistorySqlite.js";
import type { DailyAlertSearchResult } from "./daily.js";
/**
* Pure helpers that turn what the FCM scheduler saw into a history row.
* Nothing here reads the digest records, JWTs, cursors, or exception
* messages. A row keeps source outcome values, counts, and safe codes.
*/
/** The reason listPendingForDay marks a user not due. */
export const DEFER_REASON_NOTIFY_HOUR = "notify_hour_not_reached";
/** What a thrown value can say about itself without printing its message. */
export function safeErrorCode(err: unknown): string {
if (typeof err === "object" && err !== null) {
const code = (err as { code?: unknown }).code;
if (typeof code === "string" && /^[A-Za-z0-9_.:\/-]{1,64}$/.test(code)) return code;
const name = (err as { name?: unknown }).name;
if (typeof name === "string" && /^[A-Za-z0-9_.:\/-]{1,64}$/.test(name)) return name;
}
return "unknown";
}
export type NotifyCounts = {
eligible: boolean;
sent: number;
failed: number;
capped: boolean;
};
/**
* The counts deliverAlertSearchNotification returns. The scheduler's notify
* hook is typed `unknown`, so anything else means no readable result.
*/
export function readNotifyResult(value: unknown): NotifyCounts | undefined {
if (value === null || typeof value !== "object") return undefined;
const v = value as { eligible?: unknown; sent?: unknown; failed?: unknown; capped?: unknown };
if (typeof v.eligible !== "boolean") return undefined;
if (typeof v.sent !== "number" || typeof v.failed !== "number") return undefined;
return {
eligible: v.eligible,
sent: v.sent,
failed: v.failed,
capped: v.capped === true,
};
}
export type DeferredRunBase = {
passId: string;
userId: string;
utcDay: string;
recordedAt: string;
};
export function deferredRunRow(base: DeferredRunBase): SchedulerRunInput {
return {
...base,
due: false,
status: "deferred",
reason: DEFER_REASON_NOTIFY_HOUR,
};
}
export type DueRunBase = DeferredRunBase & {
startedAt: string;
elapsedMs: number;
};
export type DueRunOutcome =
| { dailyThrew: true; errorCode: string }
| {
dailyThrew: false;
daily: DailyAlertSearchResult;
/** What the notifier returned; unknown because the hook is untyped. */
notified?: unknown;
/** A safe code when the notifier threw. */
notifyErrorCode?: string;
};
/**
* One row for a due user, after its daily run and notification step.
* Status follows what each step actually returned, in this order: the daily
* run threw, was incomplete, completed without consuming its JWT, then the
* notification result. A notifier error is recorded as the error stage even
* when an earlier daily status decides the status.
*/
export function dueRunRow(base: DueRunBase, outcome: DueRunOutcome): SchedulerRunInput {
const row: SchedulerRunInput = { ...base, due: true, status: "unknown" };
if (outcome.dailyThrew) {
row.status = "daily_threw";
row.errorStage = "daily_run";
row.errorCode = outcome.errorCode;
return row;
}
const daily = outcome.daily;
row.dailyCompleted = daily.completed;
row.jwtConsumed = daily.consumed;
row.endorserOutcome = daily.endorserOutcome;
row.partnerOutcome = daily.partnerOutcome;
row.digestTotalCount = daily.digest?.totalCount ?? null;
const counts = readNotifyResult(outcome.notified);
if (counts !== undefined) {
row.notifyEligible = counts.eligible;
row.notifySent = counts.sent;
row.notifyFailed = counts.failed;
row.notifyCapped = counts.capped;
}
if (outcome.notifyErrorCode !== undefined) {
row.errorStage = "notify";
row.errorCode = outcome.notifyErrorCode;
}
if (!daily.completed) row.status = "daily_incomplete";
else if (!daily.consumed) row.status = "daily_not_consumed";
else if (outcome.notifyErrorCode !== undefined) row.status = "notify_threw";
else if (counts === undefined) row.status = "notify_result_unavailable";
else if (!counts.eligible) row.status = "notify_ineligible";
else if (counts.capped) row.status = "notify_capped";
else if (counts.sent > 0) row.status = "fcm_accepted";
else if (counts.failed > 0) row.status = "fcm_all_rejected";
else row.status = "no_devices";
return row;
}
+97
View File
@@ -0,0 +1,97 @@
import { randomUUID } from "node:crypto";
import {
FCM_ERROR_CODE_MAX_CHARS,
FCM_ERROR_DETAIL_MAX_CHARS,
FCM_MESSAGE_ID_MAX_CHARS,
sanitizeFcmDiagnostic,
} from "../util/fcmOutcome.js";
import { ALERT_PUSH_SEND_RETENTION_DAYS } from "./alertPushSendsSqlite.js";
import { getDatabase } from "./sqlite.js";
/**
* One attempted device in an AlertSearch digest send.
* `outcome` is `sent` when Firebase accepted the message and `failed` when it
* rejected it. This is not the daily-cap row and not a display receipt.
* `pushSendId` is the `alert_push_sends` id when that DID-level row was
* written, and null when every attempted device failed.
*/
export type AlertPushDeviceSendInput = {
userId: string;
deviceId: string;
platform: string | null;
pushSendId: string | null;
attemptedAt: string;
outcome: "sent" | "failed";
fcmMessageId: string | null;
fcmErrorCode: string | null;
errorDetail: string | null;
/**
* `alert_search` is a normal digest attempt. `push_watch_test` is one
* diagnostic send. Omitted means `alert_search`, including every caller
* that predates the column.
*/
kind?: "alert_search" | "push_watch_test";
};
function storedText(value: string | null, maxChars: number): string | null {
if (value === null) return null;
const text = sanitizeFcmDiagnostic(value, undefined, maxChars);
return text.length === 0 ? null : text;
}
/** Per-device FCM outcomes for AlertSearch digest sends. Not used by the daily cap. */
export const alertPushDeviceSendsDb = {
/**
* Record the devices that were actually included in one send.
* Uses the same `ALERT_PUSH_SEND_RETENTION_DAYS` window as `alert_push_sends`.
* This write drops only these DIDs' rows older than that window. Another DID's
* old rows stay until that DID is written again. There is no background cleanup.
* Callers must already have removed registration tokens from the text fields.
*/
async appendMany(rows: readonly AlertPushDeviceSendInput[]): Promise<void> {
if (rows.length === 0) return;
const db = getDatabase();
const latestByUser = new Map<string, string>();
for (const row of rows) {
const current = latestByUser.get(row.userId);
if (current === undefined || row.attemptedAt > current) {
latestByUser.set(row.userId, row.attemptedAt);
}
}
db.transaction(() => {
const insert = db.prepare(
`INSERT INTO alert_push_device_sends (
id, user_id, device_id, platform, push_send_id, attempted_at,
outcome, fcm_message_id, fcm_error_code, error_detail, kind
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
);
for (const row of rows) {
const outcome = row.outcome === "sent" ? "sent" : "failed";
insert.run(
randomUUID(),
row.userId,
row.deviceId,
storedText(row.platform, FCM_ERROR_DETAIL_MAX_CHARS),
row.pushSendId,
row.attemptedAt,
outcome,
outcome === "sent" ? storedText(row.fcmMessageId, FCM_MESSAGE_ID_MAX_CHARS) : null,
outcome === "failed" ? storedText(row.fcmErrorCode, FCM_ERROR_CODE_MAX_CHARS) : null,
outcome === "failed" ? storedText(row.errorDetail, FCM_ERROR_DETAIL_MAX_CHARS) : null,
row.kind === "push_watch_test" ? "push_watch_test" : "alert_search"
);
}
const prune = db.prepare(
`DELETE FROM alert_push_device_sends WHERE user_id = ? AND attempted_at < ?`
);
for (const [userId, attemptedAt] of latestByUser) {
const parsed = Date.parse(attemptedAt);
if (Number.isNaN(parsed)) continue;
const cutoff = new Date(
parsed - ALERT_PUSH_SEND_RETENTION_DAYS * 24 * 60 * 60 * 1000
).toISOString();
prune.run(userId, cutoff);
}
})();
},
};
+14 -4
View File
@@ -1,31 +1,41 @@
import { randomUUID } from "node:crypto";
import { getDatabase } from "./sqlite.js";
/** How long send rows are kept. The cap reads only the current UTC day. */
/**
* Shared retention for `alert_push_sends` and `alert_push_device_sends`.
* A write drops only that DID's rows older than this many days. Pruning is
* lazy: a DID with no later write can keep older rows. There is no scheduled
* global cleanup. The daily cap reads only the current UTC day.
*/
export const ALERT_PUSH_SEND_RETENTION_DAYS = 30;
/** Push-channel alerts sent, per identity: the push twin of SMS `alert-sent` rows. */
export const alertPushSendsDb = {
/** Record a send, and drop this identity's rows past the retention window. */
/**
* Record a DID-level send, and drop this DID's own rows older than
* `ALERT_PUSH_SEND_RETENTION_DAYS`. Other DIDs are left as they are.
*/
async append(input: {
userId: string;
batchId?: string | null;
sentAt?: string;
}): Promise<void> {
}): Promise<string> {
const sentAt = input.sentAt ?? new Date().toISOString();
const cutoff = new Date(
Date.parse(sentAt) - ALERT_PUSH_SEND_RETENTION_DAYS * 24 * 60 * 60 * 1000
).toISOString();
const id = randomUUID();
const db = getDatabase();
db.transaction(() => {
db.prepare(
`INSERT INTO alert_push_sends (id, user_id, batch_id, sent_at)
VALUES (?, ?, ?, ?)`
).run(randomUUID(), input.userId, input.batchId ?? null, sentAt);
).run(id, input.userId, input.batchId ?? null, sentAt);
db.prepare(
`DELETE FROM alert_push_sends WHERE user_id = ? AND sent_at < ?`
).run(input.userId, cutoff);
})();
return id;
},
/** Sends to this identity at or after `sinceIso`. */
+217
View File
@@ -0,0 +1,217 @@
import { randomUUID } from "node:crypto";
import { getDatabase } from "./sqlite.js";
/**
* How long scheduler history is kept. Pruning is global by age, run by the
* scheduler at the start of each pass. This is separate from the per-DID
* retention of `alert_push_sends` and `alert_push_device_sends`.
*/
export const ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS = 30;
export type SchedulerPassStatus = "running" | "completed" | "failed" | "skipped";
export type SchedulerRenewalStatus = "ok" | "failed" | "not_run";
export type SchedulerPassStartInput = {
id: string;
startedAt: string;
utcDay: string;
hourMinute: string;
/** `skipped` is a pass refused by the in-flight guard; it is finished at once. */
status?: "running" | "skipped";
};
export type SchedulerPassQueryInput = {
pendingUsers: number;
dueUsers: number;
deferredUsers: number;
};
export type SchedulerPassFinishInput = {
finishedAt: string;
status: "completed" | "failed";
attemptedUsers: number;
failedUsers: number;
notificationErrors: number;
renewalStatus: SchedulerRenewalStatus;
renewalErrorCode?: string;
errorStage?: string;
errorCode?: string;
};
/** One user returned by listPendingForDay. Only counts and source statuses. */
export type SchedulerRunInput = {
passId: string;
userId: string;
utcDay: string;
due: boolean;
status: string;
reason?: string;
recordedAt: string;
startedAt?: string;
elapsedMs?: number;
dailyCompleted?: boolean;
jwtConsumed?: boolean;
endorserOutcome?: string | null;
partnerOutcome?: string | null;
digestTotalCount?: number | null;
notifyEligible?: boolean;
notifySent?: number;
notifyFailed?: number;
notifyCapped?: boolean;
errorStage?: string;
errorCode?: string;
};
const LABEL_MAX_CHARS = 64;
/**
* A short code-like label. Anything with spaces or other punctuation is not a
* status or error code, so it is dropped instead of stored.
*/
export function safeLabel(value: string | null | undefined): string | null {
if (value === null || value === undefined) return null;
const text = value.trim();
if (text.length === 0 || text.length > LABEL_MAX_CHARS) return null;
return /^[A-Za-z0-9_.:\/-]+$/.test(text) ? text : null;
}
function boolColumn(value: boolean | undefined): number | null {
return value === undefined ? null : value ? 1 : 0;
}
function countColumn(value: number | null | undefined): number | null {
if (value === null || value === undefined) return null;
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : null;
}
/** Pass and per-user history for the FCM AlertSearch scheduler. */
export const alertSearchSchedulerHistoryDb = {
async startPass(input: SchedulerPassStartInput): Promise<void> {
const skipped = input.status === "skipped";
getDatabase()
.prepare(
`INSERT INTO alert_search_scheduler_passes (
id, started_at, finished_at, status, utc_day, hour_minute
) VALUES (?, ?, ?, ?, ?, ?)`
)
.run(
input.id,
input.startedAt,
skipped ? input.startedAt : null,
skipped ? "skipped" : "running",
input.utcDay,
input.hourMinute
);
},
async recordPendingCounts(id: string, counts: SchedulerPassQueryInput): Promise<void> {
getDatabase()
.prepare(
`UPDATE alert_search_scheduler_passes
SET pending_users = ?, due_users = ?, deferred_users = ?
WHERE id = ?`
)
.run(
countColumn(counts.pendingUsers),
countColumn(counts.dueUsers),
countColumn(counts.deferredUsers),
id
);
},
async finishPass(id: string, input: SchedulerPassFinishInput): Promise<void> {
getDatabase()
.prepare(
`UPDATE alert_search_scheduler_passes
SET finished_at = ?, status = ?, attempted_users = ?, failed_users = ?,
notification_errors = ?, renewal_status = ?, renewal_error_code = ?,
error_stage = ?, error_code = ?
WHERE id = ?`
)
.run(
input.finishedAt,
input.status,
countColumn(input.attemptedUsers),
countColumn(input.failedUsers),
countColumn(input.notificationErrors),
input.renewalStatus,
safeLabel(input.renewalErrorCode),
safeLabel(input.errorStage),
safeLabel(input.errorCode),
id
);
},
/** Insert the given user rows in one transaction. */
async appendRuns(runs: readonly SchedulerRunInput[]): Promise<void> {
if (runs.length === 0) return;
const db = getDatabase();
db.transaction(() => {
const insert = db.prepare(
`INSERT INTO alert_search_scheduler_runs (
id, pass_id, user_id, utc_day, due, status, reason, recorded_at,
started_at, elapsed_ms, daily_completed, jwt_consumed,
endorser_outcome, partner_outcome, digest_total_count,
notify_eligible, notify_sent, notify_failed, notify_capped,
error_stage, error_code
) VALUES (
@id, @pass_id, @user_id, @utc_day, @due, @status, @reason, @recorded_at,
@started_at, @elapsed_ms, @daily_completed, @jwt_consumed,
@endorser_outcome, @partner_outcome, @digest_total_count,
@notify_eligible, @notify_sent, @notify_failed, @notify_capped,
@error_stage, @error_code
)`
);
for (const run of runs) {
insert.run({
id: randomUUID(),
pass_id: run.passId,
user_id: run.userId,
utc_day: run.utcDay,
due: run.due ? 1 : 0,
status: safeLabel(run.status) ?? "unknown",
reason: safeLabel(run.reason),
recorded_at: run.recordedAt,
started_at: run.startedAt ?? null,
elapsed_ms: countColumn(run.elapsedMs),
daily_completed: boolColumn(run.dailyCompleted),
jwt_consumed: boolColumn(run.jwtConsumed),
endorser_outcome: safeLabel(run.endorserOutcome),
partner_outcome: safeLabel(run.partnerOutcome),
digest_total_count: countColumn(run.digestTotalCount),
notify_eligible: boolColumn(run.notifyEligible),
notify_sent: countColumn(run.notifySent),
notify_failed: countColumn(run.notifyFailed),
notify_capped: boolColumn(run.notifyCapped),
error_stage: safeLabel(run.errorStage),
error_code: safeLabel(run.errorCode),
});
}
})();
},
/**
* Delete passes that started before `cutoffIso` and every user row that
* belongs to them, in one transaction. User rows recorded before the cutoff
* go too, so a run whose pass row was never written cannot linger. Returns
* the number of passes removed.
*/
async pruneOlderThan(cutoffIso: string): Promise<number> {
const db = getDatabase();
let passes = 0;
db.transaction(() => {
db.prepare(
`DELETE FROM alert_search_scheduler_runs
WHERE pass_id IN (
SELECT id FROM alert_search_scheduler_passes WHERE started_at < ?
) OR recorded_at < ?`
).run(cutoffIso, cutoffIso);
passes = db
.prepare(`DELETE FROM alert_search_scheduler_passes WHERE started_at < ?`)
.run(cutoffIso).changes;
})();
return passes;
},
};
export type AlertSearchSchedulerHistory = typeof alertSearchSchedulerHistoryDb;
+10
View File
@@ -163,6 +163,16 @@ export const db = {
return rows.map(toStoredRow);
},
/** Every registration stored under this device id, whichever DID owns it. */
async listByDeviceId(deviceId: string): Promise<StoredRow[]> {
const rows = getDatabase()
.prepare(
`SELECT ${ROW_COLUMNS} FROM fcm_registrations WHERE device_id = ?`
)
.all(deviceId) as DbRow[];
return rows.map(toStoredRow);
},
async getByDeviceId(
userId: string,
deviceId: string
+127
View File
@@ -333,6 +333,118 @@ CREATE TABLE IF NOT EXISTS alert_push_sends (
CREATE INDEX IF NOT EXISTS idx_alert_push_sends_user_sent
ON alert_push_sends (user_id, sent_at);
-- One row per registered device included in an AlertSearch FCM send.
-- outcome is sent when Firebase accepted the message and failed when it
-- rejected it. Neither means the device displayed the notification.
-- push_send_id matches alert_push_sends when that DID-level row exists
-- (at least one device was accepted). It is null when every device failed,
-- because no DID-level row is written in that case. It is also null for a
-- diagnostic test push, which never writes alert_push_sends. The daily cap
-- does not read this table. Tokens, JWTs, and notification bodies are not stored.
-- kind is alert_search for a normal digest attempt and push_watch_test for a
-- diagnostic send. Existing rows default to alert_search.
CREATE TABLE IF NOT EXISTS alert_push_device_sends (
id TEXT PRIMARY KEY NOT NULL,
user_id TEXT NOT NULL,
device_id TEXT NOT NULL,
platform TEXT,
push_send_id TEXT,
attempted_at TEXT NOT NULL,
outcome TEXT NOT NULL,
fcm_message_id TEXT,
fcm_error_code TEXT,
error_detail TEXT,
kind TEXT NOT NULL DEFAULT 'alert_search'
);
CREATE INDEX IF NOT EXISTS idx_alert_push_device_sends_user_attempted
ON alert_push_device_sends (user_id, attempted_at);
CREATE INDEX IF NOT EXISTS idx_alert_push_device_sends_device_attempted
ON alert_push_device_sends (device_id, attempted_at);
-- One row per FCM AlertSearch scheduler pass, including a pass that found no
-- users and a pass skipped because another was in flight. Read by
-- src/tools/pushWatch.ts. Diagnostics only: no digest records, JWTs, tokens,
-- cursors, or exception messages. Pruned by age with its runs.
-- status: running (not finished) | completed | failed | skipped
-- renewal_status: ok | failed | not_run
CREATE TABLE IF NOT EXISTS alert_search_scheduler_passes (
id TEXT PRIMARY KEY NOT NULL,
started_at TEXT NOT NULL,
finished_at TEXT,
status TEXT NOT NULL,
-- The UTC day and HH:MM the pending-user query used.
utc_day TEXT NOT NULL,
hour_minute TEXT NOT NULL,
-- NULL until the pending-user query returns.
pending_users INTEGER,
due_users INTEGER,
deferred_users INTEGER,
-- Due users whose work started, daily runs that threw, and notifications
-- that threw (these two do not both count toward the scheduler's failed).
attempted_users INTEGER,
failed_users INTEGER,
notification_errors INTEGER,
renewal_status TEXT,
renewal_error_code TEXT,
-- A safe stage and error code for a pass that failed. Never a message.
error_stage TEXT,
error_code TEXT
);
CREATE INDEX IF NOT EXISTS idx_alert_search_scheduler_passes_started
ON alert_search_scheduler_passes (started_at);
-- One row per user listPendingForDay returned in a pass, due or deferred.
-- pass_id matches alert_search_scheduler_passes.id; the project keeps no
-- foreign keys, so pruning deletes both tables in one transaction.
-- status names the most specific outcome (for example deferred,
-- daily_threw, daily_incomplete, notify_capped, fcm_accepted).
-- fcm_accepted means Firebase accepted at least one device, not that the
-- notification was displayed.
CREATE TABLE IF NOT EXISTS alert_search_scheduler_runs (
id TEXT PRIMARY KEY NOT NULL,
pass_id TEXT NOT NULL,
user_id TEXT NOT NULL,
utc_day TEXT NOT NULL,
due INTEGER NOT NULL,
status TEXT NOT NULL,
reason TEXT,
recorded_at TEXT NOT NULL,
-- Due users only.
started_at TEXT,
elapsed_ms INTEGER,
-- NULL when the daily run threw before returning a result.
daily_completed INTEGER,
jwt_consumed INTEGER,
endorser_outcome TEXT,
partner_outcome TEXT,
digest_total_count INTEGER,
-- NULL when the notifier threw or returned no readable result.
notify_eligible INTEGER,
notify_sent INTEGER,
notify_failed INTEGER,
notify_capped INTEGER,
error_stage TEXT,
error_code TEXT
);
CREATE INDEX IF NOT EXISTS idx_alert_search_scheduler_runs_pass
ON alert_search_scheduler_runs (pass_id);
CREATE INDEX IF NOT EXISTS idx_alert_search_scheduler_runs_user_recorded
ON alert_search_scheduler_runs (user_id, recorded_at);
`;
let database: Database.Database | null = null;
@@ -378,6 +490,20 @@ const BATCH_TABLES = [
"sms_alert_authorization_batches",
];
/** Device-attempt rows from before diagnostic sends have no kind column. */
function addDeviceSendKindColumn(connection: Database.Database): void {
const columns = connection
.prepare(`PRAGMA table_info(alert_push_device_sends)`)
.all() as Array<{ name: string }>;
if (columns.length === 0) return;
if (!columns.some((column) => column.name === "kind")) {
connection.exec(
`ALTER TABLE alert_push_device_sends
ADD COLUMN kind TEXT NOT NULL DEFAULT 'alert_search'`
);
}
}
function addRenewalRemindedColumn(connection: Database.Database): void {
for (const table of BATCH_TABLES) {
const columns = connection
@@ -396,6 +522,7 @@ function ensureSchema(connection: Database.Database): void {
connection.exec(SCHEMA_SQL);
addCursorSearchInputColumns(connection);
addRenewalRemindedColumn(connection);
addDeviceSendKindColumn(connection);
}
/**
+328
View File
@@ -0,0 +1,328 @@
import { randomUUID } from "node:crypto";
import { alertPushDeviceSendsDb, type AlertPushDeviceSendInput } from "../db/alertPushDeviceSendsSqlite.js";
import { db, type StoredRow } from "../db/fcmTokensSqlite.js";
import { listRegisteredDevices } from "../alertSearch/notify.js";
import {
FCM_MESSAGE_ID_MAX_CHARS,
fcmFailureParts,
sanitizeFcmDiagnostic,
type FcmSendResult,
} from "../util/fcmOutcome.js";
import { maskToken } from "../util/maskToken.js";
import { log } from "../util/log.js";
import {
SHARED_FIREBASE_PROJECT_NOTE,
describeDiagnosticSendSafety,
formatDiagnosticEnvironment,
resolveDiagnosticAllowlist,
resolveDiagnosticEnvironment,
resolveDiagnosticSendOptIn,
type DiagnosticEnvironmentResult,
} from "./pushDiagnosticEnv.js";
import {
buildPushDiagnosticContent,
buildPushDiagnosticMessage,
formatPushDiagnosticPayload,
type PushDiagnosticContent,
type PushDiagnosticMessage,
} from "./pushDiagnosticPayload.js";
/** Same shape as pushWatch's maskFcmToken: a short prefix and suffix, never the middle. */
export function maskRegistrationToken(token: string): string {
if (token.length === 0) return "(empty)";
const prefix = 4;
const suffix = 4;
if (token.length < prefix + suffix + 8) return "********";
const stars = "*".repeat(Math.min(token.length - prefix - suffix, 12));
return `${token.slice(0, prefix)}${stars}${token.slice(-suffix)}`;
}
export type DiagnosticRegistrationDecision =
| { ok: true; row: StoredRow }
| { ok: false; reason: "missing" | "inactive" | "ambiguous" | "other_did"; detail: string };
/**
* Choose the one registration named by both the DID and the device id.
* A device id owned by someone else is not a match. More than one row for
* the pair is ambiguous. A blank token cannot be sent to.
*/
export function decideDiagnosticRegistration(
userId: string,
deviceId: string,
rows: readonly StoredRow[]
): DiagnosticRegistrationDecision {
const owned = rows.filter((row) => row.userId === userId && row.deviceId === deviceId);
if (owned.length > 1) {
return {
ok: false,
reason: "ambiguous",
detail: `Found ${owned.length} registrations for this DID and device id. Nothing was sent.`,
};
}
if (owned.length === 0) {
if (rows.length > 0) {
return {
ok: false,
reason: "other_did",
detail: "That device id is registered to a different DID. Nothing was sent.",
};
}
return {
ok: false,
reason: "missing",
detail: "No registration exists for this DID and device id. Nothing was sent.",
};
}
const row = owned[0];
if (row.fcmToken.trim().length === 0) {
return {
ok: false,
reason: "inactive",
detail: "The registration has no FCM token. Nothing was sent.",
};
}
return { ok: true, row };
}
export async function selectDiagnosticRegistration(
userId: string,
deviceId: string
): Promise<DiagnosticRegistrationDecision> {
const rows = await db.listByDeviceId(deviceId);
return decideDiagnosticRegistration(userId, deviceId, rows);
}
export type PushPreview = {
userId: string;
registrationCount: number;
eligible: Array<{ deviceId: string; platform: string; fcmToken: string }>;
content: PushDiagnosticContent;
environment: DiagnosticEnvironmentResult;
report: string;
};
/**
* Describe the diagnostic notification for one DID. Does not send, and does
* not initialize Firebase. Eligible devices are the ones an AlertSearch send
* would attempt: one row per distinct non-empty token.
*/
export async function previewDiagnosticPush(input: {
userId: string;
now?: Date;
env?: NodeJS.ProcessEnv;
}): Promise<PushPreview> {
const now = input.now ?? new Date();
const environment = resolveDiagnosticEnvironment(input.env);
const registrations = await db.getByUserId(input.userId);
const eligible = (await listRegisteredDevices(input.userId))
.filter((device) => device.token.trim().length > 0)
.map((device) => ({
deviceId: device.deviceId,
platform: device.platform,
fcmToken: device.token,
}))
.sort((a, b) => (a.deviceId < b.deviceId ? -1 : a.deviceId > b.deviceId ? 1 : 0));
const content = buildPushDiagnosticContent({
id: randomUUID(),
at: now.toISOString(),
});
const skipped = registrations.length - eligible.length;
const lines = [
"Preview only. No FCM message was sent.",
...formatDiagnosticEnvironment(environment),
...describeDiagnosticSendSafety(input.env ?? process.env, input.userId),
`DID: ${input.userId}`,
`Registrations: ${registrations.length}`,
`Eligible devices: ${eligible.length}`,
];
if (eligible.length === 0) {
lines.push("No eligible FCM registration for this DID.");
}
for (const device of eligible) {
lines.push(
` ${device.deviceId} ${device.platform} token ${maskRegistrationToken(device.fcmToken)}`
);
}
if (skipped > 0) {
lines.push(
`${skipped} registration${skipped === 1 ? "" : "s"} not eligible (empty token, or a token already listed).`
);
}
lines.push(
"Diagnostic notification that would be sent:",
...formatPushDiagnosticPayload(content),
"data.id and data.at are assigned when the message is built. A later send builds its own.",
"This is not an alert_search message. Tapping it does not open New Activity."
);
return {
userId: input.userId,
registrationCount: registrations.length,
eligible,
content,
environment,
report: lines.join("\n"),
};
}
export type DiagnosticSendOutcome =
| {
ok: false;
stage: "safety" | "environment" | "registration";
report: string;
}
| {
ok: true;
status: "sent" | "failed";
report: string;
diagnosticId: string;
attemptedAt: string;
history: "recorded" | "failed";
message: PushDiagnosticMessage;
};
async function defaultSend(message: PushDiagnosticMessage): Promise<string> {
const { messaging } = await import("./firebase.js");
return String(await messaging.send(message));
}
function failureCode(err: unknown): string {
const code = fcmFailureParts(err).errorCode;
return code ?? "unknown";
}
function safetyRefusal(reason: string): DiagnosticSendOutcome {
return {
ok: false,
stage: "safety",
report: `${reason}\nNothing was sent.\n${SHARED_FIREBASE_PROJECT_NOTE}`,
};
}
/**
* Send one diagnostic notification to the one allowlisted registration.
* Checks run in this order, and any failure returns before Firebase is called:
* the exact opt-in flag, the test label and service-account consistency, the
* single-recipient allowlist, then the database registration. A history-write
* failure is logged and does not send again.
*/
export async function runDiagnosticSend(input: {
userId: string;
deviceId: string;
now?: Date;
env?: NodeJS.ProcessEnv;
send?: (message: PushDiagnosticMessage) => Promise<string>;
record?: (row: AlertPushDeviceSendInput) => Promise<void>;
select?: (userId: string, deviceId: string) => Promise<DiagnosticRegistrationDecision>;
onBeforeSend?: (info: {
environment: Extract<DiagnosticEnvironmentResult, { ok: true }>;
content: PushDiagnosticContent;
row: StoredRow;
}) => void;
}): Promise<DiagnosticSendOutcome> {
const env = input.env ?? process.env;
const optIn = resolveDiagnosticSendOptIn(env);
if (!optIn.ok) return safetyRefusal(optIn.reason);
const environment = resolveDiagnosticEnvironment(env);
if (!environment.ok) {
return {
ok: false,
stage: "environment",
report: formatDiagnosticEnvironment(environment).join("\n"),
};
}
const allowed = resolveDiagnosticAllowlist(env, {
userId: input.userId,
deviceId: input.deviceId,
});
if (!allowed.ok) return safetyRefusal(allowed.reason);
const selected = await (input.select ?? selectDiagnosticRegistration)(allowed.userId, allowed.deviceId);
if (!selected.ok) {
return { ok: false, stage: "registration", report: selected.detail };
}
const now = input.now ?? new Date();
const content = buildPushDiagnosticContent({
id: randomUUID(),
at: now.toISOString(),
});
const message = buildPushDiagnosticMessage(selected.row.fcmToken, content);
input.onBeforeSend?.({ environment, content, row: selected.row });
const sender = input.send ?? defaultSend;
let outcome: FcmSendResult;
const suffix = maskToken(selected.row.fcmToken);
log.info("[PushDiagnostic] Send attempt, token suffix:", suffix);
try {
const rawId = await sender(message);
const messageId = sanitizeFcmDiagnostic(String(rawId), selected.row.fcmToken, FCM_MESSAGE_ID_MAX_CHARS);
outcome = messageId.length === 0 ? { status: "sent" } : { status: "sent", messageId };
log.info("[PushDiagnostic] Send completed, token suffix:", suffix);
} catch (err) {
outcome = { status: "failed", ...fcmFailureParts(err, selected.row.fcmToken) };
// The code only. The exception text can carry the token, a JWT, or a URL.
log.error(
"[PushDiagnostic] Send failed, token suffix:",
suffix + ":",
outcome.errorCode ?? "unknown"
);
}
const record = input.record ?? ((row) => alertPushDeviceSendsDb.appendMany([row]));
let history: "recorded" | "failed" = "recorded";
try {
await record({
userId: selected.row.userId,
deviceId: selected.row.deviceId,
platform: selected.row.platform,
pushSendId: null,
attemptedAt: content.data.at,
outcome: outcome.status,
fcmMessageId: outcome.messageId ?? null,
fcmErrorCode: outcome.errorCode ?? null,
// The FCM message text is not stored. It can contain a token or a credential.
errorDetail: null,
kind: "push_watch_test",
});
} catch (err) {
history = "failed";
log.error("[PushDiagnostic] History write failed:", failureCode(err));
}
const lines = [
`Outcome: ${outcome.status === "sent" ? "accepted by FCM" : "rejected by FCM"}`,
`DID: ${selected.row.userId}`,
`Device: ${selected.row.deviceId}`,
`Platform: ${selected.row.platform}`,
`Token: ${maskRegistrationToken(selected.row.fcmToken)}`,
`FCM message id: ${outcome.messageId ?? "-"}`,
`FCM error code: ${outcome.errorCode ?? "-"}`,
...formatPushDiagnosticPayload(content),
history === "recorded"
? "Device-attempt history: recorded as a diagnostic test. alert_push_sends was not written."
: "Device-attempt history was not recorded. The FCM result stands; nothing was sent again.",
];
return {
ok: true,
status: outcome.status,
report: lines.join("\n"),
diagnosticId: content.data.id,
attemptedAt: content.data.at,
history,
message,
};
}
/** `--name value` or `--name=value`. An empty value is treated as absent. */
export function readFlag(argv: readonly string[], name: string): string | undefined {
const prefix = `${name}=`;
const inline = argv.find((arg) => arg.startsWith(prefix));
if (inline !== undefined) {
const value = inline.slice(prefix.length).trim();
return value.length > 0 ? value : undefined;
}
const index = argv.indexOf(name);
if (index === -1) return undefined;
const value = argv[index + 1]?.trim();
if (value === undefined || value.length === 0 || value.startsWith("--")) return undefined;
return value;
}
+295
View File
@@ -0,0 +1,295 @@
/**
* Whether this process may send a diagnostic test push.
*
* Test and production use the same Firebase project. A matching project_id
* only checks that the service account is the project named by
* PUSH_TEST_FIREBASE_PROJECT_ID. It does not distinguish test from production.
*
* A send also requires MESSAGE_ENV_LABEL to be exactly `test`,
* PUSH_DIAGNOSTIC_TEST_SEND_ENABLED to be the exact string `true`, and the
* command's DID and device id to match the single configured recipient.
* There is no production override. This module does not change the process
* environment or the Firebase app.
*/
export const PUSH_DIAGNOSTIC_TEST_LABEL = "test";
export const PUSH_TEST_FIREBASE_PROJECT_ENV = "PUSH_TEST_FIREBASE_PROJECT_ID";
export const PUSH_DIAGNOSTIC_TEST_SEND_ENABLED_ENV = "PUSH_DIAGNOSTIC_TEST_SEND_ENABLED";
export const PUSH_DIAGNOSTIC_ALLOWED_DID_ENV = "PUSH_DIAGNOSTIC_ALLOWED_DID";
export const PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID_ENV = "PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID";
/** Printed wherever a project id is shown, so it is not read as an isolation boundary. */
export const SHARED_FIREBASE_PROJECT_NOTE =
"Test and production share this Firebase project. A project_id match checks the configured service account only. It does not distinguish test from production. The diagnostic send is limited to one allowlisted DID and device id.";
const PRODUCTION_LABELS = new Set(["production", "prod"]);
export type DiagnosticCredentialSource =
| "FIREBASE_SERVICE_ACCOUNT_JSON"
| "application_default"
| "missing"
| "unreadable";
export type DiagnosticEnvironmentObserved = {
label: string | null;
firebaseProjectId: string | null;
expectedProjectId: string | null;
credential: DiagnosticCredentialSource;
};
export type DiagnosticEnvironmentOk = {
ok: true;
label: typeof PUSH_DIAGNOSTIC_TEST_LABEL;
firebaseProjectId: string;
credential: "FIREBASE_SERVICE_ACCOUNT_JSON";
};
export type DiagnosticEnvironmentRefusal = {
ok: false;
/** production: the deployment is the production one. unknown: a fact is missing. ambiguous: the facts disagree. */
classification: "production" | "unknown" | "ambiguous";
reason: string;
observed: DiagnosticEnvironmentObserved;
};
export type DiagnosticEnvironmentResult = DiagnosticEnvironmentOk | DiagnosticEnvironmentRefusal;
function trimmed(value: string | undefined): string | null {
if (value === undefined) return null;
const text = value.trim();
return text.length > 0 ? text : null;
}
function projectIdFromAccount(account: unknown): string | null {
if (typeof account !== "object" || account === null) return null;
const record = account as { project_id?: unknown; projectId?: unknown };
if (typeof record.project_id === "string" && record.project_id.trim().length > 0) {
return record.project_id.trim();
}
if (typeof record.projectId === "string" && record.projectId.trim().length > 0) {
return record.projectId.trim();
}
return null;
}
function readCredential(env: NodeJS.ProcessEnv): {
credential: DiagnosticCredentialSource;
firebaseProjectId: string | null;
} {
const json = env.FIREBASE_SERVICE_ACCOUNT_JSON;
if (json === undefined || json.trim().length === 0) {
return {
credential: env.GOOGLE_APPLICATION_CREDENTIALS ? "application_default" : "missing",
firebaseProjectId: null,
};
}
try {
return {
credential: "FIREBASE_SERVICE_ACCOUNT_JSON",
firebaseProjectId: projectIdFromAccount(JSON.parse(json)),
};
} catch {
return { credential: "unreadable", firebaseProjectId: null };
}
}
function refuse(
classification: DiagnosticEnvironmentRefusal["classification"],
reason: string,
observed: DiagnosticEnvironmentObserved
): DiagnosticEnvironmentRefusal {
return { ok: false, classification, reason, observed };
}
/**
* Read the label and confirm the service account's project_id matches the
* configured project. Test and production share that project, so this is a
* consistency check, not a test-versus-production boundary. Does not
* initialize Firebase and does not include the service-account key.
*/
export function resolveDiagnosticEnvironment(
env: NodeJS.ProcessEnv = process.env
): DiagnosticEnvironmentResult {
const label = trimmed(env.MESSAGE_ENV_LABEL);
const expectedProjectId = trimmed(env[PUSH_TEST_FIREBASE_PROJECT_ENV]);
const credential = readCredential(env);
const observed: DiagnosticEnvironmentObserved = {
label,
firebaseProjectId: credential.firebaseProjectId,
expectedProjectId,
credential: credential.credential,
};
if (label === null) {
return refuse(
"production",
"MESSAGE_ENV_LABEL is unset. Production leaves it unset, so a diagnostic send is refused.",
observed
);
}
if (PRODUCTION_LABELS.has(label.toLowerCase())) {
return refuse(
"production",
`MESSAGE_ENV_LABEL is "${label}", which is a production label.`,
observed
);
}
if (label !== PUSH_DIAGNOSTIC_TEST_LABEL) {
return refuse(
"ambiguous",
`MESSAGE_ENV_LABEL is "${label}". A diagnostic send requires it to be exactly "${PUSH_DIAGNOSTIC_TEST_LABEL}".`,
observed
);
}
if (credential.credential === "missing" || credential.credential === "application_default") {
return refuse(
"unknown",
"FIREBASE_SERVICE_ACCOUNT_JSON is unset, so the Firebase project cannot be verified. Application Default Credentials are not accepted for a diagnostic send.",
observed
);
}
if (credential.credential === "unreadable") {
return refuse(
"unknown",
"FIREBASE_SERVICE_ACCOUNT_JSON is not valid JSON, so the Firebase project cannot be verified.",
observed
);
}
if (credential.firebaseProjectId === null) {
return refuse(
"unknown",
"The Firebase service account has no project_id, so the project cannot be verified.",
observed
);
}
if (expectedProjectId === null) {
return refuse(
"unknown",
`${PUSH_TEST_FIREBASE_PROJECT_ENV} is unset, so the service-account project cannot be checked for consistency.`,
observed
);
}
if (credential.firebaseProjectId !== expectedProjectId) {
return refuse(
"ambiguous",
`Firebase project "${credential.firebaseProjectId}" does not match ${PUSH_TEST_FIREBASE_PROJECT_ENV} "${expectedProjectId}".`,
observed
);
}
return {
ok: true,
label: PUSH_DIAGNOSTIC_TEST_LABEL,
firebaseProjectId: credential.firebaseProjectId,
credential: "FIREBASE_SERVICE_ACCOUNT_JSON",
};
}
export function formatDiagnosticEnvironment(result: DiagnosticEnvironmentResult): string[] {
if (result.ok) {
return [
"Environment label: test",
`Firebase project: ${result.firebaseProjectId}`,
SHARED_FIREBASE_PROJECT_NOTE,
"Credential: FIREBASE_SERVICE_ACCOUNT_JSON",
];
}
const observed = result.observed;
return [
`Diagnostic send refused (${result.classification}): ${result.reason}`,
`Observed label: ${observed.label ?? "(unset)"}`,
`Observed Firebase project: ${observed.firebaseProjectId ?? "(unverified)"}`,
`Expected configured project: ${observed.expectedProjectId ?? "(unset)"}`,
`Credential source: ${observed.credential}`,
SHARED_FIREBASE_PROJECT_NOTE,
];
}
export type DiagnosticSafetyResult =
| { ok: true }
| { ok: false; reason: string };
/** The opt-in is the exact string `true`. `TRUE`, `1`, and `true ` do not count. */
export function resolveDiagnosticSendOptIn(
env: NodeJS.ProcessEnv = process.env
): DiagnosticSafetyResult {
const raw = env[PUSH_DIAGNOSTIC_TEST_SEND_ENABLED_ENV];
if (raw === "true") return { ok: true };
if (raw === undefined || raw.length === 0) {
return {
ok: false,
reason: `${PUSH_DIAGNOSTIC_TEST_SEND_ENABLED_ENV} is unset. A diagnostic send requires the exact string true.`,
};
}
return {
ok: false,
reason: `${PUSH_DIAGNOSTIC_TEST_SEND_ENABLED_ENV} is not the exact string true. A diagnostic send is refused.`,
};
}
export type DiagnosticAllowlistResult =
| { ok: true; userId: string; deviceId: string }
| { ok: false; reason: string };
/**
* One configured recipient. The command values must equal it. A blank value,
* a list, or a wildcard is not a match.
*/
export function resolveDiagnosticAllowlist(
env: NodeJS.ProcessEnv,
requested: { userId: string; deviceId: string }
): DiagnosticAllowlistResult {
const userId = trimmed(env[PUSH_DIAGNOSTIC_ALLOWED_DID_ENV]);
const deviceId = trimmed(env[PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID_ENV]);
if (userId === null) {
return { ok: false, reason: `${PUSH_DIAGNOSTIC_ALLOWED_DID_ENV} is unset or empty. Nothing was sent.` };
}
if (deviceId === null) {
return {
ok: false,
reason: `${PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID_ENV} is unset or empty. Nothing was sent.`,
};
}
if (requested.userId !== userId) {
return {
ok: false,
reason: `The requested DID does not match ${PUSH_DIAGNOSTIC_ALLOWED_DID_ENV}. Nothing was sent.`,
};
}
if (requested.deviceId !== deviceId) {
return {
ok: false,
reason: `The requested device id does not match ${PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID_ENV}. Nothing was sent.`,
};
}
return { ok: true, userId, deviceId };
}
/** What preview can say about the send gate. It does not include tokens. */
export function describeDiagnosticSendSafety(
env: NodeJS.ProcessEnv,
previewDid?: string
): string[] {
const optIn = resolveDiagnosticSendOptIn(env);
const userId = trimmed(env[PUSH_DIAGNOSTIC_ALLOWED_DID_ENV]);
const deviceId = trimmed(env[PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID_ENV]);
const lines = [
optIn.ok
? "Diagnostic send opt-in: enabled"
: "Diagnostic send opt-in: not enabled. push:preview does not send, and push:test would refuse.",
userId === null ? "Allowlisted DID: (unset)" : `Allowlisted DID: ${userId}`,
deviceId === null ? "Allowlisted device id: (unset)" : `Allowlisted device id: ${deviceId}`,
];
if (previewDid !== undefined) {
lines.push(
userId === null
? "Preview DID matches the allowlist: no allowlist"
: `Preview DID matches the allowlist: ${userId === previewDid ? "yes" : "no"}`
);
}
lines.push(
optIn.ok && userId !== null && deviceId !== null
? "Diagnostic-send safety configuration: complete for one DID and one device id"
: "Diagnostic-send safety configuration: incomplete"
);
return lines;
}
+66
View File
@@ -0,0 +1,66 @@
import { withEnvLabel } from "../env.js";
/**
* Data type for a diagnostic test notification. The app opens New Activity
* only when `data.type` is `alert_search`, so this type is displayed and
* otherwise ignored.
*/
export const PUSH_WATCH_TEST_TYPE = "push_watch_test";
export const PUSH_DIAGNOSTIC_TITLE = "TimeSafari Push Test";
export const PUSH_DIAGNOSTIC_BODY = "This is a diagnostic notification.";
export type PushDiagnosticContent = {
title: string;
body: string;
data: {
type: typeof PUSH_WATCH_TEST_TYPE;
id: string;
at: string;
};
};
/** Notification plus data, without a device token. Preview and send both use this. */
export function buildPushDiagnosticContent(input: {
id: string;
at: string;
}): PushDiagnosticContent {
return {
title: PUSH_DIAGNOSTIC_TITLE,
body: withEnvLabel(PUSH_DIAGNOSTIC_BODY),
data: {
type: PUSH_WATCH_TEST_TYPE,
id: input.id,
at: input.at,
},
};
}
/** The Firebase Admin message for one device. `data.type` is never `alert_search`. */
export type PushDiagnosticMessage = {
token: string;
notification: { title: string; body: string };
data: PushDiagnosticContent["data"];
};
export function buildPushDiagnosticMessage(
token: string,
content: PushDiagnosticContent
): PushDiagnosticMessage {
return {
token,
notification: { title: content.title, body: content.body },
data: content.data,
};
}
/** The payload fields safe to print. The device token is not one of them. */
export function formatPushDiagnosticPayload(content: PushDiagnosticContent): string[] {
return [
`Title: ${content.title}`,
`Body: ${content.body}`,
`Data type: ${content.data.type}`,
`Data id: ${content.data.id}`,
`Data at: ${content.data.at}`,
];
}
+18 -6
View File
@@ -1,4 +1,11 @@
import { errorMessage, formatElapsedMs } from "../util/formatElapsed.js";
import { formatElapsedMs } from "../util/formatElapsed.js";
import {
FCM_MESSAGE_ID_MAX_CHARS,
fcmFailureParts,
formatFcmFailureLog,
sanitizeFcmDiagnostic,
type FcmSendResult,
} from "../util/fcmOutcome.js";
import { maskToken } from "../util/maskToken.js";
import {
ALERT_SEARCH_FCM_TYPE,
@@ -9,35 +16,40 @@ import { messaging } from "./firebase.js";
import { log } from "../util/log.js";
export { ALERT_SEARCH_FCM_TYPE };
export type { FcmSendResult };
/**
* User-visible AlertSearch digest push.
* `sent` means Firebase accepted the message. It does not mean the device
* displayed it. The message id and error fields are safe to persist.
*/
export async function sendAlertSearchPushToDevice(
fcmToken: string,
content: AlertSearchFcmContent
): Promise<"sent" | "failed"> {
): Promise<FcmSendResult> {
const suffix = maskToken(fcmToken);
const sendStarted = Date.now();
log.info("[AlertSearchPush] Send attempt, token suffix:", suffix);
try {
await messaging.send(buildAlertSearchFcmMessage(fcmToken, content));
const rawId = await messaging.send(buildAlertSearchFcmMessage(fcmToken, content));
const messageId = sanitizeFcmDiagnostic(String(rawId), fcmToken, FCM_MESSAGE_ID_MAX_CHARS);
log.info(
"[AlertSearchPush] Send completed in",
formatElapsedMs(Date.now() - sendStarted) + ",",
"token suffix:",
suffix
);
return "sent";
return messageId.length === 0 ? { status: "sent" } : { status: "sent", messageId };
} catch (err) {
const failure = fcmFailureParts(err, fcmToken);
log.error(
"[AlertSearchPush] Send failed in",
formatElapsedMs(Date.now() - sendStarted) + ",",
"token suffix:",
suffix + ":",
errorMessage(err)
formatFcmFailureLog(err, fcmToken)
);
return "failed";
return { status: "failed", ...failure };
}
}
+19
View File
@@ -0,0 +1,19 @@
/**
* Show the diagnostic notification that would be sent to a DID.
* Does not send, does not initialize Firebase, and does not write the database.
*
* pnpm push:preview -- --did <DID>
*/
import "../env.js";
import { previewDiagnosticPush, readFlag } from "../services/pushDiagnostic.js";
const did = readFlag(process.argv.slice(2), "--did");
if (did === undefined) {
console.error("Usage: pnpm push:preview -- --did <DID>");
console.error("A DID is required. Nothing was sent.");
process.exit(1);
}
const preview = await previewDiagnosticPush({ userId: did });
console.log(preview.report);
process.exit(0);
+48
View File
@@ -0,0 +1,48 @@
/**
* Send one diagnostic notification to one registration of one DID.
* Test and production share a Firebase project, so project_id is only a
* consistency check. The send also requires MESSAGE_ENV_LABEL=test,
* PUSH_DIAGNOSTIC_TEST_SEND_ENABLED=true, and an exact match with
* PUSH_DIAGNOSTIC_ALLOWED_DID and PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID.
* There is no production override. Does not write alert_push_sends.
*
* pnpm push:test -- --did <DID> --device-id <DEVICE_ID>
*/
import "../env.js";
import { maskRegistrationToken, readFlag, runDiagnosticSend } from "../services/pushDiagnostic.js";
import { formatDiagnosticEnvironment } from "../services/pushDiagnosticEnv.js";
import { formatPushDiagnosticPayload } from "../services/pushDiagnosticPayload.js";
const argv = process.argv.slice(2);
const did = readFlag(argv, "--did");
const deviceId = readFlag(argv, "--device-id");
if (did === undefined || deviceId === undefined) {
console.error("Usage: pnpm push:test -- --did <DID> --device-id <DEVICE_ID>");
console.error("Both a DID and a device id are required. Nothing was sent.");
process.exit(1);
}
const outcome = await runDiagnosticSend({
userId: did,
deviceId,
onBeforeSend(info) {
console.log(
[
...formatDiagnosticEnvironment(info.environment),
`DID: ${info.row.userId}`,
`Device: ${info.row.deviceId}`,
`Platform: ${info.row.platform}`,
`Token: ${maskRegistrationToken(info.row.fcmToken)}`,
...formatPushDiagnosticPayload(info.content),
"Sending one diagnostic notification to this device.",
].join("\n")
);
},
});
if (!outcome.ok || outcome.status !== "sent") {
console.error(outcome.report);
process.exit(1);
}
console.log(outcome.report);
process.exit(0);
File diff suppressed because it is too large Load Diff
+78
View File
@@ -0,0 +1,78 @@
/**
* Shapes and cleanup for an FCM send result that we may store.
* Firebase accepting a message is not proof the device displayed it.
* Nothing here keeps a registration token, a JWT, or a notification body.
*/
export const FCM_ERROR_DETAIL_MAX_CHARS = 200;
export const FCM_ERROR_CODE_MAX_CHARS = 80;
export const FCM_MESSAGE_ID_MAX_CHARS = 200;
/** Shorter strings are not distinctive enough to treat as a leaked token. */
const MIN_SECRET_CHARS = 8;
export type FcmSendResult = {
status: "sent" | "failed";
/** Present when Firebase accepted the message and returned an id. */
messageId?: string;
/** Firebase error code, such as messaging/registration-token-not-registered. */
errorCode?: string;
/** Short sanitized description. Never a raw token or credential. */
errorDetail?: string;
};
/**
* Collapse whitespace and control characters, drop a known secret, and
* truncate. An empty result means there is nothing useful to store.
*/
export function sanitizeFcmDiagnostic(
value: string,
secret?: string,
maxChars: number = FCM_ERROR_DETAIL_MAX_CHARS
): string {
let text = value.replace(/[\u0000-\u001f\u007f]/g, " ").replace(/\s+/g, " ").trim();
if (secret !== undefined && secret.length >= MIN_SECRET_CHARS && text.includes(secret)) {
text = text.split(secret).join("[redacted]");
}
if (text.length <= maxChars) return text;
if (maxChars <= 1) return text.slice(0, maxChars);
return `${text.slice(0, maxChars - 1)}…`;
}
function optionalDiagnostic(value: string | undefined, secret: string | undefined, maxChars: number): string | undefined {
if (value === undefined) return undefined;
const text = sanitizeFcmDiagnostic(value, secret, maxChars);
return text.length === 0 ? undefined : text;
}
/** Error code and detail from a thrown FCM failure, with the token removed. */
export function fcmFailureParts(
err: unknown,
token?: string
): Pick<FcmSendResult, "errorCode" | "errorDetail"> {
let code: string | undefined;
if (typeof err === "object" && err !== null && "code" in err) {
const value = (err as { code?: unknown }).code;
if (typeof value === "string" && value.length > 0) code = value;
}
const message = err instanceof Error && err.message.length > 0
? err.message
: typeof err === "string" && err.length > 0
? err
: "Unknown error";
return {
errorCode: optionalDiagnostic(code, token, FCM_ERROR_CODE_MAX_CHARS),
errorDetail: optionalDiagnostic(message, token, FCM_ERROR_DETAIL_MAX_CHARS),
};
}
/**
* Text safe to print for a failed FCM send. Log this string, not the thrown
* value: the raw message and stack can contain the registration token.
*/
export function formatFcmFailureLog(err: unknown, token?: string): string {
const parts = fcmFailureParts(err, token);
const code = parts.errorCode ?? "no-code";
const detail = parts.errorDetail ?? "no detail";
return `${code}: ${detail}`;
}
+324 -1
View File
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import { db as fcmDb } from "../../src/db/fcmTokensSqlite.js";
import { closeDatabase } from "../../src/db/sqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
import type { DailyAlertSearchResult } from "../../src/alertSearch/daily.js";
import type { AlertSearchDigest } from "../../src/alertSearch/digest.js";
import {
@@ -363,3 +363,326 @@ describe("push daily cap", () => {
});
});
type DeviceSendRow = {
user_id: string;
device_id: string;
platform: string | null;
push_send_id: string | null;
attempted_at: string;
outcome: string;
fcm_message_id: string | null;
fcm_error_code: string | null;
error_detail: string | null;
};
type DidSendRow = {
id: string;
user_id: string;
batch_id: string | null;
sent_at: string;
};
function deviceSendRows(): DeviceSendRow[] {
return getDatabase()
.prepare(
`SELECT user_id, device_id, platform, push_send_id, attempted_at,
outcome, fcm_message_id, fcm_error_code, error_detail
FROM alert_push_device_sends
ORDER BY device_id`
)
.all() as DeviceSendRow[];
}
function didSendRows(): DidSendRow[] {
return getDatabase()
.prepare(`SELECT id, user_id, batch_id, sent_at FROM alert_push_sends ORDER BY sent_at`)
.all() as DidSendRow[];
}
describe("per-device FCM outcome log", () => {
let dir: string;
let previousDataDir: string | undefined;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-search-device-log-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
});
afterEach(async () => {
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
async function register(deviceId: string, fcmToken: string, platform: string): Promise<void> {
await fcmDb.upsert({
userId: USER,
deviceId,
fcmToken,
platform,
updatedAt: new Date("2026-09-08T08:00:00.000Z"),
});
}
it("records one accepted device and links it to the single DID-level row", async () => {
await register("phone-ok", "token-ok", "ios");
const now = new Date("2026-09-08T09:00:00.000Z");
const result = await deliverAlertSearchNotification(daily(), {
now,
send: async () => ({ status: "sent", messageId: "projects/app/messages/msg-ok" }),
});
assert.equal(result.sent, 1);
assert.equal(result.failed, 0);
const dids = didSendRows();
const devices = deviceSendRows();
assert.equal(dids.length, 1);
assert.equal(dids[0].user_id, USER);
assert.equal(dids[0].batch_id, "batch-1");
assert.equal(dids[0].sent_at, now.toISOString());
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 1);
assert.equal(devices.length, 1);
assert.equal(devices[0].device_id, "phone-ok");
assert.equal(devices[0].platform, "ios");
assert.equal(devices[0].outcome, "sent");
assert.equal(devices[0].fcm_message_id, "projects/app/messages/msg-ok");
assert.equal(devices[0].fcm_error_code, null);
assert.equal(devices[0].error_detail, null);
assert.equal(devices[0].push_send_id, dids[0].id);
assert.equal(devices[0].attempted_at, now.toISOString());
});
it("records one rejection and does not write a DID-level cap row", async () => {
await register("phone-bad", "token-bad", "android");
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async () => ({
status: "failed",
errorCode: "messaging/registration-token-not-registered",
errorDetail: "token-bad is not registered",
}),
});
assert.equal(result.sent, 0);
assert.equal(result.failed, 1);
assert.equal(didSendRows().length, 0);
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 0);
const devices = deviceSendRows();
assert.equal(devices.length, 1);
assert.equal(devices[0].outcome, "failed");
assert.equal(devices[0].push_send_id, null);
assert.equal(devices[0].fcm_message_id, null);
assert.equal(devices[0].fcm_error_code, "messaging/registration-token-not-registered");
assert.equal(devices[0].error_detail?.includes("token-bad"), false);
assert.match(devices[0].error_detail ?? "", /\[redacted\]/);
});
it("maps mixed FCM results onto the devices that were sent, and leaves the rest out", async () => {
await register("phone-a", "token-aaaa-1111", "ios");
await register("phone-b", "token-bbbb-2222", "android");
await register("phone-c", "token-cccc-3333", "web");
const calls: string[] = [];
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
listTokens: async () => ["token-bbbb-2222", "token-aaaa-1111"],
send: async (token) => {
calls.push(token);
if (token === "token-aaaa-1111") return { status: "sent" as const, messageId: "msg-for-a" };
if (token === "token-bbbb-2222") {
return {
status: "failed" as const,
errorCode: "messaging/invalid-argument",
errorDetail: "nope token-bbbb-2222",
messageId: "not-a-success-id",
};
}
throw new Error(`unexpected ${token}`);
},
});
assert.deepEqual(calls, ["token-bbbb-2222", "token-aaaa-1111"]);
assert.equal(result.sent, 1);
assert.equal(result.failed, 1);
const dids = didSendRows();
assert.equal(dids.length, 1);
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 1);
const devices = deviceSendRows();
assert.deepEqual(devices.map((row) => row.device_id), ["phone-a", "phone-b"]);
const accepted = devices.find((row) => row.device_id === "phone-a");
const rejected = devices.find((row) => row.device_id === "phone-b");
assert.ok(accepted && rejected);
assert.equal(accepted.outcome, "sent");
assert.equal(accepted.platform, "ios");
assert.equal(accepted.fcm_message_id, "msg-for-a");
assert.equal(accepted.fcm_error_code, null);
assert.equal(accepted.push_send_id, dids[0].id);
assert.equal(rejected.outcome, "failed");
assert.equal(rejected.platform, "android");
assert.equal(rejected.fcm_message_id, null);
assert.equal(rejected.fcm_error_code, "messaging/invalid-argument");
assert.equal(rejected.error_detail?.includes("token-bbbb-2222"), false);
assert.equal(rejected.push_send_id, dids[0].id);
const capped = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T10:00:00.000Z"),
listTokens: async () => ["token-aaaa-1111", "token-bbbb-2222", "token-cccc-3333"],
send: async () => {
throw new Error("cap should have stopped the send");
},
});
assert.equal(capped.sent, 0);
assert.equal(capped.capped, true);
assert.equal(didSendRows().length, 1);
assert.equal(deviceSendRows().length, 2);
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 1);
});
it("logs the registration that owns a duplicated token and not the other row", async () => {
const nowIso = "2026-09-08T08:00:00.000Z";
const token = "shared-token-value";
getDatabase()
.prepare(
`INSERT INTO fcm_registrations (
id, user_id, device_id, fcm_token, platform, test_mode, created_at, updated_at, last_notified_at
) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, NULL)`
)
.run("reg-first", USER, "device-first", token, "ios", nowIso, nowIso);
getDatabase()
.prepare(
`INSERT INTO fcm_registrations (
id, user_id, device_id, fcm_token, platform, test_mode, created_at, updated_at, last_notified_at
) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, NULL)`
)
.run("reg-second", USER, "device-second", token, "android", nowIso, nowIso);
const first = (await fcmDb.getByUserId(USER))[0];
assert.ok(first);
const sent: string[] = [];
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async (value) => {
sent.push(value);
return { status: "sent", messageId: "msg-shared" };
},
});
assert.equal(result.sent, 1);
assert.equal(result.failed, 0);
assert.deepEqual(sent, [token]);
const devices = deviceSendRows();
assert.equal(devices.length, 1);
assert.equal(devices[0].device_id, first.deviceId);
assert.equal(devices[0].platform, first.platform);
assert.equal(didSendRows().length, 1);
});
it("stores nulls when the FCM result omits the message id and error fields", async () => {
await register("phone-ok", "token-ok", "ios");
await register("phone-bad", "token-bad", "web");
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async (token) => (token === "token-ok" ? { status: "sent" } : { status: "failed" }),
});
assert.equal(result.sent, 1);
assert.equal(result.failed, 1);
const devices = deviceSendRows();
const accepted = devices.find((row) => row.device_id === "phone-ok");
const rejected = devices.find((row) => row.device_id === "phone-bad");
assert.ok(accepted && rejected);
assert.equal(accepted.fcm_message_id, null);
assert.equal(accepted.fcm_error_code, null);
assert.equal(accepted.error_detail, null);
assert.equal(rejected.fcm_message_id, null);
assert.equal(rejected.fcm_error_code, null);
assert.equal(rejected.error_detail, null);
});
it("truncates a thrown error and does not persist the token", async () => {
await register("phone-bad", "token-secret-value", "ios");
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async () => {
throw new Error(`boom token-secret-value ${"z".repeat(400)}`);
},
});
assert.equal(result.sent, 0);
assert.equal(result.failed, 1);
assert.equal(didSendRows().length, 0);
const devices = deviceSendRows();
assert.equal(devices.length, 1);
assert.equal(devices[0].outcome, "failed");
assert.equal(devices[0].error_detail?.includes("token-secret-value"), false);
assert.match(devices[0].error_detail ?? "", /\[redacted\]/);
assert.equal(devices[0].error_detail?.length, 200);
});
it("logs a sanitized send exception and keeps the stored outcome", async () => {
const token = "fcm-registration-token-SECRETVALUE";
await register("phone-bad", token, "ios");
const err = new Error(`The registration token ${token} is not registered`);
(err as { code?: string }).code = "messaging/registration-token-not-registered";
const lines: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => {
lines.push(args.map((arg) => String(arg)).join(" "));
};
try {
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async () => {
throw err;
},
});
assert.equal(result.sent, 0);
assert.equal(result.failed, 1);
assert.equal(didSendRows().length, 0);
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 0);
const devices = deviceSendRows();
assert.equal(devices.length, 1);
assert.equal(devices[0].outcome, "failed");
assert.equal(devices[0].fcm_error_code, null);
assert.equal(devices[0].error_detail, "The registration token [redacted] is not registered");
assert.equal(devices[0].error_detail?.includes(token), false);
const logged = lines.join("\n");
assert.match(logged, /\[AlertSearchNotify\] Send threw:/);
assert.match(logged, /messaging\/registration-token-not-registered/);
assert.match(logged, /The registration token \[redacted\] is not registered/);
assert.equal(logged.includes(token), false);
} finally {
console.error = original;
}
});
it("keeps a successful send when the diagnostic write fails and does not resend", async () => {
await register("phone-ok", "token-ok", "ios");
let sends = 0;
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
send: async () => {
sends += 1;
return { status: "sent", messageId: "msg-kept" };
},
recordDeviceAttempts: async () => {
throw new Error("disk full");
},
});
assert.equal(result.eligible, true);
assert.equal(result.sent, 1);
assert.equal(result.failed, 0);
assert.equal(sends, 1);
assert.equal(didSendRows().length, 1);
assert.equal(await alertPushSendsDb.countSince(USER, "2026-09-08T00:00:00.000Z"), 1);
assert.equal(deviceSendRows().length, 0);
});
it("does not log a token that has no registration", async () => {
const result = await deliverAlertSearchNotification(daily(), {
now: new Date("2026-09-08T09:00:00.000Z"),
listTokens: async () => ["unregistered-token"],
send: async () => "sent",
});
assert.equal(result.sent, 1);
assert.equal(result.failed, 0);
assert.equal(didSendRows().length, 1);
assert.equal(deviceSendRows().length, 0);
});
});
+20
View File
@@ -400,6 +400,26 @@ describe("alertSearch scheduler pass", () => {
});
describe("alertSearch scheduler renewal sweep", () => {
// The pass writes history to the default database, so keep it in a temp dir.
let dir: string;
let previousDataDir: string | undefined;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-search-sched-renewal-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
resetAlertSearchSchedulerPassGuard();
});
afterEach(async () => {
resetAlertSearchSchedulerPassGuard();
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
it("runs the sweep once, after every user's digest", async () => {
const order: string[] = [];
const result = await runAlertSearchSchedulerPass({
@@ -0,0 +1,243 @@
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 type { DailyAlertSearchResult } from "../../src/alertSearch/daily.js";
import {
isAlertSearchSchedulerPassInFlight,
resetAlertSearchSchedulerPassGuard,
runAlertSearchSchedulerPass,
stopAlertSearchScheduler,
} from "../../src/alertSearch/scheduler.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
const NOTIFY_USER = "did:example:notify";
const DAILY_USER = "did:example:daily";
const LATER_USER = "did:example:later";
const NOW = new Date("2026-08-28T12:00:00.000Z");
/** A JWT-shaped value and an FCM-token-shaped value. Neither may reach a log line. */
const JWT = "eyJhbGciOiJIUzI1NiJ9.UNIQUEJWTSECRET.signaturevalue";
const FCM_TOKEN = "fcm-token-UNIQUESECRET9876wxyz";
const SECRET_TEXT = `bearer ${JWT} token ${FCM_TOKEN} https://user:pass@example.test/send`;
function secretError(code: string): Error {
return Object.assign(new Error(SECRET_TEXT), { code });
}
function daily(userId: string): DailyAlertSearchResult {
return {
userId,
utcDay: "2026-08-28",
batchId: "batch-1",
jwtSequence: 1,
endorserOutcome: "success",
partnerOutcome: "empty",
completed: true,
consumed: true,
digest: null,
};
}
type PassRow = {
status: string;
attempted_users: number | null;
failed_users: number | null;
notification_errors: number | null;
error_stage: string | null;
error_code: string | null;
};
function passRows(): PassRow[] {
return getDatabase()
.prepare(
`SELECT status, attempted_users, failed_users, notification_errors, error_stage, error_code
FROM alert_search_scheduler_passes ORDER BY started_at, rowid`
)
.all() as PassRow[];
}
function assertNoSecrets(text: string): void {
assert.equal(text.includes(JWT), false);
assert.equal(text.includes(FCM_TOKEN), false);
assert.equal(text.includes("UNIQUEJWTSECRET"), false);
assert.equal(text.includes("UNIQUESECRET"), false);
assert.equal(text.includes("user:pass"), false);
assert.equal(text.includes("example.test"), false);
}
describe("alertSearch scheduler failure logs and counts", () => {
let dir: string;
let previousDataDir: string | undefined;
let lines: string[];
let originalLog: typeof console.log;
let originalError: typeof console.error;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-search-sched-log-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
stopAlertSearchScheduler();
resetAlertSearchSchedulerPassGuard();
lines = [];
originalLog = console.log;
originalError = console.error;
const record = (...args: unknown[]) => {
lines.push(args.map((arg) => String(arg)).join(" "));
};
console.log = record;
console.error = record;
});
afterEach(async () => {
console.log = originalLog;
console.error = originalError;
stopAlertSearchScheduler();
resetAlertSearchSchedulerPassGuard();
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
it("logs the stage and safe code for notification, daily-run, and renewal failures", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: NOTIFY_USER, due: true },
{ userId: DAILY_USER, due: true },
],
runDaily: async (userId) => {
if (userId === DAILY_USER) throw secretError("ECONNRESET");
return daily(userId);
},
notify: async () => {
throw secretError("messaging/internal-error");
},
remindRenewal: async () => {
throw secretError("ETIMEDOUT");
},
});
assert.deepEqual(result, {
skipped: false,
userIds: [NOTIFY_USER, DAILY_USER],
attempted: 2,
failed: 1,
deferred: 0,
});
const logged = lines.join("\n");
assert.match(logged, new RegExp(`Notification failed ${NOTIFY_USER}: notify messaging/internal-error`));
assert.match(logged, new RegExp(`User failed ${DAILY_USER}: daily_run ECONNRESET`));
assert.match(logged, /Renewal sweep failed: renewal ETIMEDOUT/);
assertNoSecrets(logged);
const [pass] = passRows();
assert.equal(pass.status, "completed");
assert.equal(pass.attempted_users, 2);
assert.equal(pass.failed_users, 1);
assert.equal(pass.notification_errors, 1);
const stored = JSON.stringify(
getDatabase().prepare(`SELECT * FROM alert_search_scheduler_runs`).all()
);
assertNoSecrets(stored);
assert.match(stored, /messaging\/internal-error/);
assert.match(stored, /ECONNRESET/);
});
it("logs a safe code and records zero counts when the pending query fails", async () => {
const failure = secretError("SQLITE_BUSY");
await assert.rejects(
runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => {
throw failure;
},
runDaily: async () => {
throw new Error("pending failure must not run a user");
},
}),
(err) => err === failure
);
const logged = lines.join("\n");
assert.match(logged, /Pass failed in \d+ms: list_pending SQLITE_BUSY/);
assert.equal(logged.includes("Notification failed"), false);
assert.equal(logged.includes("User failed"), false);
assertNoSecrets(logged);
assert.deepEqual(passRows(), [
{
status: "failed",
attempted_users: 0,
failed_users: 0,
notification_errors: 0,
error_stage: "list_pending",
error_code: "SQLITE_BUSY",
},
]);
assert.equal(isAlertSearchSchedulerPassInFlight(), false);
});
it("keeps the counts so far when a pass throws after some users have started", async () => {
const seen: string[] = [];
// The log sink throws while the second user's failure is being recorded.
// That escapes the user callback and fails the pass; the third user must not start.
console.error = (...args: unknown[]) => {
const line = args.map((arg) => String(arg)).join(" ");
lines.push(line);
if (line.includes("[AlertSearchScheduler] User failed")) {
throw secretError("EPIPE");
}
};
const failure = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: NOTIFY_USER, due: true },
{ userId: DAILY_USER, due: true },
{ userId: LATER_USER, due: true },
],
runDaily: async (userId) => {
seen.push(userId);
if (userId === DAILY_USER) throw secretError("ECONNRESET");
return daily(userId);
},
notify: async () => {
throw secretError("messaging/internal-error");
},
remindRenewal: async () => {
throw new Error("renewal must not run after the pass already failed");
},
}).then(
() => {
throw new Error("the pass should have thrown");
},
(err: unknown) => err
);
assert.ok(failure instanceof Error);
assert.equal((failure as { code?: string }).code, "EPIPE");
assert.deepEqual(seen, [NOTIFY_USER, DAILY_USER]);
const logged = lines.join("\n");
assert.match(logged, /Notification failed .* notify messaging\/internal-error/);
assert.match(logged, /User failed .* daily_run ECONNRESET/);
assert.match(logged, /Pass failed in \d+ms: pass EPIPE/);
assertNoSecrets(logged);
assert.deepEqual(passRows(), [
{
status: "failed",
attempted_users: 2,
failed_users: 1,
notification_errors: 1,
error_stage: "pass",
error_code: "EPIPE",
},
]);
assert.equal(isAlertSearchSchedulerPassInFlight(), false);
});
});
+158
View File
@@ -0,0 +1,158 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import type { DailyAlertSearchResult } from "../../src/alertSearch/daily.js";
import {
DEFER_REASON_NOTIFY_HOUR,
deferredRunRow,
dueRunRow,
readNotifyResult,
safeErrorCode,
} from "../../src/alertSearch/schedulerHistory.js";
const base = {
passId: "pass-1",
userId: "did:example:one",
utcDay: "2026-11-13",
recordedAt: "2026-11-13T10:00:00.000Z",
startedAt: "2026-11-13T09:59:59.000Z",
elapsedMs: 1200,
};
function daily(overrides: Partial<DailyAlertSearchResult> = {}): DailyAlertSearchResult {
return {
userId: base.userId,
utcDay: base.utcDay,
batchId: "batch-1",
jwtSequence: 1,
endorserOutcome: "success",
partnerOutcome: "empty",
completed: true,
consumed: true,
digest: null,
...overrides,
};
}
describe("scheduler history error codes", () => {
it("keeps a code-like code or name and never a message", () => {
const withCode = Object.assign(new Error("secret token abc123 in message"), {
code: "ECONNRESET",
});
assert.equal(safeErrorCode(withCode), "ECONNRESET");
assert.equal(safeErrorCode(new TypeError("secret token abc123")), "TypeError");
assert.equal(safeErrorCode(Object.assign(new Error("x"), { code: "has spaces and secret" })), "Error");
assert.equal(safeErrorCode("a thrown string with a secret"), "unknown");
assert.equal(safeErrorCode(null), "unknown");
});
});
describe("scheduler history notify result", () => {
it("reads only the counts deliverAlertSearchNotification returns", () => {
assert.deepEqual(readNotifyResult({ eligible: true, sent: 2, failed: 1 }), {
eligible: true,
sent: 2,
failed: 1,
capped: false,
});
assert.equal(readNotifyResult({ eligible: true, sent: 0, failed: 0, capped: true })?.capped, true);
assert.equal(readNotifyResult(undefined), undefined);
assert.equal(readNotifyResult("sent"), undefined);
assert.equal(readNotifyResult({ eligible: "yes", sent: 1, failed: 0 }), undefined);
assert.equal(readNotifyResult({ eligible: true }), undefined);
});
});
describe("scheduler history row status", () => {
it("marks a deferred user with the reason listPendingForDay implies", () => {
const row = deferredRunRow({
passId: base.passId,
userId: base.userId,
utcDay: base.utcDay,
recordedAt: base.recordedAt,
});
assert.equal(row.due, false);
assert.equal(row.status, "deferred");
assert.equal(row.reason, DEFER_REASON_NOTIFY_HOUR);
assert.equal(row.startedAt, undefined);
});
it("separates a daily run that threw from every returned result", () => {
const row = dueRunRow(base, { dailyThrew: true, errorCode: "ETIMEDOUT" });
assert.equal(row.status, "daily_threw");
assert.equal(row.errorStage, "daily_run");
assert.equal(row.errorCode, "ETIMEDOUT");
assert.equal(row.dailyCompleted, undefined);
assert.equal(row.notifySent, undefined);
});
it("separates incomplete, completed-unconsumed, and consumed daily runs", () => {
const incomplete = dueRunRow(base, {
dailyThrew: false,
daily: daily({ completed: false, consumed: false, endorserOutcome: "network" }),
notified: { eligible: false, sent: 0, failed: 0 },
});
assert.equal(incomplete.status, "daily_incomplete");
assert.equal(incomplete.dailyCompleted, false);
assert.equal(incomplete.endorserOutcome, "network");
const unconsumed = dueRunRow(base, {
dailyThrew: false,
daily: daily({ completed: true, consumed: false }),
notified: { eligible: false, sent: 0, failed: 0 },
});
assert.equal(unconsumed.status, "daily_not_consumed");
assert.equal(unconsumed.jwtConsumed, false);
const consumed = dueRunRow(base, {
dailyThrew: false,
daily: daily(),
notified: { eligible: false, sent: 0, failed: 0 },
});
assert.equal(consumed.dailyCompleted, true);
assert.equal(consumed.jwtConsumed, true);
assert.equal(consumed.status, "notify_ineligible");
});
it("separates each notification outcome after a consumed daily run", () => {
const status = (notified: unknown, notifyErrorCode?: string) =>
dueRunRow(base, { dailyThrew: false, daily: daily(), notified, notifyErrorCode }).status;
assert.equal(status({ eligible: true, sent: 0, failed: 0, capped: true }), "notify_capped");
assert.equal(status({ eligible: true, sent: 2, failed: 1 }), "fcm_accepted");
assert.equal(status({ eligible: true, sent: 0, failed: 2 }), "fcm_all_rejected");
assert.equal(status({ eligible: true, sent: 0, failed: 0 }), "no_devices");
assert.equal(status(undefined), "notify_result_unavailable");
assert.equal(status(undefined, "Error"), "notify_threw");
});
it("keeps a notifier error visible even when the daily status decides the row", () => {
const row = dueRunRow(base, {
dailyThrew: false,
daily: daily({ completed: false, consumed: false }),
notifyErrorCode: "Error",
});
assert.equal(row.status, "daily_incomplete");
assert.equal(row.errorStage, "notify");
assert.equal(row.errorCode, "Error");
});
it("copies only counts and source outcomes from a digest", () => {
const row = dueRunRow(base, {
dailyThrew: false,
daily: daily({
digest: {
completed: true,
hasUpdates: true,
totalCount: 4,
counts: {} as never,
records: { claims: [{ id: "SENTINEL-CLAIM" }] } as never,
endorser: { outcome: "success", completed: true, message: "SENTINEL-MESSAGE" },
partner: { outcome: "empty", completed: true },
},
startCursors: { endorser: "SENTINEL-CURSOR", partner: "SENTINEL-CURSOR-2" },
}),
notified: { eligible: true, sent: 1, failed: 0 },
});
assert.equal(row.digestTotalCount, 4);
assert.equal(JSON.stringify(row).includes("SENTINEL"), false);
});
});
+582
View File
@@ -0,0 +1,582 @@
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 type { DailyAlertSearchResult } from "../../src/alertSearch/daily.js";
import type { AlertSearchDigest } from "../../src/alertSearch/digest.js";
import {
resetAlertSearchSchedulerPassGuard,
runAlertSearchSchedulerPass,
stopAlertSearchScheduler,
isAlertSearchSchedulerPassInFlight,
} from "../../src/alertSearch/scheduler.js";
import {
ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS,
alertSearchSchedulerHistoryDb,
type AlertSearchSchedulerHistory,
} from "../../src/db/alertSearchSchedulerHistorySqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
const A = "did:ethr:0xaaaa";
const B = "did:ethr:0xbbbb";
const C = "did:ethr:0xcccc";
const NOW = new Date("2026-08-28T12:00:00.000Z");
const SECRET = "SENTINEL-SECRET-VALUE";
function digest(): AlertSearchDigest {
return {
completed: true,
hasUpdates: true,
totalCount: 3,
counts: {
claims: 3,
personalPlanContributions: 0,
trackedPlanUpdates: 0,
trackedPlanClaims: 0,
plansNearby: 0,
profilesNearby: 0,
profilesSimilar: 0,
plansSimilar: 0,
},
records: {
claims: [{ id: `${SECRET}-claim`, issuer: `${SECRET}-issuer` }] as never,
personalPlanContributions: [],
trackedPlanUpdates: [],
trackedPlanClaims: [],
plansNearby: [],
profilesNearby: [],
profilesSimilar: [],
plansSimilar: [],
},
endorser: { outcome: "success", completed: true, message: `${SECRET}-endorser-message` },
partner: { outcome: "empty", completed: true, message: `${SECRET}-partner-message` },
};
}
function daily(userId: string, overrides: Partial<DailyAlertSearchResult> = {}): DailyAlertSearchResult {
return {
userId,
utcDay: "2026-08-28",
batchId: "batch-1",
jwtSequence: 1,
endorserOutcome: "success",
partnerOutcome: "empty",
completed: true,
consumed: true,
digest: digest(),
startCursors: { endorser: `${SECRET}-cursor`, partner: `${SECRET}-cursor-2` },
...overrides,
};
}
type PassRow = {
id: string;
status: string;
started_at: string;
finished_at: string | null;
utc_day: string;
hour_minute: string;
pending_users: number | null;
due_users: number | null;
deferred_users: number | null;
attempted_users: number | null;
failed_users: number | null;
notification_errors: number | null;
renewal_status: string | null;
renewal_error_code: string | null;
error_stage: string | null;
error_code: string | null;
};
type RunRow = {
pass_id: string;
user_id: string;
due: number;
status: string;
reason: string | null;
started_at: string | null;
elapsed_ms: number | null;
daily_completed: number | null;
jwt_consumed: number | null;
endorser_outcome: string | null;
partner_outcome: string | null;
digest_total_count: number | null;
notify_eligible: number | null;
notify_sent: number | null;
notify_failed: number | null;
notify_capped: number | null;
error_stage: string | null;
error_code: string | null;
};
function passes(): PassRow[] {
return getDatabase()
.prepare(`SELECT * FROM alert_search_scheduler_passes ORDER BY started_at, rowid`)
.all() as PassRow[];
}
function runs(): RunRow[] {
return getDatabase()
.prepare(`SELECT * FROM alert_search_scheduler_runs ORDER BY user_id`)
.all() as RunRow[];
}
function runFor(userId: string): RunRow {
const row = runs().find((r) => r.user_id === userId);
assert.ok(row, `run row for ${userId}`);
return row;
}
const okSweep = async () => undefined;
describe("alertSearch scheduler history recording", () => {
let dir: string;
let previousDataDir: string | undefined;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-search-sched-record-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
stopAlertSearchScheduler();
resetAlertSearchSchedulerPassGuard();
});
afterEach(async () => {
stopAlertSearchScheduler();
resetAlertSearchSchedulerPassGuard();
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
it("records a pass that found no pending users", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [],
remindRenewal: okSweep,
});
assert.deepEqual(result, { skipped: false, userIds: [], attempted: 0, failed: 0, deferred: 0 });
const rows = passes();
assert.equal(rows.length, 1);
assert.equal(rows[0].status, "completed");
assert.equal(rows[0].utc_day, "2026-08-28");
assert.equal(rows[0].hour_minute, "12:00");
assert.equal(rows[0].pending_users, 0);
assert.equal(rows[0].due_users, 0);
assert.equal(rows[0].deferred_users, 0);
assert.equal(rows[0].attempted_users, 0);
assert.equal(rows[0].failed_users, 0);
assert.equal(rows[0].renewal_status, "ok");
assert.equal(rows[0].error_stage, null);
assert.ok(rows[0].finished_at);
assert.equal(runs().length, 0);
});
it("records every pending user, due and deferred, against the same pass", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: A, due: true },
{ userId: B, due: false },
{ userId: C, due: false },
],
runDaily: async (userId) => daily(userId),
notify: async () => ({ eligible: true, sent: 2, failed: 1 }),
remindRenewal: okSweep,
});
assert.equal(result.attempted, 1);
assert.equal(result.deferred, 2);
const [pass] = passes();
assert.equal(pass.pending_users, 3);
assert.equal(pass.due_users, 1);
assert.equal(pass.deferred_users, 2);
assert.equal(pass.attempted_users, 1);
const rows = runs();
assert.equal(rows.length, 3);
assert.equal(rows.every((row) => row.pass_id === pass.id), true);
const due = runFor(A);
assert.equal(due.due, 1);
assert.equal(due.status, "fcm_accepted");
assert.equal(due.daily_completed, 1);
assert.equal(due.jwt_consumed, 1);
assert.equal(due.endorser_outcome, "success");
assert.equal(due.partner_outcome, "empty");
assert.equal(due.digest_total_count, 3);
assert.equal(due.notify_eligible, 1);
assert.equal(due.notify_sent, 2);
assert.equal(due.notify_failed, 1);
assert.equal(due.notify_capped, 0);
assert.ok(due.started_at);
assert.ok(due.elapsed_ms !== null && due.elapsed_ms >= 0);
for (const userId of [B, C]) {
const deferred = runFor(userId);
assert.equal(deferred.due, 0);
assert.equal(deferred.status, "deferred");
assert.equal(deferred.reason, "notify_hour_not_reached");
assert.equal(deferred.started_at, null);
assert.equal(deferred.daily_completed, null);
}
});
it("records an incomplete daily run and a completed run that did not consume its JWT", async () => {
await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: A, due: true },
{ userId: B, due: true },
],
runDaily: async (userId) =>
userId === A
? daily(userId, { completed: false, consumed: false, endorserOutcome: "network" })
: daily(userId, { completed: true, consumed: false }),
notify: async () => ({ eligible: false, sent: 0, failed: 0 }),
remindRenewal: okSweep,
});
const incomplete = runFor(A);
assert.equal(incomplete.status, "daily_incomplete");
assert.equal(incomplete.daily_completed, 0);
assert.equal(incomplete.jwt_consumed, 0);
assert.equal(incomplete.endorser_outcome, "network");
const unconsumed = runFor(B);
assert.equal(unconsumed.status, "daily_not_consumed");
assert.equal(unconsumed.daily_completed, 1);
assert.equal(unconsumed.jwt_consumed, 0);
});
it("records a daily-run exception as a failed user without its message", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: A, due: true },
{ userId: B, due: true },
],
runDaily: async (userId) => {
if (userId === A) throw Object.assign(new Error(`${SECRET} in message`), { code: "ECONNRESET" });
return daily(userId);
},
notify: async () => ({ eligible: true, sent: 1, failed: 0 }),
remindRenewal: okSweep,
});
assert.equal(result.failed, 1);
assert.equal(result.attempted, 2);
const failed = runFor(A);
assert.equal(failed.status, "daily_threw");
assert.equal(failed.error_stage, "daily_run");
assert.equal(failed.error_code, "ECONNRESET");
assert.equal(failed.daily_completed, null);
assert.equal(failed.notify_sent, null);
assert.equal(runFor(B).status, "fcm_accepted");
const [pass] = passes();
assert.equal(pass.failed_users, 1);
assert.equal(pass.notification_errors, 0);
});
it("records ineligible, capped, all-rejected, and no-device notifications", async () => {
const outcomes: Record<string, unknown> = {
[A]: { eligible: false, sent: 0, failed: 0 },
[B]: { eligible: true, sent: 0, failed: 0, capped: true },
[C]: { eligible: true, sent: 0, failed: 3 },
"did:ethr:0xdddd": { eligible: true, sent: 0, failed: 0 },
};
await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => Object.keys(outcomes).map((userId) => ({ userId, due: true })),
runDaily: async (userId) => daily(userId),
notify: async (result) => outcomes[result.userId],
remindRenewal: okSweep,
});
assert.equal(runFor(A).status, "notify_ineligible");
assert.equal(runFor(A).notify_eligible, 0);
const capped = runFor(B);
assert.equal(capped.status, "notify_capped");
assert.equal(capped.notify_capped, 1);
assert.equal(capped.notify_sent, 0);
const rejected = runFor(C);
assert.equal(rejected.status, "fcm_all_rejected");
assert.equal(rejected.notify_sent, 0);
assert.equal(rejected.notify_failed, 3);
assert.equal(runFor("did:ethr:0xdddd").status, "no_devices");
});
it("records a notification exception without failing the user", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [{ userId: A, due: true }],
runDaily: async (userId) => daily(userId),
notify: async () => {
throw Object.assign(new Error(`${SECRET} fcm down`), { code: "messaging/internal-error" });
},
remindRenewal: okSweep,
});
assert.equal(result.failed, 0);
const row = runFor(A);
assert.equal(row.status, "notify_threw");
assert.equal(row.error_stage, "notify");
assert.equal(row.error_code, "messaging/internal-error");
assert.equal(row.daily_completed, 1);
assert.equal(row.jwt_consumed, 1);
assert.equal(row.notify_eligible, null);
const [pass] = passes();
assert.equal(pass.failed_users, 0);
assert.equal(pass.notification_errors, 1);
});
it("records a renewal-sweep exception and still returns the existing result", async () => {
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [{ userId: A, due: true }],
runDaily: async (userId) => daily(userId),
notify: async () => ({ eligible: true, sent: 1, failed: 0 }),
remindRenewal: async () => {
throw Object.assign(new Error(`${SECRET} sweep`), { code: "ETIMEDOUT" });
},
});
assert.deepEqual(result, { skipped: false, userIds: [A], attempted: 1, failed: 0, deferred: 0 });
const [pass] = passes();
assert.equal(pass.status, "completed");
assert.equal(pass.renewal_status, "failed");
assert.equal(pass.renewal_error_code, "ETIMEDOUT");
assert.equal(pass.error_stage, null);
});
it("records a failed pending-user query and rethrows the original error", async () => {
const failure = Object.assign(new Error(`${SECRET} query`), { code: "SQLITE_BUSY" });
let swept = 0;
await assert.rejects(
runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => {
throw failure;
},
remindRenewal: async () => {
swept += 1;
},
}),
(err) => err === failure
);
assert.equal(swept, 0);
assert.equal(isAlertSearchSchedulerPassInFlight(), false);
const [pass] = passes();
assert.equal(pass.status, "failed");
assert.equal(pass.error_stage, "list_pending");
assert.equal(pass.error_code, "SQLITE_BUSY");
assert.equal(pass.renewal_status, "not_run");
assert.equal(pass.pending_users, null);
assert.ok(pass.finished_at);
assert.equal(runs().length, 0);
});
it("keeps each concurrent user's row on its own user and pass", async () => {
const users = ["did:ethr:0x01", "did:ethr:0x02", "did:ethr:0x03", "did:ethr:0x04", "did:ethr:0x05", "did:ethr:0x06"];
const delays = [30, 5, 20, 1, 15, 10];
await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 3,
listPending: async () => users.map((userId) => ({ userId, due: true })),
runDaily: async (userId) => {
await new Promise((resolve) => setTimeout(resolve, delays[users.indexOf(userId)]));
// Even-numbered users complete; odd-numbered users are incomplete.
const even = Number(userId.slice(-1)) % 2 === 0;
return daily(userId, even ? {} : { completed: false, consumed: false });
},
notify: async () => ({ eligible: true, sent: 1, failed: 0 }),
remindRenewal: okSweep,
});
const [pass] = passes();
assert.equal(pass.attempted_users, 6);
const rows = runs();
assert.equal(rows.length, 6);
for (const row of rows) {
assert.equal(row.pass_id, pass.id);
const even = Number(row.user_id.slice(-1)) % 2 === 0;
assert.equal(row.status, even ? "fcm_accepted" : "daily_incomplete", row.user_id);
}
});
it("does not change the pass or repeat work when every history write fails", async () => {
const failing: AlertSearchSchedulerHistory = {
startPass: async () => {
throw new Error("disk full");
},
recordPendingCounts: async () => {
throw new Error("disk full");
},
finishPass: async () => {
throw new Error("disk full");
},
appendRuns: async () => {
throw new Error("disk full");
},
pruneOlderThan: async () => {
throw new Error("disk full");
},
};
const daily1: string[] = [];
const notified: string[] = [];
let swept = 0;
const logged: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => {
logged.push(args.map(String).join(" "));
};
let result;
try {
result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
history: failing,
listPending: async () => [
{ userId: A, due: true },
{ userId: B, due: false },
{ userId: C, due: true },
],
runDaily: async (userId) => {
daily1.push(userId);
if (userId === C) throw new Error("boom");
return daily(userId);
},
notify: async (r) => {
notified.push(r.userId);
return { eligible: true, sent: 1, failed: 0 };
},
remindRenewal: async () => {
swept += 1;
},
});
} finally {
console.error = original;
}
assert.deepEqual(result, { skipped: false, userIds: [A, C], attempted: 2, failed: 1, deferred: 1 });
assert.deepEqual(daily1, [A, C]);
assert.deepEqual(notified, [A]);
assert.equal(swept, 1);
const text = logged.join("\n");
assert.match(text, /History write failed pass start/);
assert.match(text, /History write failed user run/);
assert.match(text, /History write failed pass finish/);
assert.equal(isAlertSearchSchedulerPassInFlight(), false);
});
it("does not skip the pass when pruning fails", async () => {
const failingPrune: AlertSearchSchedulerHistory = {
...alertSearchSchedulerHistoryDb,
pruneOlderThan: async () => {
throw new Error("locked");
},
};
const result = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
history: failingPrune,
listPending: async () => [{ userId: A, due: true }],
runDaily: async (userId) => daily(userId),
notify: async () => ({ eligible: true, sent: 1, failed: 0 }),
remindRenewal: okSweep,
});
assert.equal(result.attempted, 1);
assert.equal(passes().length, 1);
assert.equal(runs().length, 1);
});
it("prunes scheduler history older than 30 days when a pass starts", async () => {
const wall = Date.now();
const stale = new Date(wall - (ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS + 1) * 24 * 60 * 60 * 1000).toISOString();
const recent = new Date(wall - 24 * 60 * 60 * 1000).toISOString();
await alertSearchSchedulerHistoryDb.startPass({ id: "old", startedAt: stale, utcDay: "2026-01-01", hourMinute: "00:00" });
await alertSearchSchedulerHistoryDb.startPass({ id: "recent", startedAt: recent, utcDay: "2026-11-12", hourMinute: "00:00" });
await alertSearchSchedulerHistoryDb.appendRuns([
{ passId: "old", userId: A, utcDay: "2026-01-01", due: false, status: "deferred", recordedAt: stale },
{ passId: "recent", userId: A, utcDay: "2026-11-12", due: false, status: "deferred", recordedAt: recent },
]);
await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [],
remindRenewal: okSweep,
});
const ids = passes().map((row) => row.id);
assert.equal(ids.includes("old"), false);
assert.equal(ids.includes("recent"), true);
assert.equal(runs().some((row) => row.pass_id === "old"), false);
assert.equal(runs().some((row) => row.pass_id === "recent"), true);
});
it("records a pass skipped by the in-flight guard without disturbing the running pass", async () => {
let release!: () => void;
const blocked = new Promise<void>((resolve) => {
release = resolve;
});
const first = runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [{ userId: A, due: true }],
runDaily: async (userId) => {
await blocked;
return daily(userId);
},
notify: async () => ({ eligible: true, sent: 1, failed: 0 }),
remindRenewal: okSweep,
});
while (!isAlertSearchSchedulerPassInFlight()) await Promise.resolve();
const second = await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => {
throw new Error("second pass should not list");
},
});
assert.equal(second.skipped, true);
assert.equal(second.attempted, 0);
release();
const firstResult = await first;
assert.equal(firstResult.skipped, false);
assert.equal(firstResult.attempted, 1);
const byStatus = passes().map((row) => row.status).sort();
assert.deepEqual(byStatus, ["completed", "skipped"]);
const skipped = passes().find((row) => row.status === "skipped");
assert.ok(skipped);
assert.equal(skipped.pending_users, null);
assert.ok(skipped.finished_at);
// Only the running pass wrote user rows.
assert.equal(runs().length, 1);
});
it("stores no digest content, cursors, JWTs, messages, or tokens", async () => {
await runAlertSearchSchedulerPass({
now: NOW,
concurrency: 1,
listPending: async () => [
{ userId: A, due: true },
{ userId: B, due: true },
{ userId: C, due: false },
],
runDaily: async (userId) => {
if (userId === B) throw new Error(`${SECRET} thrown daily message`);
return daily(userId);
},
notify: async () => {
return { eligible: true, sent: 1, failed: 0, title: `${SECRET}-title`, body: `${SECRET}-body`, token: `${SECRET}-token` };
},
remindRenewal: async () => {
throw new Error(`${SECRET} renewal message`);
},
});
const dump = JSON.stringify({
passes: getDatabase().prepare(`SELECT * FROM alert_search_scheduler_passes`).all(),
runs: getDatabase().prepare(`SELECT * FROM alert_search_scheduler_runs`).all(),
});
assert.equal(dump.includes("SENTINEL"), false);
assert.equal(dump.includes("message"), false);
});
});
+274
View File
@@ -0,0 +1,274 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
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 Database from "better-sqlite3";
import { alertPushDeviceSendsDb } from "../../src/db/alertPushDeviceSendsSqlite.js";
import { ALERT_PUSH_SEND_RETENTION_DAYS, alertPushSendsDb } from "../../src/db/alertPushSendsSqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
describe("alert_push_device_sends", () => {
let dir: string;
let previousDataDir: string | undefined;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-push-device-sends-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
});
afterEach(async () => {
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
it("creates the table and the lookup indexes", () => {
const db = getDatabase();
const columns = db.prepare(`PRAGMA table_info(alert_push_device_sends)`).all() as { name: string }[];
assert.deepEqual(
columns.map((column) => column.name),
[
"id",
"user_id",
"device_id",
"platform",
"push_send_id",
"attempted_at",
"outcome",
"fcm_message_id",
"fcm_error_code",
"error_detail",
"kind",
]
);
assert.equal(columns.some((column) => column.name === "fcm_token"), false);
const indexes = db.prepare(`PRAGMA index_list(alert_push_device_sends)`).all() as { name: string }[];
const names = indexes.map((index) => index.name);
assert.equal(names.includes("idx_alert_push_device_sends_user_attempted"), true);
assert.equal(names.includes("idx_alert_push_device_sends_device_attempted"), true);
const userIndex = db
.prepare(`PRAGMA index_info(idx_alert_push_device_sends_user_attempted)`)
.all() as { name: string }[];
const deviceIndex = db
.prepare(`PRAGMA index_info(idx_alert_push_device_sends_device_attempted)`)
.all() as { name: string }[];
assert.deepEqual(
userIndex.map((column) => column.name),
["user_id", "attempted_at"]
);
assert.deepEqual(
deviceIndex.map((column) => column.name),
["device_id", "attempted_at"]
);
const capColumns = db.prepare(`PRAGMA table_info(alert_push_sends)`).all() as { name: string }[];
assert.deepEqual(
capColumns.map((column) => column.name),
["id", "user_id", "batch_id", "sent_at"]
);
});
it("stores one success and one failure and truncates a long error", async () => {
const longDetail = "e".repeat(500);
await alertPushDeviceSendsDb.appendMany([
{
userId: "did:example:one",
deviceId: "phone-ok",
platform: "ios",
pushSendId: "send-1",
attemptedAt: "2026-11-13T14:00:00.000Z",
outcome: "sent",
fcmMessageId: "projects/app/messages/msg-1",
fcmErrorCode: "should-not-stick",
errorDetail: "should-not-stick",
},
{
userId: "did:example:one",
deviceId: "phone-bad",
platform: "android",
pushSendId: null,
attemptedAt: "2026-11-13T14:00:00.000Z",
outcome: "failed",
fcmMessageId: "should-not-stick",
fcmErrorCode: null,
errorDetail: longDetail,
},
]);
const rows = getDatabase()
.prepare(
`SELECT device_id, platform, push_send_id, outcome, fcm_message_id, fcm_error_code, error_detail
FROM alert_push_device_sends ORDER BY device_id`
)
.all() as {
device_id: string;
platform: string | null;
push_send_id: string | null;
outcome: string;
fcm_message_id: string | null;
fcm_error_code: string | null;
error_detail: string | null;
}[];
assert.equal(rows.length, 2);
assert.equal(rows[0].device_id, "phone-bad");
assert.equal(rows[0].outcome, "failed");
assert.equal(rows[0].fcm_message_id, null);
assert.equal(rows[0].fcm_error_code, null);
assert.equal(rows[0].error_detail?.length, 200);
assert.equal(rows[0].error_detail?.endsWith("…"), true);
assert.equal(rows[1].device_id, "phone-ok");
assert.equal(rows[1].platform, "ios");
assert.equal(rows[1].push_send_id, "send-1");
assert.equal(rows[1].outcome, "sent");
assert.equal(rows[1].fcm_message_id, "projects/app/messages/msg-1");
assert.equal(rows[1].fcm_error_code, null);
assert.equal(rows[1].error_detail, null);
const capRows = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_sends`).get() as { n: number };
assert.equal(capRows.n, 0);
const kinds = getDatabase()
.prepare(`SELECT device_id, kind FROM alert_push_device_sends ORDER BY device_id`)
.all() as { device_id: string; kind: string }[];
assert.deepEqual(kinds, [
{ device_id: "phone-bad", kind: "alert_search" },
{ device_id: "phone-ok", kind: "alert_search" },
]);
});
it("classifies rows written before the kind column as AlertSearch attempts", async () => {
closeDatabase();
const file = path.join(dir, "notify.sqlite");
const raw = new Database(file);
raw.exec(`
CREATE TABLE alert_push_device_sends (
id TEXT PRIMARY KEY NOT NULL,
user_id TEXT NOT NULL,
device_id TEXT NOT NULL,
platform TEXT,
push_send_id TEXT,
attempted_at TEXT NOT NULL,
outcome TEXT NOT NULL,
fcm_message_id TEXT,
fcm_error_code TEXT,
error_detail TEXT
);
`);
raw.prepare(
`INSERT INTO alert_push_device_sends (
id, user_id, device_id, platform, push_send_id, attempted_at, outcome,
fcm_message_id, fcm_error_code, error_detail
) VALUES ('old', 'did:example:old', 'phone', 'ios', 'send-1', ?, 'sent', 'msg', NULL, NULL)`
).run("2026-11-01T00:00:00.000Z");
raw.close();
const row = getDatabase()
.prepare(`SELECT kind, push_send_id FROM alert_push_device_sends WHERE id = 'old'`)
.get() as { kind: string; push_send_id: string };
assert.equal(row.kind, "alert_search");
assert.equal(row.push_send_id, "send-1");
});
it("drops this identity's device rows past the retention window", async () => {
const recent = "2026-11-13T14:00:00.000Z";
const stale = new Date(
Date.parse(recent) - (ALERT_PUSH_SEND_RETENTION_DAYS + 1) * 24 * 60 * 60 * 1000
).toISOString();
await alertPushDeviceSendsDb.appendMany([
{
userId: "did:example:old",
deviceId: "old-phone",
platform: "ios",
pushSendId: null,
attemptedAt: stale,
outcome: "failed",
fcmMessageId: null,
fcmErrorCode: "messaging/internal-error",
errorDetail: "old",
},
]);
await alertPushDeviceSendsDb.appendMany([
{
userId: "did:example:old",
deviceId: "new-phone",
platform: "ios",
pushSendId: null,
attemptedAt: recent,
outcome: "sent",
fcmMessageId: "msg-new",
fcmErrorCode: null,
errorDetail: null,
},
]);
const rows = getDatabase()
.prepare(`SELECT device_id FROM alert_push_device_sends WHERE user_id = ?`)
.all("did:example:old") as { device_id: string }[];
assert.deepEqual(rows.map((row) => row.device_id), ["new-phone"]);
});
it("prunes only the DID being written, using the shared 30-day window", async () => {
assert.equal(ALERT_PUSH_SEND_RETENTION_DAYS, 30);
const deviceWriter = readFileSync(
new URL("../../src/db/alertPushDeviceSendsSqlite.ts", import.meta.url),
"utf8"
);
const didWriter = readFileSync(
new URL("../../src/db/alertPushSendsSqlite.ts", import.meta.url),
"utf8"
);
assert.match(deviceWriter, /ALERT_PUSH_SEND_RETENTION_DAYS/);
assert.equal(didWriter.includes("export const ALERT_PUSH_SEND_RETENTION_DAYS = 30"), true);
const recent = "2026-11-13T14:00:00.000Z";
const stale = new Date(
Date.parse(recent) - (ALERT_PUSH_SEND_RETENTION_DAYS + 1) * 24 * 60 * 60 * 1000
).toISOString();
const oldDevice = {
platform: "ios" as const,
pushSendId: null,
attemptedAt: stale,
outcome: "failed" as const,
fcmMessageId: null,
fcmErrorCode: "messaging/internal-error",
errorDetail: "old",
};
await alertPushDeviceSendsDb.appendMany([
{ ...oldDevice, userId: "did:example:active", deviceId: "active-old" },
{ ...oldDevice, userId: "did:example:quiet", deviceId: "quiet-old" },
]);
await alertPushDeviceSendsDb.appendMany([
{
userId: "did:example:active",
deviceId: "active-new",
platform: "ios",
pushSendId: null,
attemptedAt: recent,
outcome: "sent",
fcmMessageId: "msg-new",
fcmErrorCode: null,
errorDetail: null,
},
]);
const deviceRows = getDatabase()
.prepare(`SELECT user_id, device_id FROM alert_push_device_sends ORDER BY user_id, device_id`)
.all() as { user_id: string; device_id: string }[];
assert.deepEqual(deviceRows, [
{ user_id: "did:example:active", device_id: "active-new" },
{ user_id: "did:example:quiet", device_id: "quiet-old" },
]);
await alertPushSendsDb.append({ userId: "did:example:active", sentAt: stale });
await alertPushSendsDb.append({ userId: "did:example:quiet", sentAt: stale });
await alertPushSendsDb.append({ userId: "did:example:active", sentAt: recent });
const didRows = getDatabase()
.prepare(`SELECT user_id, sent_at FROM alert_push_sends ORDER BY user_id, sent_at`)
.all() as { user_id: string; sent_at: string }[];
assert.deepEqual(didRows, [
{ user_id: "did:example:active", sent_at: recent },
{ user_id: "did:example:quiet", sent_at: stale },
]);
});
});
+319
View File
@@ -0,0 +1,319 @@
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 { alertPushDeviceSendsDb } from "../../src/db/alertPushDeviceSendsSqlite.js";
import { ALERT_PUSH_SEND_RETENTION_DAYS, alertPushSendsDb } from "../../src/db/alertPushSendsSqlite.js";
import {
ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS,
alertSearchSchedulerHistoryDb,
safeLabel,
type SchedulerRunInput,
} from "../../src/db/alertSearchSchedulerHistorySqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
const DAY_MS = 24 * 60 * 60 * 1000;
const NOW = Date.parse("2026-11-13T12:00:00.000Z");
const iso = (offsetMs: number) => new Date(NOW + offsetMs).toISOString();
function run(passId: string, userId: string, recordedAt: string, extra: Partial<SchedulerRunInput> = {}): SchedulerRunInput {
return {
passId,
userId,
utcDay: "2026-11-13",
due: true,
status: "fcm_accepted",
recordedAt,
...extra,
};
}
async function seedPass(id: string, startedAt: string): Promise<void> {
await alertSearchSchedulerHistoryDb.startPass({
id,
startedAt,
utcDay: "2026-11-13",
hourMinute: "12:00",
});
}
function count(table: string): number {
return (getDatabase().prepare(`SELECT COUNT(*) AS n FROM ${table}`).get() as { n: number }).n;
}
describe("alert search scheduler history tables", () => {
let dir: string;
let previousDataDir: string | undefined;
beforeEach(async () => {
previousDataDir = process.env.NOTIFY_DATA_DIR;
dir = await mkdtemp(path.join(tmpdir(), "alert-search-sched-history-"));
process.env.NOTIFY_DATA_DIR = dir;
closeDatabase();
});
afterEach(async () => {
closeDatabase();
if (previousDataDir === undefined) delete process.env.NOTIFY_DATA_DIR;
else process.env.NOTIFY_DATA_DIR = previousDataDir;
await rm(dir, { recursive: true, force: true });
});
it("creates the pass and per-user tables with their indexes", () => {
const db = getDatabase();
const names = (table: string) =>
(db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[]).map((c) => c.name);
assert.deepEqual(names("alert_search_scheduler_passes"), [
"id",
"started_at",
"finished_at",
"status",
"utc_day",
"hour_minute",
"pending_users",
"due_users",
"deferred_users",
"attempted_users",
"failed_users",
"notification_errors",
"renewal_status",
"renewal_error_code",
"error_stage",
"error_code",
]);
assert.deepEqual(names("alert_search_scheduler_runs"), [
"id",
"pass_id",
"user_id",
"utc_day",
"due",
"status",
"reason",
"recorded_at",
"started_at",
"elapsed_ms",
"daily_completed",
"jwt_consumed",
"endorser_outcome",
"partner_outcome",
"digest_total_count",
"notify_eligible",
"notify_sent",
"notify_failed",
"notify_capped",
"error_stage",
"error_code",
]);
const indexColumns = (table: string, index: string) => {
const listed = (db.prepare(`PRAGMA index_list(${table})`).all() as { name: string }[]).map((i) => i.name);
assert.equal(listed.includes(index), true, index);
return (db.prepare(`PRAGMA index_info(${index})`).all() as { name: string }[]).map((c) => c.name);
};
assert.deepEqual(
indexColumns("alert_search_scheduler_passes", "idx_alert_search_scheduler_passes_started"),
["started_at"]
);
assert.deepEqual(
indexColumns("alert_search_scheduler_runs", "idx_alert_search_scheduler_runs_pass"),
["pass_id"]
);
assert.deepEqual(
indexColumns("alert_search_scheduler_runs", "idx_alert_search_scheduler_runs_user_recorded"),
["user_id", "recorded_at"]
);
});
it("stores no column that could hold content, tokens, JWTs, or messages", () => {
const db = getDatabase();
const all = [
...(db.prepare(`PRAGMA table_info(alert_search_scheduler_passes)`).all() as { name: string }[]),
...(db.prepare(`PRAGMA table_info(alert_search_scheduler_runs)`).all() as { name: string }[]),
]
.map((c) => c.name)
// jwt_consumed is a 0/1 flag, not a credential.
.filter((name) => name !== "jwt_consumed");
for (const forbidden of ["jwt", "token", "fcm_token", "records", "counts", "body", "title", "cursor", "message", "location", "payload"]) {
assert.equal(
all.some((name) => name.includes(forbidden)),
false,
forbidden
);
}
});
it("associates user rows with the pass that wrote them and reads them back", async () => {
await seedPass("pass-a", iso(-2000));
await seedPass("pass-b", iso(-1000));
await alertSearchSchedulerHistoryDb.recordPendingCounts("pass-a", {
pendingUsers: 2,
dueUsers: 1,
deferredUsers: 1,
});
await alertSearchSchedulerHistoryDb.appendRuns([
run("pass-a", "did:example:due", iso(-1900), {
startedAt: iso(-1950),
elapsedMs: 42,
dailyCompleted: true,
jwtConsumed: true,
endorserOutcome: "success",
partnerOutcome: "empty",
digestTotalCount: 3,
notifyEligible: true,
notifySent: 2,
notifyFailed: 1,
notifyCapped: false,
}),
run("pass-a", "did:example:later", iso(-1900), {
due: false,
status: "deferred",
reason: "notify_hour_not_reached",
}),
]);
await alertSearchSchedulerHistoryDb.appendRuns([run("pass-b", "did:example:due", iso(-900))]);
await alertSearchSchedulerHistoryDb.finishPass("pass-a", {
finishedAt: iso(-1800),
status: "completed",
attemptedUsers: 1,
failedUsers: 0,
notificationErrors: 0,
renewalStatus: "ok",
});
const db = getDatabase();
const a = db.prepare(`SELECT * FROM alert_search_scheduler_passes WHERE id = 'pass-a'`).get() as Record<string, unknown>;
assert.equal(a.status, "completed");
assert.equal(a.pending_users, 2);
assert.equal(a.due_users, 1);
assert.equal(a.deferred_users, 1);
assert.equal(a.attempted_users, 1);
assert.equal(a.renewal_status, "ok");
assert.equal(a.finished_at, iso(-1800));
const forA = db
.prepare(`SELECT user_id, due, status, reason, notify_sent, notify_failed, notify_capped FROM alert_search_scheduler_runs WHERE pass_id = 'pass-a' ORDER BY user_id`)
.all() as Record<string, unknown>[];
assert.deepEqual(forA, [
{ user_id: "did:example:due", due: 1, status: "fcm_accepted", reason: null, notify_sent: 2, notify_failed: 1, notify_capped: 0 },
{ user_id: "did:example:later", due: 0, status: "deferred", reason: "notify_hour_not_reached", notify_sent: null, notify_failed: null, notify_capped: null },
]);
assert.equal(count("alert_search_scheduler_runs"), 3);
const forB = db.prepare(`SELECT COUNT(*) AS n FROM alert_search_scheduler_runs WHERE pass_id = 'pass-b'`).get() as { n: number };
assert.equal(forB.n, 1);
});
it("records a skipped pass as finished and distinct from a running one", async () => {
await alertSearchSchedulerHistoryDb.startPass({
id: "skipped",
startedAt: iso(0),
utcDay: "2026-11-13",
hourMinute: "12:00",
status: "skipped",
});
await seedPass("running", iso(1000));
const rows = getDatabase()
.prepare(`SELECT id, status, finished_at FROM alert_search_scheduler_passes ORDER BY id`)
.all() as { id: string; status: string; finished_at: string | null }[];
assert.deepEqual(rows, [
{ id: "running", status: "running", finished_at: null },
{ id: "skipped", status: "skipped", finished_at: iso(0) },
]);
});
it("persists only safe status and error labels", async () => {
await seedPass("pass-safe", iso(0));
await alertSearchSchedulerHistoryDb.appendRuns([
run("pass-safe", "did:example:free-text", iso(0), {
status: "boom: Bearer eyJhbGciOi.secret failed for fcm token abc",
errorStage: "daily_run",
errorCode: "connect ECONNREFUSED https://user:pass@example.test/?jwt=eyJ.x",
endorserOutcome: "some free text with spaces",
}),
]);
await alertSearchSchedulerHistoryDb.finishPass("pass-safe", {
finishedAt: iso(10),
status: "failed",
attemptedUsers: 0,
failedUsers: 0,
notificationErrors: 0,
renewalStatus: "failed",
renewalErrorCode: "has spaces",
errorStage: "list_pending",
errorCode: "SQLITE_BUSY",
});
const db = getDatabase();
const stored = db.prepare(`SELECT status, error_stage, error_code, endorser_outcome FROM alert_search_scheduler_runs`).get() as Record<string, unknown>;
assert.deepEqual(stored, {
status: "unknown",
error_stage: "daily_run",
error_code: null,
endorser_outcome: null,
});
const pass = db.prepare(`SELECT renewal_error_code, error_stage, error_code FROM alert_search_scheduler_passes`).get() as Record<string, unknown>;
assert.deepEqual(pass, { renewal_error_code: null, error_stage: "list_pending", error_code: "SQLITE_BUSY" });
assert.equal(safeLabel("messaging/invalid-argument"), "messaging/invalid-argument");
assert.equal(safeLabel("a".repeat(65)), null);
assert.equal(safeLabel(""), null);
});
it("prunes passes and their users together past the 30-day constant", async () => {
assert.equal(ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS, 30);
const cutoff = iso(-ALERT_SEARCH_SCHEDULER_HISTORY_RETENTION_DAYS * DAY_MS);
await seedPass("old-pass", iso(-31 * DAY_MS));
await seedPass("edge-pass", cutoff);
await seedPass("new-pass", iso(-DAY_MS));
await alertSearchSchedulerHistoryDb.appendRuns([
run("old-pass", "did:example:a", iso(-31 * DAY_MS)),
run("old-pass", "did:example:b", iso(-31 * DAY_MS)),
run("edge-pass", "did:example:a", cutoff),
run("new-pass", "did:example:a", iso(-DAY_MS)),
// Its pass row was never written, and it is past the cutoff.
run("missing-pass", "did:example:c", iso(-40 * DAY_MS)),
// Its pass row was never written, but it is recent: kept until it ages out.
run("missing-recent", "did:example:c", iso(-DAY_MS)),
]);
const removed = await alertSearchSchedulerHistoryDb.pruneOlderThan(cutoff);
assert.equal(removed, 1);
const db = getDatabase();
const passes = (db.prepare(`SELECT id FROM alert_search_scheduler_passes ORDER BY id`).all() as { id: string }[]).map((r) => r.id);
assert.deepEqual(passes, ["edge-pass", "new-pass"]);
const runs = (db.prepare(`SELECT pass_id FROM alert_search_scheduler_runs ORDER BY pass_id, user_id`).all() as { pass_id: string }[]).map((r) => r.pass_id);
assert.deepEqual(runs, ["edge-pass", "missing-recent", "new-pass"]);
const orphans = db
.prepare(
`SELECT COUNT(*) AS n FROM alert_search_scheduler_runs r
WHERE r.pass_id NOT IN (SELECT id FROM alert_search_scheduler_passes)
AND r.pass_id IN ('old-pass', 'missing-pass')`
)
.get() as { n: number };
assert.equal(orphans.n, 0);
});
it("prunes scheduler history without touching per-DID send history", async () => {
assert.equal(ALERT_PUSH_SEND_RETENTION_DAYS, 30);
const stale = iso(-45 * DAY_MS);
await alertPushSendsDb.append({ userId: "did:example:quiet", sentAt: stale });
await alertPushDeviceSendsDb.appendMany([
{
userId: "did:example:quiet",
deviceId: "quiet-device",
platform: "ios",
pushSendId: null,
attemptedAt: stale,
outcome: "failed",
fcmMessageId: null,
fcmErrorCode: "messaging/internal-error",
errorDetail: "old",
},
]);
await seedPass("old-pass", stale);
await alertSearchSchedulerHistoryDb.pruneOlderThan(iso(-30 * DAY_MS));
assert.equal(count("alert_search_scheduler_passes"), 0);
// Per-DID history is pruned lazily on that DID's next write, not here.
assert.equal(count("alert_push_sends"), 1);
assert.equal(count("alert_push_device_sends"), 1);
});
});
+665
View File
@@ -0,0 +1,665 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, beforeEach, describe, it } from "node:test";
import { alertPushSendsDb } from "../../src/db/alertPushSendsSqlite.js";
import { db } from "../../src/db/fcmTokensSqlite.js";
import { closeDatabase, getDatabase } from "../../src/db/sqlite.js";
import {
decideDiagnosticRegistration,
maskRegistrationToken,
previewDiagnosticPush,
runDiagnosticSend,
} from "../../src/services/pushDiagnostic.js";
import { resolveDiagnosticEnvironment } from "../../src/services/pushDiagnosticEnv.js";
import {
PUSH_DIAGNOSTIC_BODY,
PUSH_DIAGNOSTIC_TITLE,
PUSH_WATCH_TEST_TYPE,
buildPushDiagnosticContent,
buildPushDiagnosticMessage,
} from "../../src/services/pushDiagnosticPayload.js";
import { maskFcmToken } from "../../src/tools/pushWatch.js";
import type { StoredRow } from "../../src/db/fcmTokensSqlite.js";
const DID = "did:example:preview";
const OTHER = "did:example:other";
const DEVICE = "phone-1";
const TOKEN = "abcdUNIQUESECRET9876wxyz";
const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----SENTINELPRIVATEKEY";
const JWT = "eyJhbGciOiJIUzI1NiJ9.UNIQUEJWTSECRET.signaturevalue";
const NOW = new Date("2026-11-16T08:00:00.000Z");
const PROJECT = "timesafari-test";
const ENV_KEYS = [
"MESSAGE_ENV_LABEL",
"PUSH_TEST_FIREBASE_PROJECT_ID",
"PUSH_DIAGNOSTIC_TEST_SEND_ENABLED",
"PUSH_DIAGNOSTIC_ALLOWED_DID",
"PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID",
"FIREBASE_SERVICE_ACCOUNT_JSON",
"GOOGLE_APPLICATION_CREDENTIALS",
"NOTIFY_DATA_DIR",
] as const;
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const tsx = path.join(root, "node_modules/.bin/tsx");
function serviceAccount(projectId: string): string {
return JSON.stringify({
type: "service_account",
project_id: projectId,
private_key: PRIVATE_KEY,
client_email: "push-test@timesafari-test.iam.gserviceaccount.com",
});
}
function row(partial: Partial<StoredRow> & Pick<StoredRow, "userId" | "deviceId">): StoredRow {
return {
id: "reg-1",
fcmToken: TOKEN,
platform: "android",
createdAt: NOW.toISOString(),
updatedAt: NOW.toISOString(),
...partial,
};
}
function assertNoSecrets(text: string): void {
assert.equal(text.includes(TOKEN), false);
assert.equal(text.includes("UNIQUESECRET"), false);
assert.equal(text.includes(PRIVATE_KEY), false);
assert.equal(text.includes("SENTINELPRIVATEKEY"), false);
assert.equal(text.includes(JWT), false);
assert.equal(text.includes("UNIQUEJWTSECRET"), false);
}
describe("diagnostic environment", () => {
const saved: Record<string, string | undefined> = {};
beforeEach(() => {
for (const key of ENV_KEYS) {
saved[key] = process.env[key];
delete process.env[key];
}
});
afterEach(() => {
for (const key of ENV_KEYS) {
if (saved[key] === undefined) delete process.env[key];
else process.env[key] = saved[key];
}
});
function allowTest(projectId = PROJECT): void {
process.env.MESSAGE_ENV_LABEL = "test";
process.env.PUSH_TEST_FIREBASE_PROJECT_ID = projectId;
process.env.FIREBASE_SERVICE_ACCOUNT_JSON = serviceAccount(projectId);
}
it("accepts a test label when the service account matches the configured project", () => {
allowTest();
const result = resolveDiagnosticEnvironment();
assert.equal(result.ok, true);
if (result.ok) {
assert.equal(result.firebaseProjectId, PROJECT);
assert.equal(result.label, "test");
}
const text = JSON.stringify(result);
assertNoSecrets(text);
});
it("refuses production, unknown, and ambiguous configurations", () => {
const cases: Array<{ name: string; setup: () => void; classification: string }> = [
{
name: "unset label",
setup: () => allowTest(),
classification: "production",
},
{
name: "production label",
setup: () => {
allowTest();
process.env.MESSAGE_ENV_LABEL = "production";
},
classification: "production",
},
{
name: "prod label",
setup: () => {
allowTest();
process.env.MESSAGE_ENV_LABEL = "prod";
},
classification: "production",
},
{
name: "no service account",
setup: () => {
allowTest();
delete process.env.FIREBASE_SERVICE_ACCOUNT_JSON;
process.env.GOOGLE_APPLICATION_CREDENTIALS = "/tmp/adc.json";
},
classification: "unknown",
},
{
name: "unreadable service account",
setup: () => {
allowTest();
process.env.FIREBASE_SERVICE_ACCOUNT_JSON = "not-json";
},
classification: "unknown",
},
{
name: "service account without a project",
setup: () => {
allowTest();
process.env.FIREBASE_SERVICE_ACCOUNT_JSON = JSON.stringify({ type: "service_account", private_key: PRIVATE_KEY });
},
classification: "unknown",
},
{
name: "expected project unset",
setup: () => {
allowTest();
delete process.env.PUSH_TEST_FIREBASE_PROJECT_ID;
},
classification: "unknown",
},
{
name: "label is not test",
setup: () => {
allowTest();
process.env.MESSAGE_ENV_LABEL = "staging";
},
classification: "ambiguous",
},
{
name: "project ids differ",
setup: () => {
allowTest();
process.env.PUSH_TEST_FIREBASE_PROJECT_ID = "some-other-project";
},
classification: "ambiguous",
},
];
for (const entry of cases) {
for (const key of ENV_KEYS) delete process.env[key];
entry.setup();
if (entry.name === "unset label") delete process.env.MESSAGE_ENV_LABEL;
const result = resolveDiagnosticEnvironment();
assert.equal(result.ok, false, entry.name);
if (!result.ok) assert.equal(result.classification, entry.classification, entry.name);
assertNoSecrets(JSON.stringify(result));
}
});
});
describe("diagnostic registration selection", () => {
it("rejects a missing registration, an inactive token, another DID, and an ambiguous pair", () => {
const missing = decideDiagnosticRegistration(DID, DEVICE, []);
assert.equal(missing.ok, false);
if (!missing.ok) assert.equal(missing.reason, "missing");
const blank = decideDiagnosticRegistration(DID, DEVICE, [row({ userId: DID, deviceId: DEVICE, fcmToken: " " })]);
assert.equal(blank.ok, false);
if (!blank.ok) assert.equal(blank.reason, "inactive");
const other = decideDiagnosticRegistration(DID, DEVICE, [row({ userId: OTHER, deviceId: DEVICE })]);
assert.equal(other.ok, false);
if (!other.ok) {
assert.equal(other.reason, "other_did");
assertNoSecrets(other.detail);
}
const ambiguous = decideDiagnosticRegistration(DID, DEVICE, [
row({ userId: DID, deviceId: DEVICE, id: "a" }),
row({ userId: DID, deviceId: DEVICE, id: "b", fcmToken: "zzzzUNIQUESECRET9876yyyy" }),
]);
assert.equal(ambiguous.ok, false);
if (!ambiguous.ok) {
assert.equal(ambiguous.reason, "ambiguous");
assertNoSecrets(ambiguous.detail);
}
const selected = decideDiagnosticRegistration(DID, DEVICE, [
row({ userId: OTHER, deviceId: DEVICE, fcmToken: "other-token-UNIQUESECRET-value" }),
row({ userId: DID, deviceId: DEVICE }),
]);
assert.equal(selected.ok, true);
if (selected.ok) assert.equal(selected.row.fcmToken, TOKEN);
});
it("masks a registration token the same way push:watch does", () => {
assert.equal(maskRegistrationToken(TOKEN), maskFcmToken(TOKEN));
assert.equal(maskRegistrationToken(TOKEN).includes("UNIQUESECRET"), false);
});
});
describe("diagnostic preview and send", () => {
let dir: string;
let previous: Record<string, string | undefined>;
beforeEach(async () => {
previous = {};
for (const key of ENV_KEYS) previous[key] = process.env[key];
dir = await mkdtemp(path.join(tmpdir(), "push-diagnostic-"));
process.env.NOTIFY_DATA_DIR = dir;
process.env.MESSAGE_ENV_LABEL = "test";
process.env.PUSH_TEST_FIREBASE_PROJECT_ID = PROJECT;
process.env.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED = "true";
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = DID;
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = DEVICE;
process.env.FIREBASE_SERVICE_ACCOUNT_JSON = serviceAccount(PROJECT);
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
closeDatabase();
});
afterEach(async () => {
closeDatabase();
for (const key of ENV_KEYS) {
if (previous[key] === undefined) delete process.env[key];
else process.env[key] = previous[key];
}
await rm(dir, { recursive: true, force: true });
});
async function register(userId: string, deviceId: string, fcmToken = TOKEN, platform = "android"): Promise<void> {
await db.upsert({ userId, deviceId, fcmToken, platform, updatedAt: NOW });
}
it("previews the shared payload and does not send or write", async () => {
await register(DID, DEVICE);
await register(DID, "phone-2", "zzzzUNIQUESECRET9876yyyy", "ios");
const sendsBefore = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_sends`).get() as { n: number };
const attemptsBefore = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_device_sends`).get() as { n: number };
const calls: unknown[] = [];
delete process.env.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED;
const preview = await previewDiagnosticPush({ userId: DID, now: NOW });
assert.equal(calls.length, 0);
assert.equal(preview.eligible.length, 2);
assert.match(preview.report, /Preview only\. No FCM message was sent\./);
assert.match(preview.report, /Eligible devices: 2/);
assert.match(preview.report, /Environment label: test/);
assert.match(preview.report, /does not distinguish test from production/);
assert.match(preview.report, /Diagnostic send opt-in: not enabled/);
assert.match(preview.report, new RegExp(PROJECT));
assert.match(preview.report, /phone-1/);
assert.match(preview.report, /android/);
assert.match(preview.report, new RegExp(PUSH_DIAGNOSTIC_TITLE));
assert.match(preview.report, /This is a diagnostic notification\. \(test\)/);
assert.match(preview.report, new RegExp(`Data type: ${PUSH_WATCH_TEST_TYPE}`));
assert.doesNotMatch(preview.report, /Data type: alert_search/);
assertNoSecrets(preview.report);
const message = buildPushDiagnosticMessage(TOKEN, preview.content);
assert.deepEqual(message.notification, { title: preview.content.title, body: preview.content.body });
assert.deepEqual(message.data, preview.content.data);
assert.equal(message.data.type, PUSH_WATCH_TEST_TYPE);
assert.notEqual(message.data.type, "alert_search");
const sendsAfter = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_sends`).get() as { n: number };
const attemptsAfter = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_device_sends`).get() as { n: number };
assert.equal(sendsAfter.n, sendsBefore.n);
assert.equal(attemptsAfter.n, attemptsBefore.n);
const empty = await previewDiagnosticPush({ userId: "did:example:nobody", now: NOW });
assert.match(empty.report, /Eligible devices: 0/);
assert.match(empty.report, /No eligible FCM registration for this DID/);
});
it("sends to exactly one registration and records a diagnostic attempt without touching the cap", async () => {
await register(DID, DEVICE);
await register(DID, "phone-2", "zzzzUNIQUESECRET9876yyyy", "ios");
await register(OTHER, DEVICE, "otherUNIQUESECRET9876token", "android");
await alertPushSendsDb.append({ userId: DID, batchId: "batch-1", sentAt: "2026-11-16T01:00:00.000Z" });
const capBefore = await alertPushSendsDb.countSince(DID, "2026-11-16T00:00:00.000Z");
const sent: unknown[] = [];
const logs: string[] = [];
const originalError = console.error;
const originalLog = console.log;
console.error = (...args: unknown[]) => {
logs.push(args.map(String).join(" "));
};
console.log = (...args: unknown[]) => {
logs.push(args.map(String).join(" "));
};
let outcome;
try {
outcome = await runDiagnosticSend({
userId: DID,
deviceId: DEVICE,
now: NOW,
send: async (message) => {
sent.push(message);
if (sent.length > 1) throw new Error("resent");
return "projects/timesafari-test/messages/msg-1";
},
});
} finally {
console.error = originalError;
console.log = originalLog;
}
assert.equal(outcome.ok, true);
if (!outcome.ok) return;
assert.equal(outcome.status, "sent");
assert.equal(sent.length, 1);
const expected = buildPushDiagnosticMessage(
TOKEN,
buildPushDiagnosticContent({ id: outcome.diagnosticId, at: outcome.attemptedAt })
);
assert.deepEqual(sent[0], expected);
assert.equal(expected.data.type, PUSH_WATCH_TEST_TYPE);
assert.equal(expected.notification.title, PUSH_DIAGNOSTIC_TITLE);
assert.equal(expected.notification.body, `${PUSH_DIAGNOSTIC_BODY} (test)`);
assert.equal(outcome.history, "recorded");
assertNoSecrets(outcome.report);
assertNoSecrets(logs.join("\n"));
const attempts = getDatabase()
.prepare(
`SELECT user_id, device_id, platform, push_send_id, outcome, fcm_message_id, kind
FROM alert_push_device_sends`
)
.all() as Array<Record<string, unknown>>;
assert.deepEqual(attempts, [
{
user_id: DID,
device_id: DEVICE,
platform: "android",
push_send_id: null,
outcome: "sent",
fcm_message_id: "projects/timesafari-test/messages/msg-1",
kind: "push_watch_test",
},
]);
const capAfter = await alertPushSendsDb.countSince(DID, "2026-11-16T00:00:00.000Z");
assert.equal(capBefore, 1);
assert.equal(capAfter, 1);
const sendRows = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_sends`).get() as { n: number };
assert.equal(sendRows.n, 1);
});
it("does not send when the environment or the registration is refused", async () => {
await register(OTHER, DEVICE);
const sent: unknown[] = [];
const send = async (message: unknown) => {
sent.push(message);
return "msg";
};
const otherDid = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(otherDid.ok, false);
if (!otherDid.ok) assert.equal(otherDid.stage, "registration");
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = "missing-phone";
const missing = await runDiagnosticSend({ userId: DID, deviceId: "missing-phone", now: NOW, send });
assert.equal(missing.ok, false);
if (!missing.ok) assert.equal(missing.stage, "registration");
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = DEVICE;
process.env.MESSAGE_ENV_LABEL = "production";
const production = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(production.ok, false);
if (!production.ok) {
assert.equal(production.stage, "environment");
assert.match(production.report, /production/);
assertNoSecrets(production.report);
}
process.env.MESSAGE_ENV_LABEL = "test";
process.env.PUSH_TEST_FIREBASE_PROJECT_ID = "production-project";
const ambiguous = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(ambiguous.ok, false);
if (!ambiguous.ok) assert.match(ambiguous.report, /ambiguous/);
delete process.env.FIREBASE_SERVICE_ACCOUNT_JSON;
const unknown = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(unknown.ok, false);
if (!unknown.ok) assert.match(unknown.report, /unknown/);
assert.equal(sent.length, 0);
});
it("refuses the send unless the opt-in and the one-device allowlist match exactly", async () => {
await register(DID, DEVICE);
const sent: unknown[] = [];
const send = async (message: unknown) => {
sent.push(message);
return "msg";
};
delete process.env.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED;
const missingFlag = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(missingFlag.ok, false);
if (!missingFlag.ok) {
assert.equal(missingFlag.stage, "safety");
assert.match(missingFlag.report, /PUSH_DIAGNOSTIC_TEST_SEND_ENABLED is unset/);
assertNoSecrets(missingFlag.report);
}
for (const value of ["TRUE", "1", "yes", "true ", " true", "SECRET-OPT-IN"]) {
process.env.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED = value;
const refused = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(refused.ok, false, value);
if (!refused.ok) {
assert.equal(refused.stage, "safety", value);
assert.equal(refused.report.includes("SECRET-OPT-IN"), false);
}
}
process.env.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED = "true";
delete process.env.PUSH_DIAGNOSTIC_ALLOWED_DID;
const missingDid = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(missingDid.ok, false);
if (!missingDid.ok) assert.match(missingDid.report, /PUSH_DIAGNOSTIC_ALLOWED_DID is unset or empty/);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = " ";
const blankDid = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(blankDid.ok, false);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = DID;
delete process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID;
const missingDevice = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(missingDevice.ok, false);
if (!missingDevice.ok) assert.match(missingDevice.report, /PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID is unset or empty/);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = DEVICE;
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = OTHER;
const otherDid = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(otherDid.ok, false);
if (!otherDid.ok) {
assert.equal(otherDid.stage, "safety");
assert.match(otherDid.report, /does not match PUSH_DIAGNOSTIC_ALLOWED_DID/);
}
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = `${DID},${OTHER}`;
const listed = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(listed.ok, false);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DID = DID;
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = "phone-2";
const otherDevice = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(otherDevice.ok, false);
if (!otherDevice.ok) assert.match(otherDevice.report, /does not match PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID/);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = "*";
const wildcard = await runDiagnosticSend({ userId: DID, deviceId: DEVICE, now: NOW, send });
assert.equal(wildcard.ok, false);
process.env.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID = DEVICE;
const inactive = await runDiagnosticSend({
userId: DID,
deviceId: "blank-phone",
now: NOW,
send,
env: {
...process.env,
PUSH_DIAGNOSTIC_ALLOWED_DID: DID,
PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID: "blank-phone",
},
});
await register(DID, "blank-phone", " ");
const inactiveAfter = await runDiagnosticSend({
userId: DID,
deviceId: "blank-phone",
now: NOW,
send,
env: {
...process.env,
PUSH_DIAGNOSTIC_ALLOWED_DID: DID,
PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID: "blank-phone",
},
});
assert.equal(inactive.ok, false);
assert.equal(inactiveAfter.ok, false);
if (!inactiveAfter.ok) assert.equal(inactiveAfter.stage, "registration");
const ambiguous = await runDiagnosticSend({
userId: DID,
deviceId: DEVICE,
now: NOW,
send,
select: async () => ({
ok: false,
reason: "ambiguous",
detail: "Found 2 registrations for this DID and device id. Nothing was sent.",
}),
});
assert.equal(ambiguous.ok, false);
if (!ambiguous.ok) assert.equal(ambiguous.stage, "registration");
assert.equal(sent.length, 0);
for (const outcome of [missingFlag, missingDid, otherDid, otherDevice, inactiveAfter, ambiguous]) {
assertNoSecrets(outcome.ok ? outcome.report : outcome.report);
}
});
it("records a failure without the token and does not resend when history writing throws", async () => {
await register(DID, DEVICE);
let calls = 0;
const logs: string[] = [];
const originalError = console.error;
console.error = (...args: unknown[]) => {
logs.push(args.map(String).join(" "));
};
let outcome;
try {
outcome = await runDiagnosticSend({
userId: DID,
deviceId: DEVICE,
now: NOW,
send: async () => {
calls += 1;
throw Object.assign(new Error(`token ${TOKEN} jwt ${JWT} rejected`), {
code: "messaging/invalid-argument",
});
},
record: async () => {
throw Object.assign(new Error(`disk full ${TOKEN}`), { code: "SQLITE_FULL" });
},
});
} finally {
console.error = originalError;
}
assert.equal(calls, 1);
assert.equal(outcome.ok, true);
if (!outcome.ok) return;
assert.equal(outcome.status, "failed");
assert.equal(outcome.history, "failed");
assert.match(outcome.report, /messaging\/invalid-argument/);
assert.match(outcome.report, /nothing was sent again/);
assertNoSecrets(outcome.report);
assertNoSecrets(logs.join("\n"));
assert.match(logs.join("\n"), /messaging\/invalid-argument/);
assert.match(logs.join("\n"), /SQLITE_FULL/);
const attempts = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_device_sends`).get() as { n: number };
const sends = getDatabase().prepare(`SELECT COUNT(*) AS n FROM alert_push_sends`).get() as { n: number };
assert.equal(attempts.n, 0);
assert.equal(sends.n, 0);
});
});
describe("push preview and test commands", () => {
let dir: string;
beforeEach(async () => {
dir = await mkdtemp(path.join(tmpdir(), "push-diagnostic-cli-"));
});
afterEach(async () => {
closeDatabase();
await rm(dir, { recursive: true, force: true });
});
function run(script: string, args: string[], env: NodeJS.ProcessEnv): { status: number | null; stdout: string; stderr: string } {
const result = spawnSync(tsx, [path.join(root, script), ...args], {
cwd: root,
encoding: "utf8",
timeout: 20000,
env,
});
return { status: result.status, stdout: result.stdout ?? "", stderr: result.stderr ?? "" };
}
it("requires a DID for preview and does not initialize Firebase", async () => {
const missing = run("src/tools/pushPreview.ts", [], { ...process.env, NOTIFY_DATA_DIR: dir });
assert.equal(missing.status, 1);
assert.match(missing.stderr, /--did/);
assert.match(missing.stderr, /Nothing was sent/);
const databaseDir = path.join(dir, "db");
process.env.NOTIFY_DATA_DIR = databaseDir;
closeDatabase();
await db.upsert({
userId: DID,
deviceId: DEVICE,
fcmToken: TOKEN,
platform: "android",
updatedAt: NOW,
});
closeDatabase();
delete process.env.NOTIFY_DATA_DIR;
const previewEnv: NodeJS.ProcessEnv = {
...process.env,
NOTIFY_DATA_DIR: databaseDir,
MESSAGE_ENV_LABEL: "test",
PUSH_TEST_FIREBASE_PROJECT_ID: PROJECT,
// A credential Firebase could not initialize. Preview must not load it.
FIREBASE_SERVICE_ACCOUNT_JSON: JSON.stringify({ project_id: PROJECT }),
};
delete previewEnv.PUSH_DIAGNOSTIC_TEST_SEND_ENABLED;
delete previewEnv.PUSH_DIAGNOSTIC_ALLOWED_DID;
delete previewEnv.PUSH_DIAGNOSTIC_ALLOWED_DEVICE_ID;
const preview = run("src/tools/pushPreview.ts", ["--did", DID], previewEnv);
assert.equal(preview.status, 0, preview.stderr);
assert.match(preview.stdout, /Preview only\. No FCM message was sent\./);
assert.match(preview.stdout, /Diagnostic send opt-in: not enabled/);
assert.match(preview.stdout, /safety configuration: incomplete/);
assert.match(preview.stdout, /does not distinguish test from production/);
assert.match(preview.stdout, new RegExp(`Data type: ${PUSH_WATCH_TEST_TYPE}`));
assert.match(preview.stdout, new RegExp(PUSH_DIAGNOSTIC_TITLE));
assert.equal(preview.stdout.includes(TOKEN), false);
assert.equal(preview.stderr.includes("[Firebase]"), false);
assert.doesNotMatch(preview.stdout, /Data type: alert_search/);
const tool = readFileSync(path.join(root, "src/tools/pushPreview.ts"), "utf8");
assert.equal(tool.includes("firebase"), false);
assert.equal(tool.includes("messaging"), false);
});
it("requires both a DID and a device id before a test send", () => {
const env = { ...process.env, NOTIFY_DATA_DIR: dir, MESSAGE_ENV_LABEL: "production" };
const noDid = run("src/tools/pushTest.ts", ["--device-id", DEVICE], env);
assert.equal(noDid.status, 1);
assert.match(noDid.stderr, /--did/);
assert.match(noDid.stderr, /--device-id/);
assert.match(noDid.stderr, /Nothing was sent/);
const noDevice = run("src/tools/pushTest.ts", ["--did", DID], env);
assert.equal(noDevice.status, 1);
assert.match(noDevice.stderr, /Nothing was sent/);
assert.equal(noDid.stdout.includes(TOKEN), false);
assert.equal(noDevice.stderr.includes("[Firebase]"), false);
});
});
+39
View File
@@ -0,0 +1,39 @@
import assert from "node:assert/strict";
import { mock } from "node:test";
import { describe, it } from "node:test";
import { messaging } from "../../src/services/firebase.js";
import { sendAlertSearchPushToDevice } from "../../src/services/pushService.js";
const TOKEN = "fcm-registration-token-SECRETVALUE";
describe("sendAlertSearchPushToDevice failure log", () => {
it("logs the FCM code and sanitized detail, not the registration token", async () => {
const err = new Error(`The registration token ${TOKEN} is not a valid FCM registration token`);
(err as { code?: string }).code = "messaging/invalid-registration-token";
const send = mock.method(messaging, "send", () => {
throw err;
});
const lines: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => {
lines.push(args.map((arg) => String(arg)).join(" "));
};
try {
const result = await sendAlertSearchPushToDevice(TOKEN, { title: "t", body: "b" });
assert.equal(result.status, "failed");
assert.equal(result.errorCode, "messaging/invalid-registration-token");
assert.equal(result.errorDetail?.includes(TOKEN), false);
assert.match(result.errorDetail ?? "", /\[redacted\]/);
const logged = lines.join("\n");
assert.match(logged, /\[AlertSearchPush\] Send failed in/);
assert.match(logged, /token suffix:/);
assert.match(logged, /messaging\/invalid-registration-token/);
assert.match(logged, /The registration token \[redacted\] is not a valid FCM registration token/);
assert.equal(logged.includes(TOKEN), false);
assert.equal(send.mock.calls.length, 1);
} finally {
console.error = original;
send.mock.restore();
}
});
});
File diff suppressed because it is too large Load Diff
+58
View File
@@ -0,0 +1,58 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
FCM_ERROR_CODE_MAX_CHARS,
FCM_ERROR_DETAIL_MAX_CHARS,
fcmFailureParts,
formatFcmFailureLog,
sanitizeFcmDiagnostic,
} from "../../src/util/fcmOutcome.js";
const TOKEN = "fcm-token-abcdefghij";
describe("FCM diagnostic text", () => {
it("removes a registration token and collapses control characters", () => {
const text = sanitizeFcmDiagnostic(`bad\n\t${TOKEN}\r twice ${TOKEN}`, TOKEN);
assert.equal(text.includes(TOKEN), false);
assert.equal(text, "bad [redacted] twice [redacted]");
});
it("truncates a long description", () => {
const text = sanitizeFcmDiagnostic("x".repeat(FCM_ERROR_DETAIL_MAX_CHARS + 40));
assert.equal(text.length, FCM_ERROR_DETAIL_MAX_CHARS);
assert.equal(text.endsWith("…"), true);
assert.equal(text.startsWith("x"), true);
});
it("keeps a short error code and drops an empty message", () => {
const err = new Error(" ");
(err as { code?: string }).code = "messaging/invalid-argument";
const parts = fcmFailureParts(err);
assert.equal(parts.errorCode, "messaging/invalid-argument");
assert.equal(parts.errorDetail, undefined);
});
it("omits a missing error code and strips the token from the message", () => {
const err = new Error(`rejected ${TOKEN} now`);
const parts = fcmFailureParts(err, TOKEN);
assert.equal(parts.errorCode, undefined);
assert.equal(parts.errorDetail, "rejected [redacted] now");
assert.equal(parts.errorDetail?.includes(TOKEN), false);
});
it("formats a log line with the code and detail and without the token", () => {
const err = new Error(`rejected ${TOKEN} now`);
(err as { code?: string }).code = "messaging/registration-token-not-registered";
const line = formatFcmFailureLog(err, TOKEN);
assert.equal(line.includes(TOKEN), false);
assert.equal(line, "messaging/registration-token-not-registered: rejected [redacted] now");
});
it("truncates an oversized error code", () => {
const err = new Error("no");
(err as { code?: string }).code = "c".repeat(FCM_ERROR_CODE_MAX_CHARS + 10);
const parts = fcmFailureParts(err);
assert.equal(parts.errorCode?.length, FCM_ERROR_CODE_MAX_CHARS);
assert.equal(parts.errorCode?.endsWith("…"), true);
});
});