| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- import { describe, expect, it } from "vitest";
- import {
- BUILTIN_MODEL_PROVIDER_IDS,
- type ProviderFamilyConnectionSelection,
- type EnterpriseCodingPlanPricingProduct,
- type UsageEntitlementSnapshot,
- } from "@zcode/shared";
- import {
- resolveAutomaticModelProviderFamilyConnectionSelection,
- resolveModelProviderFamilyConnectionProviderId,
- } from "@/lib/modelProviderFamilyConnectionSelection.js";
- function createEntitlementSnapshot(providerId: string): UsageEntitlementSnapshot {
- return {
- generatedAt: 1,
- authenticated: true,
- provider: {
- id: providerId,
- name: providerId,
- },
- remaining: null,
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "pro",
- productName: "GLM Coding Pro",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- quota: null,
- };
- }
- describe("resolveAutomaticModelProviderFamilyConnectionSelection", () => {
- it.each([
- ["start-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan],
- ["individual-coding-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan],
- ["team-coding-plan", BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan],
- ] as const)("把 BigModel %s 连接映射到对应 Account Provider", (kind, providerId) => {
- const selection: ProviderFamilyConnectionSelection =
- kind === "team-coding-plan"
- ? {
- kind,
- productId: "team-pro",
- organizationId: "org-1",
- projectId: "project-1",
- }
- : { kind };
- expect(
- resolveModelProviderFamilyConnectionProviderId({
- providerFamilyDomain: "bigmodel",
- selection,
- }),
- ).toBe(providerId);
- });
- function resolveBigModelSelection(params: {
- hasStart: boolean;
- hasPersonal: boolean;
- teamProducts?: EnterpriseCodingPlanPricingProduct[];
- }) {
- return resolveAutomaticModelProviderFamilyConnectionSelection({
- providerFamilyDomain: "bigmodel",
- codingPlanEntitlement: params.hasPersonal
- ? createEntitlementSnapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelIndividualCodingPlan)
- : null,
- startPlanEntitlement: params.hasStart
- ? createEntitlementSnapshot(BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan)
- : null,
- teamProducts: params.teamProducts ?? [],
- });
- }
- const subscribedTeamProduct = {
- productId: "team-pro",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-1",
- projectId: "project-1",
- },
- ],
- } satisfies EnterpriseCodingPlanPricingProduct;
- it.each([
- {
- name: "有 Start、有个人、有团队时选个人 Coding",
- hasStart: true,
- hasPersonal: true,
- teamProducts: [subscribedTeamProduct],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- {
- name: "有 Start、有个人、无团队时选个人 Coding",
- hasStart: true,
- hasPersonal: true,
- teamProducts: [],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- {
- name: "有 Start、无个人、有团队时选团队 Coding",
- hasStart: true,
- hasPersonal: false,
- teamProducts: [subscribedTeamProduct],
- expectedSelection: {
- kind: "team-coding-plan",
- productId: "team-pro",
- organizationId: "org-1",
- projectId: "project-1",
- },
- },
- {
- name: "只有 Start 时保留独立 Start,付费连接落到个人购买入口",
- hasStart: true,
- hasPersonal: false,
- teamProducts: [],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- {
- name: "无 Start、有个人、有团队时选个人 Coding",
- hasStart: false,
- hasPersonal: true,
- teamProducts: [subscribedTeamProduct],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- {
- name: "无 Start、有个人、无团队时选个人 Coding",
- hasStart: false,
- hasPersonal: true,
- teamProducts: [],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- {
- name: "无 Start、无个人、有团队时选团队 Coding",
- hasStart: false,
- hasPersonal: false,
- teamProducts: [subscribedTeamProduct],
- expectedSelection: {
- kind: "team-coding-plan",
- productId: "team-pro",
- organizationId: "org-1",
- projectId: "project-1",
- },
- },
- {
- name: "无 Start、无个人、无团队时兜底个人 Coding",
- hasStart: false,
- hasPersonal: false,
- teamProducts: [],
- expectedSelection: { kind: "individual-coding-plan" },
- },
- ] satisfies Array<{
- name: string;
- hasStart: boolean;
- hasPersonal: boolean;
- teamProducts: EnterpriseCodingPlanPricingProduct[];
- expectedSelection: ProviderFamilyConnectionSelection;
- }>)("$name", ({ hasStart, hasPersonal, teamProducts, expectedSelection }) => {
- expect(resolveBigModelSelection({ hasStart, hasPersonal, teamProducts })).toEqual(
- expectedSelection,
- );
- });
- it("多个团队时选择后端返回顺序里的第一个已订阅团队项目", () => {
- const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
- providerFamilyDomain: "bigmodel",
- codingPlanEntitlement: null,
- startPlanEntitlement: null,
- teamProducts: [
- {
- productId: "team-a",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- projectId: "project-a",
- },
- {
- organizationId: "org-a",
- projectId: "project-a-2",
- },
- ],
- } satisfies EnterpriseCodingPlanPricingProduct,
- {
- productId: "team-b",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-b",
- projectId: "project-b",
- },
- ],
- } satisfies EnterpriseCodingPlanPricingProduct,
- ],
- });
- expect(selection).toEqual({
- kind: "team-coding-plan",
- productId: "team-a",
- organizationId: "org-a",
- projectId: "project-a",
- });
- });
- it("自动选择 Team Plan 时跳过 key 不可用的团队项目", () => {
- const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
- providerFamilyDomain: "bigmodel",
- codingPlanEntitlement: null,
- startPlanEntitlement: null,
- teamProducts: [
- {
- productId: "team-a",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- teamProjects: [
- {
- organizationId: "org-a",
- projectId: "project-a",
- apiKeyStatus: "unavailable",
- },
- {
- organizationId: "org-a",
- projectId: "project-a-2",
- apiKeyStatus: "available",
- },
- ],
- } satisfies EnterpriseCodingPlanPricingProduct,
- ],
- });
- expect(selection).toEqual({
- kind: "team-coding-plan",
- productId: "team-a",
- organizationId: "org-a",
- projectId: "project-a-2",
- });
- });
- it("团队项目未返回时不选 Team Plan,避免缺少团队身份边界", () => {
- const selection = resolveAutomaticModelProviderFamilyConnectionSelection({
- providerFamilyDomain: "bigmodel",
- codingPlanEntitlement: null,
- startPlanEntitlement: null,
- teamProducts: [
- {
- productId: "team-pro",
- tier: "PRO",
- subscribeMode: "CONTINUOUS",
- subscribePeriod: "MONTHLY",
- subscribed: true,
- teamProjects: [],
- } satisfies EnterpriseCodingPlanPricingProduct,
- ],
- });
- expect(selection).toEqual({
- kind: "individual-coding-plan",
- });
- });
- });
|