| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- import { describe, expect, it } from "vitest";
- import { resolvePlanIdentitySnapshot } from "../src/plan-identity.js";
- import type { UsageEntitlementSnapshot } from "../src/usage-stats.js";
- function createEntitlementSnapshot(
- overrides: Partial<UsageEntitlementSnapshot> = {},
- ): UsageEntitlementSnapshot {
- return {
- generatedAt: 1_000,
- authenticated: true,
- provider: { id: "provider-a", name: "Provider A" },
- remaining: { count: 1, isShow: true },
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "product-a",
- productName: "Product A",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- quota: { level: "pro", limits: [] },
- ...overrides,
- };
- }
- describe("resolvePlanIdentitySnapshot", () => {
- it("prefers Coding Plan over Start Plan in the active provider family domain", () => {
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: createEntitlementSnapshot({
- generatedAt: 1_100,
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "coding-product",
- productName: "Coding Product",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- }),
- startPlanEntitlement: createEntitlementSnapshot({
- generatedAt: 1_200,
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "start-product",
- productName: "Start Product",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- }),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }),
- ).toEqual({
- generatedAt: 1_100,
- planStatus: "coding_plan",
- planProductId: "coding-product",
- });
- });
- it("uses Start Plan only after Coding Plan is confirmed absent", () => {
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "bigmodel",
- codingPlanEntitlement: createEntitlementSnapshot({
- unavailableReason: "no_plan",
- remaining: null,
- subscription: null,
- quota: null,
- }),
- startPlanEntitlement: createEntitlementSnapshot({
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "start-plan-id",
- productName: "Start Product",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- }),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }),
- ).toMatchObject({
- planStatus: "start_plan",
- planProductId: "start-plan-id",
- });
- });
- it("uses the first active Start Plan in server order as plan_product_id and follows server reordering", () => {
- // 口径快照(供数据侧评审):Start Plan subscription.details 现在是全部 active 套餐
- // 按服务端优先级排序;plan_product_id 固定取 details[0]。服务端调整排序或套餐
- // 增减时,该维度会跟随翻转——这是有意的口径,不是回归。
- const buildStartPlanDetails = (firstId: string, secondId: string) => ({
- identityType: "unknown" as const,
- identityMasked: null,
- details: [
- {
- productId: firstId,
- productName: "Start Priority One",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- {
- productId: secondId,
- productName: "Start Priority Two",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- });
- const noPlan = createEntitlementSnapshot({
- unavailableReason: "no_plan",
- remaining: null,
- subscription: null,
- quota: null,
- });
- const firstSnapshot = resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: noPlan,
- startPlanEntitlement: createEntitlementSnapshot({
- subscription: buildStartPlanDetails("start-first", "start-second"),
- }),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- });
- expect(firstSnapshot).toMatchObject({
- planStatus: "start_plan",
- planProductId: "start-first",
- });
- const reorderedSnapshot = resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: noPlan,
- startPlanEntitlement: createEntitlementSnapshot({
- subscription: buildStartPlanDetails("start-second", "start-first"),
- }),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- });
- expect(reorderedSnapshot).toMatchObject({
- planStatus: "start_plan",
- planProductId: "start-second",
- });
- });
- it("keeps Coding Plan active with an empty product id when quota is valid but subscription is missing", () => {
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: createEntitlementSnapshot({
- subscription: null,
- }),
- startPlanEntitlement: null,
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }),
- ).toMatchObject({
- planStatus: "coding_plan",
- planProductId: "",
- });
- });
- it("does not derive BigModel Team Plan product id from the selected local connection key", () => {
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "bigmodel",
- modelProviderFamilySelectedKeys: {
- bigmodel:
- "team-plan:account:bigmodel-individual-coding-plan:product-team:org-team:project-team",
- },
- codingPlanEntitlement: createEntitlementSnapshot({
- context: {
- scope: "personal",
- productId: "product-personal",
- },
- subscription: {
- identityType: "unknown",
- identityMasked: null,
- details: [
- {
- productId: "product-personal",
- productName: "Personal Coding Plan",
- purchaseTime: null,
- beginTime: null,
- expireTime: null,
- },
- ],
- },
- }),
- startPlanEntitlement: null,
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }),
- ).toEqual({
- generatedAt: 1_000,
- planStatus: "coding_plan",
- planProductId: "product-personal",
- });
- });
- it("returns no_plan only when Coding Plan and Start Plan are both explicitly absent", () => {
- const noPlan = createEntitlementSnapshot({
- unavailableReason: "no_plan",
- remaining: null,
- subscription: null,
- quota: null,
- });
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: noPlan,
- startPlanEntitlement: noPlan,
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }),
- ).toMatchObject({
- planStatus: "no_plan",
- planProductId: "",
- });
- });
- it("treats loading, failures, missing domain and stale snapshots as unknown", () => {
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: null,
- codingPlanEntitlement: createEntitlementSnapshot(),
- startPlanEntitlement: createEntitlementSnapshot(),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }).planStatus,
- ).toBe("unknown");
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: createEntitlementSnapshot({
- unavailableReason: "unavailable",
- remaining: null,
- subscription: null,
- quota: null,
- }),
- startPlanEntitlement: createEntitlementSnapshot(),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }).planStatus,
- ).toBe("unknown");
- expect(
- resolvePlanIdentitySnapshot({
- providerFamilyDomain: "zai",
- codingPlanEntitlement: createEntitlementSnapshot({
- generatedAt: 1,
- }),
- startPlanEntitlement: createEntitlementSnapshot(),
- now: 1_300,
- entitlementCacheTtlMs: 1_000,
- }).planStatus,
- ).toBe("unknown");
- });
- });
|