accountProviderSnapshotProtocol.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. zcodeProviderUpdateAccountConfigParamsSchema,
  4. zcodeProviderUpdateAccountConfigResultSchema,
  5. } from "../src/zcode-protocol/index.js";
  6. describe("Account 快照协议", () => {
  7. const snapshot = {
  8. revision: "account-1",
  9. basedOnZCodeBuiltinRevision: "builtin-1",
  10. providers: {},
  11. states: {
  12. account: {
  13. availability: "pending",
  14. entitled: false,
  15. current: true,
  16. effectiveAt: 1_800_000_000,
  17. connectionKey: "opaque-connection",
  18. },
  19. },
  20. };
  21. it("完整往返 Account State,不将其当未知字段删除", () => {
  22. expect(zcodeProviderUpdateAccountConfigParamsSchema.parse(snapshot)).toEqual(snapshot);
  23. });
  24. it.each(["not-authenticated", "not-connected", "credential-failed", "not-entitled"])(
  25. "不可用原因 %s 不阻断同一快照内其他可用套餐的同步",
  26. (unavailableReason) => {
  27. const mixedSnapshot = {
  28. ...snapshot,
  29. states: {
  30. failed: { availability: "unavailable", entitled: false, unavailableReason },
  31. working: { availability: "available", entitled: true },
  32. },
  33. };
  34. expect(zcodeProviderUpdateAccountConfigParamsSchema.parse(mixedSnapshot)).toEqual(
  35. mixedSnapshot,
  36. );
  37. },
  38. );
  39. it("缺少 states 的旧信封不能冒充完整快照,空账号状态仍可传递", () => {
  40. const { states: _states, ...withoutStates } = snapshot;
  41. expect(zcodeProviderUpdateAccountConfigParamsSchema.safeParse(withoutStates).success).toBe(
  42. false,
  43. );
  44. expect(
  45. zcodeProviderUpdateAccountConfigParamsSchema.parse({ ...withoutStates, states: {} }).states,
  46. ).toEqual({});
  47. });
  48. it("回执只确认接收,不把收到的版本宣称为已应用", () => {
  49. expect(
  50. zcodeProviderUpdateAccountConfigResultSchema.parse({
  51. receivedRevision: "account-1",
  52. providerCount: 0,
  53. status: "received",
  54. }),
  55. ).toEqual({ receivedRevision: "account-1", providerCount: 0, status: "received" });
  56. expect(
  57. zcodeProviderUpdateAccountConfigResultSchema.safeParse({
  58. appliedRevision: "account-1",
  59. providerCount: 0,
  60. status: "applied",
  61. }).success,
  62. ).toBe(false);
  63. });
  64. it.each([
  65. { current: "false" },
  66. { entitled: "true" },
  67. { token: "forbidden" },
  68. { unavailableReason: "unexpected" },
  69. { unavailableReason: null },
  70. ])("拒绝状态的错误类型和非契约字段 %j", (invalid) => {
  71. expect(() =>
  72. zcodeProviderUpdateAccountConfigParamsSchema.parse({
  73. ...snapshot,
  74. states: { account: { ...snapshot.states.account, ...invalid } },
  75. }),
  76. ).toThrow();
  77. });
  78. });