|
@@ -0,0 +1,235 @@
|
|
|
|
|
+import { TestBed } from '@angular/core/testing';
|
|
|
|
|
+import { Router } from '@angular/router';
|
|
|
|
|
+import { AppNotificationService } from './app-notification.service';
|
|
|
|
|
+import { DeepLinkService } from './deep-link.service';
|
|
|
|
|
+import { JPUSH_OPTIONS, JPUSH_PLUGIN, JPUSH_SUPPORTED, JPushNotice, JPushPlugin } from './jpush.plugin';
|
|
|
|
|
+import { JPushService } from './jpush.service';
|
|
|
|
|
+
|
|
|
|
|
+describe('JPushService', () => {
|
|
|
|
|
+ let service: JPushService;
|
|
|
|
|
+ let plugin: jasmine.SpyObj<JPushPlugin>;
|
|
|
|
|
+ let notifications: jasmine.SpyObj<AppNotificationService>;
|
|
|
|
|
+ let router: jasmine.SpyObj<Router>;
|
|
|
|
|
+ let options: { enabled: boolean };
|
|
|
|
|
+ let supported: boolean;
|
|
|
|
|
+ let listeners: Map<string, (data: never) => void>;
|
|
|
|
|
+ let removed: jasmine.Spy;
|
|
|
|
|
+
|
|
|
|
|
+ beforeEach(() => {
|
|
|
|
|
+ options = { enabled: true };
|
|
|
|
|
+ supported = true;
|
|
|
|
|
+ listeners = new Map();
|
|
|
|
|
+ removed = jasmine.createSpy('remove').and.resolveTo();
|
|
|
|
|
+ plugin = jasmine.createSpyObj<JPushPlugin>('FmodeJPush', [
|
|
|
|
|
+ 'getStatus', 'start', 'stop', 'getRegistrationId', 'setAlias', 'deleteAlias', 'addListener',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ plugin.getStatus.and.resolveTo({ available: true, started: false });
|
|
|
|
|
+ plugin.start.and.resolveTo({ available: true, started: true });
|
|
|
|
|
+ plugin.stop.and.resolveTo();
|
|
|
|
|
+ plugin.getRegistrationId.and.resolveTo({ registrationId: null });
|
|
|
|
|
+ plugin.setAlias.and.resolveTo();
|
|
|
|
|
+ plugin.deleteAlias.and.resolveTo();
|
|
|
|
|
+ plugin.addListener.and.callFake(async (event, callback) => {
|
|
|
|
|
+ listeners.set(event, callback);
|
|
|
|
|
+ return { remove: removed };
|
|
|
|
|
+ });
|
|
|
|
|
+ notifications = jasmine.createSpyObj<AppNotificationService>('notifications', ['notify', 'request']);
|
|
|
|
|
+ notifications.notify.and.resolveTo();
|
|
|
|
|
+ notifications.request.and.resolveTo(true);
|
|
|
|
|
+ router = jasmine.createSpyObj<Router>('router', ['navigateByUrl']);
|
|
|
|
|
+ router.navigateByUrl.and.resolveTo(true);
|
|
|
|
|
+ TestBed.configureTestingModule({ providers: [
|
|
|
|
|
+ JPushService, DeepLinkService,
|
|
|
|
|
+ { provide: JPUSH_OPTIONS, useValue: options },
|
|
|
|
|
+ { provide: JPUSH_SUPPORTED, useValue: () => supported },
|
|
|
|
|
+ { provide: JPUSH_PLUGIN, useValue: plugin },
|
|
|
|
|
+ { provide: AppNotificationService, useValue: notifications },
|
|
|
|
|
+ { provide: Router, useValue: router },
|
|
|
|
|
+ ] });
|
|
|
|
|
+ service = TestBed.inject(JPushService);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ function emit(event: string, data: unknown): void {
|
|
|
|
|
+ listeners.get(event)?.(data as never);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function notice(overrides: Partial<JPushNotice> = {}): JPushNotice {
|
|
|
|
|
+ return { kind: 'message', messageId: 'event-one', title: '任务完成', body: '完成', ...overrides };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ it('defaults to disabled without touching the bridge', async () => {
|
|
|
|
|
+ options.enabled = false;
|
|
|
|
|
+ expect(await service.start(true)).toBeFalse();
|
|
|
|
|
+ expect(service.phase()).toBe('disabled');
|
|
|
|
|
+ expect(plugin.getStatus).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(plugin.start).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(notifications.request).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('requires explicit privacy consent', async () => {
|
|
|
|
|
+ expect(await service.start()).toBeFalse();
|
|
|
|
|
+ expect(service.reason()).toBe('privacy-consent-required');
|
|
|
|
|
+ expect(plugin.getStatus).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('does not start on web or without native registration', async () => {
|
|
|
|
|
+ supported = false;
|
|
|
|
|
+ expect(await service.start(true)).toBeFalse();
|
|
|
|
|
+ expect(service.reason()).toBe('native-plugin-unavailable');
|
|
|
|
|
+ expect(plugin.start).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('reports missing AppKey without starting SDK or listeners', async () => {
|
|
|
|
|
+ plugin.getStatus.and.resolveTo({ available: false, started: false, reason: 'missing-or-invalid-appkey' });
|
|
|
|
|
+ expect(await service.start(true)).toBeFalse();
|
|
|
|
|
+ expect(service.reason()).toBe('missing-or-invalid-appkey');
|
|
|
|
|
+ expect(plugin.start).not.toHaveBeenCalled();
|
|
|
|
|
+ expect(plugin.addListener).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('starts once, clears stale alias and never treats an empty registration as registered', async () => {
|
|
|
|
|
+ const first = service.start(true);
|
|
|
|
|
+ expect(service.start(true)).toBe(first);
|
|
|
|
|
+ expect(await first).toBeTrue();
|
|
|
|
|
+ expect(await service.start(true)).toBeTrue();
|
|
|
|
|
+ expect(plugin.start).toHaveBeenCalledOnceWith({ privacyConsent: true });
|
|
|
|
|
+ expect(plugin.deleteAlias).toHaveBeenCalledTimes(1);
|
|
|
|
|
+ expect(service.registrationId()).toBeNull();
|
|
|
|
|
+ emit('registration', { registrationId: 'registration-test-only' });
|
|
|
|
|
+ expect(service.registrationId()).toBe('registration-test-only');
|
|
|
|
|
+ expect(notifications.request).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('cleans partial listeners and can retry startup', async () => {
|
|
|
|
|
+ plugin.addListener.and.rejectWith(new Error('listener-unavailable'));
|
|
|
|
|
+ expect(await service.start(true)).toBeFalse();
|
|
|
|
|
+ expect(service.phase()).toBe('error');
|
|
|
|
|
+ expect(service.reason()).toBe('native-start-failed');
|
|
|
|
|
+ plugin.addListener.and.callFake(async (event, callback) => {
|
|
|
|
|
+ listeners.set(event, callback);
|
|
|
|
|
+ return { remove: removed };
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(await service.start(true)).toBeTrue();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('stops if stale alias cannot be cleared', async () => {
|
|
|
|
|
+ plugin.deleteAlias.and.rejectWith(new Error('offline'));
|
|
|
|
|
+ expect(await service.start(true)).toBeFalse();
|
|
|
|
|
+ expect(plugin.stop).toHaveBeenCalled();
|
|
|
|
|
+ expect(removed).toHaveBeenCalledTimes(3);
|
|
|
|
|
+ expect(service.registrationId()).toBeNull();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('does not redisplay SDK-rendered notification and suppresses a paired data message', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ emit('notificationReceived', notice({ kind: 'notification', businessKey: 'business-one' }));
|
|
|
|
|
+ emit('notificationReceived', notice({ messageId: 'data-one', businessKey: 'business-one' }));
|
|
|
|
|
+ expect(notifications.notify).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('reuses local notifications once per event and never navigates on arrival', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ const data = notice({ deepLink: 'fmode://chat/agent/session' });
|
|
|
|
|
+ emit('notificationReceived', data);
|
|
|
|
|
+ emit('notificationReceived', data);
|
|
|
|
|
+ expect(notifications.notify).toHaveBeenCalledOnceWith({
|
|
|
|
|
+ title: '任务完成', body: '完成', deepLink: data.deepLink, key: 'jpush:event-one',
|
|
|
|
|
+ });
|
|
|
|
|
+ expect(router.navigateByUrl).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('deduplicates migrated local producers and remote messages using an explicit business event key', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ await service.notifyLocal({ title: '任务完成', body: '完成', key: 'task-group' }, 'task-event-one');
|
|
|
|
|
+ emit('notificationReceived', notice({ businessKey: 'task-event-one' }));
|
|
|
|
|
+ expect(notifications.notify).toHaveBeenCalledTimes(1);
|
|
|
|
|
+ await service.notifyLocal({ title: '任务完成', body: '另一任务完成', key: 'task-group' }, 'task-event-two');
|
|
|
|
|
+ expect(notifications.notify).toHaveBeenCalledTimes(2);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('does not confuse notification replacement keys with event deduplication keys', async () => {
|
|
|
|
|
+ const data = { title: '任务完成', body: '完成', key: 'same-session' };
|
|
|
|
|
+ await service.notifyLocal(data);
|
|
|
|
|
+ await service.notifyLocal(data);
|
|
|
|
|
+ expect(notifications.notify).toHaveBeenCalledTimes(2);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('deduplicates clicks separately from receipt and uses existing DeepLinkService', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ const data = notice({ kind: 'notification', deepLink: 'fmode://chat/agent/session' });
|
|
|
|
|
+ emit('notificationReceived', data);
|
|
|
|
|
+ emit('notificationOpened', data);
|
|
|
|
|
+ emit('notificationOpened', data);
|
|
|
|
|
+ expect(router.navigateByUrl).toHaveBeenCalledOnceWith('/mobile/chat/agent/session');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('rejects external URLs and malformed percent-encoded deep links', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ emit('notificationOpened', notice({ deepLink: 'https://example.invalid' }));
|
|
|
|
|
+ emit('notificationOpened', notice({ deepLink: 'fmode://chat/%ZZ' }));
|
|
|
|
|
+ emit('notificationOpened', notice({ deepLink: 'javascript:alert(1)' }));
|
|
|
|
|
+ expect(router.navigateByUrl).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('serializes account switches and waits for native acknowledgment', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ const firstAlias = `fm_${'a'.repeat(32)}`;
|
|
|
|
|
+ const secondAlias = `fm_${'b'.repeat(32)}`;
|
|
|
|
|
+ let acknowledge: (() => void) | undefined;
|
|
|
|
|
+ plugin.setAlias.and.callFake(() => new Promise<void>((resolve) => { acknowledge = resolve; }));
|
|
|
|
|
+ const first = service.bindAccount(firstAlias);
|
|
|
|
|
+ const second = service.bindAccount(secondAlias);
|
|
|
|
|
+ await Promise.resolve();
|
|
|
|
|
+ await Promise.resolve();
|
|
|
|
|
+ expect(service.binding()).toBe('binding');
|
|
|
|
|
+ expect(plugin.setAlias).toHaveBeenCalledOnceWith({ alias: firstAlias });
|
|
|
|
|
+ acknowledge!();
|
|
|
|
|
+ expect(await first).toBeTrue();
|
|
|
|
|
+ await Promise.resolve();
|
|
|
|
|
+ await Promise.resolve();
|
|
|
|
|
+ expect(plugin.setAlias).toHaveBeenCalledWith({ alias: secondAlias });
|
|
|
|
|
+ acknowledge!();
|
|
|
|
|
+ expect(await second).toBeTrue();
|
|
|
|
|
+ expect(service.binding()).toBe('bound');
|
|
|
|
|
+ expect(plugin.deleteAlias).toHaveBeenCalledTimes(3);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('rejects non-opaque aliases without forwarding user identifiers', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ expect(await service.bindAccount('user-name')).toBeFalse();
|
|
|
|
|
+ expect(await service.bindAccount('session-token')).toBeFalse();
|
|
|
|
|
+ expect(plugin.setAlias).not.toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('clears account binding on logout and stops if unbinding fails', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ expect(await service.bindAccount(`fm_${'a'.repeat(32)}`)).toBeTrue();
|
|
|
|
|
+ plugin.deleteAlias.and.rejectWith(new Error('offline'));
|
|
|
|
|
+ expect(await service.unbindAccount()).toBeFalse();
|
|
|
|
|
+ expect(service.binding()).toBe('error');
|
|
|
|
|
+ expect(service.phase()).toBe('error');
|
|
|
|
|
+ expect(service.reason()).toBe('alias-operation-failed');
|
|
|
|
|
+ expect(plugin.stop).toHaveBeenCalled();
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('unbinds and removes listeners when stopped', async () => {
|
|
|
|
|
+ await service.start(true);
|
|
|
|
|
+ await service.stop();
|
|
|
|
|
+ expect(plugin.deleteAlias).toHaveBeenCalledTimes(2);
|
|
|
|
|
+ expect(plugin.stop).toHaveBeenCalled();
|
|
|
|
|
+ expect(removed).toHaveBeenCalledTimes(3);
|
|
|
|
|
+ expect(service.registrationId()).toBeNull();
|
|
|
|
|
+ expect(service.phase()).toBe('disabled');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('reuses the existing permission request only when explicitly requested', async () => {
|
|
|
|
|
+ expect(await service.requestPermission()).toBeTrue();
|
|
|
|
|
+ expect(notifications.request).toHaveBeenCalledTimes(1);
|
|
|
|
|
+ });
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+describe('JPush default configuration', () => {
|
|
|
|
|
+ it('is disabled without an application-level override', () => {
|
|
|
|
|
+ expect(TestBed.inject(JPUSH_OPTIONS)).toEqual({ enabled: false });
|
|
|
|
|
+ });
|
|
|
|
|
+});
|