workspaceSidebarFooterWebRemoteControlTooltip.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import { WorkspaceSidebarFooter } from "@/WorkspaceSidebarFooter.js";
  5. import {
  6. resolveSidebarFooterProfilePlanBadge,
  7. resolveSidebarFooterPlanBadgeLabel,
  8. WorkspaceSidebarFooterPlanBadge,
  9. } from "@/WorkspaceSidebarFooterUsageSummary.js";
  10. import { resolveSidebarCurrentCodingPlanUsageSource } from "@/lib/codingPlanUsageSources.js";
  11. import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
  12. const mockFooterSettings = vi.hoisted(() => ({
  13. current: {
  14. modelProviderFamilySelectedKeys: {} as Record<string, string>,
  15. providerFamilyDomain: undefined as "zai" | "bigmodel" | undefined,
  16. },
  17. }));
  18. vi.mock("@/ControlHintTooltip.js", async (importOriginal) => ({
  19. ...(await importOriginal<typeof import("@/ControlHintTooltip.js")>()),
  20. ControlHintTooltip: ({ children, title }: { children: unknown; title: string }) =>
  21. createElement("span", { "data-tooltip-title": title }, children),
  22. }));
  23. vi.mock("@/WorkspaceWebRemoteControlTrigger.js", () => ({
  24. WorkspaceWebRemoteControlTrigger: () =>
  25. createElement("button", {
  26. "data-testid": "web-remote-control-trigger",
  27. }),
  28. }));
  29. vi.mock("@/hooks/usePlatform.js", () => ({
  30. usePlatform: () => ({
  31. executeDesktopCommand: vi.fn(),
  32. getDesktopZoomLevel: () => Promise.resolve({ zoomLevel: 0 }),
  33. onDesktopZoomLevelChanged: () => () => {},
  34. openExternal: vi.fn(),
  35. }),
  36. }));
  37. vi.mock("@/i18n/IntlProvider.js", () => ({
  38. useZCodeIntl: () => ({
  39. locale: "zh-CN",
  40. intl: {
  41. formatMessage: ({ id }: { id: string }) => id,
  42. },
  43. }),
  44. }));
  45. vi.mock("@/store/StoreProvider.js", () => ({
  46. useZCodeStore: (selector: (state: { isRestoringOAuthSession: boolean }) => unknown) =>
  47. selector({ isRestoringOAuthSession: false }),
  48. }));
  49. vi.mock("@/hooks/useModelProviders.js", () => ({
  50. useModelProviders: () => ({
  51. modelProviders: [],
  52. loading: false,
  53. }),
  54. }));
  55. vi.mock("@/hooks/useSettingService.js", () => ({
  56. useSettings: () => ({
  57. settings: mockFooterSettings.current,
  58. update: vi.fn(),
  59. }),
  60. }));
  61. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  62. useUsageEntitlement: () => ({
  63. snapshot: null,
  64. loading: false,
  65. error: null,
  66. refresh: vi.fn(),
  67. }),
  68. }));
  69. vi.mock("@/settings/model-provider-section/useEnterpriseCodingPlanProducts.js", () => ({
  70. useEnterpriseCodingPlanProducts: () => ({
  71. snapshot: null,
  72. loading: false,
  73. error: null,
  74. refresh: vi.fn(),
  75. }),
  76. }));
  77. describe("WorkspaceSidebarFooter web remote control tooltip", () => {
  78. beforeEach(() => {
  79. mockFooterSettings.current = {
  80. modelProviderFamilySelectedKeys: {},
  81. providerFamilyDomain: undefined,
  82. };
  83. });
  84. it("derives compact sidebar footer plan badge labels", () => {
  85. expect(
  86. resolveSidebarFooterPlanBadgeLabel({
  87. generatedAt: 1,
  88. authenticated: true,
  89. provider: null,
  90. remaining: null,
  91. subscription: null,
  92. quota: { level: "lite", limits: [] },
  93. }),
  94. ).toBe("Lite");
  95. expect(
  96. resolveSidebarFooterPlanBadgeLabel({
  97. generatedAt: 1,
  98. authenticated: true,
  99. provider: null,
  100. remaining: null,
  101. subscription: {
  102. identityType: "unknown",
  103. identityMasked: null,
  104. details: [
  105. {
  106. productId: "pro",
  107. productName: "GLM Coding Pro",
  108. purchaseTime: null,
  109. beginTime: null,
  110. expireTime: null,
  111. },
  112. ],
  113. },
  114. quota: null,
  115. }),
  116. ).toBe("Pro");
  117. expect(
  118. resolveSidebarFooterPlanBadgeLabel({
  119. generatedAt: 1,
  120. authenticated: true,
  121. unavailableReason: "no_plan",
  122. provider: null,
  123. remaining: null,
  124. subscription: null,
  125. quota: null,
  126. }),
  127. ).toBeNull();
  128. });
  129. it("shows Team in the profile badge for team usage sources", () => {
  130. const html = renderToStaticMarkup(
  131. createElement(WorkspaceSidebarFooterPlanBadge, {
  132. state: {
  133. profilePlanBadge: {
  134. audience: "team",
  135. },
  136. usageState: {
  137. visibleSnapshot: {
  138. generatedAt: 1,
  139. authenticated: true,
  140. provider: null,
  141. remaining: null,
  142. subscription: null,
  143. quota: { level: "max", limits: [] },
  144. },
  145. },
  146. },
  147. }),
  148. );
  149. expect(html).toContain("sidebar.usage.plan.audienceTeam");
  150. expect(html).not.toContain("Max");
  151. });
  152. it("prefers personal Coding Plan entitlement over Team entitlement", () => {
  153. const snapshot = {
  154. generatedAt: 1,
  155. authenticated: true,
  156. provider: {
  157. id: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  158. name: "BigModel Coding Plan",
  159. },
  160. remaining: null,
  161. subscription: {
  162. identityType: "unknown" as const,
  163. identityMasked: null,
  164. details: [{ productId: "pro", productName: "GLM Coding Pro", expireTime: null }],
  165. },
  166. quota: null,
  167. };
  168. expect(
  169. resolveSidebarFooterProfilePlanBadge({
  170. hasTeamPlanEntitlement: true,
  171. individualEntitlements: [
  172. {
  173. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  174. snapshot,
  175. },
  176. ],
  177. }),
  178. ).toEqual({
  179. audience: "individual",
  180. snapshot,
  181. });
  182. });
  183. it("shows Team only from confirmed Team entitlement", () => {
  184. expect(
  185. resolveSidebarFooterProfilePlanBadge({
  186. hasTeamPlanEntitlement: true,
  187. individualEntitlements: [
  188. {
  189. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  190. snapshot: null,
  191. },
  192. ],
  193. }),
  194. ).toEqual({
  195. audience: "team",
  196. });
  197. });
  198. it("keeps stale BigModel Team Plan state out of the profile badge under Z.ai domain", () => {
  199. mockFooterSettings.current = {
  200. providerFamilyDomain: "zai",
  201. modelProviderFamilySelectedKeys: {
  202. bigmodel: "team:bigmodel:team-a:project-a",
  203. },
  204. };
  205. const html = renderToStaticMarkup(
  206. createElement(WorkspaceSidebarFooter, {
  207. theme: "dark",
  208. localeMenuValue: "zh-CN",
  209. onLocaleChange: () => {},
  210. onThemeChange: () => {},
  211. onSettingsButtonClick: () => {},
  212. user: {
  213. id: "user-zai-domain",
  214. username: "zai-user",
  215. displayName: "Zai User",
  216. },
  217. }),
  218. );
  219. expect(html).not.toContain("sidebar.usage.plan.audienceTeam");
  220. });
  221. it("waits for personal Coding Plan entitlement before showing Team", () => {
  222. expect(
  223. resolveSidebarFooterProfilePlanBadge({
  224. hasTeamPlanEntitlement: true,
  225. individualEntitlements: [
  226. {
  227. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  228. snapshot: null,
  229. loading: true,
  230. },
  231. ],
  232. }),
  233. ).toBeNull();
  234. });
  235. it("does not show Team from a selected Team Plan key without Team entitlement", () => {
  236. expect(
  237. resolveSidebarFooterProfilePlanBadge({
  238. hasTeamPlanEntitlement: false,
  239. individualEntitlements: [
  240. {
  241. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  242. snapshot: null,
  243. loading: false,
  244. },
  245. ],
  246. }),
  247. ).toBeNull();
  248. });
  249. it("resolves sidebar usage to the current workspace connection only", () => {
  250. const teamSources = [
  251. {
  252. id: "team:bigmodel:team-a:org-a:project-a" as const,
  253. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  254. label: "BigModel - Team A",
  255. accountAccess: {
  256. type: "zhipu-account" as const,
  257. family: "bigmodel" as const,
  258. planKind: "team-coding-plan" as const,
  259. productId: "team-a",
  260. organizationId: "org-a",
  261. projectId: "project-a",
  262. },
  263. },
  264. {
  265. id: "team:bigmodel:team-b:org-b:project-b" as const,
  266. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  267. label: "Team B Plan",
  268. accountAccess: {
  269. type: "zhipu-account" as const,
  270. family: "bigmodel" as const,
  271. planKind: "team-coding-plan" as const,
  272. productId: "team-b",
  273. organizationId: "org-b",
  274. projectId: "project-b",
  275. },
  276. },
  277. ];
  278. expect(
  279. resolveSidebarCurrentCodingPlanUsageSource({
  280. selections: {
  281. bigmodel: {
  282. kind: "team-coding-plan",
  283. productId: "team-b",
  284. organizationId: "org-b",
  285. projectId: "project-b",
  286. },
  287. },
  288. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  289. accountAccesses: {
  290. bigmodel: {
  291. type: "zhipu-account",
  292. family: "bigmodel",
  293. mode: "individual-coding-plan",
  294. },
  295. },
  296. teamSources,
  297. }),
  298. ).toMatchObject({
  299. audience: "team",
  300. sourceId: "team:bigmodel:team-b:org-b:project-b",
  301. teamSource: { accountAccess: { projectId: "project-b" } },
  302. });
  303. expect(
  304. resolveSidebarCurrentCodingPlanUsageSource({
  305. selections: { bigmodel: { kind: "individual-coding-plan" } },
  306. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  307. accountAccesses: {
  308. bigmodel: {
  309. type: "zhipu-account",
  310. family: "bigmodel",
  311. mode: "individual-coding-plan",
  312. },
  313. },
  314. teamSources,
  315. }),
  316. ).toMatchObject({
  317. audience: "individual",
  318. sourceId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan,
  319. });
  320. });
  321. it("does not guess a Team usage source after its product identity changes", () => {
  322. expect(
  323. resolveSidebarCurrentCodingPlanUsageSource({
  324. selections: {
  325. bigmodel: {
  326. kind: "team-coding-plan",
  327. productId: "product-old",
  328. organizationId: "org-a",
  329. projectId: "project-a",
  330. },
  331. },
  332. selectedProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  333. accountAccesses: {
  334. bigmodel: {
  335. type: "zhipu-account",
  336. family: "bigmodel",
  337. planKind: "individual-coding-plan",
  338. },
  339. },
  340. teamSources: [
  341. {
  342. id: "team:bigmodel:product-current:org-a:project-a",
  343. providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
  344. label: "BigModel - Team A",
  345. accountAccess: {
  346. type: "zhipu-account",
  347. family: "bigmodel",
  348. planKind: "team-coding-plan",
  349. productId: "product-current",
  350. organizationId: "org-a",
  351. projectId: "project-a",
  352. },
  353. },
  354. ],
  355. }),
  356. ).toBeNull();
  357. });
  358. it("does not wrap the remote control trigger in a second tooltip", () => {
  359. const html = renderToStaticMarkup(
  360. createElement(WorkspaceSidebarFooter, {
  361. theme: "dark",
  362. localeMenuValue: "zh-CN",
  363. onLocaleChange: () => {},
  364. onThemeChange: () => {},
  365. onSettingsButtonClick: () => {},
  366. workspacePath: "/tmp/project",
  367. isDesktop: true,
  368. }),
  369. );
  370. expect(html).toContain('data-testid="web-remote-control-trigger"');
  371. expect(html).not.toContain('data-tooltip-title="webRemoteControl.trigger"');
  372. });
  373. });