Files
Genarrative/src/services/wechatMiniProgramSubscribe.test.ts
kdletters 59b5bd1f83 修复小程序生成前订阅授权体验
生成前订阅授权页自动弹出微信授权框

授权返回或跳过后继续执行拼图生成提交

避免订阅页改写上一页 web-view URL 导致回首页

更新订阅消息生成前授权与终态发送文档口径
2026-06-08 13:48:49 +08:00

85 lines
2.3 KiB
TypeScript

/* @vitest-environment jsdom */
import { afterEach, describe, expect, test, vi } from 'vitest';
import {
requestGenerationResultSubscribePermission,
} from './wechatMiniProgramSubscribe';
describe('wechatMiniProgramSubscribe', () => {
afterEach(() => {
window.history.replaceState(null, '', '/');
window.wx = undefined;
});
test('requests generation result subscription permission through native mini program page and resumes generation after return', async () => {
const navigateTo = vi.fn((options) => {
options.success?.();
window.setTimeout(() => {
window.dispatchEvent(new Event('focus'));
}, 0);
});
window.history.replaceState(
null,
'',
'/creation/puzzle?clientRuntime=wechat_mini_program',
);
window.wx = {
miniProgram: {
navigateTo,
},
};
const requested = await requestGenerationResultSubscribePermission();
expect(requested).toBe(true);
expect(navigateTo).toHaveBeenCalledWith({
url: expect.stringMatching(
/^\/pages\/subscribe-message\/index\?.*autoRequest=1/u,
),
success: expect.any(Function),
fail: expect.any(Function),
});
expect(window.location.hash).toBe('');
});
test('still accepts legacy hash result from native mini program page', async () => {
const navigateTo = vi.fn((options) => {
options.success?.();
window.setTimeout(() => {
window.location.hash = 'wx_subscribe_result=req-1:success';
window.dispatchEvent(new HashChangeEvent('hashchange'));
}, 0);
});
window.history.replaceState(
null,
'',
'/creation/puzzle?clientRuntime=wechat_mini_program',
);
window.wx = {
miniProgram: {
navigateTo,
},
};
const requested = await requestGenerationResultSubscribePermission();
expect(requested).toBe(true);
expect(window.location.hash).toBe('');
});
test('skips permission request outside mini program web-view', async () => {
const navigateTo = vi.fn();
window.wx = {
miniProgram: {
navigateTo,
},
};
const requested = await requestGenerationResultSubscribePermission();
expect(requested).toBe(false);
expect(navigateTo).not.toHaveBeenCalled();
});
});