web-view-share.test.cjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const config = require('../app.json');
  4. const origin = 'https://www.yuban.co';
  5. const container = '/common-page/pages/web-view/index';
  6. let definition;
  7. global.Page = page => { definition = page; };
  8. const app = { Parse: { User: { current: () => null } }, globalData: {}, checkAuth: async () => {} };
  9. global.getApp = () => app;
  10. global.wx = { getStorageSync: () => '', redirectTo: () => {} };
  11. require('../common-page/pages/web-view/index');
  12. function newPage() {
  13. return { ...definition, data: { ...definition.data }, setData(update) { Object.assign(this.data, update); } };
  14. }
  15. function recipientOptions(path) {
  16. assert.equal(path.split('?')[0], container);
  17. assert.ok(config.subpackages.some(pkg => pkg.pages.some(page => '/' + pkg.root + '/' + page === container)));
  18. return Object.fromEntries(path.split('?').slice(1).join('?').split('&').map(pair => {
  19. const split = pair.indexOf('=');
  20. return [pair.slice(0, split), pair.slice(split + 1)];
  21. }));
  22. }
  23. for (const route of ['/home','/community','/community?tab=topic']) {
  24. test('a new recipient opens '+route, async () => {
  25. const sender = newPage();
  26. sender.data.path = origin + '/home?token=sender-session';
  27. const current = origin + route + (route.includes('?') ? '&' : '?') + 'token=secret&mobile=13800000001';
  28. const card = sender.onShareAppMessage({ webViewUrl: current });
  29. assert.ok(card?.path, 'native menu must return a usable path');
  30. assert.doesNotMatch(decodeURIComponent(card.path), /token|secret|mobile|13800000001/);
  31. const recipient = newPage();
  32. recipient.checkUserLogin = () => assert.fail('sharing should open before native login');
  33. await recipient.onLoad(recipientOptions(card.path));
  34. assert.equal(recipient.data.path, origin + route);
  35. });
  36. }
  37. test('an old card without parameters opens a usable homepage', async () => {
  38. const page = newPage();
  39. await page.onLoad({});
  40. assert.equal(page.data.path, origin + '/home');
  41. });
  42. test('an old chat card with a URL opens the original public destination', async t => {
  43. global.getCurrentPages = () => [{}];
  44. wx.getEnterOptionsSync = () => ({ path: container, scene: 1008 });
  45. t.after(() => { delete global.getCurrentPages; delete wx.getEnterOptionsSync; });
  46. const page = newPage();
  47. await page.onLoad({ path: encodeURIComponent(origin + '/community?tab=topic&userId=sender') });
  48. assert.equal(page.data.path, origin + '/community?tab=topic');
  49. });
  50. test('cards preserve supported content IDs and discard other fields', async () => {
  51. for (const [path, landing] of [
  52. ['/topic/t123?token=secret', '/?shareType=topic&shareId=t123'],
  53. ['/article/abc_123', '/?shareType=story&shareId=abc_123'],
  54. ['/ai-personality?articleId=r123&mobile=123', '/?shareType=ai-report&shareId=r123&reportType=personality'],
  55. ['/family-preview/f123', '/?shareType=family-preview&shareId=f123'],
  56. ['/community?tab=mine', '/community'],
  57. ]) {
  58. const sender = newPage();
  59. sender.data.path = origin + path;
  60. const recipient = newPage();
  61. await recipient.onLoad(recipientOptions(sender.onShareAppMessage().path));
  62. assert.equal(recipient.data.path, origin + landing);
  63. }
  64. });
  65. test('a missing or invalid destination recovers to home', async () => {
  66. for (const path of ['', '%', 'https://www.yuban.co.example/home', origin + '/user-profile']) {
  67. const page = newPage();
  68. await page.onLoad({ path, fromShare: '1' });
  69. assert.equal(page.data.path, origin + '/home');
  70. }
  71. });
  72. test('ordinary in-app launches retain the original auth and carousel paths', async () => {
  73. const gated = newPage();
  74. let checked = false;
  75. gated.checkUserLogin = async () => { checked = true; return false; };
  76. await gated.onLoad({ path: encodeURIComponent(origin + '/user-profile') });
  77. assert.ok(checked);
  78. assert.equal(gated.data.path, '');
  79. const carousel = newPage();
  80. await carousel.onLoad({ path: encodeURIComponent(origin + '/home'), skipAuth: 'true' });
  81. assert.ok(carousel.data.path.startsWith(origin + '/home?'));
  82. assert.match(carousel.data.path, /fromStartup=1/);
  83. });