Skip to content

Commit e4be495

Browse files
committed
fix(mobile): retain push registration when channel lookup fails
1 parent 1354c06 commit e4be495

2 files changed

Lines changed: 62 additions & 54 deletions

File tree

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -732,63 +732,71 @@ describe("makeRelayDeviceRegistrationRequest", () => {
732732
);
733733
},
734734
);
735-
it.effect("keeps Android push delivery registered when only the alert channel is blocked", () => {
736-
vi.mocked(loadPreferences).mockResolvedValue({ liveActivitiesEnabled: true });
737-
vi.spyOn(Platform, "OS", "get").mockReturnValue("android");
738-
vi.spyOn(Platform, "Version", "get").mockReturnValue(36);
739-
vi.mocked(Notifications.getDevicePushTokenAsync).mockResolvedValue({
740-
type: "android",
741-
data: "fcm-token",
742-
});
743-
vi.mocked(Notifications.getNotificationChannelAsync).mockResolvedValue({
744-
id: "agent-alerts",
745-
name: "Agent alerts",
746-
importance: Notifications.AndroidImportance.NONE,
747-
} as Awaited<ReturnType<typeof Notifications.getNotificationChannelAsync>>);
748-
const registrations: unknown[] = [];
749-
vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => {
750-
const request = new Request(input, init);
751-
if (request.url.endsWith("/v1/client/dpop-token")) {
752-
return Response.json({
753-
access_token: "dpop",
754-
issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
755-
token_type: "DPoP",
756-
expires_in: 300,
757-
scope: "mobile:registration",
758-
});
735+
it.effect.each(["blocked", "unavailable"] as const)(
736+
"keeps Android push delivery registered when the alert channel is %s",
737+
(channelState) => {
738+
vi.mocked(loadPreferences).mockResolvedValue({ liveActivitiesEnabled: true });
739+
vi.spyOn(Platform, "OS", "get").mockReturnValue("android");
740+
vi.spyOn(Platform, "Version", "get").mockReturnValue(36);
741+
vi.mocked(Notifications.getDevicePushTokenAsync).mockResolvedValue({
742+
type: "android",
743+
data: "fcm-token",
744+
});
745+
vi.mocked(Notifications.getNotificationChannelAsync).mockResolvedValue({
746+
id: "agent-alerts",
747+
name: "Agent alerts",
748+
importance: Notifications.AndroidImportance.NONE,
749+
} as Awaited<ReturnType<typeof Notifications.getNotificationChannelAsync>>);
750+
if (channelState === "unavailable") {
751+
vi.mocked(Notifications.getNotificationChannelAsync).mockRejectedValue(
752+
new Error("Android channel lookup failed"),
753+
);
759754
}
760-
registrations.push(await request.json());
761-
return Response.json({ ok: true });
762-
});
763-
Constants.expoConfig!.extra = { relay: { url: "https://relay.example.test" } };
764-
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk"), "user-a");
765-
return Effect.gen(function* () {
766-
yield* runBackgroundOperations();
767-
expect(registrations.at(-1)).toMatchObject({
768-
pushToken: "fcm-token",
769-
preferences: { notificationsEnabled: false, liveActivitiesEnabled: true },
755+
const registrations: unknown[] = [];
756+
vi.stubGlobal("fetch", async (input: RequestInfo | URL, init?: RequestInit) => {
757+
const request = new Request(input, init);
758+
if (request.url.endsWith("/v1/client/dpop-token")) {
759+
return Response.json({
760+
access_token: "dpop",
761+
issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
762+
token_type: "DPoP",
763+
expires_in: 300,
764+
scope: "mobile:registration",
765+
});
766+
}
767+
registrations.push(await request.json());
768+
return Response.json({ ok: true });
770769
});
771-
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
770+
Constants.expoConfig!.extra = { relay: { url: "https://relay.example.test" } };
771+
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk"), "user-a");
772+
return Effect.gen(function* () {
773+
yield* runBackgroundOperations();
774+
expect(registrations.at(-1)).toMatchObject({
775+
pushToken: "fcm-token",
776+
preferences: { notificationsEnabled: false, liveActivitiesEnabled: true },
777+
});
778+
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
772779

773-
const listener = vi.mocked(Notifications.addPushTokenListener).mock.calls.at(-1)![0];
774-
listener({ type: "android", data: "rotated-fcm-token" });
775-
yield* runBackgroundOperations();
776-
expect(registrations.at(-1)).toMatchObject({
777-
pushToken: "rotated-fcm-token",
778-
preferences: { notificationsEnabled: false, liveActivitiesEnabled: true },
779-
});
780+
const listener = vi.mocked(Notifications.addPushTokenListener).mock.calls.at(-1)![0];
781+
listener({ type: "android", data: "rotated-fcm-token" });
782+
yield* runBackgroundOperations();
783+
expect(registrations.at(-1)).toMatchObject({
784+
pushToken: "rotated-fcm-token",
785+
preferences: { notificationsEnabled: false, liveActivitiesEnabled: true },
786+
});
780787

781-
vi.mocked(Notifications.getNotificationChannelAsync).mockResolvedValue(null);
782-
yield* refreshAgentAwarenessRegistration();
783-
expect(registrations.at(-1)).toMatchObject({
784-
pushToken: "fcm-token",
785-
preferences: { notificationsEnabled: true },
786-
});
787-
}).pipe(
788-
Effect.provideService(FetchHttpClient.Fetch, globalThis.fetch),
789-
Effect.provide(layerRelayTest),
790-
);
791-
});
788+
vi.mocked(Notifications.getNotificationChannelAsync).mockResolvedValue(null);
789+
yield* refreshAgentAwarenessRegistration();
790+
expect(registrations.at(-1)).toMatchObject({
791+
pushToken: "fcm-token",
792+
preferences: { notificationsEnabled: true },
793+
});
794+
}).pipe(
795+
Effect.provideService(FetchHttpClient.Fetch, globalThis.fetch),
796+
Effect.provide(layerRelayTest),
797+
);
798+
},
799+
);
792800
it.effect("preserves relay rejection errors with React Native response headers", () => {
793801
vi.spyOn(Platform, "OS", "get").mockReturnValue("android");
794802
vi.mocked(Notifications.getDevicePushTokenAsync).mockResolvedValue({

‎apps/mobile/src/features/agent-awareness/remoteRegistration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ const nativePushTokenRegistration = Effect.fn("remoteRegistration.nativePushToke
274274
operation: "read-notification-permissions",
275275
cause,
276276
}),
277-
})
277+
}).pipe(Effect.orElseSucceed(() => false))
278278
: true);
279279
if (observedPushToken) {
280280
return {

0 commit comments

Comments
 (0)