workspace-mcp-admission-projection.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { expect, it } from "vitest";
  2. import {
  3. createSessionEvent,
  4. SessionEventType,
  5. type SessionEvent,
  6. type SessionId,
  7. } from "@zcode/contracts";
  8. import {
  9. applyConversationDeltas,
  10. conversationSnapshotSchema,
  11. DELIVERY_PROFILES,
  12. filterConversationDeltasForProfile,
  13. statePatchSchema,
  14. } from "@zcode/shared/zcode-protocol-v4";
  15. import { ProductProjection } from "../src/zcode-protocol-v4/index.js";
  16. it("MCP 待审核状态经桌面 continuous 和手机 replayable 快照一致投影,恢复清理旧状态", () => {
  17. const source = new ProductProjection("session-1", "epoch-1");
  18. const pending = {
  19. pendingCount: 2,
  20. bundleDigest: "a".repeat(64),
  21. workspaceIdentity: "identity-A",
  22. };
  23. let seq = 0;
  24. const event = (type: SessionEvent["type"], payload: unknown) => ({
  25. ...createSessionEvent(type, "session-1" as SessionId, payload),
  26. sequenceNumber: ++seq,
  27. });
  28. source.applyEvent(event(SessionEventType.SessionCreated, { mode: "default" }));
  29. const initial = source.getSnapshot();
  30. const deltas = source.applyEvent(
  31. event("workspace_mcp_admission_updated" as SessionEvent["type"], pending),
  32. );
  33. expect(source.getSnapshot().workspaceMcpAdmission).toEqual(pending);
  34. for (const profile of [DELIVERY_PROFILES.continuous, DELIVERY_PROFILES.replayable]) {
  35. const delivered = filterConversationDeltasForProfile(deltas, profile);
  36. expect(applyConversationDeltas(initial, delivered).workspaceMcpAdmission).toEqual(pending);
  37. }
  38. expect(conversationSnapshotSchema.parse(source.getSnapshot()).workspaceMcpAdmission).toEqual(
  39. pending,
  40. );
  41. source.applyEvent(
  42. event("workspace_mcp_admission_updated" as SessionEvent["type"], {
  43. ...pending,
  44. pendingCount: 0,
  45. }),
  46. );
  47. expect(source.getSnapshot().workspaceMcpAdmission).toBeNull();
  48. source.applyEvent(event("workspace_mcp_admission_updated" as SessionEvent["type"], pending));
  49. source.applyEvent(event(SessionEventType.SessionResumed, { messageCount: 0 }));
  50. expect(source.getSnapshot().workspaceMcpAdmission).toBeNull();
  51. });
  52. it("MCP 准入 patch 保留有效字段,拒绝非法计数;旧快照可以缺席", () => {
  53. const valid = { pendingCount: 1, bundleDigest: "a".repeat(64), workspaceIdentity: "identity-A" };
  54. expect(statePatchSchema.parse({ workspaceMcpAdmission: valid }).workspaceMcpAdmission).toEqual(
  55. valid,
  56. );
  57. expect(
  58. statePatchSchema.safeParse({ workspaceMcpAdmission: { ...valid, pendingCount: -1 } }).success,
  59. ).toBe(false);
  60. expect(
  61. statePatchSchema.safeParse({
  62. workspaceMcpAdmission: { ...valid, bundleDigest: "not-a-digest" },
  63. }).success,
  64. ).toBe(false);
  65. expect(statePatchSchema.parse({}).workspaceMcpAdmission).toBeUndefined();
  66. });