workspaceSidebarFooterWebRemoteControlTooltip.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { beforeEach, describe, expect, it, vi } from "vitest";
  6. import { WorkspaceSidebarFooter } from "@/WorkspaceSidebarFooter.js";
  7. import {
  8. resolveSidebarFooterProfilePlanBadge,
  9. resolveSidebarFooterPlanBadgeLabel,
  10. WorkspaceSidebarFooterPlanBadge,
  11. } from "@/WorkspaceSidebarFooterUsageSummary.js";
  12. import { resolveSidebarCurrentCodingPlanUsageSource } from "@/lib/codingPlanUsageSources.js";
  13. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  14. const mockFooterSettings = vi.hoisted(() => ({
  15. current: {
  16. modelProviderFamilySelectedKeys: {} as Record<string, string>,
  17. providerFamilyDomain: undefined as "zai" | "bigmodel" | undefined,
  18. },
  19. }));
  20. vi.mock("@/ControlHintTooltip.js", () => ({
  21. ControlHintTooltip: ({ children, title }: { children: unknown; title: string }) =>
  22. createElement("span", { "data-tooltip-title": title }, children),
  23. }));
  24. vi.mock("@/WorkspaceWebRemoteControlTrigger.js", () => ({
  25. WorkspaceWebRemoteControlTrigger: () =>
  26. createElement("button", {
  27. "data-testid": "web-remote-control-trigger",
  28. }),
  29. }));
  30. vi.mock("@/hooks/usePlatform.js", () => ({
  31. usePlatform: () => ({
  32. executeDesktopCommand: vi.fn(),
  33. getDesktopZoomLevel: () => Promise.resolve({ zoomLevel: 0 }),
  34. onDesktopZoomLevelChanged: () => () => {},
  35. openExternal: vi.fn(),
  36. }),
  37. }));
  38. vi.mock("@/i18n/IntlProvider.js", () => ({
  39. useZCodeIntl: () => ({
  40. locale: "zh-CN",
  41. intl: {
  42. formatMessage: ({ id }: { id: string }) => id,
  43. },
  44. }),
  45. }));
  46. vi.mock("@/store/StoreProvider.js", () => ({
  47. useZCodeStore: (selector: (state: { isRestoringOAuthSession: boolean }) => unknown) =>
  48. selector({ isRestoringOAuthSession: false }),
  49. }));
  50. vi.mock("@/hooks/useModelProviders.js", () => ({
  51. useModelProviders: () => ({
  52. modelProviders: [],
  53. loading: false,
  54. }),
  55. }));
  56. vi.mock("@/hooks/useSettingService.js", () => ({
  57. useSettings: () => ({
  58. settings: mockFooterSettings.current,
  59. update: vi.fn(),
  60. }),
  61. }));
  62. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  63. useUsageEntitlement: () => ({
  64. snapshot: null,
  65. loading: false,
  66. error: null,
  67. refresh: vi.fn(),
  68. }),
  69. }));
  70. vi.mock("@/settings/model-provider-section/useEnterpriseCodingPlanProducts.js", () => ({
  71. useEnterpriseCodingPlanProducts: () => ({
  72. snapshot: null,
  73. loading: false,
  74. error: null,
  75. refresh: vi.fn(),
  76. }),
  77. }));
  78. describe("WorkspaceSidebarFooter web remote control tooltip", () => {
  79. beforeEach(() => {
  80. mockFooterSettings.current = {
  81. modelProviderFamilySelectedKeys: {},
  82. providerFamilyDomain: undefined,
  83. };
  84. });
  85. it("keeps long profile names constrained inside the footer row", () => {
  86. const html = renderToStaticMarkup(
  87. createElement(WorkspaceSidebarFooter, {
  88. theme: "dark",
  89. localeMenuValue: "zh-CN",
  90. onLocaleChange: () => {},
  91. onThemeChange: () => {},
  92. onSettingsButtonClick: () => {},
  93. user: {
  94. id: "user-long-name",
  95. username: "very-very-very-very-very-very-long-user-name",
  96. displayName: "VeryVeryVeryVeryVeryVeryVeryVeryLongDisplayName",
  97. },
  98. }),
  99. );
  100. expect(html).toContain("min-w-0 flex-1");
  101. expect(html).toContain("overflow-hidden");
  102. expect(html).toContain("text-left");
  103. expect(html).toContain("truncate text-ui-base font-semibold");
  104. });
  105. it("derives compact sidebar footer plan badge labels", () => {
  106. expect(
  107. resolveSidebarFooterPlanBadgeLabel({
  108. generatedAt: 1,
  109. authenticated: true,
  110. provider: null,
  111. remaining: null,
  112. subscription: null,
  113. quota: { level: "lite", limits: [] },
  114. }),
  115. ).toBe("Lite");
  116. expect(
  117. resolveSidebarFooterPlanBadgeLabel({
  118. generatedAt: 1,
  119. authenticated: true,
  120. provider: null,
  121. remaining: null,
  122. subscription: {
  123. identityType: "unknown",
  124. identityMasked: null,
  125. details: [
  126. {
  127. productId: "pro",
  128. productName: "GLM Coding Pro",
  129. purchaseTime: null,
  130. beginTime: null,
  131. expireTime: null,
  132. },
  133. ],
  134. },
  135. quota: null,
  136. }),
  137. ).toBe("Pro");
  138. expect(
  139. resolveSidebarFooterPlanBadgeLabel({
  140. generatedAt: 1,
  141. authenticated: true,
  142. unavailableReason: "no_plan",
  143. provider: null,
  144. remaining: null,
  145. subscription: null,
  146. quota: null,
  147. }),
  148. ).toBeNull();
  149. });
  150. it("shows Team in the profile badge for team usage sources", () => {
  151. const html = renderToStaticMarkup(
  152. createElement(WorkspaceSidebarFooterPlanBadge, {
  153. state: {
  154. profilePlanBadge: {
  155. audience: "team",
  156. },
  157. usageState: {
  158. visibleSnapshot: {
  159. generatedAt: 1,
  160. authenticated: true,
  161. provider: null,
  162. remaining: null,
  163. subscription: null,
  164. quota: { level: "max", limits: [] },
  165. },
  166. },
  167. },
  168. }),
  169. );
  170. expect(html).toContain("sidebar.usage.plan.audienceTeam");
  171. expect(html).toContain("text-ui-xs");
  172. expect(html).not.toContain("text-ui-sm");
  173. expect(html).not.toContain("Max");
  174. });
  175. it("prefers personal Coding Plan entitlement over Team entitlement", () => {
  176. const snapshot = {
  177. generatedAt: 1,
  178. authenticated: true,
  179. provider: {
  180. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  181. name: "BigModel Coding Plan",
  182. },
  183. remaining: null,
  184. subscription: {
  185. identityType: "unknown" as const,
  186. identityMasked: null,
  187. details: [{ productId: "pro", productName: "GLM Coding Pro", expireTime: null }],
  188. },
  189. quota: null,
  190. };
  191. expect(
  192. resolveSidebarFooterProfilePlanBadge({
  193. hasTeamPlanEntitlement: true,
  194. individualEntitlements: [
  195. {
  196. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  197. snapshot,
  198. },
  199. ],
  200. }),
  201. ).toEqual({
  202. audience: "individual",
  203. snapshot,
  204. });
  205. });
  206. it("shows Team only from confirmed Team entitlement", () => {
  207. expect(
  208. resolveSidebarFooterProfilePlanBadge({
  209. hasTeamPlanEntitlement: true,
  210. individualEntitlements: [
  211. {
  212. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  213. snapshot: null,
  214. },
  215. ],
  216. }),
  217. ).toEqual({
  218. audience: "team",
  219. });
  220. });
  221. it("keeps stale BigModel Team Plan state out of the profile badge under Z.ai domain", () => {
  222. mockFooterSettings.current = {
  223. providerFamilyDomain: "zai",
  224. modelProviderFamilySelectedKeys: {
  225. bigmodel: "team:bigmodel:team-a:project-a",
  226. },
  227. };
  228. const html = renderToStaticMarkup(
  229. createElement(WorkspaceSidebarFooter, {
  230. theme: "dark",
  231. localeMenuValue: "zh-CN",
  232. onLocaleChange: () => {},
  233. onThemeChange: () => {},
  234. onSettingsButtonClick: () => {},
  235. user: {
  236. id: "user-zai-domain",
  237. username: "zai-user",
  238. displayName: "Zai User",
  239. },
  240. }),
  241. );
  242. expect(html).not.toContain("sidebar.usage.plan.audienceTeam");
  243. });
  244. it("waits for personal Coding Plan entitlement before showing Team", () => {
  245. expect(
  246. resolveSidebarFooterProfilePlanBadge({
  247. hasTeamPlanEntitlement: true,
  248. individualEntitlements: [
  249. {
  250. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  251. snapshot: null,
  252. loading: true,
  253. },
  254. ],
  255. }),
  256. ).toBeNull();
  257. });
  258. it("does not show Team from a selected Team Plan key without Team entitlement", () => {
  259. expect(
  260. resolveSidebarFooterProfilePlanBadge({
  261. hasTeamPlanEntitlement: false,
  262. individualEntitlements: [
  263. {
  264. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  265. snapshot: null,
  266. loading: false,
  267. },
  268. ],
  269. }),
  270. ).toBeNull();
  271. });
  272. it("resolves sidebar usage to the current workspace connection only", () => {
  273. const teamSources = [
  274. {
  275. id: "team:bigmodel:team-a:org-a:project-a" as const,
  276. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  277. label: "BigModel - Team A",
  278. accountAccess: {
  279. type: "zhipu-account" as const,
  280. family: "bigmodel" as const,
  281. planKind: "team-coding-plan" as const,
  282. productId: "team-a",
  283. organizationId: "org-a",
  284. projectId: "project-a",
  285. },
  286. },
  287. {
  288. id: "team:bigmodel:team-b:org-b:project-b" as const,
  289. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  290. label: "Team B Plan",
  291. accountAccess: {
  292. type: "zhipu-account" as const,
  293. family: "bigmodel" as const,
  294. planKind: "team-coding-plan" as const,
  295. productId: "team-b",
  296. organizationId: "org-b",
  297. projectId: "project-b",
  298. },
  299. },
  300. ];
  301. expect(
  302. resolveSidebarCurrentCodingPlanUsageSource({
  303. selections: {
  304. bigmodel: {
  305. kind: "team-coding-plan",
  306. productId: "team-b",
  307. organizationId: "org-b",
  308. projectId: "project-b",
  309. },
  310. },
  311. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  312. accountAccesses: {
  313. bigmodel: {
  314. type: "zhipu-account",
  315. family: "bigmodel",
  316. mode: "individual-coding-plan",
  317. },
  318. },
  319. teamSources,
  320. }),
  321. ).toMatchObject({
  322. audience: "team",
  323. sourceId: "team:bigmodel:team-b:org-b:project-b",
  324. teamSource: { accountAccess: { projectId: "project-b" } },
  325. });
  326. expect(
  327. resolveSidebarCurrentCodingPlanUsageSource({
  328. selections: { bigmodel: { kind: "individual-coding-plan" } },
  329. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  330. accountAccesses: {
  331. bigmodel: {
  332. type: "zhipu-account",
  333. family: "bigmodel",
  334. mode: "individual-coding-plan",
  335. },
  336. },
  337. teamSources,
  338. }),
  339. ).toMatchObject({
  340. audience: "individual",
  341. sourceId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  342. });
  343. });
  344. it("does not guess a Team usage source after its product identity changes", () => {
  345. expect(
  346. resolveSidebarCurrentCodingPlanUsageSource({
  347. selections: {
  348. bigmodel: {
  349. kind: "team-coding-plan",
  350. productId: "product-old",
  351. organizationId: "org-a",
  352. projectId: "project-a",
  353. },
  354. },
  355. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  356. accountAccesses: {
  357. bigmodel: {
  358. type: "zhipu-account",
  359. family: "bigmodel",
  360. planKind: "individual-coding-plan",
  361. },
  362. },
  363. teamSources: [
  364. {
  365. id: "team:bigmodel:product-current:org-a:project-a",
  366. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  367. label: "BigModel - Team A",
  368. accountAccess: {
  369. type: "zhipu-account",
  370. family: "bigmodel",
  371. planKind: "team-coding-plan",
  372. productId: "product-current",
  373. organizationId: "org-a",
  374. projectId: "project-a",
  375. },
  376. },
  377. ],
  378. }),
  379. ).toBeNull();
  380. });
  381. it("does not wrap the remote control trigger in a second tooltip", () => {
  382. const html = renderToStaticMarkup(
  383. createElement(WorkspaceSidebarFooter, {
  384. theme: "dark",
  385. localeMenuValue: "zh-CN",
  386. onLocaleChange: () => {},
  387. onThemeChange: () => {},
  388. onSettingsButtonClick: () => {},
  389. workspacePath: "/tmp/project",
  390. isDesktop: true,
  391. }),
  392. );
  393. expect(html).toContain('data-testid="web-remote-control-trigger"');
  394. expect(html).not.toContain('data-tooltip-title="webRemoteControl.trigger"');
  395. });
  396. it("shows desktop zoom commands under an interface zoom submenu", () => {
  397. const source = readFileSync(
  398. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  399. "utf8",
  400. );
  401. expect(source).toContain("{isDesktop ? (");
  402. expect(source).toContain('id: "sidebar.settings.interfaceZoom"');
  403. expect(source).toContain("<DropdownMenuSubContent");
  404. expect(source).toContain("DesktopCommandIds.ZoomIn");
  405. expect(source).toContain("DesktopCommandIds.ZoomOut");
  406. expect(source).toContain("DesktopCommandIds.ResetZoom");
  407. expect(source).toContain('id: "titleBar.menu.view.zoomIn"');
  408. expect(source).toContain('id: "titleBar.menu.view.zoomOut"');
  409. expect(source).toContain('id: "titleBar.menu.view.actualSize"');
  410. });
  411. it("keeps the interface zoom submenu gated to desktop mode", () => {
  412. const source = readFileSync(
  413. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  414. "utf8",
  415. );
  416. expect(source).toContain("{isDesktop ? (");
  417. expect(source).toContain(") : null}");
  418. });
  419. it("keeps profile menu grouped as language theme zoom usage auth", () => {
  420. const source = readFileSync(
  421. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  422. "utf8",
  423. );
  424. const localeIndex = source.indexOf('id: "settings.locale"');
  425. const themeIndex = source.indexOf('id: "settings.themeMode"');
  426. const zoomIndex = source.indexOf('id: "sidebar.settings.interfaceZoom"');
  427. const usageIndex = source.indexOf("<WorkspaceSidebarFooterUsageSummary");
  428. const loginIndex = source.indexOf('id: "app.login"');
  429. const logoutIndex = source.indexOf('id: "app.logout"');
  430. const separatorBeforeAuthIndex = source.indexOf("<DropdownMenuSeparator />", usageIndex);
  431. expect(localeIndex).toBeGreaterThan(-1);
  432. expect(themeIndex).toBeGreaterThan(localeIndex);
  433. expect(zoomIndex).toBeGreaterThan(themeIndex);
  434. expect(usageIndex).toBeGreaterThan(zoomIndex);
  435. expect(separatorBeforeAuthIndex).toBeGreaterThan(usageIndex);
  436. expect(loginIndex).toBeGreaterThan(separatorBeforeAuthIndex);
  437. expect(logoutIndex).toBeGreaterThan(separatorBeforeAuthIndex);
  438. });
  439. it("keeps the always-visible upgrade entry inside the footer summary", () => {
  440. const footerSource = readFileSync(
  441. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  442. "utf8",
  443. );
  444. const summarySource = readFileSync(
  445. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooterUsageSummary.tsx"),
  446. "utf8",
  447. );
  448. const usageIndex = footerSource.indexOf("<WorkspaceSidebarFooterUsageSummary");
  449. // 偏好设置与用量之间由 summary 统一管理分隔线,账号操作组另有分隔线。
  450. const preferencesIndex = footerSource.indexOf('id: "settings.locale"');
  451. expect(footerSource.slice(preferencesIndex, usageIndex)).not.toContain(
  452. "<DropdownMenuSeparator />",
  453. );
  454. expect(summarySource).toContain("<DropdownMenuSeparator />");
  455. expect(summarySource.indexOf("sidebar.usage.plan.openStats")).toBeLessThan(
  456. summarySource.indexOf("onUpgradeClick?.("),
  457. );
  458. expect(summarySource).toContain("setPendingSettingsUsageIntent();");
  459. expect(summarySource).toContain("onUsageClick?.();");
  460. expect(summarySource).toContain("upgradeTargetProviderId");
  461. expect(summarySource).toContain("onUpgradeClick?.(");
  462. expect(summarySource).not.toContain("if (!upgradeProviderId");
  463. expect(summarySource).not.toContain("if (!upgradeTargetProviderId");
  464. expect(summarySource).toContain("<DropdownMenuSeparator />");
  465. expect(summarySource).not.toContain("<CodingPlanUsageRemainingPanel");
  466. });
  467. it("keeps settings-page usage stats separate from the back button", () => {
  468. const footerSource = readFileSync(
  469. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  470. "utf8",
  471. );
  472. const settingsSource = readFileSync(
  473. resolve(process.cwd(), "packages/ui/src/SettingsPage.tsx"),
  474. "utf8",
  475. );
  476. expect(footerSource).toContain("onUsageClick,");
  477. expect(footerSource).toContain(
  478. "const usageButtonClick = onUsageClick ?? onSettingsButtonClick;",
  479. );
  480. expect(footerSource).toContain("onUsageClick={usageButtonClick}");
  481. expect(settingsSource).toContain("const handleOpenUsageSettings = useCallback(() => {");
  482. expect(settingsSource).toContain('setActiveSettingsSection("usage");');
  483. expect(settingsSource).toContain("onSettingsButtonClick={onBack}");
  484. expect(settingsSource).toContain("onUsageClick={handleOpenUsageSettings}");
  485. });
  486. it("keeps duplicated help actions out of the profile menu", () => {
  487. const source = readFileSync(
  488. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  489. "utf8",
  490. );
  491. expect(source).not.toContain('id: "titleBar.menu.help.feedback"');
  492. expect(source).not.toContain('id: "sidebar.menu.community"');
  493. expect(source).not.toContain('id: "titleBar.menu.help.exportLogs"');
  494. expect(source).not.toContain("<DropdownMenuItem onSelect={onSettingsButtonClick}>");
  495. expect(source).not.toContain("runExportLogsAction");
  496. });
  497. it("keeps usage remaining mounted across profile menu toggles", () => {
  498. const source = readFileSync(
  499. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  500. "utf8",
  501. );
  502. // 此用例验证菜单保持挂载,不应绑定会随国际化文案调整的宽度。
  503. expect(source).toMatch(/<DropdownMenuContent\b[^>]*\bforceMount\s*>/);
  504. expect(source).toContain("<WorkspaceSidebarFooterUsageSummary");
  505. expect(source).toContain("enabled");
  506. expect(source).not.toContain("enabled={profileMenuOpen}");
  507. });
  508. it("renders the profile plan badge from the shared usage summary state", () => {
  509. const source = readFileSync(
  510. resolve(process.cwd(), "packages/ui/src/WorkspaceSidebarFooter.tsx"),
  511. "utf8",
  512. );
  513. expect(source).toContain("useWorkspaceSidebarFooterUsageSummaryState");
  514. expect(source).toContain("<WorkspaceSidebarFooterPlanBadge state={usageSummaryState} />");
  515. expect(source).toContain("<WorkspaceSidebarFooterUsageSummaryContent");
  516. expect(source).toContain("state={usageSummaryState}");
  517. });
  518. });