import { describe, expect, test } from 'vitest'; import webViewBridge from './index.shared.js'; const { appendLaunchTargetToEntryUrl, buildWebViewSharePath, buildWebViewShareTimelineQuery, resolveShareTargetFromWebViewMessage, resolveWebViewUrlFromRuntimeConfig, } = webViewBridge; const runtimeConfig = { sourceQuery: { clientType: 'mini_program', clientRuntime: 'wechat_mini_program', }, webViewEntryUrl: 'https://www.genarrative.world', }; describe('mini program web-view launch target', () => { test('opens the H5 public work detail when launch query carries work params', () => { expect( appendLaunchTargetToEntryUrl('https://www.genarrative.world?foo=bar', { targetPath: '/works/detail', work: 'BB-12345678', }), ).toBe( 'https://www.genarrative.world/works/detail?foo=bar&work=BB-12345678', ); const webViewUrl = resolveWebViewUrlFromRuntimeConfig( null, { targetPath: '/works/detail', work: 'BB-12345678', }, runtimeConfig, ); const url = new URL(webViewUrl); expect(url.pathname).toBe('/works/detail'); expect(url.searchParams.get('work')).toBe('BB-12345678'); expect(url.searchParams.get('clientRuntime')).toBe('wechat_mini_program'); }); test('ignores unsupported launch target paths', () => { const webViewUrl = resolveWebViewUrlFromRuntimeConfig( null, { targetPath: '/admin', work: 'BB-12345678', }, runtimeConfig, ); const url = new URL(webViewUrl); expect(url.pathname).toBe('/'); expect(url.searchParams.get('work')).toBeNull(); }); test('keeps public work params in native mini program share paths', () => { const sharePath = buildWebViewSharePath({ targetPath: '/works/detail', work: 'BB-12345678', }); const url = new URL(sharePath, 'https://mini.test'); expect(url.pathname).toBe('/pages/web-view/index'); expect(url.searchParams.get('targetPath')).toBe('/works/detail'); expect(url.searchParams.get('work')).toBe('BB-12345678'); expect( buildWebViewShareTimelineQuery({ targetPath: '/works/detail', work: 'BB-12345678', }), ).toBe('targetPath=%2Fworks%2Fdetail&work=BB-12345678'); }); test('reads the latest H5 recommended work share target from web-view messages', () => { expect( resolveShareTargetFromWebViewMessage({ data: [ { data: { type: 'genarrative:share-target', payload: { targetPath: '/works/detail', work: 'PZ-0001', title: '旧作品', }, }, }, { data: { type: 'genarrative:share-target', payload: { targetPath: '/works/detail', work: 'BB-12345678', title: '汪汪声浪', }, }, }, ], }), ).toEqual({ targetPath: '/works/detail', work: 'BB-12345678', title: '汪汪声浪', }); }); });