conversationShareRoute.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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("dev server base 不能写死 /cn/share/,否则英文路由本地不可达", () => {
  30. // Bug 根因:dev 脚本写死 --base=/cn/share/,Vite 只服务该前缀,访问 /share/<code>
  31. // 会被 dev server 直接拒掉("server is configured with a public base URL of /cn/share/")。
  32. const source = readFileSync("scripts/dev-web-share-env.mjs", "utf8");
  33. expect(source).toContain('"--base=/"');
  34. expect(source).not.toContain('"--base=/cn/share/"');
  35. });
  36. });