071faa482c
纳入 AGC Cargo workspace 的统一 rustfmt 检查与格式化入口 完成项目 TypeScript/Prettier 与 Rust 全量格式化 修复 Pingora expected executable 门禁的空白敏感误报 同步开发运维文档与 AGC skill pack 格式化忽略规则
118 lines
3.3 KiB
TypeScript
118 lines
3.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\?/u),
|
|
success: expect.any(Function),
|
|
fail: expect.any(Function),
|
|
});
|
|
expect(navigateTo.mock.calls[0]?.[0].url).not.toContain('autoRequest=1');
|
|
expect(window.location.hash).toBe('');
|
|
});
|
|
|
|
test('keeps waiting even when native page returns immediately after navigate success', async () => {
|
|
const navigateTo = vi.fn((options) => {
|
|
window.dispatchEvent(new Event('focus'));
|
|
options.success?.();
|
|
});
|
|
window.history.replaceState(
|
|
null,
|
|
'',
|
|
'/creation/puzzle?clientRuntime=wechat_mini_program',
|
|
);
|
|
window.wx = {
|
|
miniProgram: {
|
|
navigateTo,
|
|
},
|
|
};
|
|
|
|
const requested = await requestGenerationResultSubscribePermission();
|
|
|
|
expect(requested).toBe(true);
|
|
});
|
|
|
|
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 requested = await requestGenerationResultSubscribePermission();
|
|
|
|
expect(requested).toBe(false);
|
|
});
|
|
|
|
test('uses an already available mini program bridge even without query marker', async () => {
|
|
const navigateTo = vi.fn((options) => {
|
|
options.success?.();
|
|
window.setTimeout(() => {
|
|
window.dispatchEvent(new Event('focus'));
|
|
}, 0);
|
|
});
|
|
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),
|
|
});
|
|
});
|
|
});
|