conversationShareRoute.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { readFileSync } from "node:fs";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. isConversationSharePath,
  5. resolveConversationShareCodeFromPath,
  6. } from "../src/share/conversationShareRoute.js";
  7. import { resolveConversationShareRouteLocale } from "../src/share/conversationSharePreviewClient.js";
  8. describe("conversation share route", () => {
  9. it("recognizes share paths without taking over remote routes", () => {
  10. expect(isConversationSharePath("/cn/share")).toBe(true);
  11. expect(isConversationSharePath("/cn/share/share-1")).toBe(true);
  12. expect(isConversationSharePath("/share")).toBe(true);
  13. expect(isConversationSharePath("/share/share-1")).toBe(true);
  14. expect(isConversationSharePath("/remote/v4")).toBe(false);
  15. expect(isConversationSharePath("/sharegate/x")).toBe(false);
  16. });
  17. it("英文站的裸 /share/ 不再被拒绝,两种前缀都能解析出 code", () => {
  18. // 此前 /share/<code> 走 isRejectedConversationSharePath,直接渲染 not_found,
  19. // 等于英文分享页根本不存在。
  20. expect(resolveConversationShareCodeFromPath("/cn/share/share-1")).toBe("share-1");
  21. expect(resolveConversationShareCodeFromPath("/share/share-1")).toBe("share-1");
  22. expect(resolveConversationShareCodeFromPath("/cn/share/share-1/extra")).toBeNull();
  23. expect(resolveConversationShareCodeFromPath("/share/")).toBeNull();
  24. });
  25. it("页面语言由路径前缀决定", () => {
  26. expect(resolveConversationShareRouteLocale("/cn/share/share-1")).toBe("zh-CN");
  27. expect(resolveConversationShareRouteLocale("/share/share-1")).toBe("en-US");
  28. });
  29. it("结果物计数跟随语言,不再中英混排", () => {
  30. // Bug 根因:原本硬编码英文 artifact/artifacts 且手写复数,中文页会渲染成
  31. // 「2026年9月3日 11:55 · 1 artifact」。
  32. const source = readFileSync("packages/web/src/share/ConversationShareLandingPage.tsx", "utf8");
  33. expect(source).toContain("artifactCountOne");
  34. expect(source).toContain("artifactCountOther");
  35. expect(source).toContain("{count} 个结果物");
  36. expect(source).not.toContain("artifact{artifactCount === 1");
  37. });
  38. it("英文分享 OAuth callback 与中文 callback 走同一回调页面", () => {
  39. const mainSource = readFileSync("packages/web/src/main.tsx", "utf8");
  40. const authSource = readFileSync("packages/web/src/auth/webAuthService.ts", "utf8");
  41. const stateSource = readFileSync("packages/web/src/auth/oauthStateCodec.ts", "utf8");
  42. expect(mainSource).toContain('"/share/callback"');
  43. expect(authSource).toContain('"/share/callback"');
  44. expect(stateSource).toContain('"/share/callback"');
  45. expect(stateSource).toContain("const SHARE_PATH_PATTERN");
  46. expect(stateSource).toContain("cn\\/share|share");
  47. });
  48. it("分享页同步 html lang 与浏览器标签标题", () => {
  49. // index.html 固定 lang="en";不同步会让中文分享页对无障碍与浏览器翻译都报错语言。
  50. // 而 document.title 此前从未设置,标签只显示 index.html 的通用标题。
  51. const source = readFileSync("packages/web/src/main.tsx", "utf8");
  52. expect(source).toContain("document.documentElement.lang = routeLocale");
  53. expect(source).toMatch(/document\.title =\s*routeLocale === "zh-CN"/u);
  54. });
  55. it("dev server base 不能写死 /cn/share/,否则英文路由本地不可达", () => {
  56. // Bug 根因:dev 脚本写死 --base=/cn/share/,Vite 只服务该前缀,访问 /share/<code>
  57. // 会被 dev server 直接拒掉("server is configured with a public base URL of /cn/share/")。
  58. const source = readFileSync("scripts/dev-web-share-env.mjs", "utf8");
  59. expect(source).toContain('"--base=/"');
  60. expect(source).not.toContain('"--base=/cn/share/"');
  61. });
  62. it("continue-help 横幅与正文同宽,下载链接不带 /download", () => {
  63. const source = readFileSync("packages/web/src/share/ConversationShareLandingPage.tsx", "utf8");
  64. // Bug 根因一:横幅原本是 ShareContentRail 的直接子元素,没套 ShareContentInset,
  65. // 缺了 px-4 / @md:px-6,比上方时间行和下方正文都宽出一截。
  66. expect(source).toMatch(
  67. /showContinueHelp \? \(\s*(?:\/\/[^\n]*\n\s*)*<ShareContentInset>/u,
  68. );
  69. // Bug 根因二:站点首页本身就是下载入口,/download 这个 path 是 404。
  70. expect(source).toContain('const ZCODE_DOWNLOAD_URL = "https://zcode.z.ai"');
  71. expect(source).not.toContain("zcode.z.ai/download");
  72. });
  73. });