|
1 | | -import { GenericError, createHostApi, createTransport, enumValue } from '@novasamatech/host-api'; |
| 1 | +import { GenericError, PushNotificationError, createHostApi, createTransport, enumValue } from '@novasamatech/host-api'; |
2 | 2 | import type { ContainerHandlerOf } from '@novasamatech/host-container'; |
3 | 3 | import { createContainer } from '@novasamatech/host-container'; |
4 | 4 |
|
@@ -124,3 +124,176 @@ describe('Host API: PushNotification', () => { |
124 | 124 | expect(handler).not.toHaveBeenCalled(); |
125 | 125 | }); |
126 | 126 | }); |
| 127 | + |
| 128 | +describe('Host API: PushNotification (v2)', () => { |
| 129 | + it('should deliver an immediate notification and return a NotificationId', async () => { |
| 130 | + const { container, hostApi } = setup(); |
| 131 | + const payload = { text: 'Hello v2', deeplink: 'https://example.com/x', scheduledAt: undefined }; |
| 132 | + |
| 133 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 134 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationV2>>((_, { ok }) => ok(42)); |
| 135 | + container.handlePushNotificationV2(handler); |
| 136 | + |
| 137 | + const result = await hostApi.pushNotification(enumValue('v2', payload)); |
| 138 | + |
| 139 | + result.match( |
| 140 | + ok => { |
| 141 | + expect(ok.tag).toBe('v2'); |
| 142 | + expect(ok.value).toBe(42); |
| 143 | + }, |
| 144 | + () => { |
| 145 | + throw new Error('Expected success'); |
| 146 | + }, |
| 147 | + ); |
| 148 | + expect(handler).toBeCalledWith(payload, { ok: expect.any(Function), err: expect.any(Function) }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should deliver a scheduled notification carrying scheduledAt as a u64', async () => { |
| 152 | + const { container, hostApi } = setup(); |
| 153 | + const scheduledAt = BigInt(Date.UTC(2027, 0, 1)); |
| 154 | + const payload = { text: 'reminder', deeplink: undefined, scheduledAt }; |
| 155 | + |
| 156 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 157 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationV2>>((_, { ok }) => ok(7)); |
| 158 | + container.handlePushNotificationV2(handler); |
| 159 | + |
| 160 | + const result = await hostApi.pushNotification(enumValue('v2', payload)); |
| 161 | + |
| 162 | + expect(result.isOk()).toBe(true); |
| 163 | + expect(handler).toBeCalledWith(payload, { ok: expect.any(Function), err: expect.any(Function) }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should propagate ScheduleLimitReached', async () => { |
| 167 | + const { container, hostApi } = setup(); |
| 168 | + const payload = { text: 'full queue', deeplink: undefined, scheduledAt: BigInt(2_000_000_000_000) }; |
| 169 | + const error = new PushNotificationError.ScheduleLimitReached(); |
| 170 | + |
| 171 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 172 | + container.handlePushNotificationV2((_, { err }) => err(error)); |
| 173 | + |
| 174 | + const result = await hostApi.pushNotification(enumValue('v2', payload)); |
| 175 | + |
| 176 | + result.match( |
| 177 | + () => { |
| 178 | + throw new Error('Expected failure'); |
| 179 | + }, |
| 180 | + failure => { |
| 181 | + expect(failure.tag).toBe('v2'); |
| 182 | + expect(failure.value).toEqual(error); |
| 183 | + }, |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should propagate Unknown { reason } round-trip', async () => { |
| 188 | + const { container, hostApi } = setup(); |
| 189 | + const payload = { text: 'boom', deeplink: undefined, scheduledAt: undefined }; |
| 190 | + const error = new PushNotificationError.Unknown({ reason: 'OS rejected' }); |
| 191 | + |
| 192 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 193 | + container.handlePushNotificationV2((_, { err }) => err(error)); |
| 194 | + |
| 195 | + const result = await hostApi.pushNotification(enumValue('v2', payload)); |
| 196 | + |
| 197 | + result.match( |
| 198 | + () => { |
| 199 | + throw new Error('Expected failure'); |
| 200 | + }, |
| 201 | + failure => { |
| 202 | + expect(failure.tag).toBe('v2'); |
| 203 | + expect(failure.value).toEqual(error); |
| 204 | + }, |
| 205 | + ); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should reject v2 when Notifications permission is denied', async () => { |
| 209 | + const { container, hostApi } = setup(); |
| 210 | + const payload = { text: 'blocked v2', deeplink: undefined, scheduledAt: undefined }; |
| 211 | + |
| 212 | + container.handleDevicePermission((_, { ok }) => ok(false)); |
| 213 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationV2>>((_, { ok }) => ok(1)); |
| 214 | + container.handlePushNotificationV2(handler); |
| 215 | + |
| 216 | + const result = await hostApi.pushNotification(enumValue('v2', payload)); |
| 217 | + |
| 218 | + expect(result.isErr()).toBe(true); |
| 219 | + result.match( |
| 220 | + () => { |
| 221 | + throw new Error('Expected failure'); |
| 222 | + }, |
| 223 | + failure => { |
| 224 | + expect(failure.tag).toBe('v2'); |
| 225 | + expect(failure.value).toBeInstanceOf(PushNotificationError); |
| 226 | + }, |
| 227 | + ); |
| 228 | + expect(handler).not.toHaveBeenCalled(); |
| 229 | + }); |
| 230 | +}); |
| 231 | + |
| 232 | +describe('Host API: PushNotificationCancel', () => { |
| 233 | + it('should cancel a pending notification by id', async () => { |
| 234 | + const { container, hostApi } = setup(); |
| 235 | + |
| 236 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 237 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationCancel>>((_, { ok }) => |
| 238 | + ok(undefined), |
| 239 | + ); |
| 240 | + container.handlePushNotificationCancel(handler); |
| 241 | + |
| 242 | + const result = await hostApi.pushNotificationCancel(enumValue('v1', 42)); |
| 243 | + |
| 244 | + expect(result.isOk()).toBe(true); |
| 245 | + expect(handler).toBeCalledWith(42, { ok: expect.any(Function), err: expect.any(Function) }); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should transport ok for any id without inspecting it (host enforces RFC §5 idempotency)', async () => { |
| 249 | + const { container, hostApi } = setup(); |
| 250 | + |
| 251 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 252 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationCancel>>((_, { ok }) => |
| 253 | + ok(undefined), |
| 254 | + ); |
| 255 | + container.handlePushNotificationCancel(handler); |
| 256 | + |
| 257 | + // The SDK does NOT enforce idempotency itself — that's the host's responsibility per RFC §5. |
| 258 | + // This test only verifies the wire faithfully carries an arbitrary id and the host's Ok response. |
| 259 | + const result = await hostApi.pushNotificationCancel(enumValue('v1', 99999)); |
| 260 | + |
| 261 | + expect(result.isOk()).toBe(true); |
| 262 | + expect(handler).toBeCalledWith(99999, { ok: expect.any(Function), err: expect.any(Function) }); |
| 263 | + }); |
| 264 | + |
| 265 | + it('should propagate GenericError when the host returns one', async () => { |
| 266 | + const { container, hostApi } = setup(); |
| 267 | + const error = new GenericError({ reason: 'cancel failed' }); |
| 268 | + |
| 269 | + container.handleDevicePermission((_, { ok }) => ok(true)); |
| 270 | + container.handlePushNotificationCancel((_, { err }) => err(error)); |
| 271 | + |
| 272 | + const result = await hostApi.pushNotificationCancel(enumValue('v1', 1)); |
| 273 | + |
| 274 | + result.match( |
| 275 | + () => { |
| 276 | + throw new Error('Expected failure'); |
| 277 | + }, |
| 278 | + failure => { |
| 279 | + expect(failure.tag).toBe('v1'); |
| 280 | + expect(failure.value).toEqual(error); |
| 281 | + }, |
| 282 | + ); |
| 283 | + }); |
| 284 | + |
| 285 | + it('should reject cancel when Notifications permission is denied', async () => { |
| 286 | + const { container, hostApi } = setup(); |
| 287 | + |
| 288 | + container.handleDevicePermission((_, { ok }) => ok(false)); |
| 289 | + const handler = vi.fn<ContainerHandlerOf<typeof container.handlePushNotificationCancel>>((_, { ok }) => |
| 290 | + ok(undefined), |
| 291 | + ); |
| 292 | + container.handlePushNotificationCancel(handler); |
| 293 | + |
| 294 | + const result = await hostApi.pushNotificationCancel(enumValue('v1', 5)); |
| 295 | + |
| 296 | + expect(result.isErr()).toBe(true); |
| 297 | + expect(handler).not.toHaveBeenCalled(); |
| 298 | + }); |
| 299 | +}); |
0 commit comments