Files
Genarrative/src/services/wechatMiniProgramSubscribe.test.ts
kdletters 3a918687c5 修复拼图生成前订阅授权
新增小程序原生订阅消息授权页,在用户点击后请求生成结果通知授权。

拼图 compile_puzzle_draft 前等待授权页返回或跳过后再发起生成 action。

移除 web-view message 订阅授权路径,改用 storage/hash 回写订阅结果。

补充订阅授权测试、文档和团队踩坑记录。
2026-06-08 13:06:07 +08:00

59 lines
1.6 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', 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(navigateTo).toHaveBeenCalledWith({
url: expect.stringMatching(/^\/pages\/subscribe-message\/index\?/u),
success: expect.any(Function),
fail: expect.any(Function),
});
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();
});
});